From 55d27459feea49093c0d2f50eaa65af0ed49b6ee Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 21 Apr 2024 11:27:51 +0300 Subject: [PATCH] fix: code lens should respect "errorLens.enabled" #206 --- .eslintrc.json | 2 +- src/codeLens.ts | 46 +++++++++++++------------- src/commands/codeLensOnClickCommand.ts | 10 ++++-- src/utils/vscodeUtils.ts | 11 ++++++ 4 files changed, 42 insertions(+), 27 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index fba82f2..430532b 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -732,7 +732,7 @@ "no-octal": 1, "no-octal-escape": 1, "no-param-reassign": [ - 1, + 0, { "props": true, "ignorePropertyModificationsFor": [ diff --git a/src/codeLens.ts b/src/codeLens.ts index 696f691..6270b05 100644 --- a/src/codeLens.ts +++ b/src/codeLens.ts @@ -1,10 +1,10 @@ import _ from 'lodash'; import { CommandId } from 'src/commands'; import { $config } from 'src/extension'; -import { CodeLens, EventEmitter, Location, Selection, languages, window, type CancellationToken, type CodeLensProvider, type Diagnostic, type Disposable, type Event, type ExtensionContext, type ProviderResult, type Range, type TextDocument, type Uri } from 'vscode'; -import { extUtils } from './utils/extUtils'; -import { utils } from 'src/utils/utils'; import { Constants } from 'src/types'; +import { utils } from 'src/utils/utils'; +import { CodeLens, EventEmitter, Location, languages, type CancellationToken, type CodeLensProvider, type Diagnostic, type Disposable, type Event, type ExtensionContext, type ProviderResult, type Range, type TextDocument, type Uri } from 'vscode'; +import { extUtils } from './utils/extUtils'; interface GroupedDiagnostic { range: Range; @@ -31,14 +31,6 @@ export class ErrorLensCodeLens implements CodeLensProvider { ]; } - static setCaretInEditor(range: Range): void { - const editor = window.activeTextEditor; - if (editor) { - editor.selection = new Selection(range.start, range.end); - editor.revealRange(range); - } - } - static formatDiagnostic(diagnostic: Diagnostic): string { return extUtils.prepareMessage({ template: $config.codeLensTemplate, @@ -96,8 +88,8 @@ export class ErrorLensCodeLens implements CodeLensProvider { /** * Called by Vscode to provide code lenses */ - public provideCodeLenses(document: TextDocument, _cancellationToken: CancellationToken): CodeLens[] | Thenable { - if (!$config.codeLensEnabled) { + provideCodeLenses(document: TextDocument, _cancellationToken: CancellationToken): CodeLens[] | Thenable { + if (!this.isEnabled()) { return []; } @@ -120,22 +112,30 @@ export class ErrorLensCodeLens implements CodeLensProvider { /** * Called by Vscode - AFAIK there is nothing to resolve */ - public resolveCodeLens(codeLens: CodeLens, _cancellationToken: CancellationToken): ProviderResult { + resolveCodeLens(codeLens: CodeLens, _cancellationToken: CancellationToken): ProviderResult { return codeLens; } - public dispose(): void { - for (const disposable of this.disposables) { - disposable?.dispose(); - } - this.disposables = []; + isEnabled(): boolean { + return ( + $config.enabled && + $config.codeLensEnabled + ); } - public update(): void { - if (!$config.codeLensEnabled) { - return; - } + update(): void { this.onDidChangeEventEmitter.fire(); } + + dispose(): void { + this.update(); + + setInterval(() => { + for (const disposable of this.disposables) { + disposable?.dispose(); + } + this.disposables = []; + }, 500); + } } diff --git a/src/commands/codeLensOnClickCommand.ts b/src/commands/codeLensOnClickCommand.ts index 911fb7d..744e1fb 100644 --- a/src/commands/codeLensOnClickCommand.ts +++ b/src/commands/codeLensOnClickCommand.ts @@ -1,7 +1,7 @@ -import { ErrorLensCodeLens } from 'src/codeLens'; import { CommandId } from 'src/commands'; import { $config } from 'src/extension'; import { Constants } from 'src/types'; +import { vscodeUtils } from 'src/utils/vscodeUtils'; import { commands, type Diagnostic, type Location } from 'vscode'; export function codeLensOnClickCommand(location: Location, diagnostics: Diagnostic[]): void { @@ -10,11 +10,15 @@ export function codeLensOnClickCommand(location: Location, diagnostics: Diagnost commands.executeCommand(Constants.OpenProblemsViewCommandId); break; case 'showQuickFix': - ErrorLensCodeLens.setCaretInEditor(diagnostics[0].range); + vscodeUtils.setCaretInEditor({ + range: diagnostics[0].range, + }); commands.executeCommand(Constants.QuickFixCommandId, diagnostics[0]); break; case 'searchForProblem': - ErrorLensCodeLens.setCaretInEditor(diagnostics[0].range); + vscodeUtils.setCaretInEditor({ + range: diagnostics[0].range, + }); commands.executeCommand(CommandId.SearchForProblem, diagnostics[0]); break; case 'none': diff --git a/src/utils/vscodeUtils.ts b/src/utils/vscodeUtils.ts index 5c7ef47..6757c7d 100644 --- a/src/utils/vscodeUtils.ts +++ b/src/utils/vscodeUtils.ts @@ -132,6 +132,16 @@ function getIndentationAtLine(document: TextDocument, lineNumber: number): strin return textLine.text.slice(0, textLine.firstNonWhitespaceCharacterIndex); } +function setCaretInEditor({ editor, range }: { editor?: TextEditor; range: Range }): void { + if (!editor) { + editor = window.activeTextEditor; + } + if (editor) { + editor.selection = new Selection(range.start, range.end); + editor.revealRange(range); + } +} + export const vscodeUtils = { updateGlobalSetting, toggleGlobalBooleanSetting, @@ -145,4 +155,5 @@ export const vscodeUtils = { openFileInVscode, revealLine, getIndentationAtLine, + setCaretInEditor, };