Skip to content

Commit

Permalink
Merge pull request #125 from nix6839/visible-editors
Browse files Browse the repository at this point in the history
Use visible editors
  • Loading branch information
moalamri authored Sep 21, 2023
2 parents 1e6cbc8 + 20d88d5 commit af01992
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 72 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

<br/>

> ### v0.2.5:
- feat: trigger folding when the editor is not active by [@nix6839](https://github.com/nix6839)

> ### v0.2.4:
- fix: [#108](https://github.com/moalamri/vscode-inline-fold/issues/108)
- fix: [#112](https://github.com/moalamri/vscode-inline-fold/issues/112)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Inline fold",
"description": "A custom decorator that \"fold\" matching content in single line",
"icon": "res/icon.png",
"version": "0.2.4",
"version": "0.2.5",
"publisher": "moalamri",
"homepage": "https://github.com/moalamri/vscode-inline-fold",
"bugs": "https://github.com/moalamri/vscode-inline-fold/issues",
Expand Down
38 changes: 21 additions & 17 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { window } from "vscode";
import { Settings } from "./enums";
import { ExtSettings } from "./settings";

Expand All @@ -8,43 +7,48 @@ class CacheClass {
this._instance = this._instance ? this._instance : new CacheClass();
return this._instance;
}
StateCache: Map<string, boolean> = new Map<string, boolean>();
CacheMap = new Map<string | undefined, boolean>();

// Get the autoFold setting
autoFold() {
return ExtSettings.Get<boolean>(Settings.autoFold);
private autoFold(langId?: string) {
return ExtSettings.Get<boolean>(Settings.autoFold, langId);
}

// Get the togglePerFile setting
private togglePerFile(langId?: string) {
return ExtSettings.Get<boolean>(Settings.togglePerFile, langId)
}

// Set the state of the extension
set State(_state: boolean) {
if (ExtSettings.Get<boolean>(Settings.togglePerFile)) {
this.StateCache.set(window.activeTextEditor?.document.uri.path, _state);
public SetShouldFold(key: string | undefined, shouldToggle: boolean, langId?: string) {
if (this.togglePerFile(langId)) {
this.CacheMap.set(key, shouldToggle);
} else {
this.StateCache.set("global", _state);
this.CacheMap.set("global", shouldToggle);
}
}

// Get the state of the extension
get State(): boolean {
if (ExtSettings.Get<boolean>(Settings.togglePerFile)) {
return this.StateCache.get(window.activeTextEditor?.document.uri.path) ?? this.autoFold();
public ShouldFold(key: string | undefined, langId?: string): boolean {
if (this.togglePerFile(langId)) {
return this.CacheMap.get(key) ?? this.autoFold(langId);
} else {
return this.StateCache.get("global") ?? this.autoFold();
return this.CacheMap.get("global") ?? this.autoFold(langId);
}
}

// Toggle the state of the extension
public ToggleState() {
this.State = !this.State;
public ToggleShouldFold(key: string | undefined, langId?: string) {
this.SetShouldFold(key, !this.ShouldFold(key, langId), langId)
}

// Clear the state cache
public ClearCache() {
this.StateCache.clear();
public Clear() {
this.CacheMap.clear();
}

constructor () { }
}

// We will use singleton pattern to make sure that we only have one instance of the state
export const Cache = CacheClass.Instance;
export const Cache = CacheClass.Instance;
21 changes: 10 additions & 11 deletions src/decoration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ExtSettings } from "./settings";
* on demand.
*/
export class DecoratorTypeOptions {
private cache = new Map<string, TextEditorDecorationType>();
private cache = new Map<string | undefined, TextEditorDecorationType>();

public ClearCache() {
this.cache.forEach((decOp) => {
Expand All @@ -20,21 +20,21 @@ export class DecoratorTypeOptions {
this.cache.clear();
}

public UnfoldDecorationType = (): TextEditorDecorationType => {
public UnfoldDecorationType = (langId?: string): TextEditorDecorationType => {
return window.createTextEditorDecorationType({
rangeBehavior: DecorationRangeBehavior.ClosedOpen,
opacity: ExtSettings.Get<string>(Settings.unfoldedOpacity).toString()
opacity: ExtSettings.Get<string>(Settings.unfoldedOpacity, langId).toString()
})
}

public MatchedDecorationType = (): TextEditorDecorationType => {
public MatchedDecorationType = (langId?: string): TextEditorDecorationType => {
return window.createTextEditorDecorationType({
before: {
contentText: ExtSettings.Get<string>(Settings.maskChar),
color: ExtSettings.Get<string>(Settings.maskColor),
contentText: ExtSettings.Get<string>(Settings.maskChar, langId),
color: ExtSettings.Get<string>(Settings.maskColor, langId),
},
after: {
contentText: ExtSettings.Get<string>(Settings.after),
contentText: ExtSettings.Get<string>(Settings.after, langId),
},
textDecoration: "none; display: none;"
});
Expand All @@ -43,16 +43,15 @@ export class DecoratorTypeOptions {

public PlainDecorationType = (): TextEditorDecorationType => window.createTextEditorDecorationType({})

public MaskDecorationTypeCache(): TextEditorDecorationType {
const langId = window.activeTextEditor.document.languageId;
public MaskDecorationTypeCache(langId?: string): TextEditorDecorationType {
if (this.cache.has(langId)) {
return this.cache.get(langId) as TextEditorDecorationType;
}
const decorationType = this.MatchedDecorationType();
const decorationType = this.MatchedDecorationType(langId);
this.cache.set(langId, decorationType);
return decorationType;
}

constructor () { }

}
}
32 changes: 12 additions & 20 deletions src/decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ export class Decorator {
EndLine: number = 0;

/**
* To set/update the current working text editor.
* It's neccessary to call this method when the active editor changes
* because somethimes it return as undefined.
* To set/update the current visible text editor.
* @param textEditor TextEditor
*/
activeEditor(textEditor: TextEditor) {
editor(textEditor: TextEditor) {
if (!textEditor) return;
this.CurrentEditor = textEditor;
this.startLine(textEditor.visibleRanges[0].start.line);
Expand Down Expand Up @@ -53,14 +51,6 @@ export class Decorator {
}
}

/**
* Set the active state of the decorator (used for command)
*/
toggle() {
Cache.ToggleState();
this.updateDecorations();
}

/**
* This method gets triggered when the extension settings are changed
* @param extConfs: Workspace configs
Expand All @@ -72,17 +62,19 @@ export class Decorator {
}

updateDecorations() {
if (!this.SupportedLanguages.includes(this.CurrentEditor.document.languageId)) {
const currentLangId = this.CurrentEditor.document.languageId

if (!this.SupportedLanguages.includes(currentLangId)) {
return;
}

const regEx: RegExp = ExtSettings.Regex();
const unFoldOnLineSelect = ExtSettings.Get<boolean>(Settings.unfoldOnLineSelect);
const regEx: RegExp = ExtSettings.Regex(currentLangId);
const unFoldOnLineSelect = ExtSettings.Get<boolean>(Settings.unfoldOnLineSelect, currentLangId);
const text = this.CurrentEditor.document.getText();
const regexGroup: number = ExtSettings.Get<number>(Settings.regexGroup) as number | 1;
const matchDecorationType = this.DTOs.MaskDecorationTypeCache();
const regexGroup: number = ExtSettings.Get<number>(Settings.regexGroup, currentLangId) as number | 1;
const matchDecorationType = this.DTOs.MaskDecorationTypeCache(currentLangId);
const plainDecorationType = this.DTOs.PlainDecorationType();
const unfoldDecorationType = this.DTOs.UnfoldDecorationType();
const unfoldDecorationType = this.DTOs.UnfoldDecorationType(currentLangId);
const foldRanges: DecorationOptions[] = [];
const unfoldRanges: Range[] = [];

Expand All @@ -100,7 +92,7 @@ export class Decorator {

/* Checking if the toggle command is active or not. without conflicts with default state settings.
If it is not active, it will remove all decorations. */
if (!Cache.State) {
if (!Cache.ShouldFold(this.CurrentEditor.document.uri.path, currentLangId)) {
this.CurrentEditor.setDecorations(plainDecorationType, []);
break;
}
Expand All @@ -110,7 +102,7 @@ export class Decorator {
continue;
}

/* Checking if the range is selected by the user.
/* Checking if the range is selected by the user.
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) ||
Expand Down
19 changes: 10 additions & 9 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,22 @@ export function activate(context: ExtensionContext) {
elimit.Lead();

function triggerUpdateDecorations(): void {
const textEditor = window.activeTextEditor;
if (!textEditor) return;
decorator.activeEditor(textEditor);
for (const textEditor of window.visibleTextEditors) {
decorator.editor(textEditor);
}
}

const toggleCommand = commands.registerCommand(Commands.InlineFoldToggle, () => {
decorator.toggle();
Cache.ToggleShouldFold(window.activeTextEditor?.document.uri.path, window.activeTextEditor?.document.languageId)
triggerUpdateDecorations()
});

const clearCacheCommand = commands.registerCommand(Commands.InlineFoldClearCache, () => {
Cache.ClearCache();
Cache.Clear();
});

const activeTextEditor = window.onDidChangeActiveTextEditor((e) => {
if (!e) return;
const changeVisibleTextEditors = window.onDidChangeVisibleTextEditors((editors) => {
if (editors.length < 1) return;
elimit.Trail();
});

Expand All @@ -53,7 +54,7 @@ export function activate(context: ExtensionContext) {
const changeConfiguration = workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration(Settings.identifier)) {
if (!event.affectsConfiguration(Settings.autoFold)) {
Cache.ClearCache();
Cache.Clear();
}
decorator.updateConfigs(workspace.getConfiguration(Settings.identifier));
}
Expand All @@ -64,7 +65,7 @@ export function activate(context: ExtensionContext) {
context.subscriptions.push(changeText);
context.subscriptions.push(toggleCommand);
context.subscriptions.push(changeSelection);
context.subscriptions.push(activeTextEditor);
context.subscriptions.push(changeVisibleTextEditors);
context.subscriptions.push(clearCacheCommand);
context.subscriptions.push(changeVisibleRange);
context.subscriptions.push(changeConfiguration);
Expand Down
24 changes: 10 additions & 14 deletions src/settings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { window, workspace, WorkspaceConfiguration } from "vscode";
import { workspace, WorkspaceConfiguration } from "vscode";
import { Settings } from "./enums";

class ExtensionSettings {
Expand All @@ -24,15 +24,11 @@ class ExtensionSettings {
* @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;
private getPerLanguage<T>(_section: string, languageId: string) {
// Get the configuration of the active language id
const lang_config: WorkspaceConfiguration = workspace.getConfiguration(Settings.identifier, { languageId: language_Id });
const langConfig: WorkspaceConfiguration = workspace.getConfiguration(Settings.identifier, { languageId });
// return the configuration for the given section
return lang_config.get<T>(_section);
return langConfig.get<T>(_section);
}

/**
Expand All @@ -58,13 +54,13 @@ class ExtensionSettings {
* @param _section : the key of the configuration
* @returns Language's scoped configuration or fall back to global configuration
*/
public Get<T>(_section: Settings): T {
public Get<T>(_section: Settings, langId?: string): T {
// Try to get language scope configuration, otherwise fallback to global configuration
const getGlobal = this.configs.get(Settings.useGlobal)
if (getGlobal) {
return this.configs.get<T>(_section) as T
if (getGlobal || langId === undefined) {
return this.configs.get<T>(_section) as T;
}
return (this.getPerLanguage<T>(_section) as T) ?? (this.configs.get<T>(_section) as T);
return this.getPerLanguage<T>(_section, langId);
}

/**
Expand All @@ -79,8 +75,8 @@ class ExtensionSettings {
return supported;
}

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

constructor () { }
Expand Down

0 comments on commit af01992

Please sign in to comment.