forked from phil294/coffeesense
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavascript.ts
1006 lines (909 loc) · 39.1 KB
/
javascript.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { LanguageModelCache, getLanguageModelCache } from '../../embeddedSupport/languageModelCache';
import {
SymbolInformation,
CompletionItem,
Location,
SignatureHelp,
SignatureInformation,
ParameterInformation,
Definition,
TextEdit,
Diagnostic,
DiagnosticSeverity,
Range,
CompletionItemKind,
Hover,
MarkedString,
DocumentHighlight,
DocumentHighlightKind,
CompletionList,
Position,
DiagnosticTag,
MarkupContent,
CodeAction,
CodeActionKind,
CompletionItemTag,
CodeActionContext
} from 'vscode-languageserver-types';
import { TextDocument } from 'vscode-languageserver-textdocument';
import { LanguageMode } from '../../embeddedSupport/languageModes';
import { CoffeescriptDocumentRegions, LanguageRange, LanguageId } from '../../embeddedSupport/embeddedSupport';
import { getFileFsPath, getFilePath } from '../../utils/paths';
import { URI } from 'vscode-uri';
import type ts from 'typescript';
import { NULL_SIGNATURE } from '../nullMode';
import { DependencyService, RuntimeLibrary } from '../../services/dependencyService';
import { CodeActionData, CodeActionDataKind, OrganizeImportsActionData } from '../../types';
import { IServiceHost } from '../../services/typescriptService/serviceHost';
import { toCompletionItemKind, toSymbolKind } from '../../services/typescriptService/util';
import * as Previewer from './previewer';
import { isVCancellationRequested, VCancellationToken } from '../../utils/cancellationToken';
import { EnvironmentService } from '../../services/EnvironmentService';
import { FILE_EXTENSION, FILE_EXTENSION2, LANGUAGE_ID } from '../../language';
import transpile_service, { common_js_variable_name_character, get_word_around_position } from '../../services/transpileService';
import { LineMap } from 'coffeescript';
export async function getJavascriptMode(
tsModule: RuntimeLibrary['typescript'],
serviceHost: IServiceHost,
env: EnvironmentService,
documentRegions: LanguageModelCache<CoffeescriptDocumentRegions>,
dependencyService: DependencyService
): Promise<LanguageMode> {
const jsDocuments = getLanguageModelCache(10, 60, document => {
const coffeescriptDocument = documentRegions.refreshAndGet(document);
return coffeescriptDocument.getSingleTypeDocument('script');
});
const { updateCurrentCoffeescriptTextDocument } = serviceHost;
let supportedCodeFixCodes: Set<number>;
function getUserPreferences(scriptDoc: TextDocument): ts.UserPreferences {
return getUserPreferencesByLanguageId(scriptDoc.languageId);
}
function getUserPreferencesByLanguageId(languageId: string): ts.UserPreferences {
const baseConfig = env.getConfig()[languageId === 'javascript' ? 'javascript' : 'typescript'];
const preferencesConfig = baseConfig?.preferences;
if (!baseConfig || !preferencesConfig) {
return {};
}
function safeGetConfigValue<V extends string | boolean, A extends Array<V>, D = undefined>(
configValue: any,
allowValues: A,
defaultValue?: D
) {
return allowValues.includes(configValue) ? (configValue as A[number]) : (defaultValue as D);
}
return {
quotePreference: 'auto',
importModuleSpecifierPreference: safeGetConfigValue(preferencesConfig.importModuleSpecifier, [
'relative',
'non-relative'
]),
importModuleSpecifierEnding: safeGetConfigValue(
preferencesConfig.importModuleSpecifierEnding,
['minimal', 'index', 'js'],
'auto'
),
allowTextChangesInNewFiles: true,
providePrefixAndSuffixTextForRename:
preferencesConfig.renameShorthandProperties === false ? false : preferencesConfig.useAliasesForRenames,
// @ts-expect-error
allowRenameOfImportPath: true,
includeAutomaticOptionalChainCompletions: baseConfig.suggest.includeAutomaticOptionalChainCompletions ?? true,
provideRefactorNotApplicableReason: true
};
}
return {
getId() {
return 'javascript';
},
async doValidation(coffee_doc: TextDocument, cancellationToken?: VCancellationToken): Promise<Diagnostic[]> {
if (await isVCancellationRequested(cancellationToken)) {
return [];
}
const { scriptDoc: js_doc, service } = updateCurrentCoffeescriptTextDocument(coffee_doc);
if (!languageServiceIncludesFile(service, coffee_doc.uri)) {
return [];
}
if (await isVCancellationRequested(cancellationToken)) {
return [];
}
const transpilation = transpile_service.result_by_uri.get(coffee_doc.uri)
if(!transpilation)
return []
if(transpilation.diagnostics)
return transpilation.diagnostics || []
const fileFsPath = getFileFsPath(coffee_doc.uri);
const program = service.getProgram();
const sourceFile = program?.getSourceFile(fileFsPath);
if (!program || !sourceFile) {
return [];
}
let rawScriptDiagnostics = [
...program.getSyntacticDiagnostics(sourceFile, cancellationToken?.tsToken),
...program.getSemanticDiagnostics(sourceFile, cancellationToken?.tsToken),
...service.getSuggestionDiagnostics(fileFsPath)
];
const compilerOptions = program.getCompilerOptions();
if (compilerOptions.declaration || compilerOptions.composite) {
rawScriptDiagnostics = [
...rawScriptDiagnostics,
...program.getDeclarationDiagnostics(sourceFile, cancellationToken?.tsToken)
];
}
const js_text = js_doc.getText()
return rawScriptDiagnostics
.filter(diag => !env.getConfig().coffeesense.ignoredTypescriptErrorCodes.includes(diag.code))
.filter(diag => diag.messageText !== "Parameter '_' implicitly has an 'any' type." &&
diag.messageText !== "'_' is declared but its value is never read.")
.map(diag => {
const tags: DiagnosticTag[] = [];
let message = tsModule.flattenDiagnosticMessageText(diag.messageText, '\n')
if (diag.reportsUnnecessary) {
tags.push(DiagnosticTag.Unnecessary);
}
if (diag.reportsDeprecated) {
tags.push(DiagnosticTag.Deprecated);
}
let range = convertRange(js_doc, diag as ts.TextSpan)
if(js_text.slice(js_doc.offsetAt({ line: range.start.line, character: 0 }))
.match(/^\s*var /)) {
// Position of errors shown at variable declaration are most often useless, it would
// be better to show them at their (first) usage instead which implies declaration
// in CS. Luckily, this is possible using highlight querying:
const occurrence = service.getOccurrencesAtPosition(fileFsPath, js_doc.offsetAt(range.start))?.[1]
if(occurrence)
range = convertRange(js_doc, occurrence.textSpan)
}
if(transpilation.source_map) {
const coffee_range = transpile_service.range_js_to_coffee(transpilation.source_map, range)
if(coffee_range) {
range = coffee_range
} else {
message += `\n\nThe position of this error could not be mapped back to CoffeeScript, sorry. Here's the failing JavaScript context:\n\n${js_text.slice(
js_doc.offsetAt({ line: range.start.line - 2, character: 0}),
js_doc.offsetAt({ line: range.start.line + 2, character: Number.MAX_VALUE}))}`
range = Range.create(0, 0, 0, 0)
}
if(range.end.line < range.start.line || range.end.line === range.start.line && range.end.character < range.start.character)
// end character is messed up (happens often). just use whole word instead
// Setting char end to start or to start+1 only highlights the first character.
// No idea how to properly highlight the full next word? Doing it the manual way:
range.end = { line: range.start.line, character: range.start.character + 1 }
while(coffee_doc.getText(Range.create(range.end, { line: range.end.line, character: range.end.character + 1}))
.match(common_js_variable_name_character)) {
range.end.character++;
}
}
// syntactic/semantic diagnostic always has start and length
// so we can safely cast diag to TextSpan
return <Diagnostic>{
range,
severity: convertTSDiagnosticCategoryToDiagnosticSeverity(tsModule, diag.category),
message,
tags,
code: diag.code,
source: 'CoffeeSense [TS]'
};
});
},
doComplete(coffee_doc: TextDocument, coffee_position: Position): CompletionList {
const { scriptDoc: js_doc, service } = updateCurrentCoffeescriptTextDocument(coffee_doc);
if (!languageServiceIncludesFile(service, coffee_doc.uri)) {
return { isIncomplete: false, items: [] };
}
const fileFsPath = getFileFsPath(coffee_doc.uri);
const transpilation = transpile_service.result_by_uri.get(coffee_doc.uri)
if(!transpilation)
return { isIncomplete: false, items: [] }
const coffee_text = coffee_doc.getText()
const coffee_last_char = coffee_text[coffee_doc.offsetAt(coffee_position) - 1]
let position: Position
if(transpilation.source_map) {
// For position reverse mapping, remove . char, and add again to result afterwards.
// Otherwise, the source map does not know what you're talking of
const coffee_position_excl_trigger_char = {
line: coffee_position.line,
character: coffee_position.character - (coffee_last_char==='.'? 1 : 0)
}
let js_position = transpile_service.position_coffee_to_js(transpilation, coffee_position_excl_trigger_char, coffee_doc)
if(!js_position) {
// The following works great in principle, but is not useful as cs indentation is wrong,
// comma is missing, scope is mostly simply wrong
/*
// Fallback: Current line in coffee does not exist in JS, e.g. empty line, perhaps
// indented. In this case, find the next previous mapping-existing line and move cursor forward
// one character/line.
const i_coffee_pos = { character: 0, line: coffee_position_excl_trigger_char.line }
while(--i_coffee_pos.line > 0) {
js_position = transpile_service.position_coffee_to_js(transpilation, i_coffee_pos, coffee_doc)
if(js_position)
break
}
if(js_position) {
js_position.line++
js_position.character = 0
}
*/
}
if(!js_position)
return { isIncomplete: false, items: [] }
position = {
line: js_position.line,
character: js_position.character + (coffee_last_char==='.'? 1 : 0)
}
} else {
// If no source map, the file is passed as coffee text which must not be mapped
position = coffee_position
}
let js_offset = js_doc.offsetAt(position);
if(position.character > 1000) // End of line (Number.MAX_VALUE)
js_offset--
let char_offset = 0
const js_text = js_doc.getText()
const js_last_char = js_text[js_offset - 1]
const js_next_char = js_text[js_offset]
// When CS cursor is e.g. at `a('|')`, completion does not work bc of bad source mapping,
// JS cursor is falsely `a(|'')`. Circumvent this:
const special_trigger_chars = ['"', "'"]
for(const s of special_trigger_chars) {
if(coffee_last_char === s && js_last_char !== s && js_next_char === s) {
char_offset += 1
break
}
}
js_offset += char_offset
if(char_offset === 0) {
if(js_text.substr(js_offset, 14) === 'this.valueOf()') {
// CS cursor: `...@|`
js_offset += 'this.'.length
} else if(transpilation.fake_line !== undefined) {
const coffee_line_until_cursor = coffee_text.slice(coffee_doc.offsetAt({ line:coffee_position.line, character:0 }), coffee_doc.offsetAt(coffee_position))
// CS cursor can be everything, but in case it is at `...@a.|` or `...@a b|`,
// the `@`s to `this` conversions need to be considered because fake lines are
// CS only.
// Edge case error: current_line != fake_line (so current_line is JS) and
// current_line.includes('@'), but let's ignore that
js_offset += (coffee_line_until_cursor.split('@').length - 1) * ('this.'.length - '@'.length)
}
}
const completions = service.getCompletionsAtPosition(fileFsPath, js_offset, {
...getUserPreferences(js_doc),
triggerCharacter: getTsTriggerCharacter(coffee_last_char || ''),
includeCompletionsWithInsertText: true,
includeCompletionsForModuleExports: true
});
if (!completions) {
return { isIncomplete: false, items: [] };
}
return {
isIncomplete: false,
items: completions.entries.map((entry, index) => {
let range = entry.replacementSpan && convertRange(js_doc, entry.replacementSpan);
if(range) {
if(transpilation.source_map)
range = transpile_service.range_js_to_coffee(transpilation.source_map, range) || range
range.start.character += char_offset
range.end.character += char_offset
// Or maybe do not calculate range at all, just set to coffee_position + entry length? Should work too
}
const { label, detail } = calculateLabelAndDetailTextForPathImport(entry);
const item: CompletionItem = {
uri: coffee_doc.uri,
position,
preselect: entry.isRecommended ? true : undefined,
label,
detail,
filterText: getFilterText(entry.insertText),
sortText: entry.sortText + index,
kind: toCompletionItemKind(entry.kind),
textEdit: range && TextEdit.replace(range, entry.insertText || entry.name),
insertText: entry.insertText,
data: {
// data used for resolving item details (see 'doResolve')
languageId: js_doc.languageId,
uri: coffee_doc.uri,
offset: js_offset,
source: entry.source,
tsData: entry.data
}
} as CompletionItem;
// fix: Missing vue extension in filename with import autocomplete
// https://github.com/vuejs/vetur/issues/2908
if (item.kind === CompletionItemKind.File && !item.detail?.endsWith('.js') && !item.detail?.endsWith('.ts')) {
item.insertText = item.detail;
}
if (entry.kindModifiers) {
const kindModifiers = parseKindModifier(entry.kindModifiers ?? '');
if (kindModifiers.optional) {
if (!item.insertText) {
item.insertText = item.label;
}
if (!item.filterText) {
item.filterText = item.label;
}
item.label += '?';
}
if (kindModifiers.deprecated) {
item.tags = [CompletionItemTag.Deprecated];
}
if (kindModifiers.color) {
item.kind = CompletionItemKind.Color;
}
}
return item;
})
};
function calculateLabelAndDetailTextForPathImport(entry: ts.CompletionEntry) {
// Is import path completion
if (entry.kind === tsModule.ScriptElementKind.scriptElement) {
if (entry.kindModifiers) {
return {
label: entry.name,
detail: entry.name + entry.kindModifiers
};
} else {
if (entry.name.endsWith(`.${FILE_EXTENSION}`)) {
return {
label: entry.name.slice(0, -`.${FILE_EXTENSION}`.length),
detail: entry.name
};
} else if (entry.name.endsWith(`.${FILE_EXTENSION2}`)) {
return {
label: entry.name.slice(0, -`.${FILE_EXTENSION2}`.length),
detail: entry.name
};
}
}
}
return {
label: entry.name,
detail: undefined
};
}
},
doResolve(coffee_doc: TextDocument, item: CompletionItem): CompletionItem {
const { scriptDoc: js_doc, service } = updateCurrentCoffeescriptTextDocument(coffee_doc);
if (!languageServiceIncludesFile(service, coffee_doc.uri)) {
return item;
}
const fileFsPath = getFileFsPath(coffee_doc.uri);
const transpilation = transpile_service.result_by_uri.get(coffee_doc.uri)
if(!transpilation)
return item
const details = service.getCompletionEntryDetails(
fileFsPath,
item.data.offset,
item.label,
{},
item.data.source,
getUserPreferences(js_doc),
item.data.tsData
);
if (details && item.kind !== CompletionItemKind.File && item.kind !== CompletionItemKind.Folder) {
item.detail = Previewer.plain(tsModule.displayPartsToString(details.displayParts));
const documentation: MarkupContent = {
kind: 'markdown',
value: tsModule.displayPartsToString(details.documentation) + '\n\n'
};
if (details.tags) {
if (details.tags) {
details.tags.forEach(x => {
const tagDoc = Previewer.getTagDocumentation(x);
if (tagDoc) {
documentation.value += tagDoc + '\n\n';
}
});
}
}
if (details.codeActions) {
// auto imports
const textEdits = details.codeActions.map(action =>
action.changes.map(change =>
change.textChanges.map(text_change => {
let range
if(transpilation.source_map) {
range = convertRange(js_doc, text_change.span)
const js_range = range
let coffee_range = transpile_service.range_js_to_coffee(transpilation.source_map, js_range)
if(coffee_range) {
const coffee_line = coffee_doc.getText().split('\n')[coffee_range.start.line]!
let coffee_range_end_of_named_group
let coffee_end_of_named_group_col = coffee_line.indexOf('}')
if(coffee_end_of_named_group_col > -1) {
if(coffee_line[coffee_end_of_named_group_col - 1] === ' ')
coffee_end_of_named_group_col--
const coffee_pos = { line: coffee_range.start.line, character: coffee_end_of_named_group_col }
coffee_range_end_of_named_group = Range.create(coffee_pos, coffee_pos)
}
if(text_change.newText.startsWith(', { ')) {
// Add new named imports group to existing default import
const coffee_from_col = coffee_line.indexOf(' from ')
if(coffee_from_col > -1) {
const coffee_pos = { line: coffee_range.start.line, character: coffee_from_col }
coffee_range = Range.create(coffee_pos, coffee_pos)
}
} else if(text_change.newText.startsWith(', ')) {
// Add named import to existing named imports group
coffee_range = coffee_range_end_of_named_group
} else if(text_change.newText[0] === '\n') {
// Add named import to existing named imports group in new line
// We don't want new line and add a missing comma
text_change.newText = text_change.newText.replace(/^\s+(.+)$/, (_, named_import) =>
', ' + named_import)
coffee_range = coffee_range_end_of_named_group
} else if(text_change.newText === ',') {
// named import to existing named imports group actions consist of two text changes,
// the first one being a comma, ignore it
text_change.newText = ''
} else if(text_change.newText.trim().endsWith(',')) {
// Add named import to existing named imports group, possibly new line,
// in between two other named imports
// We don't insert in between but only at the end of group instead
text_change.newText = ', ' + text_change.newText.trim().slice(0, -1)
coffee_range = coffee_range_end_of_named_group
} else {
// Add entirely new import. Line should start with 'import',
// but it can also be CommonJS-style, I haven't checked this further
const coffee_pos = { line: 0, character: 0 }
coffee_range = Range.create(coffee_pos, coffee_pos)
}
}
range = coffee_range
}
if(!range)
range = Range.create(0, 0, 0, 0)
return {
range,
newText: text_change.newText.replace(/;/g,'')
}
}))).flat().flat()
item.additionalTextEdits = textEdits;
details.codeActions.forEach(action => {
if (action.description) {
documentation.value += '\n' + action.description;
}
});
}
item.documentation = documentation;
delete item.data;
}
return item;
},
doHover(doc: TextDocument, position: Position): Hover {
const { scriptDoc, service } = updateCurrentCoffeescriptTextDocument(doc);
if (!languageServiceIncludesFile(service, doc.uri)) {
return { contents: [] };
}
const fileFsPath = getFileFsPath(doc.uri);
const transpilation = transpile_service.result_by_uri.get(doc.uri)
if(!transpilation)
return { contents: [] }
if(transpilation.source_map)
position = transpile_service.position_coffee_to_js(transpilation, position, doc) || position
const info = service.getQuickInfoAtPosition(fileFsPath, scriptDoc.offsetAt(position));
if (info) {
const display = tsModule.displayPartsToString(info.displayParts);
const markedContents: MarkedString[] = [{ language: 'ts', value: display }];
let hoverMdDoc = '';
const doc = Previewer.plain(tsModule.displayPartsToString(info.documentation));
if (doc) {
hoverMdDoc += doc + '\n\n';
}
if (info.tags) {
info.tags.forEach(x => {
const tagDoc = Previewer.getTagDocumentation(x);
if (tagDoc) {
hoverMdDoc += tagDoc + '\n\n';
}
});
}
if (hoverMdDoc.trim() !== '') {
markedContents.push(hoverMdDoc);
}
let range = convertRange(scriptDoc, info.textSpan)
if(transpilation.source_map)
range = transpile_service.range_js_to_coffee(transpilation.source_map, range) || range
return {
range,
contents: markedContents
};
}
return { contents: [] };
},
doSignatureHelp(coffee_doc: TextDocument, position: Position): SignatureHelp | null {
const { scriptDoc: js_doc, service } = updateCurrentCoffeescriptTextDocument(coffee_doc);
if (!languageServiceIncludesFile(service, coffee_doc.uri)) {
return NULL_SIGNATURE;
}
const fileFsPath = getFileFsPath(coffee_doc.uri);
const transpilation = transpile_service.result_by_uri.get(coffee_doc.uri)
if(!transpilation)
return NULL_SIGNATURE
const prev_coffee_char = coffee_doc.getText(Range.create(Position.create(position.line, position.character - 1), position))
const next_coffee_char = coffee_doc.getText(Range.create(position, Position.create(position.line, position.character + 1)))
if(transpilation.source_map) {
position = transpile_service.position_coffee_to_js(transpilation, position, coffee_doc) || position
if([' ', '('].includes(prev_coffee_char) && next_coffee_char === '\n') {
// js: 3 characters backwards from eol: `\n`, `;`, `)`: into ()
position.character = js_doc.positionAt(js_doc.offsetAt(Position.create(position.line, Number.MAX_VALUE)) - 3).character
}
}
const signatureHelpItems = service.getSignatureHelpItems(fileFsPath, js_doc.offsetAt(position), undefined);
if (!signatureHelpItems) {
return NULL_SIGNATURE;
}
const signatures: SignatureInformation[] = [];
signatureHelpItems.items.forEach(item => {
let sigLabel = '';
let sigMdDoc = '';
const sigParamemterInfos: ParameterInformation[] = [];
sigLabel += tsModule.displayPartsToString(item.prefixDisplayParts);
item.parameters.forEach((p, i, a) => {
const label = tsModule.displayPartsToString(p.displayParts);
const parameter: ParameterInformation = {
label,
documentation: tsModule.displayPartsToString(p.documentation)
};
sigLabel += label;
sigParamemterInfos.push(parameter);
if (i < a.length - 1) {
sigLabel += tsModule.displayPartsToString(item.separatorDisplayParts);
}
});
sigLabel += tsModule.displayPartsToString(item.suffixDisplayParts);
item.tags
.filter(x => x.name !== 'param')
.forEach(x => {
const tagDoc = Previewer.getTagDocumentation(x);
if (tagDoc) {
sigMdDoc += tagDoc + '\n\n';
}
});
signatures.push({
label: sigLabel,
documentation: {
kind: 'markdown',
value: sigMdDoc
},
parameters: sigParamemterInfos
});
});
return {
activeSignature: signatureHelpItems.selectedItemIndex,
activeParameter: signatureHelpItems.argumentIndex,
signatures
};
},
findDocumentHighlight(doc: TextDocument, position: Position): DocumentHighlight[] {
const { scriptDoc: js_doc, service } = updateCurrentCoffeescriptTextDocument(doc);
if (!languageServiceIncludesFile(service, doc.uri)) {
return [];
}
const fileFsPath = getFileFsPath(doc.uri);
const transpilation = transpile_service.result_by_uri.get(doc.uri)
if(!transpilation?.source_map)
return []
position = transpile_service.position_coffee_to_js(transpilation, position, doc) || position
const js_text = js_doc.getText()
const occurrences = service.getOccurrencesAtPosition(fileFsPath, js_doc.offsetAt(position));
if (occurrences) {
return occurrences
.map(entry => ({
entry,
range: convertRange(js_doc, entry.textSpan)
})).filter(({ range }) =>
! js_text.slice(js_doc.offsetAt({ line: range.start.line, character: 0 })).match(/^\s*var /)
).map(({ entry, range }) => {
if(transpilation.source_map) {
range = transpile_service.range_js_to_coffee(transpilation.source_map, range) || range
if(range.end.line < range.start.line)
range.end.line = range.start.line
range.end.character = range.start.character + entry.textSpan.length
}
return {
range,
kind: entry.isWriteAccess ? DocumentHighlightKind.Write : DocumentHighlightKind.Text
};
});
}
return [];
},
findDocumentSymbols(doc: TextDocument): SymbolInformation[] {
const { scriptDoc, service } = updateCurrentCoffeescriptTextDocument(doc);
if (!languageServiceIncludesFile(service, doc.uri)) {
return [];
}
const fileFsPath = getFileFsPath(doc.uri);
const transpilation = transpile_service.result_by_uri.get(doc.uri)
if(!transpilation?.source_map)
return []
const items = service.getNavigationBarItems(fileFsPath);
if (!items) {
return [];
}
const result: SymbolInformation[] = [];
const existing: { [k: string]: boolean } = {};
const collectSymbols = (item: ts.NavigationBarItem, containerLabel?: string) => {
const sig = item.text + item.kind + item.spans[0]!.start;
if (item.kind !== 'script' && !existing[sig]) {
let range = convertRange(scriptDoc, item.spans[0]!)
if(transpilation?.source_map)
range = transpile_service.range_js_to_coffee(transpilation.source_map, range) || range
const symbol: SymbolInformation = {
name: item.text,
kind: toSymbolKind(item.kind),
location: {
uri: doc.uri,
range
},
containerName: containerLabel
};
existing[sig] = true;
result.push(symbol);
containerLabel = item.text;
}
if (item.childItems && item.childItems.length > 0) {
for (const child of item.childItems) {
collectSymbols(child, containerLabel);
}
}
};
items.forEach(item => collectSymbols(item));
return result;
},
findDefinition(coffee_doc: TextDocument, position: Position): Definition {
const { scriptDoc: js_doc, service } = updateCurrentCoffeescriptTextDocument(coffee_doc);
if (!languageServiceIncludesFile(service, coffee_doc.uri)) {
return [];
}
const fileFsPath = getFileFsPath(coffee_doc.uri);
const transpilation = transpile_service.result_by_uri.get(coffee_doc.uri)
if(!transpilation)
return []
if(transpilation.source_map)
position = transpile_service.position_coffee_to_js(transpilation, position, coffee_doc) || position
const definitions = service.getDefinitionAtPosition(fileFsPath, js_doc.offsetAt(position));
if (!definitions) {
return [];
}
const definitionResults: Definition = [];
const program = service.getProgram();
if (!program) {
return [];
}
definitions.forEach(d => {
const definitionTargetDoc = getSourceDoc(d.fileName, program);
let range = convertRange(definitionTargetDoc, d.textSpan)
const uri = URI.file(d.fileName).toString()
const uri_transpilation = transpile_service.result_by_uri.get(uri)
if(uri_transpilation?.source_map)
range = transpile_service.range_js_to_coffee(uri_transpilation.source_map, range) || range
definitionResults.push({
uri,
range
});
});
return definitionResults;
},
findReferences(doc: TextDocument, position: Position): Location[] {
const { scriptDoc, service } = updateCurrentCoffeescriptTextDocument(doc);
if (!languageServiceIncludesFile(service, doc.uri)) {
return [];
}
const fileFsPath = getFileFsPath(doc.uri);
const transpilation = transpile_service.result_by_uri.get(doc.uri)
if(!transpilation)
return []
if(transpilation.source_map)
position = transpile_service.position_coffee_to_js(transpilation, position, doc) || position
const references = service.getReferencesAtPosition(fileFsPath, scriptDoc.offsetAt(position));
if (!references) {
return [];
}
const referenceResults: Location[] = [];
const program = service.getProgram();
if (!program) {
return [];
}
references.forEach(r => {
const referenceTargetDoc = getSourceDoc(r.fileName, program);
let range = convertRange(referenceTargetDoc, r.textSpan)
const uri = URI.file(r.fileName).toString()
const uri_transpilation = transpile_service.result_by_uri.get(uri)
if(uri_transpilation?.source_map)
range = transpile_service.range_js_to_coffee(uri_transpilation.source_map, range) || range
if (referenceTargetDoc) {
referenceResults.push({
uri,
range
});
}
});
return referenceResults;
},
getCodeActions(doc: TextDocument, coffee_range: Range, context: CodeActionContext) {
const transpilation = transpile_service.result_by_uri.get(doc.uri)
if(!transpilation?.source_map)
return []
const js_range = transpile_service.range_coffee_to_js(transpilation, coffee_range, doc)
if(!js_range)
return []
const { scriptDoc, service } = updateCurrentCoffeescriptTextDocument(doc);
const fileName = getFileFsPath(scriptDoc.uri);
const start = scriptDoc.offsetAt(js_range.start);
const end = scriptDoc.offsetAt(js_range.end);
const textRange = { pos: start, end };
const preferences = getUserPreferences(scriptDoc);
if (!supportedCodeFixCodes) {
supportedCodeFixCodes = new Set(
tsModule
.getSupportedCodeFixes()
.map(Number)
.filter(x => !isNaN(x))
);
}
const result: CodeAction[] = [];
provideOrganizeImports(doc.uri, scriptDoc.languageId as LanguageId, textRange, context, result);
return result;
},
doCodeActionResolve(doc, action) {
const { scriptDoc, service } = updateCurrentCoffeescriptTextDocument(doc);
if (!languageServiceIncludesFile(service, doc.uri)) {
return action;
}
const transpilation = transpile_service.result_by_uri.get(doc.uri)
if(!transpilation?.source_map)
return action;
const preferences = getUserPreferences(scriptDoc);
const fileFsPath = getFileFsPath(doc.uri);
const data = action.data as CodeActionData;
if (data.kind === CodeActionDataKind.OrganizeImports) {
const text_range_length = data.textRange.end - data.textRange.pos
const mapped_pos_start = transpile_service.position_coffee_to_js(transpilation, doc.positionAt(data.textRange.pos), doc)
if(!mapped_pos_start)
return action
data.textRange.pos = doc.offsetAt(mapped_pos_start)
data.textRange.end = data.textRange.pos + text_range_length
const response = service.organizeImports({ type: 'file', fileName: fileFsPath }, {}, preferences);
const edit = { changes: createUriMappingForEdits(response.slice(), service) };
const doc_changes = edit.changes?.[doc.uri] || []
for(const change of doc_changes) {
const range = transpile_service.range_js_to_coffee(transpilation.source_map, change.range)
if(!range)
return action
if(change.range.start.line === change.range.end.line && change.range.start.character === 0 && change.range.end.character === 0)
// Import removed; fix line range
change.range.end.line++
}
action.edit = edit
}
delete action.data;
return action;
},
onDocumentRemoved(document: TextDocument) {
jsDocuments.onDocumentRemoved(document);
},
onDocumentChanged(filePath: string) {
serviceHost.updateExternalDocument(filePath);
},
dispose() {
jsDocuments.dispose();
}
};
}
function provideOrganizeImports(
uri: string,
languageId: LanguageId,
textRange: { pos: number; end: number },
context: CodeActionContext,
result: CodeAction[]
) {
if (
!context.only ||
(!context.only.includes(CodeActionKind.SourceOrganizeImports) && !context.only.includes(CodeActionKind.Source))
) {
return;
}
result.push({
title: 'Organize Imports',
kind: CodeActionKind.SourceOrganizeImports,
data: {
uri,
languageId,
textRange,
kind: CodeActionDataKind.OrganizeImports
} as OrganizeImportsActionData
});
}
function createUriMappingForEdits(changes: ts.FileTextChanges[], service: ts.LanguageService) {
const program = service.getProgram()!;
const result: Record<string, TextEdit[]> = {};
for (const { fileName, textChanges } of changes) {
const targetDoc = getSourceDoc(fileName, program);
const edits = textChanges.map(({ newText, span }) => ({
newText,
range: convertRange(targetDoc, span)
}));
const uri = URI.file(fileName).toString();
if (result[uri]) {
result[uri]!.push(...edits);
} else {
result[uri] = edits;
}
}
return result;
}
function getSourceDoc(fileName: string, program: ts.Program): TextDocument {
const sourceFile = program.getSourceFile(fileName)!;
return TextDocument.create(fileName, LANGUAGE_ID, 0, sourceFile.getFullText());
}
export function languageServiceIncludesFile(ls: ts.LanguageService, documentUri: string): boolean {
const filePaths = ls.getProgram()!.getRootFileNames();
const filePath = getFilePath(documentUri);
return filePaths.includes(filePath);
}
function convertRange(document: TextDocument, span: ts.TextSpan): Range {
const startPosition = document.positionAt(span.start);
const endPosition = document.positionAt(span.start + span.length);
return Range.create(startPosition, endPosition);
}
// Parameter must to be string, Otherwise I don't like it semantically.
function getTsTriggerCharacter(triggerChar: string) {
// Sometimes autocomplete does not work with spaces indented inside objects (empty line).
// Not sure why, but TS rejects space as a valid trigger character in these scenarios.
// This function does not make any sense anymore anyway in CoffeeScript land: Most of
// these tokens have a completely different meaning than in JS.
// Setting to `.` allows for completion, no matter what. (typescript.js: `isValidTrigger`)
return '.';
const legalChars = ['@', '#', '.', '"', "'", '`', '/', '<', ' '];
if (legalChars.includes(triggerChar)) {
return triggerChar as ts.CompletionsTriggerCharacter;
}
return undefined;
}
function parseKindModifier(kindModifiers: string) {
const kinds = new Set(kindModifiers.split(/,|\s+/g));
return {
optional: kinds.has('optional'),
deprecated: kinds.has('deprecated'),
color: kinds.has('color')
};
}
function convertTSDiagnosticCategoryToDiagnosticSeverity(
tsModule: RuntimeLibrary['typescript'],
c: ts.DiagnosticCategory
) {
switch (c) {
case tsModule.DiagnosticCategory.Error:
return DiagnosticSeverity.Error;
case tsModule.DiagnosticCategory.Warning:
return DiagnosticSeverity.Warning;
case tsModule.DiagnosticCategory.Message:
return DiagnosticSeverity.Information;
case tsModule.DiagnosticCategory.Suggestion:
return DiagnosticSeverity.Hint;
default:
return DiagnosticSeverity.Error;
}
}
/* tslint:disable:max-line-length */
/**
* Adapted from https://github.com/microsoft/vscode/blob/2b090abd0fdab7b21a3eb74be13993ad61897f84/extensions/typescript-language-features/src/languageFeatures/completions.ts#L147-L181
*/
function getFilterText(insertText: string | undefined): string | undefined {
// For `this.` completions, generally don't set the filter text since we don't want them to be overly prioritized. #74164
if (insertText?.startsWith('this.')) {
return undefined;
}
// Handle the case:
// ```
// const xyz = { 'ab c': 1 };
// xyz.ab|
// ```
// In which case we want to insert a bracket accessor but should use `.abc` as the filter text instead of
// the bracketed insert text.
else if (insertText?.startsWith('[')) {