Skip to content

Commit b3e6ced

Browse files
authored
Merge pull request #24 from darkriszty/feature/Alignments
Feature/alignments
2 parents 33d8cb8 + f07e1c7 commit b3e6ced

75 files changed

Lines changed: 859 additions & 333 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
"out": false // set this to true to hide the "out" folder with the compiled JS files
55
},
66
"search.exclude": {
7-
"out": true // set this to false to include "out" folder in search results
7+
"**/.git": true,
8+
"**/out": true,
9+
"**/node_modules": true
810
}
911
}

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ All notable changes to the `markdowntableprettify` extension will be documented
55
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/).
66

77
## [Unreleased]
8+
### Added
9+
- Issue #15: Support alignment options
10+
11+
## 2.1.0 - 2018-09-15
12+
### Added
13+
- Issue #15: Support alignment options
814

9-
## 2.0.0 - 2012-02-09
15+
## 2.0.0 - 2018-02-09
1016
### Added
1117
- Issue #12: Full rewrite for refactoring.
1218
- Issue #11: Support escaping of separators with backslash.

package-lock.json

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 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": "2.0.0",
5+
"version": "2.1.0",
66
"publisher": "darkriszty",
77
"repository": {
88
"type": "git",
@@ -33,12 +33,12 @@
3333
"test": "gulp copy-systemTest-resources && node ./node_modules/vscode/bin/test"
3434
},
3535
"devDependencies": {
36-
"@types/mocha": "~2.2.48",
37-
"@types/node": "~6.0.117",
36+
"@types/mocha": "~5.2.0",
37+
"@types/node": "~10.9.4",
3838
"gulp": "~4.0.0",
3939
"mocha": "~5.2.0",
4040
"typemoq": "~1.8.0",
41-
"typescript": "~2.9.2",
41+
"typescript": "~3.0.3",
4242
"vscode": "~1.1.21"
4343
},
4444
"license": "MIT"

src/extension/extension.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import { RowViewModelFactory } from '../viewModelFactories/rowViewModelFactory';
1313
import { TrimmerTransformer } from '../modelFactory/transformers/trimmerTransformer';
1414
import { BorderTransformer } from '../modelFactory/transformers/borderTransformer';
1515
import { SelectionInterpreter } from '../modelFactory/selectionInterpreter';
16-
import { SeparatorPadCalculator } from '../padCalculation/separatorPadCalculator';
1716
import { PadCalculatorSelector } from '../padCalculation/padCalculatorSelector';
17+
import { AlignmentMarkerStrategy } from '../viewModelFactories/alignmentMarking';
1818

1919
// This method is called when the extension is activated.
2020
// The extension is activated the very first time the command is executed.
@@ -31,8 +31,8 @@ export function activate(context: vscode.ExtensionContext): void {
3131
new TableValidator(new SelectionInterpreter()),
3232
new TableViewModelFactory(
3333
new RowViewModelFactory(
34-
new ContentPadCalculator(new PadCalculatorSelector(), " "),
35-
new SeparatorPadCalculator(new PadCalculatorSelector(), "-")
34+
new ContentPadCalculator(new PadCalculatorSelector(), " "),
35+
new AlignmentMarkerStrategy(":")
3636
)
3737
),
3838
new TableStringWriter(),
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import { Alignment } from "../models/alignment";
22

33
export class AlignmentFactory {
4+
45
public createAlignments(cells: string[]): Alignment[] {
5-
let result: Alignment[] = [];
6+
return cells.map(this.alignmentOf);
7+
}
68

7-
const len: number = cells.length;
8-
for (let i = 0; i < len; i++) {
9-
const cell = cells[i];
10-
const left = cell[0] == ":";;
11-
const right = cell[cell.length - 1] == ":";
9+
private alignmentOf(cell: string): Alignment {
10+
const left = cell[0] == ":";
11+
const right = cell[cell.length - 1] == ":";
1212

13-
if (left && right)
14-
result.push(Alignment.Center);
15-
else if (right)
16-
result.push(Alignment.Right);
17-
else
18-
result.push(Alignment.Left);
19-
}
13+
if (left && right) return Alignment.Center;
14+
if (right) return Alignment.Right;
15+
if (left) return Alignment.Left;
2016

21-
return result;
17+
return Alignment.NotSet;
2218
}
2319
}

src/modelFactory/tableFactory.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ export class TableFactory {
1818
throw new Error("Can't create table model from null table text.");
1919

2020
const rowsWithoutSeparator = this._selectionInterpreter.allRows(text).filter((v, i) => i != 1);
21+
const separator = this._selectionInterpreter.separator(text);
2122

22-
const alignments: Alignment[] = rowsWithoutSeparator != null && rowsWithoutSeparator.length > 1
23-
? this._alignmentFactory.createAlignments(rowsWithoutSeparator[1])
23+
const alignments: Alignment[] = separator != null && separator.length > 0
24+
? this._alignmentFactory.createAlignments(separator)
2425
: [];
2526

2627
const cells = rowsWithoutSeparator.map(row => row.map(c => new Cell(c)));

src/models/alignment.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export enum Alignment {
2+
NotSet,
23
Left,
34
Center,
45
Right

src/padCalculation/basePadCalculator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Table } from "../models/table";
22
import { Cell } from "../models/cell";
33

44
export abstract class BasePadCalculator {
5-
public abstract getLeftPadding(paddingChar: string, table: Table, cell: Cell): string;
5+
public abstract getLeftPadding(paddingChar: string, table: Table, row: number, column: number): string;
66
public abstract getRightPadding(paddingChar: string, table: Table, row: number, column: number): string;
77

88
protected baseGetRightPadding(paddingChar: string, table: Table, row: number, column: number): string {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Table } from "../../models/table";
2+
import { Cell } from "../../models/cell";
3+
import { BasePadCalculator } from "../basePadCalculator";
4+
5+
export abstract class CenterPadCalculator extends BasePadCalculator {
6+
public getLeftPadding(paddingChar: string, table: Table, row: number, column: number): string {
7+
return paddingChar.repeat(Math.floor(this.totalPadCount(table, column, row)));
8+
}
9+
10+
public getRightPadding(paddingChar: string, table: Table, row: number, column: number): string {
11+
return paddingChar.repeat(Math.ceil(this.totalPadCount(table, column, row)));
12+
}
13+
14+
protected totalPadCount(table: Table, column: number, row: number): number {
15+
const longestColumnLength = table.getLongestColumnLengths()[column];
16+
let padCount = longestColumnLength > 0
17+
? longestColumnLength - table.rows[row][column].getLength()
18+
: 1;
19+
padCount += this.extraPadCount(table);
20+
return padCount / 2;
21+
}
22+
23+
protected extraPadCount(table: Table): number {
24+
return 2;
25+
}
26+
}

0 commit comments

Comments
 (0)