From d579215cdf075b48741f961e89c9c1d69539e07d Mon Sep 17 00:00:00 2001 From: Krisztian Daroczi Date: Thu, 25 Mar 2021 19:43:46 +0100 Subject: [PATCH] Add a new extension setting to configure more languageIds --- CHANGELOG.md | 4 ++++ README.md | 5 +++-- package.json | 11 +++++++--- src/extension/extension.ts | 20 +++++++++++++------ src/extension/prettyfierFactory.ts | 4 ++++ .../tableDocumentPrettyfierCommand.ts | 4 ---- .../tableDocumentPrettyfierCommand.test.ts | 6 +++--- 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1d8a2c2..b7c2e2a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 2f326d7..30841fa 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index 67319cc..af9590c 100644 --- a/package.json +++ b/package.json @@ -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", @@ -24,7 +24,7 @@ "formatter" ], "activationEvents": [ - "onLanguage:markdown" + "onStartupFinished" ], "main": "./out/src/extension/extension", "contributes": { @@ -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' ]" } } }, @@ -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" } ] }, diff --git a/src/extension/extension.ts b/src/extension/extension.ts index c0b5f71..469e23d 100644 --- a/src/extension/extension.ts +++ b/src/extension/extension.ts @@ -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); + }) ); } diff --git a/src/extension/prettyfierFactory.ts b/src/extension/prettyfierFactory.ts index 42785b4..6c91b8b 100644 --- a/src/extension/prettyfierFactory.ts +++ b/src/extension/prettyfierFactory.ts @@ -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>("extendedLanguages", []) ]; +} + export function getDocumentRangePrettyfier() { return new TableDocumentRangePrettyfier(getMultiTablePrettyfier()); } diff --git a/src/extension/tableDocumentPrettyfierCommand.ts b/src/extension/tableDocumentPrettyfierCommand.ts index 2f45b43..44de745 100644 --- a/src/extension/tableDocumentPrettyfierCommand.ts +++ b/src/extension/tableDocumentPrettyfierCommand.ts @@ -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 => { diff --git a/test/unitTests/extension/tableDocumentPrettyfierCommand.test.ts b/test/unitTests/extension/tableDocumentPrettyfierCommand.test.ts index 68103e6..e9bf408 100644 --- a/test/unitTests/extension/tableDocumentPrettyfierCommand.test.ts +++ b/test/unitTests/extension/tableDocumentPrettyfierCommand.test.ts @@ -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"); @@ -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 {