use new API for documentHighlight

This commit is contained in:
chemzqm 2019-06-09 23:01:07 +08:00
parent 046670f54b
commit a3d81e2cb8
2 changed files with 21 additions and 23 deletions

View file

@ -7,6 +7,7 @@ import { DocumentHighlightProvider } from 'coc.nvim/lib/provider'
import Proto from '../protocol' import Proto from '../protocol'
import { ITypeScriptServiceClient } from '../typescriptService' import { ITypeScriptServiceClient } from '../typescriptService'
import * as typeConverters from '../utils/typeConverters' import * as typeConverters from '../utils/typeConverters'
import { flatten } from '../../utils/arrays'
export default class TypeScriptDocumentHighlightProvider implements DocumentHighlightProvider { export default class TypeScriptDocumentHighlightProvider implements DocumentHighlightProvider {
public constructor(private readonly client: ITypeScriptServiceClient) { } public constructor(private readonly client: ITypeScriptServiceClient) { }
@ -19,30 +20,28 @@ export default class TypeScriptDocumentHighlightProvider implements DocumentHigh
const file = this.client.toPath(resource.uri) const file = this.client.toPath(resource.uri)
if (!file) return [] if (!file) return []
const args = typeConverters.Position.toFileLocationRequestArgs( const args = {
file, ...typeConverters.Position.toFileLocationRequestArgs(file, position),
position filesToSearch: [file]
)
try {
const response = await this.client.execute('occurrences', args, token)
if (response.type == 'response' && response.body) {
return response.body
.filter(x => !x.isInString)
.map(documentHighlightFromOccurance)
} }
} catch { const response = await this.client.execute('documentHighlights', args, token)
// noop if (response.type !== 'response' || !response.body) {
}
return [] return []
} }
}
function documentHighlightFromOccurance( return flatten(
occurrence: Proto.OccurrencesResponseItem // tslint:disable-line response.body
): DocumentHighlight { .filter(highlight => highlight.file === file)
return { .map(convertDocumentHighlight))
range: typeConverters.Range.fromTextSpan(occurrence),
kind: occurrence.isWriteAccess ? DocumentHighlightKind.Write : DocumentHighlightKind.Read
} }
} }
function convertDocumentHighlight(highlight: Proto.DocumentHighlightsItem): ReadonlyArray<DocumentHighlight> {
return highlight.highlightSpans.map(span => {
return {
range: typeConverters.Range.fromTextSpan(span),
kind: span.kind === 'writtenReference' ? DocumentHighlightKind.Write : DocumentHighlightKind.Read
}
})
}

View file

@ -55,13 +55,12 @@ export interface TypeScriptRequestTypes {
'jsxClosingTag': [Proto.JsxClosingTagRequestArgs, Proto.JsxClosingTagResponse] 'jsxClosingTag': [Proto.JsxClosingTagRequestArgs, Proto.JsxClosingTagResponse]
'navto': [Proto.NavtoRequestArgs, Proto.NavtoResponse] 'navto': [Proto.NavtoRequestArgs, Proto.NavtoResponse]
'navtree': [Proto.FileRequestArgs, Proto.NavTreeResponse] 'navtree': [Proto.FileRequestArgs, Proto.NavTreeResponse]
// tslint:disable-next-line: deprecation
'occurrences': [Proto.FileLocationRequestArgs, Proto.OccurrencesResponse]
'organizeImports': [Proto.OrganizeImportsRequestArgs, Proto.OrganizeImportsResponse] 'organizeImports': [Proto.OrganizeImportsRequestArgs, Proto.OrganizeImportsResponse]
'projectInfo': [Proto.ProjectInfoRequestArgs, Proto.ProjectInfoResponse] 'projectInfo': [Proto.ProjectInfoRequestArgs, Proto.ProjectInfoResponse]
'quickinfo': [Proto.FileLocationRequestArgs, Proto.QuickInfoResponse] 'quickinfo': [Proto.FileLocationRequestArgs, Proto.QuickInfoResponse]
'references': [Proto.FileLocationRequestArgs, Proto.ReferencesResponse] 'references': [Proto.FileLocationRequestArgs, Proto.ReferencesResponse]
'rename': [Proto.RenameRequestArgs, Proto.RenameResponse] 'rename': [Proto.RenameRequestArgs, Proto.RenameResponse]
'selectionRange': [Proto.SelectionRangeRequestArgs, Proto.SelectionRangeResponse]
'signatureHelp': [Proto.SignatureHelpRequestArgs, Proto.SignatureHelpResponse] 'signatureHelp': [Proto.SignatureHelpRequestArgs, Proto.SignatureHelpResponse]
'typeDefinition': [Proto.FileLocationRequestArgs, Proto.TypeDefinitionResponse] 'typeDefinition': [Proto.FileLocationRequestArgs, Proto.TypeDefinitionResponse]
} }