From 5d3f5d8b48dedbb6fae308193a58888098985954 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Sun, 19 Apr 2026 21:00:40 +0100 Subject: [PATCH 1/3] refactor: replace `var` with `let` for block scoping in `Configuration` --- src/configuration.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/configuration.ts b/src/configuration.ts index 4701071..41a6c34 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -904,12 +904,12 @@ export class Configuration { // Get the langId from the auto-supported langs. If it doesn't exist, try getting it from // the custom-supported langs instead. - var style: SingleLineCommentStyle | ExtraSingleLineCommentStyles = singleLineLangs.get(langId) ?? customSingleLineLangs.get(langId); + let style: SingleLineCommentStyle | ExtraSingleLineCommentStyles = singleLineLangs.get(langId) ?? customSingleLineLangs.get(langId); if (style && textEditor.selection.isEmpty) { let line = textEditor.document.lineAt(textEditor.selection.active); let isCommentLine = true; - var indentRegex: RegExp; + let indentRegex: RegExp; if (style === "//" && line.text.search(/^\s*\/\/\s*/) !== -1) { indentRegex = /\//; @@ -939,7 +939,7 @@ export class Configuration { return; } - var indentedNewLine = "\n" + line.text.substring(0, line.text.search(indentRegex)); + let indentedNewLine = "\n" + line.text.substring(0, line.text.search(indentRegex)); let isOnEnter = this.getConfigurationValue("singleLineBlockOnEnter"); if (!isOnEnter) { indentedNewLine += style + " "; From 8b7326ab3608ac8a4665c4d8252f27b946ee4f68 Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Sun, 19 Apr 2026 22:00:02 +0100 Subject: [PATCH 2/3] refactor: simplify blade comment handling in `Configuration`. - Extract the "getting" logic in `setBladeComments` method into a new `getBladeOrHtmlComments` method. This removes confusion about what the function does. It was doing too many things. It now adheres to the DOT principle. - The `getBladeOrHtmlComments` method now only returns the blade/html comments. - The `setBladeComments` method now only sets the comments configuration for the blade language. It also uses the new `getBladeOrHtmlComments`. - Changed the `setBladeComments` call to use the new `getBladeOrHtmlComments` method in `setLanguageConfiguration`. --- src/configuration.ts | 66 +++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 35 deletions(-) diff --git a/src/configuration.ts b/src/configuration.ts index 41a6c34..a3bd204 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -184,46 +184,42 @@ export class Configuration { } /** - * Sets the block comments for the blade language determined by the user setting. - * - * @param bladeOverrideComments A boolean indicating whether or not the user setting "Blade Override Comments" is enabled. + * Get the appropriate comment style for the blade language. Either blade or html comments. * - * @param [onStart=false] A boolean indicating whether or not the method was called - * on starting the extension. - * If `true`, it returns the comments, if `false` (default), it sets the comments to - * the language directly. - * - * @returns {vscode.CharacterPair | void} Returns the blade comments if `onStart` is `true`, otherwise nothing. + * @param bladeOverrideComments A boolean indicating whether or not the user setting + * "Blade Override Comments" is enabled. * + * @returns {vscode.CharacterPair} The appropriate comment style for the blade language. */ - public setBladeComments(bladeOverrideComments: boolean, onStart: boolean = false): vscode.CharacterPair | void { - // Is enabled AND blade langId is NOT set as disabled... + private getBladeOrHtmlComments(bladeOverrideComments: boolean): vscode.CharacterPair { + // If blade override is enabled AND blade langId is NOT set as disabled, + // return the blade comments. if (bladeOverrideComments === true && !this.isLangIdDisabled("blade")) { - const bladeComments: vscode.CharacterPair = ["{{--", "--}}"]; - - if (onStart) { - return bladeComments; - } else { - vscode.languages.setLanguageConfiguration("blade", { - comments: { - blockComment: bladeComments, - }, - }); - } + return ["{{--", "--}}"]; } - // Is disabled OR blade langId is set as disabled... - else if (!bladeOverrideComments || this.isLangIdDisabled("blade")) { - const htmlComments: vscode.CharacterPair = [""]; - if (onStart) { - return htmlComments; - } else { - vscode.languages.setLanguageConfiguration("blade", { - comments: { - blockComment: htmlComments, - }, - }); - } + // Otherwise, return the html comments. + return [""]; + } + + /** + * Sets the block comments for the blade language determined by the user setting. + * + * @param bladeOverrideComments A boolean indicating whether or not the user setting + * "Blade Override Comments" is enabled. + */ + public setBladeComments(bladeOverrideComments: boolean): void { + // If blade langId is NOT set as disabled... + if (!this.isLangIdDisabled("blade")) { + // Get blade or html comments. + const comments = this.getBladeOrHtmlComments(bladeOverrideComments); + + // Set the comments into the language config for blade. + vscode.languages.setLanguageConfiguration("blade", { + comments: { + blockComment: comments, + }, + }); } } @@ -751,7 +747,7 @@ export class Configuration { * Get the user settings/configuration and set the blade or html comments accordingly. */ if (langId === "blade") { - const bladeComments = this.setBladeComments(this.getConfigurationValue("bladeOverrideComments"), true); + const bladeComments = this.getBladeOrHtmlComments(this.getConfigurationValue("bladeOverrideComments")); // If bladeComments has a value... if (bladeComments) { From 0f4cfff1eff6aae39ab36c958ac6b5d506464c3a Mon Sep 17 00:00:00 2001 From: yCodeTech Date: Sun, 19 Apr 2026 22:39:00 +0100 Subject: [PATCH 3/3] refactor: extract duplicate custom single-line language logic into helper - Refactor the custom single-line language logic in `setSingleLineCommentLanguageDefinitions` method to replace the duplicate loops and logic by a single new helper method: `addCustomSingleLineLanguages`. --- src/configuration.ts | 59 ++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 32 deletions(-) diff --git a/src/configuration.ts b/src/configuration.ts index a3bd204..d05ff85 100644 --- a/src/configuration.ts +++ b/src/configuration.ts @@ -582,6 +582,29 @@ export class Configuration { this.multiLineBlocksMap.set("customSupportedLanguages", langArray.sort()); } + /** + * Add custom single-line languages to the map from a configuration setting. + * + * @param tempMap The temp map to add languages to. + * @param settingKey The configuration setting key to read languages from. + * @param style The comment style to associate with these languages. + */ + private addCustomSingleLineLanguages( + tempMap: Map, + settingKey: "slashStyleBlocks" | "hashStyleBlocks" | "semicolonStyleBlocks", + style: SingleLineCommentStyle + ): void { + const customLangs = this.getConfigurationValue(settingKey); + for (const langId of customLangs) { + // If langId exists (ie. not NULL or empty string) AND + // the langId is longer than 0, AND + // the langId isn't set as disabled... + if (langId && langId.length > 0 && !this.isLangIdDisabled(langId)) { + tempMap.set(langId, style); + } + } + } + /** * Set the single-line comments language definitions. */ @@ -633,38 +656,10 @@ export class Configuration { // Empty the tempMap to reuse it. tempMap.clear(); - // Get user-customized langIds for the //-style and add to the map. - let customSlashLangs = this.getConfigurationValue("slashStyleBlocks"); - for (let langId of customSlashLangs) { - // If langId is exists (ie. not NULL or empty string) AND - // the langId is longer than 0, AND - // the langId isn't set as disabled... - if (langId && langId.length > 0) { - tempMap.set(langId, "//"); - } - } - - // Get user-customized langIds for the #-style and add to the map. - let customHashLangs = this.getConfigurationValue("hashStyleBlocks"); - for (let langId of customHashLangs) { - // If langId is exists (ie. not NULL or empty string) AND - // the langId is longer than 0, AND - // the langId isn't set as disabled... - if (langId && langId.length > 0 && !this.isLangIdDisabled(langId)) { - tempMap.set(langId, "#"); - } - } - - // Get user-customized langIds for the ;-style and add to the map. - let customSemicolonLangs = this.getConfigurationValue("semicolonStyleBlocks"); - for (let langId of customSemicolonLangs) { - // If langId is exists (ie. not NULL or empty string) AND - // the langId is longer than 0, AND - // the langId isn't set as disabled... - if (langId && langId.length > 0 && !this.isLangIdDisabled(langId)) { - tempMap.set(langId, ";"); - } - } + // Add user-customized langIds for each comment style. + this.addCustomSingleLineLanguages(tempMap, "slashStyleBlocks", "//"); + this.addCustomSingleLineLanguages(tempMap, "hashStyleBlocks", "#"); + this.addCustomSingleLineLanguages(tempMap, "semicolonStyleBlocks", ";"); // Set the customSupportedLanguages tempMap into the singleLineBlocksMap, // sorted in ascending order, for sanity reasons.