Tags, selectionRange & accessor support for document symbols

This commit is contained in:
Qiming Zhao 2021-07-22 21:34:47 +08:00
parent f76e310d42
commit 05c2a824a0
3 changed files with 48 additions and 13 deletions

View file

@ -344,7 +344,7 @@ export default class TypeScriptCompletionItemProvider implements CompletionItemP
if (!this.completeOption.importStatementSuggestions || !this.client.apiVersion.lt(API.v430)) {
return false
}
return pre === 'import ';
return pre === 'import '
}
return true

View file

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
import { TextDocument } from 'coc.nvim'
import { DocumentSymbolProvider } from 'coc.nvim'
import { CancellationToken, DocumentSymbol, Range, SymbolKind } from 'vscode-languageserver-protocol'
import { CancellationToken, DocumentSymbol, Range, SymbolKind, SymbolTag } from 'vscode-languageserver-protocol'
import * as Proto from '../protocol'
import * as PConst from '../protocol.const'
import { ITypeScriptServiceClient } from '../typescriptService'
@ -82,20 +82,15 @@ export default class TypeScriptDocumentSymbolProvider implements DocumentSymbolP
}
private static convertNavTree(
bucket: DocumentSymbol[],
output: DocumentSymbol[],
item: Proto.NavigationTree,
): boolean {
let shouldInclude = TypeScriptDocumentSymbolProvider.shouldInclueEntry(item)
const children = new Set(item.childItems || [])
for (const span of item.spans) {
const range = typeConverters.Range.fromTextSpan(span)
const symbolInfo = DocumentSymbol.create(
item.text,
'',
getSymbolKind(item.kind),
range,
range)
symbolInfo.children = children.size > 0 ? [] : null
const symbolInfo = TypeScriptDocumentSymbolProvider.convertSymbol(item, range)
if (children.size) symbolInfo.children = []
for (const child of children) {
if (child.spans.some(span => !!containsRange(range, typeConverters.Range.fromTextSpan(span)))) {
@ -106,13 +101,33 @@ export default class TypeScriptDocumentSymbolProvider implements DocumentSymbolP
}
if (shouldInclude) {
bucket.push(symbolInfo)
output.push(symbolInfo)
}
}
return shouldInclude
}
private static convertSymbol(item: Proto.NavigationTree, range: Range): DocumentSymbol {
const selectionRange = item.nameSpan ? typeConverters.Range.fromTextSpan(item.nameSpan) : range
let label = item.text
switch (item.kind) {
case PConst.Kind.memberGetAccessor: label = `(get) ${label}`; break
case PConst.Kind.memberSetAccessor: label = `(set) ${label}`; break
}
const symbolInfo = DocumentSymbol.create(
label,
'',
getSymbolKind(item.kind),
range,
containsRange(range, selectionRange) ? selectionRange : range)
const kindModifiers = parseKindModifier(item.kindModifiers)
if (kindModifiers.has(PConst.KindModifiers.deprecated)) {
symbolInfo.tags = [SymbolTag.Deprecated]
}
return symbolInfo
}
private static shouldInclueEntry(
item: Proto.NavigationTree | Proto.NavigationBarItem
): boolean {
@ -142,3 +157,7 @@ function containsRange(range: Range, otherRange: Range): boolean {
}
return true
}
function parseKindModifier(kindModifiers: string): Set<string> {
return new Set(kindModifiers.split(/,|\s+/g))
}

View file

@ -60,7 +60,7 @@ export class KindModifiers {
KindModifiers.tsxFile,
KindModifiers.jsFile,
KindModifiers.jsxFile,
KindModifiers.jsonFile,
KindModifiers.jsonFile
]
}
@ -72,3 +72,19 @@ export class DisplayPartKind {
public static readonly punctuation = 'punctuation'
public static readonly text = 'text'
}
export enum EventName {
syntaxDiag = 'syntaxDiag',
semanticDiag = 'semanticDiag',
suggestionDiag = 'suggestionDiag',
configFileDiag = 'configFileDiag',
telemetry = 'telemetry',
projectLanguageServiceState = 'projectLanguageServiceState',
projectsUpdatedInBackground = 'projectsUpdatedInBackground',
beginInstallTypes = 'beginInstallTypes',
endInstallTypes = 'endInstallTypes',
typesInstallerInitializationFailed = 'typesInstallerInitializationFailed',
surveyReady = 'surveyReady',
projectLoadingStart = 'projectLoadingStart',
projectLoadingFinish = 'projectLoadingFinish'
}