-
-
Notifications
You must be signed in to change notification settings - Fork 187
/
CodeActionsProvider.ts
997 lines (879 loc) · 35.2 KB
/
CodeActionsProvider.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
import ts from 'typescript';
import {
CancellationToken,
CodeAction,
CodeActionContext,
CodeActionKind,
Diagnostic,
OptionalVersionedTextDocumentIdentifier,
Position,
Range,
TextDocumentEdit,
TextEdit,
WorkspaceEdit
} from 'vscode-languageserver';
import {
Document,
getLineAtPosition,
isAtEndOfLine,
isInTag,
isRangeInTag,
mapRangeToOriginal
} from '../../../lib/documents';
import { LSConfigManager } from '../../../ls-config';
import {
flatten,
getIndent,
isNotNullOrUndefined,
modifyLines,
pathToUrl,
possiblyComponent
} from '../../../utils';
import { CodeActionsProvider } from '../../interfaces';
import { DocumentSnapshot, SvelteDocumentSnapshot } from '../DocumentSnapshot';
import { LSAndTSDocResolver } from '../LSAndTSDocResolver';
import { changeSvelteComponentName, convertRange } from '../utils';
import { CompletionsProviderImpl } from './CompletionProvider';
import {
findContainingNode,
FormatCodeBasis,
getFormatCodeBasis,
getQuotePreference,
isTextSpanInGeneratedCode,
SnapshotMap
} from './utils';
/**
* TODO change this to protocol constant if it's part of the protocol
*/
export const SORT_IMPORT_CODE_ACTION_KIND = 'source.sortImports';
interface RefactorArgs {
type: 'refactor';
refactorName: string;
textRange: ts.TextRange;
originalRange: Range;
}
export class CodeActionsProviderImpl implements CodeActionsProvider {
constructor(
private readonly lsAndTsDocResolver: LSAndTSDocResolver,
private readonly completionProvider: CompletionsProviderImpl,
private readonly configManager: LSConfigManager
) {}
async getCodeActions(
document: Document,
range: Range,
context: CodeActionContext,
cancellationToken?: CancellationToken
): Promise<CodeAction[]> {
if (context.only?.[0] === CodeActionKind.SourceOrganizeImports) {
return await this.organizeImports(document, cancellationToken);
}
if (context.only?.[0] === SORT_IMPORT_CODE_ACTION_KIND) {
return await this.organizeImports(
document,
cancellationToken,
/**skipDestructiveCodeActions */ true
);
}
// for source action command (all source.xxx)
// vscode would show different source code action kinds to choose from
if (context.only?.[0] === CodeActionKind.Source) {
return [
...(await this.organizeImports(document, cancellationToken)),
...(await this.organizeImports(
document,
cancellationToken,
/**skipDestructiveCodeActions */ true
))
];
}
if (
context.diagnostics.length &&
(!context.only || context.only.includes(CodeActionKind.QuickFix))
) {
return await this.applyQuickfix(document, range, context, cancellationToken);
}
if (!context.only || context.only.includes(CodeActionKind.Refactor)) {
return await this.getApplicableRefactors(document, range, cancellationToken);
}
return [];
}
private async organizeImports(
document: Document,
cancellationToken: CancellationToken | undefined,
skipDestructiveCodeActions = false
): Promise<CodeAction[]> {
if (!document.scriptInfo && !document.moduleScriptInfo) {
return [];
}
const { lang, tsDoc, userPreferences } = await this.getLSAndTSDoc(document);
if (cancellationToken?.isCancellationRequested || tsDoc.parserError) {
// If there's a parser error, we fall back to only the script contents,
// so organize imports likely throws out a lot of seemingly unused imports
// because they are only used in the template. Therefore do nothing in this case.
return [];
}
const changes = lang.organizeImports(
{
fileName: tsDoc.filePath,
type: 'file',
skipDestructiveCodeActions
},
{
...(await this.configManager.getFormatCodeSettingsForFile(
document,
tsDoc.scriptKind
)),
// handle it on our own
baseIndentSize: undefined
},
userPreferences
);
const documentChanges = await Promise.all(
changes.map(async (change) => {
// Organize Imports will only affect the current file, so no need to check the file path
return TextDocumentEdit.create(
OptionalVersionedTextDocumentIdentifier.create(document.url, null),
change.textChanges.map((edit) => {
const range = this.checkRemoveImportCodeActionRange(
edit,
tsDoc,
mapRangeToOriginal(tsDoc, convertRange(tsDoc, edit.span))
);
return this.fixIndentationOfImports(
TextEdit.replace(range, edit.newText),
document
);
})
);
})
);
return [
CodeAction.create(
skipDestructiveCodeActions ? 'Sort Imports' : 'Organize Imports',
{ documentChanges },
skipDestructiveCodeActions
? SORT_IMPORT_CODE_ACTION_KIND
: CodeActionKind.SourceOrganizeImports
)
];
}
private fixIndentationOfImports(edit: TextEdit, document: Document): TextEdit {
// "Organize Imports" will have edits that delete a group of imports by return empty edits
// and one edit which contains all the organized imports of the group. Fix indentation
// of that one by prepending all lines with the indentation of the first line.
const { newText, range } = edit;
if (!newText || range.start.character === 0) {
return edit;
}
const line = getLineAtPosition(range.start, document.getText());
const leadingChars = line.substring(0, range.start.character);
if (leadingChars.trim() !== '') {
return edit;
}
const fixedNewText = modifyLines(edit.newText, (line, idx) =>
idx === 0 || !line ? line : leadingChars + line
);
if (range.end.character > 0) {
const endLine = getLineAtPosition(range.end, document.getText());
const isIndent = !endLine.substring(0, range.end.character).trim();
if (isIndent) {
const trimmedEndLine = endLine.trim();
// imports that would be removed by the next delete edit
if (trimmedEndLine && !trimmedEndLine.startsWith('import')) {
range.end.character = 0;
}
}
}
return TextEdit.replace(range, fixedNewText);
}
private checkRemoveImportCodeActionRange(
edit: ts.TextChange,
snapshot: DocumentSnapshot,
range: Range
) {
// Handle svelte2tsx wrong import mapping:
// The character after the last import maps to the start of the script
// TODO find a way to fix this in svelte2tsx and then remove this
if (
(range.end.line === 0 && range.end.character === 1) ||
range.end.line < range.start.line
) {
edit.span.length -= 1;
range = mapRangeToOriginal(snapshot, convertRange(snapshot, edit.span));
if (!(snapshot instanceof SvelteDocumentSnapshot)) {
range.end.character += 1;
return range;
}
const line = getLineAtPosition(range.end, snapshot.getOriginalText());
// remove-import code action will removes the
// line break generated by svelte2tsx,
// but when there's no line break in the source
// move back to next character would remove the next character
if ([';', '"', "'"].includes(line[range.end.character])) {
range.end.character += 1;
}
if (isAtEndOfLine(line, range.end.character)) {
range.end.line += 1;
range.end.character = 0;
}
}
return range;
}
private async applyQuickfix(
document: Document,
range: Range,
context: CodeActionContext,
cancellationToken: CancellationToken | undefined
) {
const { lang, tsDoc, userPreferences } = await this.getLSAndTSDoc(document);
if (cancellationToken?.isCancellationRequested) {
return [];
}
const start = tsDoc.offsetAt(tsDoc.getGeneratedPosition(range.start));
const end = tsDoc.offsetAt(tsDoc.getGeneratedPosition(range.end));
const errorCodes: number[] = context.diagnostics.map((diag) => Number(diag.code));
const cannotFoundNameDiagnostic = context.diagnostics.filter(
(diagnostic) => diagnostic.code === 2304
); // "Cannot find name '...'."
const formatCodeSettings = await this.configManager.getFormatCodeSettingsForFile(
document,
tsDoc.scriptKind
);
const formatCodeBasis = getFormatCodeBasis(formatCodeSettings);
let codeFixes = cannotFoundNameDiagnostic.length
? this.getComponentImportQuickFix(
start,
end,
lang,
tsDoc,
userPreferences,
cannotFoundNameDiagnostic,
formatCodeSettings
)
: undefined;
codeFixes =
// either-or situation
codeFixes ||
lang
.getCodeFixesAtPosition(
tsDoc.filePath,
start,
end,
errorCodes,
formatCodeSettings,
userPreferences
)
.concat(
await this.getSvelteQuickFixes(
lang,
document,
cannotFoundNameDiagnostic,
tsDoc,
formatCodeBasis,
userPreferences
)
);
const snapshots = new SnapshotMap(this.lsAndTsDocResolver);
snapshots.set(tsDoc.filePath, tsDoc);
const codeActionsPromises = codeFixes.map(async (fix) => {
const documentChangesPromises = fix.changes.map(async (change) => {
const snapshot = await snapshots.retrieve(change.fileName);
return TextDocumentEdit.create(
OptionalVersionedTextDocumentIdentifier.create(
pathToUrl(change.fileName),
null
),
change.textChanges
.map((edit) => {
if (
fix.fixName === 'import' &&
snapshot instanceof SvelteDocumentSnapshot
) {
return this.completionProvider.codeActionChangeToTextEdit(
document,
snapshot,
edit,
true,
range.start
);
}
if (isTextSpanInGeneratedCode(snapshot.getFullText(), edit.span)) {
return undefined;
}
let originalRange = mapRangeToOriginal(
snapshot,
convertRange(snapshot, edit.span)
);
if (fix.fixName === 'unusedIdentifier') {
originalRange = this.checkRemoveImportCodeActionRange(
edit,
snapshot,
originalRange
);
}
if (fix.fixName === 'fixMissingFunctionDeclaration') {
originalRange = this.checkEndOfFileCodeInsert(
originalRange,
range,
document
);
// ts doesn't add base indent to the first line
if (formatCodeSettings.baseIndentSize) {
const emptyLine = formatCodeBasis.newLine.repeat(2);
edit.newText =
emptyLine +
formatCodeBasis.baseIndent +
edit.newText.trimLeft();
}
}
if (fix.fixName === 'disableJsDiagnostics') {
if (edit.newText.includes('ts-nocheck')) {
return this.checkTsNoCheckCodeInsert(document, edit);
}
return this.checkDisableJsDiagnosticsCodeInsert(
originalRange,
document,
edit
);
}
if (fix.fixName === 'inferFromUsage') {
originalRange = this.checkAddJsDocCodeActionRange(
snapshot,
originalRange,
document
);
}
if (originalRange.start.line < 0 || originalRange.end.line < 0) {
return undefined;
}
return TextEdit.replace(originalRange, edit.newText);
})
.filter(isNotNullOrUndefined)
);
});
const documentChanges = await Promise.all(documentChangesPromises);
return CodeAction.create(
fix.description,
{
documentChanges
},
CodeActionKind.QuickFix
);
});
const codeActions = await Promise.all(codeActionsPromises);
// filter out empty code action
return codeActions.filter((codeAction) =>
codeAction.edit?.documentChanges?.every(
(change) => (<TextDocumentEdit>change).edits.length > 0
)
);
}
/**
* import quick fix requires the symbol name to be the same as where it's defined.
* But we have suffix on component default export to prevent conflict with
* a local variable. So we use auto-import completion as a workaround here.
*/
private getComponentImportQuickFix(
start: number,
end: number,
lang: ts.LanguageService,
tsDoc: DocumentSnapshot,
userPreferences: ts.UserPreferences,
diagnostics: Diagnostic[],
formatCodeSetting: ts.FormatCodeSettings
): readonly ts.CodeFixAction[] | undefined {
const sourceFile = lang.getProgram()?.getSourceFile(tsDoc.filePath);
if (!sourceFile) {
return;
}
const node = findContainingNode(
sourceFile,
{
start,
length: end - start
},
(node): node is ts.JsxOpeningLikeElement | ts.JsxClosingElement | ts.Identifier =>
this.configManager.getConfig().svelte.useNewTransformation
? ts.isCallExpression(node.parent) &&
ts.isIdentifier(node.parent.expression) &&
node.parent.expression.text === '__sveltets_2_ensureComponent' &&
ts.isIdentifier(node)
: ts.isJsxClosingElement(node) || ts.isJsxOpeningLikeElement(node)
);
if (!node) {
return;
}
const tagName = ts.isIdentifier(node) ? node : node.tagName;
const tagNameEnd = tagName.getEnd();
const tagNameEndOriginalPosition = tsDoc.offsetAt(
tsDoc.getOriginalPosition(tsDoc.positionAt(tagNameEnd))
);
const name = tagName.getText();
if (!possiblyComponent(name)) {
return;
}
const hasDiagnosticForTag = diagnostics.some(
({ range }) =>
tsDoc.offsetAt(range.start) <= tagNameEndOriginalPosition &&
tagNameEndOriginalPosition <= tsDoc.offsetAt(range.end)
);
if (!hasDiagnosticForTag) {
return;
}
const completion = lang.getCompletionsAtPosition(
tsDoc.filePath,
tagNameEnd,
userPreferences,
formatCodeSetting
);
if (!completion) {
return;
}
const suffixedName = name + '__SvelteComponent_';
const errorPreventingUserPreferences =
this.completionProvider.fixUserPreferencesForSvelteComponentImport(userPreferences);
const toFix = (c: ts.CompletionEntry) =>
lang
.getCompletionEntryDetails(
tsDoc.filePath,
end,
c.name,
formatCodeSetting,
c.source,
errorPreventingUserPreferences,
c.data
)
?.codeActions?.map((a) => ({
...a,
description: changeSvelteComponentName(a.description),
fixName: 'import'
})) ?? [];
return flatten(
completion.entries.filter((c) => c.name === name || c.name === suffixedName).map(toFix)
);
}
private async getSvelteQuickFixes(
lang: ts.LanguageService,
document: Document,
diagnostics: Diagnostic[],
tsDoc: DocumentSnapshot,
formatCodeBasis: FormatCodeBasis,
userPreferences: ts.UserPreferences
): Promise<ts.CodeFixAction[]> {
const program = lang.getProgram();
const sourceFile = program?.getSourceFile(tsDoc.filePath);
if (!program || !sourceFile) {
return [];
}
const typeChecker = program.getTypeChecker();
const results: ts.CodeFixAction[] = [];
const quote = getQuotePreference(sourceFile, userPreferences);
for (const diagnostic of diagnostics) {
const start = tsDoc.offsetAt(tsDoc.getGeneratedPosition(diagnostic.range.start));
const end = tsDoc.offsetAt(tsDoc.getGeneratedPosition(diagnostic.range.end));
const identifier = findContainingNode(
sourceFile,
{ start, length: end - start },
ts.isIdentifier
);
if (!identifier) {
continue;
}
const isQuickFixTargetTargetStore =
identifier?.escapedText.toString().startsWith('$') && diagnostic.code === 2304;
const isQuickFixTargetEventHandler = this.isQuickFixForEventHandler(
document,
diagnostic
);
if (isQuickFixTargetTargetStore) {
results.push(
...(await this.getSvelteStoreQuickFixes(
identifier,
lang,
document,
tsDoc,
userPreferences
))
);
}
if (isQuickFixTargetEventHandler) {
results.push(
...this.getEventHandlerQuickFixes(
identifier,
tsDoc,
typeChecker,
quote,
formatCodeBasis
)
);
}
}
return results;
}
private async getSvelteStoreQuickFixes(
identifier: ts.Identifier,
lang: ts.LanguageService,
document: Document,
tsDoc: DocumentSnapshot,
userPreferences: ts.UserPreferences
): Promise<ts.CodeFixAction[]> {
const storeIdentifier = identifier.escapedText.toString().substring(1);
const formatCodeSettings = await this.configManager.getFormatCodeSettingsForFile(
document,
tsDoc.scriptKind
);
const completion = lang.getCompletionsAtPosition(
tsDoc.filePath,
0,
userPreferences,
formatCodeSettings
);
if (!completion) {
return [];
}
const toFix = (c: ts.CompletionEntry) =>
lang
.getCompletionEntryDetails(
tsDoc.filePath,
0,
c.name,
formatCodeSettings,
c.source,
userPreferences,
c.data
)
?.codeActions?.map((a) => ({
...a,
changes: a.changes.map((change) => {
return {
...change,
textChanges: change.textChanges.map((textChange) => {
// For some reason, TS sometimes adds the `type` modifier. Remove it.
return {
...textChange,
newText: textChange.newText.replace(' type ', ' ')
};
})
};
}),
fixName: 'import'
})) ?? [];
return flatten(completion.entries.filter((c) => c.name === storeIdentifier).map(toFix));
}
/**
* Workaround for TypeScript doesn't provide a quick fix if the signature is typed as union type, like `(() => void) | null`
* We can remove this once TypeScript doesn't have this limitation.
*/
private getEventHandlerQuickFixes(
identifier: ts.Identifier,
tsDoc: DocumentSnapshot,
typeChecker: ts.TypeChecker,
quote: string,
formatCodeBasis: FormatCodeBasis
): ts.CodeFixAction[] {
const type = identifier && typeChecker.getContextualType(identifier);
// if it's not union typescript should be able to do it. no need to enhance
if (!type || !type.isUnion()) {
return [];
}
const nonNullable = type.getNonNullableType();
if (
!(
nonNullable.flags & ts.TypeFlags.Object &&
(nonNullable as ts.ObjectType).objectFlags & ts.ObjectFlags.Anonymous
)
) {
return [];
}
const signature = typeChecker.getSignaturesOfType(nonNullable, ts.SignatureKind.Call)[0];
const parameters = signature.parameters.map((p) => {
const declaration = p.valueDeclaration ?? p.declarations?.[0];
const typeString = declaration
? typeChecker.typeToString(typeChecker.getTypeOfSymbolAtLocation(p, declaration))
: '';
return { name: p.name, typeString };
});
const returnType = typeChecker.typeToString(signature.getReturnType());
const useJsDoc =
tsDoc.scriptKind === ts.ScriptKind.JS || tsDoc.scriptKind === ts.ScriptKind.JSX;
const parametersText = (
useJsDoc
? parameters.map((p) => p.name)
: parameters.map((p) => p.name + (p.typeString ? ': ' + p.typeString : ''))
).join(', ');
const jsDoc = useJsDoc
? ['/**', ...parameters.map((p) => ` * @param {${p.typeString}} ${p.name}`), ' */']
: [];
const newText = [
...jsDoc,
`function ${identifier.text}(${parametersText})${useJsDoc ? '' : ': ' + returnType} {`,
formatCodeBasis.indent +
`throw new Error(${quote}Function not implemented.${quote})` +
formatCodeBasis.semi,
'}'
]
.map((line) => formatCodeBasis.baseIndent + line + formatCodeBasis.newLine)
.join('');
return [
{
description: `Add missing function declaration '${identifier.text}'`,
fixName: 'fixMissingFunctionDeclaration',
changes: [
{
fileName: tsDoc.filePath,
textChanges: [
{
newText,
span: { start: 0, length: 0 }
}
]
}
]
}
];
}
private isQuickFixForEventHandler(document: Document, diagnostic: Diagnostic) {
const htmlNode = document.html.findNodeAt(document.offsetAt(diagnostic.range.start));
if (
!htmlNode.attributes ||
!Object.keys(htmlNode.attributes).some((attr) => attr.startsWith('on:'))
) {
return false;
}
return true;
}
private async getApplicableRefactors(
document: Document,
range: Range,
cancellationToken: CancellationToken | undefined
): Promise<CodeAction[]> {
if (
!isRangeInTag(range, document.scriptInfo) &&
!isRangeInTag(range, document.moduleScriptInfo)
) {
return [];
}
// Don't allow refactorings when there is likely a store subscription.
// Reason: Extracting that would lead to svelte2tsx' transformed store representation
// showing up, which will confuse the user. In the long run, we maybe have to
// setup a separate ts language service which only knows of the original script.
const textInRange = document
.getText()
.substring(document.offsetAt(range.start), document.offsetAt(range.end));
if (textInRange.includes('$')) {
return [];
}
const { lang, tsDoc, userPreferences } = await this.getLSAndTSDoc(document);
if (cancellationToken?.isCancellationRequested) {
return [];
}
const textRange = {
pos: tsDoc.offsetAt(tsDoc.getGeneratedPosition(range.start)),
end: tsDoc.offsetAt(tsDoc.getGeneratedPosition(range.end))
};
const applicableRefactors = lang.getApplicableRefactors(
document.getFilePath() || '',
textRange,
userPreferences
);
return (
this.applicableRefactorsToCodeActions(applicableRefactors, document, range, textRange)
// Only allow refactorings from which we know they work
.filter(
(refactor) =>
refactor.command?.command.includes('function_scope') ||
refactor.command?.command.includes('constant_scope') ||
refactor.command?.command === 'Infer function return type'
)
// The language server also proposes extraction into const/function in module scope,
// which is outside of the render function, which is svelte2tsx-specific and unmapped,
// so it would both not work and confuse the user ("What is this render? Never declared that").
// So filter out the module scope proposal and rename the render-title
.filter((refactor) => !refactor.title.includes('module scope'))
.map((refactor) => ({
...refactor,
title: refactor.title
.replace(
"Extract to inner function in function 'render'",
'Extract to function'
)
.replace("Extract to constant in function 'render'", 'Extract to constant')
}))
);
}
private applicableRefactorsToCodeActions(
applicableRefactors: ts.ApplicableRefactorInfo[],
document: Document,
originalRange: Range,
textRange: { pos: number; end: number }
) {
return flatten(
applicableRefactors.map((applicableRefactor) => {
if (applicableRefactor.inlineable === false) {
return [
CodeAction.create(applicableRefactor.description, {
title: applicableRefactor.description,
command: applicableRefactor.name,
arguments: [
document.uri,
<RefactorArgs>{
type: 'refactor',
textRange,
originalRange,
refactorName: 'Extract Symbol'
}
]
})
];
}
return applicableRefactor.actions.map((action) => {
return CodeAction.create(action.description, {
title: action.description,
command: action.name,
arguments: [
document.uri,
<RefactorArgs>{
type: 'refactor',
textRange,
originalRange,
refactorName: applicableRefactor.name
}
]
});
});
})
);
}
async executeCommand(
document: Document,
command: string,
args?: any[]
): Promise<WorkspaceEdit | null> {
if (!(args?.[1]?.type === 'refactor')) {
return null;
}
const { lang, tsDoc, userPreferences } = await this.getLSAndTSDoc(document);
const path = document.getFilePath() || '';
const { refactorName, originalRange, textRange } = <RefactorArgs>args[1];
const edits = lang.getEditsForRefactor(
path,
{},
textRange,
refactorName,
command,
userPreferences
);
if (!edits || edits.edits.length === 0) {
return null;
}
const documentChanges = edits?.edits.map((edit) =>
TextDocumentEdit.create(
OptionalVersionedTextDocumentIdentifier.create(document.uri, null),
edit.textChanges.map((edit) => {
const range = mapRangeToOriginal(tsDoc, convertRange(tsDoc, edit.span));
return TextEdit.replace(
this.checkEndOfFileCodeInsert(range, originalRange, document),
edit.newText
);
})
)
);
return { documentChanges };
}
/**
* Some refactorings place the new code at the end of svelte2tsx' render function,
* which is unmapped. In this case, add it to the end of the script tag ourselves.
*/
private checkEndOfFileCodeInsert(resultRange: Range, targetRange: Range, document: Document) {
if (resultRange.start.line < 0 || resultRange.end.line < 0) {
if (isRangeInTag(targetRange, document.moduleScriptInfo)) {
return Range.create(
document.moduleScriptInfo.endPos,
document.moduleScriptInfo.endPos
);
}
if (document.scriptInfo) {
return Range.create(document.scriptInfo.endPos, document.scriptInfo.endPos);
}
}
return resultRange;
}
private checkTsNoCheckCodeInsert(
document: Document,
edit: ts.TextChange
): TextEdit | undefined {
const scriptInfo = document.moduleScriptInfo ?? document.scriptInfo;
if (!scriptInfo) {
return undefined;
}
const newText = ts.sys.newLine + edit.newText;
return TextEdit.insert(scriptInfo.startPos, newText);
}
private checkDisableJsDiagnosticsCodeInsert(
originalRange: Range,
document: Document,
edit: ts.TextChange
): TextEdit | null {
const inModuleScript = isInTag(originalRange.start, document.moduleScriptInfo);
if (!isInTag(originalRange.start, document.scriptInfo) && !inModuleScript) {
return null;
}
const position = inModuleScript
? originalRange.start
: this.fixPropsCodeActionRange(originalRange.start, document) ?? originalRange.start;
// fix the length of trailing indent
const linesOfNewText = edit.newText.split('\n');
if (/^[ \t]*$/.test(linesOfNewText[linesOfNewText.length - 1])) {
const line = getLineAtPosition(originalRange.start, document.getText());
const indent = getIndent(line);
linesOfNewText[linesOfNewText.length - 1] = indent;
}
return TextEdit.insert(position, linesOfNewText.join('\n'));
}
/**
* svelte2tsx removes export in instance script
*/
private fixPropsCodeActionRange(start: Position, document: Document): Position | undefined {
const documentText = document.getText();
const offset = document.offsetAt(start);
const exportKeywordOffset = documentText.lastIndexOf('export', offset);
// export let a;
if (
exportKeywordOffset < 0 ||
documentText.slice(exportKeywordOffset + 'export'.length, offset).trim()
) {
return;
}
const charBeforeExport = documentText[exportKeywordOffset - 1];
if (
(charBeforeExport !== undefined && !charBeforeExport.trim()) ||
charBeforeExport === ';'
) {
return document.positionAt(exportKeywordOffset);
}
}
private checkAddJsDocCodeActionRange(
snapshot: DocumentSnapshot,
originalRange: Range,
document: Document
): Range {
if (
snapshot.scriptKind !== ts.ScriptKind.JS &&
snapshot.scriptKind !== ts.ScriptKind.JSX &&
!isInTag(originalRange.start, document.scriptInfo)
) {
return originalRange;
}
const position = this.fixPropsCodeActionRange(originalRange.start, document);
if (position) {
return {
start: position,
end: position
};
}
return originalRange;
}
private async getLSAndTSDoc(document: Document) {
return this.lsAndTsDocResolver.getLSAndTSDoc(document);
}
}