Skip to content

Commit

Permalink
refact: extract existence check of activeEditor
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshi389111 committed Mar 21, 2023
1 parent 1eafa5f commit 2b5cfa8
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,10 @@ export const activate = (context: vscode.ExtensionContext): void => {
})
);
context.subscriptions.push(...decorationTypes);
let activeEditor = vscode.window.activeTextEditor;

const REGEX_LINE = /^([ \t]*>)*[ \t]*\|[^\r\n]*$/mg;
const REGEX_COLUMN = /\|[^|\r\n]*(?=\|)/g;
const updateDecorations = (): void => {
const editor = activeEditor;
if (!editor) {
return;
}
const updateDecorations = (editor: vscode.TextEditor): void => {
const options: vscode.DecorationOptions[][] = decorationTypes.map(_ => []);
Array.from(editor.document.getText().matchAll(REGEX_LINE)).forEach(matchLine => {
Array.from(matchLine[0].matchAll(REGEX_COLUMN)).forEach((matchColumn, index) => {
Expand All @@ -42,16 +37,24 @@ export const activate = (context: vscode.ExtensionContext): void => {
);
};

let activeEditor = vscode.window.activeTextEditor;
const updateDecorationsIfPossible = (): void => {
const editor = activeEditor;
if (editor) {
updateDecorations(editor);
}
};

let timer: NodeJS.Timer | undefined = undefined;
const triggerUpdateDecorations = (throttle = false): void => {
if (timer) {
clearTimeout(timer);
timer = undefined;
}
if (throttle) {
timer = setTimeout(updateDecorations, updateDelay);
timer = setTimeout(updateDecorationsIfPossible, updateDelay);
} else {
updateDecorations();
updateDecorationsIfPossible();
}
};

Expand All @@ -61,9 +64,7 @@ export const activate = (context: vscode.ExtensionContext): void => {

vscode.window.onDidChangeActiveTextEditor(editor => {
activeEditor = editor;
if (editor) {
triggerUpdateDecorations();
}
triggerUpdateDecorations();
}, null, context.subscriptions);

vscode.workspace.onDidChangeTextDocument(event => {
Expand Down

0 comments on commit 2b5cfa8

Please sign in to comment.