Skip to content

Commit

Permalink
Merge pull request #81 from Zer0xTJ/dev/0.2.0
Browse files Browse the repository at this point in the history
language-specific settings
  • Loading branch information
moalamri authored Jan 18, 2023
2 parents 545dceb + c4567f2 commit 41ef2a3
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 15 deletions.
66 changes: 66 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"inlineFold.autoFold": {
"type": "boolean",
"order": 0,
"scope": "language-overridable",
"description": "Set the default state of inline fold when opening a file",
"default": true
},
Expand Down Expand Up @@ -75,6 +76,7 @@
"type": "string",
"order": 2,
"description": "Regex to match",
"scope": "language-overridable",
"default": "(class|className)=(({(`|))|(['\"`]))(.*?)(\\2|(\\4)})",
"examples": [
"(class|className)=(({(`|))|(['\"`]))(.*?)(\\2|(\\4)})",
Expand All @@ -85,6 +87,7 @@
"inlineFold.regexFlags": {
"type": "string",
"order": 3,
"scope": "language-overridable",
"description": "Regex flags",
"default": "g",
"examples": [
Expand All @@ -95,6 +98,7 @@
"inlineFold.regexGroup": {
"type": "string",
"order": 4,
"scope": "language-overridable",
"description": "Regex capture group number for content that will be folded",
"default": "6",
"examples": [
Expand All @@ -109,6 +113,7 @@
"inlineFold.maskChar": {
"type": "string",
"order": 5,
"scope": "language-overridable",
"description": "The mask that covers the folded text",
"default": "◦◦◦",
"examples": [
Expand All @@ -120,6 +125,7 @@
"inlineFold.maskColor": {
"type": "string",
"order": 6,
"scope": "language-overridable",
"description": "The color of the covering mask in hex",
"default": "#68D7AC",
"examples": [
Expand All @@ -130,6 +136,7 @@
"inlineFold.unfoldedOpacity": {
"type": "number",
"order": 7,
"scope": "language-overridable",
"description": "The opacity of the unfolded text (from 0 to 1)",
"default": 0.7,
"enum": [
Expand All @@ -149,6 +156,7 @@
"inlineFold.after": {
"type": "string",
"order": 8,
"scope": "language-overridable",
"description": "(Optional) Add any text/character to be appended to the folded text",
"default": "",
"examples": [
Expand All @@ -159,6 +167,7 @@
},
"inlineFold.unfoldOnLineSelect": {
"type": "boolean",
"scope": "language-overridable",
"order": 9,
"description": "(Optional) unfold the line when any part of the line is selected",
"default": false
Expand Down
14 changes: 7 additions & 7 deletions src/decorator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Position, Range, TextEditor, WorkspaceConfiguration } from "vscode";
import { Position, Range, TextEditor, WorkspaceConfiguration, window } from "vscode";
import { Cache } from "./cache";
import { DecoratorTypeOptions } from "./decoration";
import { Settings } from "./enums";
Expand Down Expand Up @@ -67,12 +67,12 @@ export class Decorator {
*/
updateConfigs(extConfs: WorkspaceConfiguration) {
ExtSettings.Update(extConfs);
this.SupportedLanguages = ExtSettings.Get<string[]>(Settings.supportedLanguages);
this.SupportedLanguages = ExtSettings.GetSupportedLanguages();
this.DTOs.ClearCache();
}

updateDecorations() {
if (!this.SupportedLanguages || !this.SupportedLanguages.includes(this.CurrentEditor.document.languageId)) {
if (!this.SupportedLanguages.includes(this.CurrentEditor.document.languageId)) {
return;
}

Expand All @@ -90,8 +90,8 @@ export class Decorator {
while (match = regEx.exec(text)) {

// if the matched content is undefined, skip it and continue to the next match
if(match && !match[regexGroup]) continue;
if (match && !match[regexGroup]) continue;

const matched = match[regexGroup];
const foldIndex = match[0].lastIndexOf(matched);
const startPosition = this.startPositionLine(match.index, foldIndex);
Expand All @@ -114,8 +114,8 @@ export class Decorator {
first check is for single selection, second is for multiple cursor selections.
or if the user has enabled the unfoldOnLineSelect option. */
if (this.CurrentEditor.selection.contains(range) ||
this.CurrentEditor.selections.find(s => range.contains(s)) ||
unFoldOnLineSelect && this.CurrentEditor.selections.find(s => s.start.line === range.start.line)) {
this.CurrentEditor.selections.find(s => range.contains(s)) ||
unFoldOnLineSelect && this.CurrentEditor.selections.find(s => s.start.line === range.start.line)) {
unfoldRanges.push(range);
} else {
foldRanges.push(range);
Expand Down
60 changes: 52 additions & 8 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WorkspaceConfiguration } from "vscode";
import { window, workspace, WorkspaceConfiguration } from "vscode";
import { Settings } from "./enums";

class ExtensionSettings {
Expand All @@ -11,32 +11,76 @@ class ExtensionSettings {
}
private configs: WorkspaceConfiguration;


/**
* Gets triggered when any of the extension configuration changes.
* @param _configs : the key of the configuration
* @param _languageid : editor's active language id
*/
public Update(_configs: WorkspaceConfiguration, _languageid?: string) {
public Update(_configs: WorkspaceConfiguration) {
this.configs = _configs;
}

/**
* Get the extension's configs
* @param _section The extension's identifier
* @returns Workspace's configuration for the extension
*/
private getPerLanguage<T>(_section: string) {
// Check if the window has an active editor (document file)
if (!window.activeTextEditor) return;
// Get the current active editor languageid
const language_Id = window.activeTextEditor.document.languageId;
// Get the configuration of the active language id
const lang_config: WorkspaceConfiguration = workspace.getConfiguration(Settings.identifier, { languageId: language_Id });
// return the configuration for the given section
return lang_config.get<T>(_section);
}

/**
* Get list of language ids that has configuration
* @returns List of language ids
*/
private getLanguagesWithRegex(): string[] {
// skip unnecessary keys excludedSettings
const excludedSettings: string[] = ["inlineFold", "supportedLanguages"];
const settings = Object.values(Settings).filter((v) => !excludedSettings.includes(v));
const langs = [];
settings.forEach((v) => {
const lang = this.configs.inspect(v).languageIds;
if (lang) {
langs.push(...lang);
}
});
return langs;
}

/**
* Get typed configuration for the given settings key
* @param _section : the key of the configuration
* @param _languageId : editor's active language id
* @returns Language's scoped configuration or fall back to global configuration
*/
public Get<T>(_section: Settings): T {
return this.configs.get<T>(_section) as T;
// Try to get language scope configuration, otherwise fallback to global configuration
return (this.getPerLanguage<T>(_section) as T) ?? (this.configs.get<T>(_section) as T);
}

/**
* Get supported langauges from global configuration and add any language id
* that has configuration for the extension's regex and regex group
* @returns List all of the supported language ids
*/
public GetSupportedLanguages(): string[] {
const langs: string[] = this.Get<string[]>(Settings.supportedLanguages);
const withLangs: string[] = this.getLanguagesWithRegex();
const supported: string[] = [...new Set([...langs, ...withLangs])];
return supported;
}

public Regex(): RegExp {
return RegExp(this.Get<RegExp>(Settings.regex), this.Get<string>(Settings.regexFlags));
}

constructor () {}
constructor() {}
}

// Yes, a singleton :0
export const ExtSettings = ExtensionSettings.instance;
export const ExtSettings = ExtensionSettings.instance;

0 comments on commit 41ef2a3

Please sign in to comment.