From ed1e5c7acb549b35d3c5702f685ed588d880c3a7 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Mon, 6 Jan 2025 22:59:33 +0000 Subject: [PATCH] feat: enable the languages to be updated automatically when user settings have changed. Added: - A new user setting: `auto-comment-blocks.supportUnsupportedLanguages`. This is the new and preferred way of adding unsupported languages to be supported for all single and multi-line comment blocks. The `supportUnsupportedLanguages` object combines the old individual settings into one setting so it's easier to detect when they change. The properties have the same names as the now deprecated individual settings: `multiLineStyleBlocks`, `slashStyleBlocks`, `hashStyleBlocks`, `semicolonStyleBlocks`. - `updateSingleLineCommentLanguageDefinitions` public method to update the single-line comment language definitions. - An on change event for the new supportUnsupportedLanguages setting instead of the multiple events for the deprecated old settings, making the code DRY. Also added code to call the new `updateSingleLineCommentLanguageDefinitions` method to update the language definitions when the user changes the settings. - `getCustomSupportedLangConfigurationValue` method to get the custom supported language configuration value for the specified key. If the old user settings have values, then it will use the old deprecated setting, otherwise it will get the value from the new `supportUnsupportedLanguages` setting. Changed all relevant code in the `setSingleLineCommentLanguageDefinitions` method to use this new method of getting the user config settings. Deprecated: The following user settings are now deprecated: - `auto-comment-blocks.slashStyleBlocks` - `auto-comment-blocks.hashStyleBlocks` - `auto-comment-blocks.semicolonStyleBlocks` - auto-comment-blocks.multiLineStyleBlocks They all have deprecation description warnings that are shown in the settings.json. These settings also no longer appear in the Settings UI panel, in favour of the new combined setting `auto-comment-blocks.supportUnsupportedLanguages`. These deprecated settings will be removed in the major version v2.0.0 Removed: - The on change events for the deprecated individual settings. --- package.json | 56 ++++++++++++++++++++++++++++++++++---- src/configuration.ts | 42 ++++++++++++++++++++++++++-- src/extension.ts | 65 +++++--------------------------------------- 3 files changed, 97 insertions(+), 66 deletions(-) diff --git a/package.json b/package.json index 76955a1..720ec8b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "automatic-comment-blocks", "displayName": "Automatic Comment Blocks", "description": "Provides block comment completion for Javadoc-style multi-line comments and single-line comment blocks for most officially supported languages.", - "version": "1.1.4", + "version": "1.2.0", "publisher": "ycodetech", "homepage": "https://github.com/ycodetech/auto-comment-blocks", "repository": { @@ -45,20 +45,64 @@ "default": [], "markdownDescription": "Add language IDs here to disable any comment completion for that language." }, + "auto-comment-blocks.supportUnsupportedLanguages": { + "type": "object", + "default": { + "multiLineStyleBlocks": [ + "blade", + "html" + ], + "slashStyleBlocks": [], + "hashStyleBlocks": [], + "semicolonStyleBlocks": [] + }, + "markdownDescription": "Enables unsupported languages to have comment completion. \n\rProperties: \n- `multiLineStyleBlocks` to enable multi-line block comment support. The default is `['blade', 'html']` \n- `slashStyleBlocks` to enable `//`-style single-line comment support. \n- `hashStyleBlocks` to enable `#`-style single-line comment support. \n- `semicolonStyleBlocks` to enable `;`-style single-line comment support.", + "properties": { + "multiLineStyleBlocks": { + "type": "array", + "default": [ + "blade", + "html" + ], + "markdownDescription": "Add language IDs here to enable multi line comment blocks support for that language. This allows unsupported languages to have comment completion. The default is `['blade', 'html']`" + }, + "slashStyleBlocks": { + "type": "array", + "default": [], + "markdownDescription": "Add language IDs to enable `//`-style single-line comment blocks for that language." + }, + "hashStyleBlocks": { + "type": "array", + "default": [], + "markdownDescription": "Add language IDs to enable `#`-style single-line comment blocks for that language." + }, + "semicolonStyleBlocks": { + "type": "array", + "default": [], + "markdownDescription": "Add language IDs to enable `;`-style single-line comment blocks for that language." + } + } + }, "auto-comment-blocks.slashStyleBlocks": { "type": "array", "default": [], - "markdownDescription": "Add language IDs here to enable `//` and `///`-style single-line comment blocks for that language. This allows unsupported languages to have comment completion." + "markdownDescription": "Add language IDs here to enable `//` and `///`-style single-line comment blocks for that language. This allows unsupported languages to have comment completion.", + "deprecationMessage": "**Deprecated**: Please use `auto-comment-blocks.supportUnsupportedLanguages` with the property key `slashStyleBlocks` instead.", + "markdownDeprecationMessage": "**Deprecated**: Please use `#auto-comment-blocks.supportUnsupportedLanguages#` with the property key `slashStyleBlocks` instead." }, "auto-comment-blocks.hashStyleBlocks": { "type": "array", "default": [], - "markdownDescription": "Add language IDs here to enable `#`-style single-line comment blocks for that language. This allows unsupported languages to have comment completion." + "markdownDescription": "Add language IDs here to enable `#`-style single-line comment blocks for that language. This allows unsupported languages to have comment completion.", + "deprecationMessage": "**Deprecated**: Please use `auto-comment-blocks.supportUnsupportedLanguages` with the property key `slashStyleBlocks` instead.", + "markdownDeprecationMessage": "**Deprecated**: Please use `#auto-comment-blocks.supportUnsupportedLanguages#` with the property key `hashStyleBlocks` instead." }, "auto-comment-blocks.semicolonStyleBlocks": { "type": "array", "default": [], - "markdownDescription": "Add language IDs here to enable `;`-style single-line comment blocks for that language. This allows unsupported languages to have comment completion." + "markdownDescription": "Add language IDs here to enable `;`-style single-line comment blocks for that language. This allows unsupported languages to have comment completion.", + "deprecationMessage": "**Deprecated**: Please use `auto-comment-blocks.supportUnsupportedLanguages` with the property key `slashStyleBlocks` instead.", + "markdownDeprecationMessage": "**Deprecated**: Please use `#auto-comment-blocks.supportUnsupportedLanguages#` with the property key `semicolonStyleBlocks` instead." }, "auto-comment-blocks.multiLineStyleBlocks": { "type": "array", @@ -66,7 +110,9 @@ "blade", "html" ], - "markdownDescription": "Add language IDs here to enable multi-line comment blocks support for that language. This allows unsupported languages to have comment completion. The default is `['blade', 'html']`" + "markdownDescription": "Add language IDs here to enable multi-line comment blocks support for that language. This allows unsupported languages to have comment completion. The default is `['blade', 'html']`", + "deprecationMessage": "**Deprecated**: Please use `auto-comment-blocks.supportUnsupportedLanguages` with the property key `multiLineStyleBlocks` instead.", + "markdownDeprecationMessage": "**Deprecated**: Please use `#auto-comment-blocks.supportUnsupportedLanguages#` with the property key `multiLineStyleBlocks` instead." }, "auto-comment-blocks.overrideDefaultLanguageMultiLineComments": { "type": "object", diff --git a/src/configuration.ts b/src/configuration.ts index 7fe8ce3..19e04d5 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -247,6 +247,17 @@ export class Configuration { return this.getConfigurationValue("disabledLanguages").includes(langId); } + /** + * Update the single-line comment language definitions. + */ + public updateSingleLineCommentLanguageDefinitions() { + // Remove all elements from the current Map, so we can update + // the definitions with an empty Map. + this.singleLineBlocksMap.clear(); + // Update the definitions. + this.setSingleLineCommentLanguageDefinitions(); + } + /** * Is the multi-line comment overridden for the specified language ID? * @@ -271,6 +282,31 @@ export class Configuration { return overriddenList[langId]; } + /** + * Get the custom supported language configuration value for the specified key. + * + * Decides which setting to return, either the old or new setting. + * + * @param {string} key - The configuration key to retrieve. Can be one of `"slashStyleBlocks"`, `"hashStyleBlocks"`, or `"semicolonStyleBlocks"`. + * @returns {string[]} An array of strings representing the configuration values. + */ + private getCustomSupportedLangConfigurationValue(key: "slashStyleBlocks" | "hashStyleBlocks" | "semicolonStyleBlocks"): string[] { + /** + * @deprecated since v1.2.0, will be removed in v2.0.0 + */ + // Get the old configuration property. + const oldConfigProp = this.getConfigurationValue(key); + + // If old config property has a length more than 0 (ie. it has values in the array), + // then return the old property array. + if (oldConfigProp.length > 0) { + return oldConfigProp; + } + + // Otherwise, return the property array from the new supportUnsupportedLanguages setting. + return this.getConfigurationValue("supportUnsupportedLanguages")[key]; + } + /** * Get an array of languages to skip, like plaintext, that don't have comment syntax * @@ -509,7 +545,7 @@ export class Configuration { tempMap.clear(); // Get user-customized langIds for the //-style and add to the map. - let customSlashLangs = this.getConfigurationValue("slashStyleBlocks"); + let customSlashLangs = this.getCustomSupportedLangConfigurationValue("slashStyleBlocks"); for (let langId of customSlashLangs) { if (langId && langId.length > 0) { tempMap.set(langId, "//"); @@ -517,7 +553,7 @@ export class Configuration { } // Get user-customized langIds for the #-style and add to the map. - let customHashLangs = this.getConfigurationValue("hashStyleBlocks"); + let customHashLangs = this.getCustomSupportedLangConfigurationValue("hashStyleBlocks"); for (let langId of customHashLangs) { if (langId && langId.length > 0) { tempMap.set(langId, "#"); @@ -525,7 +561,7 @@ export class Configuration { } // Get user-customized langIds for the ;-style and add to the map. - let customSemicolonLangs = this.getConfigurationValue("semicolonStyleBlocks"); + let customSemicolonLangs = this.getCustomSupportedLangConfigurationValue("semicolonStyleBlocks"); for (let langId of customSemicolonLangs) { if (langId && langId.length > 0) { tempMap.set(langId, ";"); diff --git a/src/extension.ts b/src/extension.ts index 27d8208..658f229 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,5 +1,7 @@ "use strict"; +// INFO: How to publish extension: https://code.visualstudio.com/api/working-with-extensions/publishing-extension#publishing-extensions + import * as vscode from "vscode"; import {Configuration} from "./configuration"; @@ -80,67 +82,14 @@ export function activate(context: vscode.ExtensionContext) { } /** - * Multi-line style Block Comments + * Support Unsupported Languages */ - if (event.affectsConfiguration(`${extensionName}.multiLineStyleBlocks`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.multiLineStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); - } + if (event.affectsConfiguration(`${extensionName}.supportUnsupportedLanguages`)) { + configuration.updateSingleLineCommentLanguageDefinitions(); - /** - * //-style single-line comments - */ - if (event.affectsConfiguration(`${extensionName}.slashStyleBlocks`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.slashStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); - } + const configureCommentBlocksDisposable = configuration.configureCommentBlocks(context); - /** - * #-style single-line comments - */ - if (event.affectsConfiguration(`${extensionName}.hashStyleBlocks`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.hashStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); - } - - /** - * ;-style single-line comments - */ - if (event.affectsConfiguration(`${extensionName}.semicolonStyleBlocks`)) { - vscode.window - .showInformationMessage( - `The ${extensionName}.semicolonStyleBlocks setting has been changed. Please reload the Extension Host to take effect.`, - "Reload" - ) - .then((selection) => { - if (selection === "Reload") { - vscode.commands.executeCommand("workbench.action.restartExtensionHost"); - } - }); + disposables.push(...configureCommentBlocksDisposable); } });