Skip to content

Commit

Permalink
fix: code lens should respect "errorLens.enabled" #206
Browse files Browse the repository at this point in the history
  • Loading branch information
usernamehw committed Apr 21, 2024
1 parent 571a839 commit 55d2745
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 27 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@
"no-octal": 1,
"no-octal-escape": 1,
"no-param-reassign": [
1,
0,
{
"props": true,
"ignorePropertyModificationsFor": [
Expand Down
46 changes: 23 additions & 23 deletions src/codeLens.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -96,8 +88,8 @@ export class ErrorLensCodeLens implements CodeLensProvider {
/**
* Called by Vscode to provide code lenses
*/
public provideCodeLenses(document: TextDocument, _cancellationToken: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
if (!$config.codeLensEnabled) {
provideCodeLenses(document: TextDocument, _cancellationToken: CancellationToken): CodeLens[] | Thenable<CodeLens[]> {
if (!this.isEnabled()) {
return [];
}

Expand All @@ -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<CodeLens> {
resolveCodeLens(codeLens: CodeLens, _cancellationToken: CancellationToken): ProviderResult<CodeLens> {
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);
}
}

10 changes: 7 additions & 3 deletions src/commands/codeLensOnClickCommand.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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':
Expand Down
11 changes: 11 additions & 0 deletions src/utils/vscodeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -145,4 +155,5 @@ export const vscodeUtils = {
openFileInVscode,
revealLine,
getIndentationAtLine,
setCaretInEditor,
};

0 comments on commit 55d2745

Please sign in to comment.