Skip to content

Commit

Permalink
Merge pull request #55 from darkriszty/feature/extended-languages
Browse files Browse the repository at this point in the history
Add a new extension setting to configure more languageIds
  • Loading branch information
darkriszty authored Mar 25, 2021
2 parents 7259526 + d579215 commit 16d8dac
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p

## [Unreleased]

## 3.3.0 - 2020-03-25
### Added
- Issue #54: Config to support more languages in VSCode formatting.

## 3.2.2 - 2020-01-11
### Added
- Issue #49: NPM package support.
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ Makes tables more readable for humans. Compatible with the Markdown writer plugi
The extension is available for markdown language mode. It can either prettify a selection (`Format Selection`) or the entire document (`Format Document`).
A VSCode command called `Prettify markdown tables` is also available to format the currently opened document.

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

## NPM
Expand Down
11 changes: 8 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "markdown-table-prettify",
"displayName": "Markdown Table Prettifier",
"description": "Transforms markdown tables to be more readable.",
"version": "3.2.2",
"version": "3.3.0",
"publisher": "darkriszty",
"repository": {
"type": "git",
Expand All @@ -24,7 +24,7 @@
"formatter"
],
"activationEvents": [
"onLanguage:markdown"
"onStartupFinished"
],
"main": "./out/src/extension/extension",
"contributes": {
Expand All @@ -41,6 +41,11 @@
"type": "integer",
"default": 1000000,
"description": "The maximum text length to apply formatting to."
},
"markdownTablePrettify.extendedLanguages": {
"type": "array",
"default": [ ],
"description": "Additional VSCode language identifiers to register the formatter for. Eg: [ 'ruby' ]"
}
}
},
Expand All @@ -55,7 +60,7 @@
"command": "markdownTablePrettify.prettifyTables",
"key": "ctrl+alt+m",
"mac": "cmd+alt+m",
"when": "editorTextFocus && editorLangId == markdown && !editorReadonly && !inCompositeEditor"
"when": "editorTextFocus && !editorReadonly && !inCompositeEditor"
}
]
},
Expand Down
20 changes: 14 additions & 6 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
'use strict';
import * as vscode from 'vscode';
import { getDocumentRangePrettyfier, getDocumentPrettyfier, getDocumentPrettyfierCommand } from './prettyfierFactory';
import { getSupportLanguageIds, getDocumentRangePrettyfier, getDocumentPrettyfier, getDocumentPrettyfierCommand } from './prettyfierFactory';

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

const supportedLanguageIds = getSupportLanguageIds();
for (let language of supportedLanguageIds) {
context.subscriptions.push(
vscode.languages.registerDocumentRangeFormattingEditProvider({ language }, getDocumentRangePrettyfier()),
vscode.languages.registerDocumentFormattingEditProvider({ language }, getDocumentPrettyfier())
);
}

const command = "markdownTablePrettify.prettifyTables";
context.subscriptions.push(
vscode.languages.registerDocumentRangeFormattingEditProvider(MD_MODE, getDocumentRangePrettyfier()),
vscode.languages.registerDocumentFormattingEditProvider(MD_MODE, getDocumentPrettyfier()),
vscode.commands.registerTextEditorCommand(command, textEditor => getDocumentPrettyfierCommand().prettifyDocument(textEditor))
vscode.commands.registerTextEditorCommand(command, textEditor => {
if (supportedLanguageIds.indexOf(textEditor.document.languageId) >= 0)
getDocumentPrettyfierCommand().prettifyDocument(textEditor);
})
);
}

Expand Down
4 changes: 4 additions & 0 deletions src/extension/prettyfierFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import { MultiTablePrettyfier } from '../prettyfiers/multiTablePrettyfier';
import { SingleTablePrettyfier } from '../prettyfiers/singleTablePrettyfier';
import { FairTableIndentationDetector } from '../modelFactory/tableIndentationDetector';

export function getSupportLanguageIds() {
return [ "markdown", ...getConfigurationValue<Array<string>>("extendedLanguages", []) ];
}

export function getDocumentRangePrettyfier() {
return new TableDocumentRangePrettyfier(getMultiTablePrettyfier());
}
Expand Down
4 changes: 0 additions & 4 deletions src/extension/tableDocumentPrettyfierCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ export class TableDocumentPrettyfierCommand {
) { }

public prettifyDocument(editor: vscode.TextEditor) {
if (editor.document.languageId !== "markdown") {
return;
}

const formattedDocument: string = this._multiTablePrettyfier.formatTables(editor.document.getText());

editor.edit(textEditorEdit => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ suite("TableDocumentPrettyfierCommand tests", () => {
_multiTablePrettyfier.verify(multiTablePrettyfier => multiTablePrettyfier.formatTables(It.isAny()), Times.once());
});

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

sut.prettifyDocument(textEditor.object);

textEditor.verify(e => e.edit(It.isAny()), Times.never());
_multiTablePrettyfier.verify(multiTablePrettyfier => multiTablePrettyfier.formatTables(It.isAny()), Times.never());
textEditor.verify(e => e.edit(It.isAny()), Times.once());
_multiTablePrettyfier.verify(multiTablePrettyfier => multiTablePrettyfier.formatTables(It.isAny()), Times.once());
});

function createSut(): TableDocumentPrettyfierCommand {
Expand Down

0 comments on commit 16d8dac

Please sign in to comment.