Introduce experimental fade out of unused variables

Gated behind undocumented setting. Requires proper vscode API

Part of #15710
This commit is contained in:
Matt Bierner
2018-04-10 14:33:41 -07:00
parent 5b6d54a4b2
commit 50a7fe9bce
3 changed files with 81 additions and 1 deletions

View File

@@ -5,7 +5,7 @@
import * as vscode from 'vscode';
class DiagnosticSet {
export class DiagnosticSet {
private _map: ObjectMap<vscode.Diagnostic[]> = Object.create(null);
public set(

View File

@@ -0,0 +1,73 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import { DiagnosticSet } from './diagnostics';
export class UnusedHighlighter {
private readonly _decorationType: vscode.TextEditorDecorationType;
private readonly _diagnostics = new DiagnosticSet();
private _validate: boolean = true;
constructor(
) {
this._decorationType = vscode.window.createTextEditorDecorationType({
opacity: '0.7'
});
}
public dispose() {
this._decorationType.dispose();
}
public reInitialize(): void {
this._diagnostics.clear();
for (const editor of vscode.window.visibleTextEditors) {
editor.setDecorations(this._decorationType, []);
}
}
public set validate(value: boolean) {
if (this._validate === value) {
return;
}
this._validate = value;
if (!value) {
for (const editor of vscode.window.visibleTextEditors) {
editor.setDecorations(this._decorationType, []);
}
}
}
public diagnosticsReceived(
file: vscode.Uri,
diagnostics: vscode.Diagnostic[]
): void {
// Undocumented flag to enable
if (!vscode.workspace.getConfiguration('typescript').get('showUnused.experimentalFade')) {
return;
}
this._diagnostics.set(file, diagnostics);
this._updateCurrentHighlights(file);
}
private _updateCurrentHighlights(file: vscode.Uri) {
for (const editor of vscode.window.visibleTextEditors) {
if (editor.document.uri.fsPath !== file.fsPath) {
continue;
}
const diagnostics = this._diagnostics.get(editor.document.uri);
if (diagnostics) {
editor.setDecorations(this._decorationType, diagnostics.map(x => x.range));
}
}
}
}

View File

@@ -21,6 +21,7 @@ import { CachedNavTreeResponse } from './features/baseCodeLensProvider';
import { memoize } from './utils/memoize';
import { disposeAll } from './utils/dipose';
import TelemetryReporter from './utils/telemetry';
import { UnusedHighlighter } from './features/unusedHighlighter';
const validateSetting = 'validate.enable';
const suggestionSetting = 'suggestionActions.enabled';
@@ -29,6 +30,7 @@ const foldingSetting = 'typescript.experimental.syntaxFolding';
export default class LanguageProvider {
private readonly diagnosticsManager: DiagnosticsManager;
private readonly bufferSyncSupport: BufferSyncSupport;
private readonly ununsedHighlighter: UnusedHighlighter;
private readonly fileConfigurationManager: FileConfigurationManager;
private readonly toUpdateOnConfigurationChanged: ({ updateConfiguration: () => void })[] = [];
@@ -56,6 +58,7 @@ export default class LanguageProvider {
}, this._validate);
this.diagnosticsManager = new DiagnosticsManager(description.diagnosticOwner);
this.ununsedHighlighter = new UnusedHighlighter();
workspace.onDidChangeConfiguration(this.configurationChanged, this, this.disposables);
this.configurationChanged();
@@ -226,6 +229,7 @@ export default class LanguageProvider {
public reInitialize(): void {
this.diagnosticsManager.reInitialize();
this.ununsedHighlighter.reInitialize();
this.bufferSyncSupport.reOpenDocuments();
this.bufferSyncSupport.requestAllDiagnostics();
this.fileConfigurationManager.reset();
@@ -261,6 +265,9 @@ export default class LanguageProvider {
public diagnosticsReceived(diagnosticsKind: DiagnosticKind, file: Uri, diagnostics: (Diagnostic & { reportUnnecessary: any })[]): void {
const config = workspace.getConfiguration(this.id);
const reportUnnecessary = config.get<boolean>('showUnused.enabled', true);
if (diagnosticsKind === DiagnosticKind.Suggestion) {
this.ununsedHighlighter.diagnosticsReceived(file, diagnostics.filter(diag => diag.reportUnnecessary));
}
this.diagnosticsManager.diagnosticsReceived(diagnosticsKind, file, diagnostics.filter(diag => diag.reportUnnecessary ? reportUnnecessary : true));
}