Skip to content

Commit d579215

Browse files
committed
Add a new extension setting to configure more languageIds
1 parent 7259526 commit d579215

7 files changed

Lines changed: 36 additions & 18 deletions

File tree

CHANGELOG.md

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

77
## [Unreleased]
88

9+
## 3.3.0 - 2020-03-25
10+
### Added
11+
- Issue #54: Config to support more languages in VSCode formatting.
12+
913
## 3.2.2 - 2020-01-11
1014
### Added
1115
- Issue #49: NPM package support.

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ Makes tables more readable for humans. Compatible with the Markdown writer plugi
2424
The extension is available for markdown language mode. It can either prettify a selection (`Format Selection`) or the entire document (`Format Document`).
2525
A VSCode command called `Prettify markdown tables` is also available to format the currently opened document.
2626

27-
Configurable settings:
28-
- The maximum texth length of a selection/entire document to consider for formatting. Defaults to 1M chars. (limit does not apply from CLI or NPM).
27+
### Configurable settings:
28+
- The maximum texth length of a selection/entire document to consider for formatting. Default: 1M chars (limit does not apply from CLI or NPM).
29+
- Additional languages to support formatting for besides `markdown`. See possible configurable values [here](https://code.visualstudio.com/docs/languages/identifiers#_known-language-identifiers). Default: `[ ]`.
2930
- Keyboard shortcut to prettify the currently opened markdown document. Default: CTRL+ALT+M (CMD+ALT+M on Mac).
3031

3132
## NPM

package.json

Lines changed: 8 additions & 3 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": "3.2.2",
5+
"version": "3.3.0",
66
"publisher": "darkriszty",
77
"repository": {
88
"type": "git",
@@ -24,7 +24,7 @@
2424
"formatter"
2525
],
2626
"activationEvents": [
27-
"onLanguage:markdown"
27+
"onStartupFinished"
2828
],
2929
"main": "./out/src/extension/extension",
3030
"contributes": {
@@ -41,6 +41,11 @@
4141
"type": "integer",
4242
"default": 1000000,
4343
"description": "The maximum text length to apply formatting to."
44+
},
45+
"markdownTablePrettify.extendedLanguages": {
46+
"type": "array",
47+
"default": [ ],
48+
"description": "Additional VSCode language identifiers to register the formatter for. Eg: [ 'ruby' ]"
4449
}
4550
}
4651
},
@@ -55,7 +60,7 @@
5560
"command": "markdownTablePrettify.prettifyTables",
5661
"key": "ctrl+alt+m",
5762
"mac": "cmd+alt+m",
58-
"when": "editorTextFocus && editorLangId == markdown && !editorReadonly && !inCompositeEditor"
63+
"when": "editorTextFocus && !editorReadonly && !inCompositeEditor"
5964
}
6065
]
6166
},

src/extension/extension.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
'use strict';
22
import * as vscode from 'vscode';
3-
import { getDocumentRangePrettyfier, getDocumentPrettyfier, getDocumentPrettyfierCommand } from './prettyfierFactory';
3+
import { getSupportLanguageIds, getDocumentRangePrettyfier, getDocumentPrettyfier, getDocumentPrettyfierCommand } from './prettyfierFactory';
44

55
// This method is called when the extension is activated.
66
// The extension is activated the very first time the command is executed.
77
export function activate(context: vscode.ExtensionContext): void {
8-
const MD_MODE: vscode.DocumentFilter = { language: "markdown" };
9-
const command = "markdownTablePrettify.prettifyTables";
108

9+
const supportedLanguageIds = getSupportLanguageIds();
10+
for (let language of supportedLanguageIds) {
11+
context.subscriptions.push(
12+
vscode.languages.registerDocumentRangeFormattingEditProvider({ language }, getDocumentRangePrettyfier()),
13+
vscode.languages.registerDocumentFormattingEditProvider({ language }, getDocumentPrettyfier())
14+
);
15+
}
16+
17+
const command = "markdownTablePrettify.prettifyTables";
1118
context.subscriptions.push(
12-
vscode.languages.registerDocumentRangeFormattingEditProvider(MD_MODE, getDocumentRangePrettyfier()),
13-
vscode.languages.registerDocumentFormattingEditProvider(MD_MODE, getDocumentPrettyfier()),
14-
vscode.commands.registerTextEditorCommand(command, textEditor => getDocumentPrettyfierCommand().prettifyDocument(textEditor))
19+
vscode.commands.registerTextEditorCommand(command, textEditor => {
20+
if (supportedLanguageIds.indexOf(textEditor.document.languageId) >= 0)
21+
getDocumentPrettyfierCommand().prettifyDocument(textEditor);
22+
})
1523
);
1624
}
1725

src/extension/prettyfierFactory.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ import { MultiTablePrettyfier } from '../prettyfiers/multiTablePrettyfier';
2323
import { SingleTablePrettyfier } from '../prettyfiers/singleTablePrettyfier';
2424
import { FairTableIndentationDetector } from '../modelFactory/tableIndentationDetector';
2525

26+
export function getSupportLanguageIds() {
27+
return [ "markdown", ...getConfigurationValue<Array<string>>("extendedLanguages", []) ];
28+
}
29+
2630
export function getDocumentRangePrettyfier() {
2731
return new TableDocumentRangePrettyfier(getMultiTablePrettyfier());
2832
}

src/extension/tableDocumentPrettyfierCommand.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ export class TableDocumentPrettyfierCommand {
88
) { }
99

1010
public prettifyDocument(editor: vscode.TextEditor) {
11-
if (editor.document.languageId !== "markdown") {
12-
return;
13-
}
14-
1511
const formattedDocument: string = this._multiTablePrettyfier.formatTables(editor.document.getText());
1612

1713
editor.edit(textEditorEdit => {

test/unitTests/extension/tableDocumentPrettyfierCommand.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ suite("TableDocumentPrettyfierCommand tests", () => {
2828
_multiTablePrettyfier.verify(multiTablePrettyfier => multiTablePrettyfier.formatTables(It.isAny()), Times.once());
2929
});
3030

31-
test("prettifyDocument() for non-markdown documents doesn't do anything", () => {
31+
test("prettifyDocument() for non-markdown documents it still calls MultiTablePrettyfier and edit()", () => {
3232
const sut = createSut();
3333
const input = Array(10).fill("hello world").join("\n");
3434
const expectedResult = Array(10).fill("expected result").join("\n");
@@ -40,8 +40,8 @@ suite("TableDocumentPrettyfierCommand tests", () => {
4040

4141
sut.prettifyDocument(textEditor.object);
4242

43-
textEditor.verify(e => e.edit(It.isAny()), Times.never());
44-
_multiTablePrettyfier.verify(multiTablePrettyfier => multiTablePrettyfier.formatTables(It.isAny()), Times.never());
43+
textEditor.verify(e => e.edit(It.isAny()), Times.once());
44+
_multiTablePrettyfier.verify(multiTablePrettyfier => multiTablePrettyfier.formatTables(It.isAny()), Times.once());
4545
});
4646

4747
function createSut(): TableDocumentPrettyfierCommand {

0 commit comments

Comments
 (0)