Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/check-ts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ on:

name: Compile TypeScript to check for errors
jobs:
deploy:
check-ts:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand Down
38 changes: 18 additions & 20 deletions src/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {Rules} from "./rules";
import {logger} from "./logger";
import * as utils from "./utils";
import {ExtensionData} from "./extensionData";
import {Settings} from "./interfaces/settings";

export class Configuration {
/**************
Expand Down Expand Up @@ -218,18 +219,16 @@ export class Configuration {
/**
* Get value of the specified key from the extension's user configuration settings.
*
* @param {string} key The key of the specific setting.
*
* @returns {T} Returns the value of the `key`.
* @param {K} key The key of the specific setting.
*
* NOTE: Return is typed as `T`, which is a generic type that represents the type that is declared when called (as explained in this StackOverflow answer: https://stackoverflow.com/a/49622066/2358222)
* @returns {Settings[K]} Returns the value of the `key` with proper typing.
*
* @example ```ts
* this.getConfigurationValue<string[]>("disabledLanguages");
* this.getConfigurationValue("disabledLanguages"); // Returns string[] with full type safety
* ```
*/
public getConfigurationValue<T>(key: string): T {
return this.getConfiguration().get<T>(key);
public getConfigurationValue<K extends keyof Settings>(key: K): Settings[K] {
return this.getConfiguration().get<Settings[K]>(key);
}

/**
Expand All @@ -254,7 +253,7 @@ export class Configuration {
* @returns {boolean}
*/
public isLangIdDisabled(langId: string): boolean {
return this.getConfigurationValue<string[]>("disabledLanguages").includes(langId);
return this.getConfigurationValue("disabledLanguages").includes(langId);
}

/**
Expand All @@ -264,7 +263,7 @@ export class Configuration {
* @returns {boolean}
*/
private isLangIdMultiLineCommentOverridden(langId: string): boolean {
const overriddenList = this.getConfigurationValue<string[]>("overrideDefaultLanguageMultiLineComments");
const overriddenList = this.getConfigurationValue("overrideDefaultLanguageMultiLineComments");

return overriddenList.hasOwnProperty(langId);
}
Expand All @@ -276,7 +275,7 @@ export class Configuration {
* @returns {string}
*/
private getOverriddenMultiLineComment(langId: string) {
const overriddenList = this.getConfigurationValue<string[]>("overrideDefaultLanguageMultiLineComments");
const overriddenList = this.getConfigurationValue("overrideDefaultLanguageMultiLineComments");

return overriddenList[langId];
}
Expand Down Expand Up @@ -552,7 +551,7 @@ export class Configuration {
// for sanity reasons.
this.multiLineBlocksMap.set("supportedLanguages", langArray.sort());

const multiLineStyleBlocksLangs = this.getConfigurationValue<string[]>("multiLineStyleBlocks");
const multiLineStyleBlocksLangs = this.getConfigurationValue("multiLineStyleBlocks");

// Empty the langArray to reuse it.
langArray = [];
Expand Down Expand Up @@ -622,7 +621,7 @@ export class Configuration {
tempMap.clear();

// Get user-customized langIds for the //-style and add to the map.
let customSlashLangs = this.getConfigurationValue<string[]>("slashStyleBlocks");
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
Expand All @@ -633,7 +632,7 @@ export class Configuration {
}

// Get user-customized langIds for the #-style and add to the map.
let customHashLangs = this.getConfigurationValue<string[]>("hashStyleBlocks");
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
Expand All @@ -644,7 +643,7 @@ export class Configuration {
}

// Get user-customized langIds for the ;-style and add to the map.
let customSemicolonLangs = this.getConfigurationValue<string[]>("semicolonStyleBlocks");
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
Expand Down Expand Up @@ -721,12 +720,11 @@ export class Configuration {
* Get the user settings/configuration and set the blade or html comments accordingly.
*/
if (langId === "blade") {
langConfig.comments.blockComment = this.setBladeComments(this.getConfigurationValue<boolean>("bladeOverrideComments"), true);
langConfig.comments.blockComment = this.setBladeComments(this.getConfigurationValue("bladeOverrideComments"), true);
}
}

let isOnEnter = this.getConfigurationValue<boolean>("singleLineBlockOnEnter");

let isOnEnter = this.getConfigurationValue("singleLineBlockOnEnter");
// Add the single-line onEnter rules to the langConfig.
//
// If isOnEnter is true AND singleLineStyle isn't false, i.e. is a string,
Expand Down Expand Up @@ -906,7 +904,7 @@ export class Configuration {
}

var indentedNewLine = "\n" + line.text.substring(0, line.text.search(indentRegex));
let isOnEnter = this.getConfigurationValue<boolean>("singleLineBlockOnEnter");
let isOnEnter = this.getConfigurationValue("singleLineBlockOnEnter");
if (!isOnEnter) {
indentedNewLine += style + " ";
}
Expand All @@ -928,7 +926,7 @@ export class Configuration {
// Only carry out function if languageId is blade.
if (langId === "blade" && !this.isLangIdDisabled(langId)) {
// Read current value
let isOverridden = this.getConfigurationValue<boolean>("bladeOverrideComments");
let isOverridden = this.getConfigurationValue("bladeOverrideComments");

if (isOverridden === false) {
// Update to true
Expand All @@ -938,7 +936,7 @@ export class Configuration {
this.updateConfigurationValue("bladeOverrideComments", false);
}
// Read new value
let bladeOverrideComments = this.getConfigurationValue<boolean>("bladeOverrideComments");
let bladeOverrideComments = this.getConfigurationValue("bladeOverrideComments");

// Set the comments for blade language.
this.setBladeComments(bladeOverrideComments);
Expand Down
4 changes: 2 additions & 2 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export function activate(context: vscode.ExtensionContext) {

const extensionDisplayName = extensionData.get("displayName");

let disabledLangConfig: string[] = configuration.getConfigurationValue<string[]>("disabledLanguages");
let disabledLangConfig: string[] = configuration.getConfigurationValue("disabledLanguages");

if (disabledLangConfig.length > 0) {
vscode.window.showInformationMessage(`${disabledLangConfig.join(", ")} languages are disabled for ${extensionDisplayName}.`);
Expand All @@ -41,7 +41,7 @@ export function activate(context: vscode.ExtensionContext) {
// If the affected setting is bladeOverrideComments...
if (event.affectsConfiguration(`${extensionName}.bladeOverrideComments`)) {
// Get the setting.
let bladeOverrideComments: boolean = configuration.getConfigurationValue<boolean>("bladeOverrideComments");
let bladeOverrideComments: boolean = configuration.getConfigurationValue("bladeOverrideComments");

configuration.setBladeComments(bladeOverrideComments);

Expand Down
10 changes: 10 additions & 0 deletions src/interfaces/settings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Settings {
singleLineBlockOnEnter: boolean;
disabledLanguages: string[];
slashStyleBlocks: string[];
hashStyleBlocks: string[];
semicolonStyleBlocks: string[];
multiLineStyleBlocks: string[];
overrideDefaultLanguageMultiLineComments: Record<string, string>;
bladeOverrideComments: boolean;
}