forked from sveltejs/language-tools
/
completions.ts
150 lines (139 loc) · 5.75 KB
/
completions.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
import { basename, dirname } from 'path';
import type ts from 'typescript/lib/tsserverlibrary';
import { Logger } from '../logger';
import { isSvelteFilePath, replaceDeep } from '../utils';
const componentPostfix = '__SvelteComponent_';
export function decorateCompletions(ls: ts.LanguageService, logger: Logger): void {
const getCompletionsAtPosition = ls.getCompletionsAtPosition;
ls.getCompletionsAtPosition = (fileName, position, options) => {
const completions = getCompletionsAtPosition(fileName, position, options);
if (!completions) {
return completions;
}
// Add ./$types imports for SvelteKit since TypeScript is bad at it
if (basename(fileName).startsWith('+')) {
const $typeImports = new Map<string, ts.CompletionEntry>();
for (const c of completions.entries) {
if (c.source?.includes('.svelte-kit/types') && c.data) {
$typeImports.set(c.name, c);
}
}
for (const $typeImport of $typeImports.values()) {
// resolve path from FileName to svelte-kit/types
// src/routes/foo/+page.svelte -> .svelte-kit/types/foo/$types.d.ts
const routesFolder = 'src/routes'; // TODO somehow get access to kit.files.routes in here
const relativeFileName = fileName.split(routesFolder)[1]?.slice(1);
if (relativeFileName) {
const modifiedSource =
$typeImport.source!.split('.svelte-kit/types')[0] +
// note the missing .d.ts at the end - TS wants it that way for some reason
`.svelte-kit/types/${routesFolder}/${dirname(relativeFileName)}/$types`;
completions.entries.push({
...$typeImport,
// Ensure it's sorted above the other imports
sortText: !isNaN(Number($typeImport.sortText))
? String(Number($typeImport.sortText) - 1)
: $typeImport.sortText,
source: modifiedSource,
data: {
...$typeImport.data,
fileName: $typeImport.data!.fileName?.replace(
$typeImport.source!,
modifiedSource
),
moduleSpecifier: $typeImport.data!.moduleSpecifier?.replace(
$typeImport.source!,
modifiedSource
),
__is_sveltekit$typeImport: true
} as any
});
}
}
}
return {
...completions,
entries: completions.entries.map((entry) => {
if (
!isSvelteFilePath(entry.source || '') ||
!entry.name.endsWith(componentPostfix)
) {
return entry;
}
return {
...entry,
name: entry.name.slice(0, -componentPostfix.length)
};
})
};
};
const getCompletionEntryDetails = ls.getCompletionEntryDetails;
ls.getCompletionEntryDetails = (
fileName,
position,
entryName,
formatOptions,
source,
preferences,
data
) => {
const is$typeImport = (data as any).__is_sveltekit$typeImport;
const details = getCompletionEntryDetails(
fileName,
position,
entryName,
formatOptions,
source,
preferences,
data
);
if (details) {
if (is$typeImport) {
details.codeActions = details.codeActions?.map((codeAction) => {
codeAction.description = adjustPath(codeAction.description);
codeAction.changes = codeAction.changes.map((change) => {
change.textChanges = change.textChanges.map((textChange) => {
textChange.newText = adjustPath(textChange.newText);
return textChange;
});
return change;
});
return codeAction;
});
return details;
} else if (isSvelteFilePath(source || '')) {
logger.debug('TS found Svelte Component import completion details');
return replaceDeep(details, componentPostfix, '');
} else {
return details;
}
}
if (!isSvelteFilePath(source || '')) {
return details;
}
// In the completion list we removed the component postfix. Internally,
// the language service saved the list with the postfix, so details
// won't match anything. Therefore add it back and remove it afterwards again.
const svelteDetails = getCompletionEntryDetails(
fileName,
position,
entryName + componentPostfix,
formatOptions,
source,
preferences,
data
);
if (!svelteDetails) {
return undefined;
}
logger.debug('Found Svelte Component import completion details');
return replaceDeep(svelteDetails, componentPostfix, '');
};
}
function adjustPath(path: string) {
return path.replace(
/(['"])(.+?)['"]/,
// .js logic for node16 module resolution
(_match, quote, path) => `${quote}./$types${path.endsWith('.js') ? '.js' : ''}${quote}`
);
}