Skip to content

Commit 7aa59ed

Browse files
authored
Merge pull request #9 from darkriszty/feature/CJKSupport
Feature/cjk support
2 parents a485a55 + ae7cce5 commit 7aa59ed

22 files changed

Lines changed: 326 additions & 255 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
55

66
## [Unreleased]
77

8+
## 1.1.0 - 2017-05-09
9+
- Issue #6: Formatting when there's only a single table in the entire file.
10+
- Issue #4: Add support for CJK characters.
11+
812
## 1.0.1 - 2017-04-08
913
- Fixed issue #1 by improving the detection of header separator to avoid unintended table formatting failures.
1014

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Makes tables more readable for humans. Compatible with the Markdown writer plugi
1010
- Create missing ending table border if the beginning already has a border, so the table _will end_ with "|".
1111
- Save space by not right-padding the last column if the table has no border.
1212
- Support empty columns inside tables.
13+
- Support character lengths for Chinese, Japanese and Korean characters.
1314

1415
![feature X](assets/animation.gif)
1516

@@ -19,4 +20,4 @@ The extension is available for markdown (_.md_) files. To format a table just se
1920

2021
## Known Issues
2122

22-
- Formatting multiple tables at once and formatting an entire file is not supported.
23+
- Formatting multiple tables at once is not supported (issue #7).

demonstration.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,10 @@ Support empty columns inside tables:
2424
Primitive Type||Size(bit)|Class
2525
-|-|-|-
2626
byte||8|Byte
27-
char||16|Character
27+
char||16|Character
28+
29+
Support for CJK character lengths:
30+
Column 1 | Column 2 | Column 3
31+
-|-|-
32+
1|𠁻|a
33+
x|𣄿|a

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "markdown-table-prettify",
33
"displayName": "Markdown Table Prettifier",
44
"description": "Transforms markdown tables to be more readable.",
5-
"version": "1.0.1",
5+
"version": "1.1.0",
66
"publisher": "darkriszty",
77
"repository": {
88
"type": "git",
@@ -22,9 +22,9 @@
2222
"onLanguage:markdown"
2323
],
2424
"main": "./out/src/extension/extension",
25-
"contributes": { },
25+
"contributes": {},
2626
"capabilities": {
27-
"documentFormattingProvider" : "true"
27+
"documentFormattingProvider": "true"
2828
},
2929
"scripts": {
3030
"vscode:prepublish": "tsc -p ./",
@@ -37,8 +37,8 @@
3737
"@types/node": "^6.0.40",
3838
"mocha": "^2.3.3",
3939
"typemoq": "^1.4.1",
40-
"typescript": "^2.0.3",
40+
"typescript": "^2.3.2",
4141
"vscode": "^1.0.0"
4242
},
4343
"license": "MIT"
44-
}
44+
}

src/column/columnFactory.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/column/rawColumn.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/extension/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
import * as vscode from 'vscode';
33
import { TableRangePrettyfier } from "./tableRangePrettyfier";
4-
import { TableFactory } from "../table/tableFactory";
4+
import { TableFactory } from "../models/tableFactory";
55
import { VsWindowLogger } from "../diagnostics/logger";
66

77
// This method is called when the extension is activated.

src/extension/tableRangePrettyfier.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from "vscode";
2-
import { ITable } from "../table/table";
3-
import { ITableFactory } from "../table/tableFactory";
2+
import { ITable } from "../models/table";
3+
import { ITableFactory } from "../models/tableFactory";
44
import { ILogger } from "../diagnostics/logger";
55

66
export class TableRangePrettyfier implements vscode.DocumentRangeFormattingEditProvider {

src/models/cell.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
export class Cell {
2+
private _value: string;
3+
private _length: number;
4+
5+
constructor(value: string) {
6+
this._value = value;
7+
this._length = this._getOccupiedLength(value);
8+
}
9+
10+
public static Empty = new Cell(" ");
11+
12+
public getValue(): string {
13+
return this._value;
14+
}
15+
16+
public getLength(): number {
17+
return this._length;
18+
}
19+
20+
private _getOccupiedLength(value: string): number {
21+
let length: number = 0;
22+
23+
for (let i = 0, n = value.length; i < n; i++)
24+
length += this._getCharDisplayLength(value.charAt(i));
25+
26+
return length;
27+
}
28+
29+
private _getCharDisplayLength(character: string): number {
30+
// for the specified ranges use a length of 2, otherwise a length of 1
31+
return /^(([\u{4E00}-\u{9FFF}])|([\u{3400}-\u{4DBF}])|([\u{20000}-\u{2A6DF}])|([\u{2A700}-\u{2B73F}])|([\u{2B740}-\u{2B81F}]))$/u.test(character)
32+
? 2
33+
: 1;
34+
}
35+
}
Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import { RawColumn } from "./rawColumn";
21
import { ColumnPositioning } from "./columnPositioning";
2+
import { Cell } from "./cell";
33

44
export class Column {
55
private _normalizedColumnValues: string[] = [];
6+
private _columnType: ColumnPositioning;
7+
private _columnLength: number = 0;
68

7-
constructor(
8-
private _rawColumn: RawColumn,
9-
private _columnType: ColumnPositioning) {
10-
this._normalizeColumns();
9+
constructor(private _cells: Cell[]) {
10+
this._setColumnLength();
1111
}
1212

13-
public getSize(): number {
14-
return this._normalizedColumnValues.length;
13+
public getNumberOfRows(): number {
14+
return this._cells.length + 1;
1515
}
1616

1717
public isEmpty(): boolean {
18-
return this._rawColumn.isEmpty();
18+
return this._columnLength == 0;
1919
}
2020

2121
public getValue(row: number): string {
@@ -26,24 +26,30 @@ export class Column {
2626
return this._columnType;
2727
}
2828

29+
public setPositioning(position: ColumnPositioning): void {
30+
this._columnType = position;
31+
this._normalizeColumns();
32+
}
33+
2934
private _normalizeColumns(): void {
30-
for (let row = 0, rowCount = this._rawColumn.columnValues.length ; row < rowCount; row++) {
35+
for (let row = 0, rowCount = this._cells.length ; row < rowCount; row++) {
3136
const currentRowValue = this.isEmpty() && this._columnType == ColumnPositioning.Middle
32-
? " "
33-
: this._rawColumn.columnValues[row];
37+
? Cell.Empty
38+
: this._cells[row];
3439
this._addValueWithPadding(currentRowValue, " ");
3540
if (row == 0)
36-
this._addValueWithPadding("-", "-");
41+
this._addValueWithPadding(new Cell("-"), "-");
3742
}
3843
}
3944

40-
private _addValueWithPadding(value: string, padChar: string): void {
45+
private _addValueWithPadding(cell: Cell, padChar: string): void {
4146
let newValue = "";
47+
const cellValue = cell.getValue();
4248
if (!this.isEmpty() || this._columnType == ColumnPositioning.Middle) {
43-
const left = this._getLeftPad(value, padChar);
44-
const right = this._getRightPad(value, padChar);
49+
const left = this._getLeftPad(cellValue, padChar);
50+
const right = this._getRightPad(cell, padChar);
4551

46-
newValue = left + value + right;
52+
newValue = left + cellValue + right;
4753
}
4854
this._normalizedColumnValues.push(newValue);
4955
}
@@ -63,16 +69,25 @@ export class Column {
6369
}
6470
}
6571

66-
private _getRightPad(value: string, rightPad: string): string {
72+
private _getRightPad(cell: Cell, rightPad: string): string {
6773
const seperatorBeingAdded = this._oneRowExists();
6874
// only the separator has padding in the last column
6975
if (this._columnType == ColumnPositioning.Last && !seperatorBeingAdded)
7076
return "";
7177

72-
const extraPaddingCount = Math.max(this._rawColumn.cellLength - value.length + 2, 2);
78+
const extraPaddingCount = Math.max(this._columnLength - cell.getLength() + 2, 2);
7379
return new Array(extraPaddingCount).join(rightPad);
7480
}
7581

82+
private _setColumnLength(): void {
83+
const rowCount = this._cells.length;
84+
for (let row = 0 ; row < rowCount; row++) {
85+
const currentLength = this._cells[row].getLength();
86+
if (currentLength > this._columnLength)
87+
this._columnLength = currentLength;
88+
}
89+
}
90+
7691
private _oneRowExists(): boolean {
7792
return this._normalizedColumnValues.length == 1;
7893
}

0 commit comments

Comments
 (0)