forked from neoclide/coc.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
languages.ts
747 lines (682 loc) · 31.2 KB
/
languages.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
import { Neovim } from '@chemzqm/neovim'
import { SelectionRange, CancellationToken, CancellationTokenSource, CodeAction, CodeActionContext, CodeActionKind, CodeLens, ColorInformation, ColorPresentation, CompletionItem, CompletionItemKind, CompletionList, CompletionTriggerKind, Disposable, DocumentHighlight, DocumentLink, DocumentSelector, DocumentSymbol, FoldingRange, FormattingOptions, Hover, InsertTextFormat, Location, LocationLink, Position, Range, SignatureHelp, SymbolInformation, TextDocument, TextEdit, WorkspaceEdit } from 'vscode-languageserver-protocol'
import commands from './commands'
import diagnosticManager from './diagnostic/manager'
import Document from './model/document'
import { CodeActionProvider, CodeLensProvider, CompletionItemProvider, DeclarationProvider, DefinitionProvider, DocumentColorProvider, DocumentFormattingEditProvider, DocumentLinkProvider, DocumentRangeFormattingEditProvider, DocumentSymbolProvider, FoldingContext, FoldingRangeProvider, HoverProvider, ImplementationProvider, OnTypeFormattingEditProvider, ReferenceContext, ReferenceProvider, RenameProvider, SignatureHelpProvider, TypeDefinitionProvider, WorkspaceSymbolProvider, SelectionRangeProvider } from './provider'
import CodeActionManager from './provider/codeActionmanager'
import SelectionRangeManager from './provider/rangeManager'
import CodeLensManager from './provider/codeLensManager'
import DeclarationManager from './provider/declarationManager'
import DefinitionManager from './provider/definitionManager'
import DocumentColorManager from './provider/documentColorManager'
import DocumentHighlightManager from './provider/documentHighlightManager'
import DocumentLinkManager from './provider/documentLinkManager'
import DocumentSymbolManager from './provider/documentSymbolManager'
import FoldingRangeManager from './provider/foldingRangeManager'
import FormatManager from './provider/formatManager'
import FormatRangeManager from './provider/formatRangeManager'
import HoverManager from './provider/hoverManager'
import ImplementationManager from './provider/implementatioinManager'
import OnTypeFormatManager from './provider/onTypeFormatManager'
import ReferenceManager from './provider/referenceManager'
import RenameManager from './provider/renameManager'
import SignatureManager from './provider/signatureManager'
import TypeDefinitionManager from './provider/typeDefinitionManager'
import WorkspaceSymbolManager from './provider/workspaceSymbolsManager'
import snippetManager from './snippets/manager'
import sources from './sources'
import { CompleteOption, CompleteResult, CompletionContext, DiagnosticCollection, Documentation, ISource, SourceType, VimCompleteItem } from './types'
import { wait } from './util'
import * as complete from './util/complete'
import { getChangedFromEdits, rangeOverlap } from './util/position'
import { byteLength } from './util/string'
import workspace from './workspace'
const logger = require('./util/logger')('languages')
export interface CompletionSource {
id: string
source: ISource
languageIds: string[]
}
interface CompleteConfig {
defaultKindText: string
priority: number
echodocSupport: boolean
waitTime: number
detailMaxLength: number
detailField: string
}
function fixDocumentation(str: string): string {
return str.replace(/ /g, ' ')
}
export function check<R extends (...args: any[]) => Promise<R>>(_target: any, key: string, descriptor: any): void {
let fn = descriptor.value
if (typeof fn !== 'function') {
return
}
descriptor.value = function(...args: any[]): Promise<R> {
let { cancelTokenSource } = this
this.cancelTokenSource = new CancellationTokenSource()
return new Promise((resolve, reject): void => { // tslint:disable-line
let resolved = false
let timer = setTimeout(() => {
cancelTokenSource.cancel()
logger.error(`${key} timeout after 5s`)
if (!resolved) reject(new Error(`${key} timeout after 5s`))
}, 5000)
Promise.resolve(fn.apply(this, args)).then(res => {
clearTimeout(timer)
resolve(res)
}, e => {
clearTimeout(timer)
reject(e)
})
})
}
}
class Languages {
private completeConfig: CompleteConfig
private onTypeFormatManager = new OnTypeFormatManager()
private documentLinkManager = new DocumentLinkManager()
private documentColorManager = new DocumentColorManager()
private foldingRangeManager = new FoldingRangeManager()
private renameManager = new RenameManager()
private formatManager = new FormatManager()
private codeActionManager = new CodeActionManager()
private workspaceSymbolsManager = new WorkspaceSymbolManager()
private formatRangeManager = new FormatRangeManager()
private hoverManager = new HoverManager()
private signatureManager = new SignatureManager()
private documentSymbolManager = new DocumentSymbolManager()
private documentHighlightManager = new DocumentHighlightManager()
private definitionManager = new DefinitionManager()
private declarationManager = new DeclarationManager()
private typeDefinitionManager = new TypeDefinitionManager()
private referenceManager = new ReferenceManager()
private implementatioinManager = new ImplementationManager()
private codeLensManager = new CodeLensManager()
private selectionRangeManager = new SelectionRangeManager()
private cancelTokenSource: CancellationTokenSource = new CancellationTokenSource()
private completionItemKindMap: Map<CompletionItemKind, string>
constructor() {
workspace.onWillSaveUntil(event => {
let { languageId } = event.document
let config = workspace.getConfiguration('coc.preferences', event.document.uri)
let filetypes = config.get<string[]>('formatOnSaveFiletypes', [])
if (filetypes.indexOf(languageId) !== -1) {
let willSaveWaitUntil = async (): Promise<TextEdit[]> => {
let options = await workspace.getFormatOptions(event.document.uri)
let textEdits = await this.provideDocumentFormattingEdits(event.document, options)
return textEdits
}
event.waitUntil(willSaveWaitUntil())
}
}, null, 'languageserver')
workspace.ready.then(() => {
this.loadCompleteConfig()
}, _e => {
// noop
})
workspace.onDidChangeConfiguration(this.loadCompleteConfig, this)
}
private get nvim(): Neovim {
return workspace.nvim
}
private loadCompleteConfig(): void {
let config = workspace.getConfiguration('coc.preferences')
let suggest = workspace.getConfiguration('suggest')
function getConfig<T>(key, defaultValue: T): T {
return config.get<T>(key, suggest.get<T>(key, defaultValue))
}
let labels = suggest.get<{ [key: string]: string }>('completionItemKindLabels', {})
this.completionItemKindMap = new Map([
[CompletionItemKind.Text, labels['text'] || 'v'],
[CompletionItemKind.Method, labels['method'] || 'f'],
[CompletionItemKind.Function, labels['function'] || 'f'],
[CompletionItemKind.Constructor, typeof labels['constructor'] == 'function' ? 'f' : labels['con' + 'structor']],
[CompletionItemKind.Field, labels['field'] || 'm'],
[CompletionItemKind.Variable, labels['variable'] || 'v'],
[CompletionItemKind.Class, labels['class'] || 'C'],
[CompletionItemKind.Interface, labels['interface'] || 'I'],
[CompletionItemKind.Module, labels['module'] || 'M'],
[CompletionItemKind.Property, labels['property'] || 'm'],
[CompletionItemKind.Unit, labels['unit'] || 'U'],
[CompletionItemKind.Value, labels['value'] || 'v'],
[CompletionItemKind.Enum, labels['enum'] || 'E'],
[CompletionItemKind.Keyword, labels['keyword'] || 'k'],
[CompletionItemKind.Snippet, labels['snippet'] || 'S'],
[CompletionItemKind.Color, labels['color'] || 'v'],
[CompletionItemKind.File, labels['file'] || 'F'],
[CompletionItemKind.Reference, labels['reference'] || 'r'],
[CompletionItemKind.Folder, labels['folder'] || 'F'],
[CompletionItemKind.EnumMember, labels['enumMember'] || 'm'],
[CompletionItemKind.Constant, labels['constant'] || 'v'],
[CompletionItemKind.Struct, labels['struct'] || 'S'],
[CompletionItemKind.Event, labels['event'] || 'E'],
[CompletionItemKind.Operator, labels['operator'] || 'O'],
[CompletionItemKind.TypeParameter, labels['typeParameter'] || 'T'],
])
this.completeConfig = {
defaultKindText: labels['default'] || '',
priority: getConfig<number>('languageSourcePriority', 99),
echodocSupport: getConfig<boolean>('echodocSupport', false),
waitTime: getConfig<number>('triggerCompletionWait', 60),
detailField: getConfig<string>('detailField', 'abbr'),
detailMaxLength: getConfig<number>('detailMaxLength', 50)
}
}
public registerOnTypeFormattingEditProvider(
selector: DocumentSelector,
provider: OnTypeFormattingEditProvider,
triggerCharacters: string[]
): Disposable {
return this.onTypeFormatManager.register(selector, provider, triggerCharacters)
}
public registerCompletionItemProvider(
name: string,
shortcut: string,
languageIds: string | string[] | null,
provider: CompletionItemProvider,
triggerCharacters: string[] = [],
priority?: number
): Disposable {
languageIds = typeof languageIds == 'string' ? [languageIds] : languageIds
let source = this.createCompleteSource(name, shortcut, provider, languageIds, triggerCharacters, priority)
sources.addSource(source)
logger.debug('created service source', name)
return {
dispose: () => {
sources.removeSource(source)
}
}
}
public registerCodeActionProvider(selector: DocumentSelector, provider: CodeActionProvider, clientId: string, codeActionKinds?: CodeActionKind[]): Disposable {
return this.codeActionManager.register(selector, provider, clientId, codeActionKinds)
}
public registerHoverProvider(selector: DocumentSelector, provider: HoverProvider): Disposable {
return this.hoverManager.register(selector, provider)
}
public registerSelectionRangeProvider(selector: DocumentSelector, provider: SelectionRangeProvider): Disposable {
return this.selectionRangeManager.register(selector, provider)
}
public registerSignatureHelpProvider(
selector: DocumentSelector,
provider: SignatureHelpProvider,
triggerCharacters?: string[]): Disposable {
return this.signatureManager.register(selector, provider, triggerCharacters)
}
public registerDocumentSymbolProvider(selector: DocumentSelector, provider: DocumentSymbolProvider): Disposable {
return this.documentSymbolManager.register(selector, provider)
}
public registerFoldingRangeProvider(selector: DocumentSelector, provider: FoldingRangeProvider): Disposable {
return this.foldingRangeManager.register(selector, provider)
}
public registerDocumentHighlightProvider(selector: DocumentSelector, provider: any): Disposable {
return this.documentHighlightManager.register(selector, provider)
}
public registerCodeLensProvider(selector: DocumentSelector, provider: CodeLensProvider): Disposable {
return this.codeLensManager.register(selector, provider)
}
public registerDocumentLinkProvider(selector: DocumentSelector, provider: DocumentLinkProvider): Disposable {
return this.documentLinkManager.register(selector, provider)
}
public registerDocumentColorProvider(selector: DocumentSelector, provider: DocumentColorProvider): Disposable {
return this.documentColorManager.register(selector, provider)
}
public registerDefinitionProvider(selector: DocumentSelector, provider: DefinitionProvider): Disposable {
return this.definitionManager.register(selector, provider)
}
public registerDeclarationProvider(selector: DocumentSelector, provider: DeclarationProvider): Disposable {
return this.declarationManager.register(selector, provider)
}
public registerTypeDefinitionProvider(selector: DocumentSelector, provider: TypeDefinitionProvider): Disposable {
return this.typeDefinitionManager.register(selector, provider)
}
public registerImplementationProvider(selector: DocumentSelector, provider: ImplementationProvider): Disposable {
return this.implementatioinManager.register(selector, provider)
}
public registerReferencesProvider(selector: DocumentSelector, provider: ReferenceProvider): Disposable {
return this.referenceManager.register(selector, provider)
}
public registerRenameProvider(selector: DocumentSelector, provider: RenameProvider): Disposable {
return this.renameManager.register(selector, provider)
}
public registerWorkspaceSymbolProvider(selector: DocumentSelector, provider: WorkspaceSymbolProvider): Disposable {
return this.workspaceSymbolsManager.register(selector, provider)
}
public registerDocumentFormatProvider(selector: DocumentSelector, provider: DocumentFormattingEditProvider, priority = 0): Disposable {
return this.formatManager.register(selector, provider, priority)
}
public registerDocumentRangeFormatProvider(selector: DocumentSelector, provider: DocumentRangeFormattingEditProvider, priority = 0): Disposable {
return this.formatRangeManager.register(selector, provider, priority)
}
public shouldTriggerSignatureHelp(document: TextDocument, triggerCharacter: string): boolean {
return this.signatureManager.shouldTrigger(document, triggerCharacter)
}
@check
public async getHover(document: TextDocument, position: Position): Promise<Hover[]> {
return await this.hoverManager.provideHover(document, position, this.token)
}
public async getSignatureHelp(document: TextDocument, position: Position, token: CancellationToken): Promise<SignatureHelp> {
return await this.signatureManager.provideSignatureHelp(document, position, token)
}
@check
public async getDefinition(document: TextDocument, position: Position): Promise<Location[]> {
if (!this.definitionManager.hasProvider(document)) return null
return await this.definitionManager.provideDefinition(document, position, this.token)
}
@check
public async getDeclaration(document: TextDocument, position: Position): Promise<Location[] | Location | LocationLink[] | null> {
if (!this.declarationManager.hasProvider(document)) return null
return await this.declarationManager.provideDeclaration(document, position, this.token)
}
@check
public async getTypeDefinition(document: TextDocument, position: Position): Promise<Location[]> {
if (!this.typeDefinitionManager.hasProvider(document)) return null
return await this.typeDefinitionManager.provideTypeDefinition(document, position, this.token)
}
@check
public async getImplementation(document: TextDocument, position: Position): Promise<Location[]> {
if (!this.implementatioinManager.hasProvider(document)) return null
return await this.implementatioinManager.provideReferences(document, position, this.token)
}
@check
public async getReferences(document: TextDocument, context: ReferenceContext, position: Position): Promise<Location[]> {
if (!this.referenceManager.hasProvider(document)) return null
return await this.referenceManager.provideReferences(document, position, context, this.token)
}
@check
public async getDocumentSymbol(document: TextDocument): Promise<SymbolInformation[] | DocumentSymbol[]> {
return await this.documentSymbolManager.provideDocumentSymbols(document, this.token)
}
@check
public async getSelectionRanges(document: TextDocument, positions: Position[]): Promise<SelectionRange[] | null> {
return await this.selectionRangeManager.provideSelectionRanges(document, positions, this.token)
}
@check
public async getWorkspaceSymbols(document: TextDocument, query: string): Promise<SymbolInformation[]> {
query = query || ''
return await this.workspaceSymbolsManager.provideWorkspaceSymbols(document, query, this.token)
}
@check
public async resolveWorkspaceSymbol(symbol: SymbolInformation): Promise<SymbolInformation> {
return await this.workspaceSymbolsManager.resolveWorkspaceSymbol(symbol, this.token)
}
@check
public async provideRenameEdits(document: TextDocument, position: Position, newName: string): Promise<WorkspaceEdit> {
if (!this.renameManager.hasProvider(document)) {
let doc = workspace.getDocument(document.uri)
if (!doc) return null
let range = doc.getWordRangeAtPosition(position)
if (!range) return null
let word = doc.textDocument.getText(range)
let ranges = doc.getSymbolRanges(word)
if (!ranges.length) return null
return {
changes: {
[doc.uri]: ranges.map(r => {
return {
range: r,
newText: newName
}
})
}
}
}
return await this.renameManager.provideRenameEdits(document, position, newName, this.token)
}
@check
public async prepareRename(document: TextDocument, position: Position): Promise<Range | { range: Range; placeholder: string } | false> {
return await this.renameManager.prepareRename(document, position, this.token)
}
@check
public async provideDocumentFormattingEdits(document: TextDocument, options: FormattingOptions): Promise<TextEdit[]> {
if (!this.formatManager.hasProvider(document)) {
let hasRangeFormater = this.formatRangeManager.hasProvider(document)
if (!hasRangeFormater) {
logger.error('Format provider not found for current document', 'error')
return null
}
let end = document.positionAt(document.getText().length)
let range = Range.create(Position.create(0, 0), end)
return await this.provideDocumentRangeFormattingEdits(document, range, options)
}
return await this.formatManager.provideDocumentFormattingEdits(document, options, this.token)
}
@check
public async provideDocumentRangeFormattingEdits(document: TextDocument, range: Range, options: FormattingOptions): Promise<TextEdit[]> {
if (!this.formatRangeManager.hasProvider(document)) return null
return await this.formatRangeManager.provideDocumentRangeFormattingEdits(document, range, options, this.token)
}
/**
* Get CodeAction list for current document
*
* @public
* @param {TextDocument} document
* @param {Range} range
* @param {CodeActionContext} context
* @returns {Promise<CodeAction[]>}
*/
@check
public async getCodeActions(document: TextDocument, range: Range, context: CodeActionContext, silent = false): Promise<Map<string, CodeAction[]>> {
if (!silent && !this.codeActionManager.hasProvider(document)) {
return null
}
return await this.codeActionManager.provideCodeActions(document, range, context, this.token)
}
@check
public async getDocumentHighLight(document: TextDocument, position: Position): Promise<DocumentHighlight[]> {
return await this.documentHighlightManager.provideDocumentHighlights(document, position, this.token)
}
@check
public async getDocumentLinks(document: TextDocument): Promise<DocumentLink[]> {
if (!this.documentLinkManager.hasProvider(document)) {
return null
}
return (await this.documentLinkManager.provideDocumentLinks(document, this.token)) || []
}
@check
public async resolveDocumentLink(link: DocumentLink): Promise<DocumentLink> {
return await this.documentLinkManager.resolveDocumentLink(link, this.token)
}
@check
public async provideDocumentColors(document: TextDocument): Promise<ColorInformation[] | null> {
return await this.documentColorManager.provideDocumentColors(document, this.token)
}
@check
public async provideFoldingRanges(document: TextDocument, context: FoldingContext): Promise<FoldingRange[] | null> {
if (!this.formatRangeManager.hasProvider(document)) {
return null
}
return await this.foldingRangeManager.provideFoldingRanges(document, context, this.token)
}
@check
public async provideColorPresentations(color: ColorInformation, document: TextDocument, ): Promise<ColorPresentation[]> {
return await this.documentColorManager.provideColorPresentations(color, document, this.token)
}
@check
public async getCodeLens(document: TextDocument): Promise<CodeLens[]> {
return await this.codeLensManager.provideCodeLenses(document, this.token)
}
@check
public async resolveCodeLens(codeLens: CodeLens): Promise<CodeLens> {
return await this.codeLensManager.resolveCodeLens(codeLens, this.token)
}
@check
public async provideDocumentOntTypeEdits(character: string, document: TextDocument, position: Position): Promise<TextEdit[] | null> {
return this.onTypeFormatManager.onCharacterType(character, document, position, this.token)
}
public hasOnTypeProvider(character: string, document: TextDocument): boolean {
return this.onTypeFormatManager.getProvider(document, character) != null
}
public dispose(): void {
// noop
}
public createDiagnosticCollection(owner: string): DiagnosticCollection {
return diagnosticManager.create(owner)
}
private createCompleteSource(
name: string,
shortcut: string,
provider: CompletionItemProvider,
languageIds: string[] | null,
triggerCharacters: string[],
priority?: number
): ISource {
// track them for resolve
let completeItems: CompletionItem[] = []
// line used for TextEdit
let hasResolve = typeof provider.resolveCompletionItem === 'function'
priority = priority == null ? this.completeConfig.priority : priority
// index set of resolved items
let resolvedIndexes: Set<number> = new Set()
let doc: Document = null
let waitTime = Math.min(Math.max(50, this.completeConfig.waitTime), 300)
let source: ISource = {
name,
priority,
shortcut,
enable: true,
sourceType: SourceType.Service,
filetypes: languageIds,
triggerCharacters: triggerCharacters || [],
doComplete: async (opt: CompleteOption, token: CancellationToken): Promise<CompleteResult | null> => {
let { triggerCharacter, bufnr } = opt
doc = workspace.getDocument(bufnr)
if (!doc) return null
resolvedIndexes = new Set()
let isTrigger = triggerCharacters && triggerCharacters.indexOf(triggerCharacter) != -1
let triggerKind: CompletionTriggerKind = CompletionTriggerKind.Invoked
if (opt.triggerForInComplete) {
triggerKind = CompletionTriggerKind.TriggerForIncompleteCompletions
} else if (isTrigger) {
triggerKind = CompletionTriggerKind.TriggerCharacter
}
if (opt.triggerCharacter) await wait(waitTime)
if (token.isCancellationRequested) return null
let position = complete.getPosition(opt)
let context: CompletionContext = { triggerKind, option: opt }
if (isTrigger) context.triggerCharacter = triggerCharacter
let result
try {
result = await Promise.resolve(provider.provideCompletionItems(doc.textDocument, position, token, context))
} catch (e) {
// don't disturb user
logger.error(`Source "${name}" complete error:`, e)
return null
}
if (!result || token.isCancellationRequested) return null
completeItems = Array.isArray(result) ? result : result.items
if (!completeItems || completeItems.length == 0) return null
// used for fixed col
let option: CompleteOption = Object.assign({}, opt)
if (typeof (result as any).startcol == 'number') {
option.col = (result as any).startcol
}
let items: VimCompleteItem[] = completeItems.map((o, index) => {
let item = this.convertVimCompleteItem(o, shortcut, option)
item.index = index
return item
})
return {
startcol: (result as any).startcol,
isIncomplete: !!(result as CompletionList).isIncomplete,
items
}
},
onCompleteResolve: async (item: VimCompleteItem, token: CancellationToken): Promise<void> => {
let resolving = completeItems[item.index]
if (!resolving) return
if (hasResolve && !resolvedIndexes.has(item.index)) {
let resolved = await Promise.resolve(provider.resolveCompletionItem(resolving, token))
if (token.isCancellationRequested) return
resolvedIndexes.add(item.index)
if (resolved) Object.assign(resolving, resolved)
}
if (item.documentation == null) {
let { documentation, detail } = resolving
if (!documentation && !detail) return
let docs: Documentation[] = []
if (detail && !item.detailShown && detail != item.word) {
detail = detail.replace(/\n\s*/g, ' ')
if (detail.length) {
let isText = /^[\w-\s.,\t]+$/.test(detail)
docs.push({ filetype: isText ? 'txt' : doc.filetype, content: detail })
}
}
if (documentation) {
if (typeof documentation == 'string') {
docs.push({
filetype: 'markdown',
content: fixDocumentation(documentation)
})
} else if (documentation.value) {
docs.push({
filetype: documentation.kind == 'markdown' ? 'markdown' : 'txt',
content: fixDocumentation(documentation.value)
})
}
}
item.documentation = docs
}
},
onCompleteDone: async (vimItem: VimCompleteItem, opt: CompleteOption): Promise<void> => {
let item = completeItems[vimItem.index]
if (!item) return
let line = opt.linenr - 1
// tslint:disable-next-line: deprecation
if (item.insertText && !item.textEdit) {
item.textEdit = {
range: Range.create(line, opt.col, line, opt.colnr - 1),
// tslint:disable-next-line: deprecation
newText: item.insertText
}
}
if (vimItem.line) Object.assign(opt, { line: vimItem.line })
let snippet = await this.applyTextEdit(item, opt)
let { additionalTextEdits } = item
if (additionalTextEdits && item.textEdit) {
let r = item.textEdit.range
additionalTextEdits = additionalTextEdits.filter(edit => {
if (rangeOverlap(r, edit.range)) {
logger.info('Filtered overlap additionalTextEdit:', edit)
return false
}
return true
})
}
await this.applyAdditionalEdits(additionalTextEdits, opt.bufnr, snippet)
if (snippet && !snippetManager.isPlainText(item.textEdit.newText)) {
await snippetManager.selectCurrentPlaceholder()
}
if (item.command) commands.execute(item.command)
doc = null
},
shouldCommit: (item: VimCompleteItem, character: string): boolean => {
let completeItem = completeItems[item.index]
if (!completeItem) return false
let { commitCharacters } = completeItem
if (commitCharacters && commitCharacters.indexOf(character) !== -1) {
return true
}
return false
}
}
return source
}
private get token(): CancellationToken {
this.cancelTokenSource = new CancellationTokenSource()
return this.cancelTokenSource.token
}
private async applyTextEdit(item: CompletionItem, option: CompleteOption): Promise<boolean> {
let { nvim } = this
let { textEdit } = item
if (!textEdit) return false
let { line, bufnr, linenr } = option
let { range, newText } = textEdit
let isSnippet = item.insertTextFormat === InsertTextFormat.Snippet
// replace inserted word
let start = line.substr(0, range.start.character)
let end = line.substr(range.end.character)
if (isSnippet) {
let doc = workspace.getDocument(bufnr)
await doc.applyEdits(nvim, [{
range: Range.create(linenr - 1, 0, linenr, 0),
newText: `${start}${end}\n`
}])
// can't select, since additionalTextEdits would break selection
let pos = Position.create(linenr - 1, range.start.character)
return await snippetManager.insertSnippet(newText, false, Range.create(pos, pos))
}
let newLines = `${start}${newText}${end}`.split('\n')
if (newLines.length == 1) {
await nvim.call('coc#util#setline', [linenr, newLines[0]])
await workspace.moveTo(Position.create(linenr - 1, (start + newText).length))
} else {
let document = workspace.getDocument(bufnr)
if (document) {
await document.buffer.setLines(newLines, {
start: linenr - 1,
end: linenr,
strictIndexing: false
})
}
let line = linenr - 1 + newLines.length - 1
let character = newLines[newLines.length - 1].length - end.length
await workspace.moveTo({ line, character })
}
return false
}
private async applyAdditionalEdits(
textEdits: TextEdit[],
bufnr: number,
snippet: boolean): Promise<void> {
if (!textEdits || textEdits.length == 0) return
let document = workspace.getDocument(bufnr)
if (!document) return
await wait(workspace.isVim ? 100 : 10)
// how to move cursor after edit
let changed = null
let pos = await workspace.getCursorPosition()
if (!snippet) changed = getChangedFromEdits(pos, textEdits)
await document.applyEdits(this.nvim, textEdits)
if (changed) {
await workspace.moveTo(Position.create(pos.line + changed.line, pos.character + changed.character))
}
}
private convertVimCompleteItem(item: CompletionItem, shortcut: string, opt: CompleteOption): VimCompleteItem {
let { echodocSupport, detailField, detailMaxLength } = this.completeConfig
let hasAdditionalEdit = item.additionalTextEdits && item.additionalTextEdits.length > 0
let isSnippet = item.insertTextFormat === InsertTextFormat.Snippet || hasAdditionalEdit
let label = item.label.trim()
// tslint:disable-next-line:deprecation
if (isSnippet && item.insertText && item.insertText.indexOf('$') == -1 && !hasAdditionalEdit) {
// fix wrong insert format
isSnippet = false
item.insertTextFormat = InsertTextFormat.PlainText
}
let obj: VimCompleteItem = {
word: complete.getWord(item, opt),
abbr: label,
menu: `[${shortcut}]`,
kind: complete.completionKindString(item.kind, this.completionItemKindMap, this.completeConfig.defaultKindText),
sortText: item.sortText || null,
filterText: item.filterText || label,
isSnippet,
dup: 1
}
if (item && item.detail && detailField != 'preview') {
let detail = item.detail.replace(/\n\s*/g, ' ')
if (byteLength(detail) < detailMaxLength) {
if (detailField == 'menu') {
obj.menu = `${detail} ${obj.menu}`
} else if (detailField == 'abbr') {
obj.abbr = `${obj.abbr} - ${detail}`
}
obj.detailShown = 1
}
}
if (item.documentation) {
obj.info = typeof item.documentation == 'string' ? item.documentation : item.documentation.value
}
if (!obj.word) obj.empty = 1
if (item.textEdit) obj.line = opt.line
if (item.kind == CompletionItemKind.Folder && !obj.abbr.endsWith('/')) {
obj.abbr = obj.abbr + '/'
}
if (echodocSupport && item.kind >= 2 && item.kind <= 4) {
let fields = [item.detail || '', obj.abbr, obj.word]
for (let s of fields) {
if (s.indexOf('(') !== -1) {
obj.signature = s
break
}
}
}
if (item.preselect) obj.preselect = true
item.data = item.data || {}
if (item.data.optional) obj.abbr = obj.abbr + '?'
return obj
}
}
export default new Languages()