Skip to content

Commit

Permalink
Add setting to control show unused variables
Browse files Browse the repository at this point in the history
  • Loading branch information
mjbvz committed May 10, 2018
1 parent eced4d1 commit 1a820a0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 19 deletions.
12 changes: 12 additions & 0 deletions extensions/typescript-language-features/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,18 @@
"default": "auto",
"description": "%typescript.preferences.importModuleSpecifier%",
"scope": "resource"
},
"javascript.showUnused.enabled": {
"type": "boolean",
"default": true,
"description": "%typescript.showUnused.enabled%",
"scope": "resource"
},
"typescript.showUnused.enabled": {
"type": "boolean",
"default": true,
"description": "%typescript.showUnused.enabled%",
"scope": "resource"
}
}
},
Expand Down
3 changes: 2 additions & 1 deletion extensions/typescript-language-features/package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,6 @@
"javascript.suggestionActions.enabled": "Enable/disable suggestion diagnostics for JavaScript files in the editor. Requires TypeScript >= 2.8",
"typescript.suggestionActions.enabled": "Enable/disable suggestion diagnostics for TypeScript files in the editor. Requires TypeScript >= 2.8",
"typescript.preferences.quoteStyle": "Preferred quote style to use for quick fixes: 'single' quotes, 'double' quotes, or 'auto' infer quote type from existing imports. Requires TS >= 2.9",
"typescript.preferences.importModuleSpecifier": "Preferred path style for auto imports: 'relative' paths, 'non-relative' paths, or 'auto' infer the shortest path type. Requires TS >= 2.9"
"typescript.preferences.importModuleSpecifier": "Preferred path style for auto imports: 'relative' paths, 'non-relative' paths, or 'auto' infer the shortest path type. Requires TS >= 2.9",
"typescript.showUnused.enabled": "Enable/disable highlighting of unused variables in code. Requires TypeScript >= 2.9"
}
27 changes: 17 additions & 10 deletions extensions/typescript-language-features/src/features/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ export class DiagnosticsManager {
public diagnosticsReceived(
kind: DiagnosticKind,
file: vscode.Uri,
syntaxDiagnostics: vscode.Diagnostic[]
diagnostics: vscode.Diagnostic[]
): void {
const diagnostics = this._diagnostics.get(kind);
if (diagnostics) {
diagnostics.set(file, syntaxDiagnostics);
const collection = this._diagnostics.get(kind);
if (collection) {
collection.set(file, diagnostics);
this.updateCurrentDiagnostics(file);
}
}
Expand All @@ -112,15 +112,22 @@ export class DiagnosticsManager {
return;
}

const allDiagnostics: vscode.Diagnostic[] = [];
allDiagnostics.push(...this._diagnostics.get(DiagnosticKind.Syntax)!.get(file));
allDiagnostics.push(...this._diagnostics.get(DiagnosticKind.Semantic)!.get(file));
if (this._enableSuggestions) {
allDiagnostics.push(...this._diagnostics.get(DiagnosticKind.Suggestion)!.get(file));
}
const allDiagnostics = [
...this._diagnostics.get(DiagnosticKind.Syntax)!.get(file),
...this._diagnostics.get(DiagnosticKind.Semantic)!.get(file),
...this.getSuggestionDiagnostics(file),
];
this._currentDiagnostics.set(file, allDiagnostics);
}

private getSuggestionDiagnostics(file: vscode.Uri) {
if (!this._enableSuggestions) {
return [];
}

return this._diagnostics.get(DiagnosticKind.Suggestion)!.get(file);
}

public getDiagnostics(file: vscode.Uri): vscode.Diagnostic[] {
return this._currentDiagnostics.get(file) || [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ export default class LanguageProvider {
}

private configurationChanged(): void {
const config = workspace.getConfiguration(this.id);
const config = workspace.getConfiguration(this.id, null);
this.updateValidate(config.get(validateSetting, true));
this.updateSuggestionDiagnostics(config.get(suggestionSetting, true));

Expand Down Expand Up @@ -258,8 +258,10 @@ export default class LanguageProvider {
this.bufferSyncSupport.requestAllDiagnostics();
}

public diagnosticsReceived(diagnosticsKind: DiagnosticKind, file: Uri, syntaxDiagnostics: Diagnostic[]): void {
this.diagnosticsManager.diagnosticsReceived(diagnosticsKind, file, syntaxDiagnostics);
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);
this.diagnosticsManager.diagnosticsReceived(diagnosticsKind, file, diagnostics.filter(diag => diag.reportUnnecessary ? reportUnnecessary : true));
}

public configFileDiagnosticsReceived(file: Uri, diagnostics: Diagnostic[]): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ export default class TypeScriptServiceClientHost {
}

private configurationChanged(): void {
const config = workspace.getConfiguration('typescript');
this.reportStyleCheckAsWarnings = config.get('reportStyleChecksAsWarnings', true);
const typescriptConfig = workspace.getConfiguration('typescript');

this.reportStyleCheckAsWarnings = typescriptConfig.get('reportStyleChecksAsWarnings', true);
}

private async findLanguage(resource: Uri): Promise<LanguageProvider | undefined> {
Expand Down Expand Up @@ -239,11 +240,14 @@ export default class TypeScriptServiceClientHost {
});
}

private createMarkerDatas(diagnostics: Proto.Diagnostic[], source: string): Diagnostic[] {
private createMarkerDatas(
diagnostics: Proto.Diagnostic[],
source: string
): (Diagnostic & { reportUnnecessary: any })[] {
return diagnostics.map(tsDiag => this.tsDiagnosticToVsDiagnostic(tsDiag, source));
}

private tsDiagnosticToVsDiagnostic(diagnostic: Proto.Diagnostic, source: string) {
private tsDiagnosticToVsDiagnostic(diagnostic: Proto.Diagnostic, source: string): Diagnostic & { reportUnnecessary: any } {
const { start, end, text } = diagnostic;
const range = new Range(typeConverters.Position.fromLocation(start), typeConverters.Position.fromLocation(end));
const converted = new Diagnostic(range, text);
Expand All @@ -252,7 +256,8 @@ export default class TypeScriptServiceClientHost {
if (diagnostic.code) {
converted.code = diagnostic.code;
}
return converted;
(converted as Diagnostic & { reportUnnecessary: any }).reportUnnecessary = diagnostic.reportsUnnecessary;
return converted as Diagnostic & { reportUnnecessary: any };
}

private getDiagnosticSeverity(diagnostic: Proto.Diagnostic): DiagnosticSeverity {
Expand Down

0 comments on commit 1a820a0

Please sign in to comment.