Skip to content

Commit 50a7fe9

Browse files
committed
Introduce experimental fade out of unused variables
Gated behind undocumented setting. Requires proper vscode API Part of microsoft#15710
1 parent 5b6d54a commit 50a7fe9

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

extensions/typescript-language-features/src/features/diagnostics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import * as vscode from 'vscode';
77

8-
class DiagnosticSet {
8+
export class DiagnosticSet {
99
private _map: ObjectMap<vscode.Diagnostic[]> = Object.create(null);
1010

1111
public set(
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*---------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
*--------------------------------------------------------------------------------------------*/
5+
6+
import * as vscode from 'vscode';
7+
import { DiagnosticSet } from './diagnostics';
8+
9+
10+
export class UnusedHighlighter {
11+
12+
private readonly _decorationType: vscode.TextEditorDecorationType;
13+
14+
private readonly _diagnostics = new DiagnosticSet();
15+
private _validate: boolean = true;
16+
17+
constructor(
18+
) {
19+
this._decorationType = vscode.window.createTextEditorDecorationType({
20+
opacity: '0.7'
21+
});
22+
}
23+
24+
public dispose() {
25+
this._decorationType.dispose();
26+
}
27+
28+
public reInitialize(): void {
29+
this._diagnostics.clear();
30+
31+
for (const editor of vscode.window.visibleTextEditors) {
32+
editor.setDecorations(this._decorationType, []);
33+
}
34+
}
35+
36+
public set validate(value: boolean) {
37+
if (this._validate === value) {
38+
return;
39+
}
40+
41+
this._validate = value;
42+
if (!value) {
43+
for (const editor of vscode.window.visibleTextEditors) {
44+
editor.setDecorations(this._decorationType, []);
45+
}
46+
}
47+
}
48+
49+
public diagnosticsReceived(
50+
file: vscode.Uri,
51+
diagnostics: vscode.Diagnostic[]
52+
): void {
53+
// Undocumented flag to enable
54+
if (!vscode.workspace.getConfiguration('typescript').get('showUnused.experimentalFade')) {
55+
return;
56+
}
57+
this._diagnostics.set(file, diagnostics);
58+
this._updateCurrentHighlights(file);
59+
}
60+
61+
private _updateCurrentHighlights(file: vscode.Uri) {
62+
for (const editor of vscode.window.visibleTextEditors) {
63+
if (editor.document.uri.fsPath !== file.fsPath) {
64+
continue;
65+
}
66+
67+
const diagnostics = this._diagnostics.get(editor.document.uri);
68+
if (diagnostics) {
69+
editor.setDecorations(this._decorationType, diagnostics.map(x => x.range));
70+
}
71+
}
72+
}
73+
}

extensions/typescript-language-features/src/languageProvider.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { CachedNavTreeResponse } from './features/baseCodeLensProvider';
2121
import { memoize } from './utils/memoize';
2222
import { disposeAll } from './utils/dipose';
2323
import TelemetryReporter from './utils/telemetry';
24+
import { UnusedHighlighter } from './features/unusedHighlighter';
2425

2526
const validateSetting = 'validate.enable';
2627
const suggestionSetting = 'suggestionActions.enabled';
@@ -29,6 +30,7 @@ const foldingSetting = 'typescript.experimental.syntaxFolding';
2930
export default class LanguageProvider {
3031
private readonly diagnosticsManager: DiagnosticsManager;
3132
private readonly bufferSyncSupport: BufferSyncSupport;
33+
private readonly ununsedHighlighter: UnusedHighlighter;
3234
private readonly fileConfigurationManager: FileConfigurationManager;
3335

3436
private readonly toUpdateOnConfigurationChanged: ({ updateConfiguration: () => void })[] = [];
@@ -56,6 +58,7 @@ export default class LanguageProvider {
5658
}, this._validate);
5759

5860
this.diagnosticsManager = new DiagnosticsManager(description.diagnosticOwner);
61+
this.ununsedHighlighter = new UnusedHighlighter();
5962

6063
workspace.onDidChangeConfiguration(this.configurationChanged, this, this.disposables);
6164
this.configurationChanged();
@@ -226,6 +229,7 @@ export default class LanguageProvider {
226229

227230
public reInitialize(): void {
228231
this.diagnosticsManager.reInitialize();
232+
this.ununsedHighlighter.reInitialize();
229233
this.bufferSyncSupport.reOpenDocuments();
230234
this.bufferSyncSupport.requestAllDiagnostics();
231235
this.fileConfigurationManager.reset();
@@ -261,6 +265,9 @@ export default class LanguageProvider {
261265
public diagnosticsReceived(diagnosticsKind: DiagnosticKind, file: Uri, diagnostics: (Diagnostic & { reportUnnecessary: any })[]): void {
262266
const config = workspace.getConfiguration(this.id);
263267
const reportUnnecessary = config.get<boolean>('showUnused.enabled', true);
268+
if (diagnosticsKind === DiagnosticKind.Suggestion) {
269+
this.ununsedHighlighter.diagnosticsReceived(file, diagnostics.filter(diag => diag.reportUnnecessary));
270+
}
264271
this.diagnosticsManager.diagnosticsReceived(diagnosticsKind, file, diagnostics.filter(diag => diag.reportUnnecessary ? reportUnnecessary : true));
265272
}
266273

0 commit comments

Comments
 (0)