Skip to content

Commit 9f51a2b

Browse files
authored
refactor: configuration class (#30)
* Extracted the logic for determining the appropriate Blade or HTML comment style into a new private method `getBladeOrHtmlComments`, simplifying the public API and improving clarity. The `setBladeComments` method now only sets the configuration and no longer returns values based on an `onStart` flag (the flag was removed in favour using the new private method directly). * Replaced repetitive code for adding custom single-line comment languages with a new private method `addCustomSingleLineLanguages`, reducing duplication and improving maintainability. * Replaced `var` with `let` for variable declarations in several places to align with modern best practices.
2 parents 6b395b2 + 0f4cfff commit 9f51a2b

1 file changed

Lines changed: 61 additions & 70 deletions

File tree

src/configuration.ts

Lines changed: 61 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -184,46 +184,42 @@ export class Configuration {
184184
}
185185

186186
/**
187-
* Sets the block comments for the blade language determined by the user setting.
188-
*
189-
* @param bladeOverrideComments A boolean indicating whether or not the user setting "Blade Override Comments" is enabled.
190-
*
191-
* @param [onStart=false] A boolean indicating whether or not the method was called
192-
* on starting the extension.
193-
* If `true`, it returns the comments, if `false` (default), it sets the comments to
194-
* the language directly.
187+
* Get the appropriate comment style for the blade language. Either blade or html comments.
195188
*
196-
* @returns {vscode.CharacterPair | void} Returns the blade comments if `onStart` is `true`, otherwise nothing.
189+
* @param bladeOverrideComments A boolean indicating whether or not the user setting
190+
* "Blade Override Comments" is enabled.
197191
*
192+
* @returns {vscode.CharacterPair} The appropriate comment style for the blade language.
198193
*/
199-
public setBladeComments(bladeOverrideComments: boolean, onStart: boolean = false): vscode.CharacterPair | void {
200-
// Is enabled AND blade langId is NOT set as disabled...
194+
private getBladeOrHtmlComments(bladeOverrideComments: boolean): vscode.CharacterPair {
195+
// If blade override is enabled AND blade langId is NOT set as disabled,
196+
// return the blade comments.
201197
if (bladeOverrideComments === true && !this.isLangIdDisabled("blade")) {
202-
const bladeComments: vscode.CharacterPair = ["{{--", "--}}"];
203-
204-
if (onStart) {
205-
return bladeComments;
206-
} else {
207-
vscode.languages.setLanguageConfiguration("blade", {
208-
comments: {
209-
blockComment: bladeComments,
210-
},
211-
});
212-
}
198+
return ["{{--", "--}}"];
213199
}
214-
// Is disabled OR blade langId is set as disabled...
215-
else if (!bladeOverrideComments || this.isLangIdDisabled("blade")) {
216-
const htmlComments: vscode.CharacterPair = ["<!--", "-->"];
217200

218-
if (onStart) {
219-
return htmlComments;
220-
} else {
221-
vscode.languages.setLanguageConfiguration("blade", {
222-
comments: {
223-
blockComment: htmlComments,
224-
},
225-
});
226-
}
201+
// Otherwise, return the html comments.
202+
return ["<!--", "-->"];
203+
}
204+
205+
/**
206+
* Sets the block comments for the blade language determined by the user setting.
207+
*
208+
* @param bladeOverrideComments A boolean indicating whether or not the user setting
209+
* "Blade Override Comments" is enabled.
210+
*/
211+
public setBladeComments(bladeOverrideComments: boolean): void {
212+
// If blade langId is NOT set as disabled...
213+
if (!this.isLangIdDisabled("blade")) {
214+
// Get blade or html comments.
215+
const comments = this.getBladeOrHtmlComments(bladeOverrideComments);
216+
217+
// Set the comments into the language config for blade.
218+
vscode.languages.setLanguageConfiguration("blade", {
219+
comments: {
220+
blockComment: comments,
221+
},
222+
});
227223
}
228224
}
229225

@@ -586,6 +582,29 @@ export class Configuration {
586582
this.multiLineBlocksMap.set("customSupportedLanguages", langArray.sort());
587583
}
588584

585+
/**
586+
* Add custom single-line languages to the map from a configuration setting.
587+
*
588+
* @param tempMap The temp map to add languages to.
589+
* @param settingKey The configuration setting key to read languages from.
590+
* @param style The comment style to associate with these languages.
591+
*/
592+
private addCustomSingleLineLanguages(
593+
tempMap: Map<LanguageId, SingleLineCommentStyle>,
594+
settingKey: "slashStyleBlocks" | "hashStyleBlocks" | "semicolonStyleBlocks",
595+
style: SingleLineCommentStyle
596+
): void {
597+
const customLangs = this.getConfigurationValue(settingKey);
598+
for (const langId of customLangs) {
599+
// If langId exists (ie. not NULL or empty string) AND
600+
// the langId is longer than 0, AND
601+
// the langId isn't set as disabled...
602+
if (langId && langId.length > 0 && !this.isLangIdDisabled(langId)) {
603+
tempMap.set(langId, style);
604+
}
605+
}
606+
}
607+
589608
/**
590609
* Set the single-line comments language definitions.
591610
*/
@@ -637,38 +656,10 @@ export class Configuration {
637656
// Empty the tempMap to reuse it.
638657
tempMap.clear();
639658

640-
// Get user-customized langIds for the //-style and add to the map.
641-
let customSlashLangs = this.getConfigurationValue("slashStyleBlocks");
642-
for (let langId of customSlashLangs) {
643-
// If langId is exists (ie. not NULL or empty string) AND
644-
// the langId is longer than 0, AND
645-
// the langId isn't set as disabled...
646-
if (langId && langId.length > 0) {
647-
tempMap.set(langId, "//");
648-
}
649-
}
650-
651-
// Get user-customized langIds for the #-style and add to the map.
652-
let customHashLangs = this.getConfigurationValue("hashStyleBlocks");
653-
for (let langId of customHashLangs) {
654-
// If langId is exists (ie. not NULL or empty string) AND
655-
// the langId is longer than 0, AND
656-
// the langId isn't set as disabled...
657-
if (langId && langId.length > 0 && !this.isLangIdDisabled(langId)) {
658-
tempMap.set(langId, "#");
659-
}
660-
}
661-
662-
// Get user-customized langIds for the ;-style and add to the map.
663-
let customSemicolonLangs = this.getConfigurationValue("semicolonStyleBlocks");
664-
for (let langId of customSemicolonLangs) {
665-
// If langId is exists (ie. not NULL or empty string) AND
666-
// the langId is longer than 0, AND
667-
// the langId isn't set as disabled...
668-
if (langId && langId.length > 0 && !this.isLangIdDisabled(langId)) {
669-
tempMap.set(langId, ";");
670-
}
671-
}
659+
// Add user-customized langIds for each comment style.
660+
this.addCustomSingleLineLanguages(tempMap, "slashStyleBlocks", "//");
661+
this.addCustomSingleLineLanguages(tempMap, "hashStyleBlocks", "#");
662+
this.addCustomSingleLineLanguages(tempMap, "semicolonStyleBlocks", ";");
672663

673664
// Set the customSupportedLanguages tempMap into the singleLineBlocksMap,
674665
// sorted in ascending order, for sanity reasons.
@@ -751,7 +742,7 @@ export class Configuration {
751742
* Get the user settings/configuration and set the blade or html comments accordingly.
752743
*/
753744
if (langId === "blade") {
754-
const bladeComments = this.setBladeComments(this.getConfigurationValue("bladeOverrideComments"), true);
745+
const bladeComments = this.getBladeOrHtmlComments(this.getConfigurationValue("bladeOverrideComments"));
755746

756747
// If bladeComments has a value...
757748
if (bladeComments) {
@@ -904,12 +895,12 @@ export class Configuration {
904895

905896
// Get the langId from the auto-supported langs. If it doesn't exist, try getting it from
906897
// the custom-supported langs instead.
907-
var style: SingleLineCommentStyle | ExtraSingleLineCommentStyles = singleLineLangs.get(langId) ?? customSingleLineLangs.get(langId);
898+
let style: SingleLineCommentStyle | ExtraSingleLineCommentStyles = singleLineLangs.get(langId) ?? customSingleLineLangs.get(langId);
908899

909900
if (style && textEditor.selection.isEmpty) {
910901
let line = textEditor.document.lineAt(textEditor.selection.active);
911902
let isCommentLine = true;
912-
var indentRegex: RegExp;
903+
let indentRegex: RegExp;
913904

914905
if (style === "//" && line.text.search(/^\s*\/\/\s*/) !== -1) {
915906
indentRegex = /\//;
@@ -939,7 +930,7 @@ export class Configuration {
939930
return;
940931
}
941932

942-
var indentedNewLine = "\n" + line.text.substring(0, line.text.search(indentRegex));
933+
let indentedNewLine = "\n" + line.text.substring(0, line.text.search(indentRegex));
943934
let isOnEnter = this.getConfigurationValue("singleLineBlockOnEnter");
944935
if (!isOnEnter) {
945936
indentedNewLine += style + " ";

0 commit comments

Comments
 (0)