Skip to content

Commit

Permalink
Introduce experimental fade out of unused variables
Browse files Browse the repository at this point in the history
Gated behind undocumented setting. Requires proper vscode API

Part of #15710
  • Loading branch information
mjbvz committed May 10, 2018
1 parent 5b6d54a commit 50a7fe9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import * as vscode from 'vscode';

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

public set(
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 })[] = [];
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}

Expand Down

0 comments on commit 50a7fe9

Please sign in to comment.