support deprecated tag

This commit is contained in:
Qiming Zhao 2021-07-27 23:03:19 +08:00
parent 5c7e44f36f
commit 8ba598b218
6 changed files with 50 additions and 10 deletions

View file

@ -265,6 +265,11 @@
"default": true, "default": true,
"description": "Show unused variable hint." "description": "Show unused variable hint."
}, },
"typescript.showDeprecated": {
"type": "boolean",
"default": true,
"description": "Show deprecated variable hint."
},
"typescript.updateImportsOnFileMove.enable": { "typescript.updateImportsOnFileMove.enable": {
"type": "boolean", "type": "boolean",
"default": true, "default": true,
@ -473,6 +478,11 @@
"default": true, "default": true,
"description": "Show unused variable hint." "description": "Show unused variable hint."
}, },
"javascript.showDeprecated": {
"type": "boolean",
"default": true,
"description": "Show deprecated variable hint."
},
"javascript.updateImportsOnFileMove.enable": { "javascript.updateImportsOnFileMove.enable": {
"type": "boolean", "type": "boolean",
"default": true "default": true

View file

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { DiagnosticCollection, languages, workspace } from 'coc.nvim' import { DiagnosticCollection, languages, workspace } from 'coc.nvim'
import { Diagnostic } from 'vscode-languageserver-protocol' import { Diagnostic, DiagnosticTag } from 'vscode-languageserver-protocol'
import { ResourceMap } from './resourceMap' import { ResourceMap } from './resourceMap'
export class DiagnosticSet { export class DiagnosticSet {
@ -146,8 +146,7 @@ export class DiagnosticsManager {
.get(uri) .get(uri)
.filter(x => { .filter(x => {
if (!enabled) { if (!enabled) {
// Still show unused return x.tags && (x.tags.includes(DiagnosticTag.Unnecessary) || x.tags.includes(DiagnosticTag.Deprecated))
return x.code == 6133
} }
return enabled return enabled
}) })

View file

@ -4,12 +4,17 @@
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { workspace } from 'coc.nvim' import { workspace } from 'coc.nvim'
import { WorkspaceSymbolProvider } from 'coc.nvim' import { WorkspaceSymbolProvider } from 'coc.nvim'
import { CancellationToken, Range, SymbolInformation, SymbolKind } from 'vscode-languageserver-protocol' import { CancellationToken, Range, SymbolInformation, SymbolKind, SymbolTag } from 'vscode-languageserver-protocol'
import * as Proto from '../protocol' import * as Proto from '../protocol'
import * as PConst from '../protocol.const'
import { ITypeScriptServiceClient } from '../typescriptService' import { ITypeScriptServiceClient } from '../typescriptService'
import API from '../utils/api' import API from '../utils/api'
import * as typeConverters from '../utils/typeConverters' import * as typeConverters from '../utils/typeConverters'
function parseKindModifier(kindModifiers: string): Set<string> {
return new Set(kindModifiers.split(/,|\s+/g))
}
function getSymbolKind(item: Proto.NavtoItem): SymbolKind { function getSymbolKind(item: Proto.NavtoItem): SymbolKind {
switch (item.kind) { switch (item.kind) {
case 'method': case 'method':
@ -75,7 +80,10 @@ export default class TypeScriptWorkspaceSymbolProvider implements WorkspaceSymbo
getSymbolKind(item), getSymbolKind(item),
range, range,
this.client.toResource(item.file)) this.client.toResource(item.file))
const kindModifiers = item.kindModifiers ? parseKindModifier(item.kindModifiers) : undefined
if (kindModifiers?.has(PConst.KindModifiers.deprecated)) {
symbolInfo.tags = [SymbolTag.Deprecated]
}
result.push(symbolInfo) result.push(symbolInfo)
} }
return result return result

View file

@ -179,17 +179,22 @@ export default class LanguageProvider {
public diagnosticsReceived( public diagnosticsReceived(
diagnosticsKind: DiagnosticKind, diagnosticsKind: DiagnosticKind,
file: Uri, file: Uri,
diagnostics: (Diagnostic & { reportUnnecessary: any })[] diagnostics: (Diagnostic & { reportUnnecessary: any, reportDeprecated: any })[]
): void { ): void {
const config = workspace.getConfiguration(this.id, file.toString()) const config = workspace.getConfiguration(this.id, file.toString())
const reportUnnecessary = config.get<boolean>('showUnused', true) const reportUnnecessary = config.get<boolean>('showUnused', true)
const reportDeprecated = config.get<boolean>('showDeprecated', true)
this.client.diagnosticsManager.diagnosticsReceived(diagnosticsKind, file.toString(), diagnostics.filter(diag => { this.client.diagnosticsManager.diagnosticsReceived(diagnosticsKind, file.toString(), diagnostics.filter(diag => {
if (!reportUnnecessary) { if (!reportUnnecessary) {
diag.tags = undefined
if (diag.reportUnnecessary && diag.severity === DiagnosticSeverity.Information) { if (diag.reportUnnecessary && diag.severity === DiagnosticSeverity.Information) {
return false return false
} }
} }
if (!reportDeprecated) {
if (diag.reportDeprecated && diag.severity === DiagnosticSeverity.Hint) {
return false
}
}
return true return true
})) }))
} }

View file

@ -3,7 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { disposeAll, languages, TextDocument, Uri, workspace } from 'coc.nvim' import { disposeAll, languages, TextDocument, Uri, workspace } from 'coc.nvim'
import { CancellationToken, Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, Disposable, Position, Range } from 'vscode-languageserver-protocol' import { CancellationToken, Diagnostic, DiagnosticRelatedInformation, DiagnosticSeverity, DiagnosticTag, Disposable, Position, Range } from 'vscode-languageserver-protocol'
import { flatten } from '../utils/arrays' import { flatten } from '../utils/arrays'
import { PluginManager } from '../utils/plugins' import { PluginManager } from '../utils/plugins'
import { DiagnosticKind } from './features/diagnostics' import { DiagnosticKind } from './features/diagnostics'
@ -217,11 +217,11 @@ export default class TypeScriptServiceClientHost implements Disposable {
} }
} }
private createMarkerDatas(diagnostics: Proto.Diagnostic[]): (Diagnostic & { reportUnnecessary: any })[] { private createMarkerDatas(diagnostics: Proto.Diagnostic[]): (Diagnostic & { reportUnnecessary: any, reportDeprecated: any })[] {
return diagnostics.map(tsDiag => this.tsDiagnosticToLspDiagnostic(tsDiag)) return diagnostics.map(tsDiag => this.tsDiagnosticToLspDiagnostic(tsDiag))
} }
private tsDiagnosticToLspDiagnostic(diagnostic: Proto.Diagnostic): (Diagnostic & { reportUnnecessary: any }) { private tsDiagnosticToLspDiagnostic(diagnostic: Proto.Diagnostic): (Diagnostic & { reportUnnecessary: any, reportDeprecated: any }) {
const { start, end, text } = diagnostic const { start, end, text } = diagnostic
const range = { const range = {
start: typeConverters.Position.fromLocation(start), start: typeConverters.Position.fromLocation(start),
@ -237,11 +237,22 @@ export default class TypeScriptServiceClientHost implements Disposable {
} }
}) })
} }
let tags: DiagnosticTag[] | undefined = []
if (diagnostic.reportsUnnecessary) {
tags.push(DiagnosticTag.Unnecessary)
}
if (diagnostic.reportsDeprecated) {
tags.push(DiagnosticTag.Deprecated)
}
tags = tags.length ? tags : undefined
return { return {
range, range,
tags,
message: text, message: text,
code: diagnostic.code ? diagnostic.code : null, code: diagnostic.code ? diagnostic.code : null,
severity: this.getDiagnosticSeverity(diagnostic), severity: this.getDiagnosticSeverity(diagnostic),
reportDeprecated: diagnostic.reportsDeprecated,
reportUnnecessary: diagnostic.reportsUnnecessary, reportUnnecessary: diagnostic.reportsUnnecessary,
source: diagnostic.source || 'tsserver', source: diagnostic.source || 'tsserver',
relatedInformation relatedInformation

View file

@ -3,6 +3,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information. * Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/ *--------------------------------------------------------------------------------------------*/
import { Range, CompletionItem, CompletionItemKind, InsertTextFormat, Position, TextEdit } from 'coc.nvim' import { Range, CompletionItem, CompletionItemKind, InsertTextFormat, Position, TextEdit } from 'coc.nvim'
import { CompletionItemTag } from 'vscode-languageserver-protocol'
import * as Proto from '../protocol' import * as Proto from '../protocol'
import * as PConst from '../protocol.const' import * as PConst from '../protocol.const'
@ -52,6 +53,7 @@ export function convertCompletionEntry(
let insertText = tsEntry.insertText let insertText = tsEntry.insertText
let commitCharacters = getCommitCharacters(tsEntry, context) let commitCharacters = getCommitCharacters(tsEntry, context)
let tags: CompletionItemTag[]
if (tsEntry.isImportStatementCompletion) { if (tsEntry.isImportStatementCompletion) {
insertText = label insertText = label
@ -75,6 +77,10 @@ export function convertCompletionEntry(
label += '?' label += '?'
} }
if (kindModifiers.has(PConst.KindModifiers.deprecated)) {
tags = [CompletionItemTag.Deprecated]
}
if (kindModifiers.has(PConst.KindModifiers.color)) { if (kindModifiers.has(PConst.KindModifiers.color)) {
kind = CompletionItemKind.Color kind = CompletionItemKind.Color
} }
@ -97,6 +103,7 @@ export function convertCompletionEntry(
insertText, insertText,
textEdit, textEdit,
kind, kind,
tags,
preselect, preselect,
insertTextFormat, insertTextFormat,
sortText, sortText,