@astrojs/language-server 0.13.4 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +43 -0
- package/dist/check.js +1 -2
- package/dist/core/documents/DocumentMapper.js +2 -4
- package/dist/core/documents/parseAstro.js +1 -1
- package/dist/core/documents/utils.d.ts +5 -0
- package/dist/core/documents/utils.js +18 -5
- package/dist/plugins/PluginHost.d.ts +3 -2
- package/dist/plugins/PluginHost.js +37 -10
- package/dist/plugins/astro/AstroPlugin.js +1 -1
- package/dist/plugins/astro/features/CompletionsProvider.js +30 -15
- package/dist/plugins/css/CSSPlugin.js +20 -4
- package/dist/plugins/html/features/astro-attributes.js +43 -27
- package/dist/plugins/interfaces.d.ts +2 -2
- package/dist/plugins/typescript/LanguageServiceManager.js +1 -1
- package/dist/plugins/typescript/TypeScriptPlugin.d.ts +8 -4
- package/dist/plugins/typescript/TypeScriptPlugin.js +18 -5
- package/dist/plugins/typescript/astro-sys.js +3 -5
- package/dist/plugins/typescript/astro2tsx.d.ts +1 -1
- package/dist/plugins/typescript/astro2tsx.js +12 -8
- package/dist/plugins/typescript/features/CodeActionsProvider.d.ts +14 -0
- package/dist/plugins/typescript/features/CodeActionsProvider.js +141 -0
- package/dist/plugins/typescript/features/CompletionsProvider.d.ts +24 -6
- package/dist/plugins/typescript/features/CompletionsProvider.js +262 -52
- package/dist/plugins/typescript/features/DiagnosticsProvider.js +45 -60
- package/dist/plugins/typescript/features/DocumentSymbolsProvider.js +5 -6
- package/dist/plugins/typescript/features/FoldingRangesProvider.d.ts +9 -0
- package/dist/plugins/typescript/features/FoldingRangesProvider.js +64 -0
- package/dist/plugins/typescript/features/SemanticTokenProvider.js +2 -2
- package/dist/plugins/typescript/features/SignatureHelpProvider.js +2 -2
- package/dist/plugins/typescript/features/utils.d.ts +4 -0
- package/dist/plugins/typescript/features/utils.js +25 -3
- package/dist/plugins/typescript/language-service.d.ts +1 -1
- package/dist/plugins/typescript/language-service.js +44 -24
- package/dist/plugins/typescript/module-loader.js +1 -1
- package/dist/plugins/typescript/previewer.js +1 -1
- package/dist/plugins/typescript/snapshots/SnapshotManager.js +1 -1
- package/dist/plugins/typescript/snapshots/utils.js +27 -9
- package/dist/plugins/typescript/utils.d.ts +4 -0
- package/dist/plugins/typescript/utils.js +29 -1
- package/dist/server.js +44 -7
- package/dist/utils.d.ts +20 -0
- package/dist/utils.js +72 -3
- package/package.json +3 -3
|
@@ -1,92 +1,302 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
4
24
|
};
|
|
5
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.CompletionsProviderImpl = void 0;
|
|
7
|
-
const utils_1 = require("../../../core/documents/utils");
|
|
8
|
-
const typescript_1 = __importDefault(require("typescript"));
|
|
26
|
+
exports.codeActionChangeToTextEdit = exports.CompletionsProviderImpl = exports.completionOptions = void 0;
|
|
9
27
|
const vscode_languageserver_1 = require("vscode-languageserver");
|
|
28
|
+
const vscode_languageserver_protocol_1 = require("vscode-languageserver-protocol");
|
|
29
|
+
const utils_1 = require("../../../core/documents/utils");
|
|
30
|
+
const documents_1 = require("../../../core/documents");
|
|
31
|
+
const typescript_1 = __importStar(require("typescript"));
|
|
32
|
+
const vscode_languageserver_2 = require("vscode-languageserver");
|
|
10
33
|
const utils_2 = require("../utils");
|
|
11
|
-
const
|
|
34
|
+
const utils_3 = require("../../../utils");
|
|
35
|
+
const lodash_1 = require("lodash");
|
|
36
|
+
const previewer_1 = require("../previewer");
|
|
37
|
+
const utils_4 = require("./utils");
|
|
38
|
+
exports.completionOptions = {
|
|
12
39
|
importModuleSpecifierPreference: 'relative',
|
|
13
40
|
importModuleSpecifierEnding: 'auto',
|
|
14
41
|
quotePreference: 'single',
|
|
15
|
-
|
|
42
|
+
includeCompletionsForModuleExports: true,
|
|
43
|
+
includeCompletionsForImportStatements: true,
|
|
44
|
+
includeCompletionsWithInsertText: true,
|
|
45
|
+
allowIncompleteCompletions: true,
|
|
46
|
+
includeCompletionsWithSnippetText: true,
|
|
47
|
+
};
|
|
48
|
+
// `import {...} from '..'` or `import ... from '..'`
|
|
49
|
+
// Note: Does not take into account if import is within a comment.
|
|
50
|
+
const scriptImportRegex = /\bimport\s+{([^}]*?)}\s+?from\s+['"`].+?['"`]|\bimport\s+(\w+?)\s+from\s+['"`].+?['"`]/g;
|
|
16
51
|
class CompletionsProviderImpl {
|
|
17
52
|
constructor(languageServiceManager) {
|
|
18
53
|
this.languageServiceManager = languageServiceManager;
|
|
54
|
+
this.validTriggerCharacters = ['.', '"', "'", '`', '/', '@', '<', '#'];
|
|
55
|
+
}
|
|
56
|
+
isValidTriggerCharacter(character) {
|
|
57
|
+
return this.validTriggerCharacters.includes(character);
|
|
19
58
|
}
|
|
20
|
-
async getCompletions(document, position,
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
59
|
+
async getCompletions(document, position, completionContext, cancellationToken) {
|
|
60
|
+
const triggerCharacter = completionContext?.triggerCharacter;
|
|
61
|
+
const triggerKind = completionContext?.triggerKind;
|
|
62
|
+
const validTriggerCharacter = this.isValidTriggerCharacter(triggerCharacter) ? triggerCharacter : undefined;
|
|
63
|
+
const isCustomTriggerCharacter = triggerKind === vscode_languageserver_1.CompletionTriggerKind.TriggerCharacter;
|
|
64
|
+
if ((isCustomTriggerCharacter && !validTriggerCharacter) || cancellationToken?.isCancellationRequested) {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
if (this.canReuseLastCompletion(this.lastCompletion, triggerKind, triggerCharacter, document, position)) {
|
|
68
|
+
this.lastCompletion.position = position;
|
|
69
|
+
return this.lastCompletion.completionList;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
this.lastCompletion = undefined;
|
|
73
|
+
}
|
|
74
|
+
const html = document.html;
|
|
75
|
+
const offset = document.offsetAt(position);
|
|
76
|
+
const node = html.findNodeAt(offset);
|
|
77
|
+
// TODO: Add support for script tags
|
|
78
|
+
if (node.tag === 'script') {
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
const isCompletionInsideFrontmatter = (0, utils_1.isInsideFrontmatter)(document.getText(), offset);
|
|
82
|
+
const isCompletionInsideExpression = (0, utils_1.isInsideExpression)(document.getText(), node.start, offset);
|
|
83
|
+
// PERF: Getting TS completions is fairly slow and I am currently not sure how to speed it up
|
|
84
|
+
// As such, we'll try to avoid getting them when unneeded, such as when we're doing HTML stuff
|
|
85
|
+
// When at the root of the document TypeScript offer all kinds of completions, because it doesn't know yet that
|
|
86
|
+
// it's JSX and not JS. As such, people who are using Emmet to write their template suffer from a very degraded experience
|
|
87
|
+
// from what they're used to in HTML files (which is instant completions). So let's disable ourselves when we're at the root
|
|
88
|
+
if (!isCompletionInsideFrontmatter && !node.parent && !isCompletionInsideExpression) {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
// If the user just typed `<` with nothing else, let's disable ourselves until we're more sure if the user wants TS completions
|
|
92
|
+
if (!isCompletionInsideFrontmatter && node.parent && node.tag === undefined && !isCompletionInsideExpression) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
// If the current node is not a component (aka, it doesn't start with a caps), let's disable ourselves as the user
|
|
96
|
+
// is most likely looking for HTML completions
|
|
97
|
+
if (!isCompletionInsideFrontmatter && !(0, utils_1.isComponentTag)(node) && !isCompletionInsideExpression) {
|
|
24
98
|
return null;
|
|
25
99
|
}
|
|
26
100
|
const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
|
|
27
101
|
const filePath = (0, utils_2.toVirtualAstroFilePath)(tsDoc.filePath);
|
|
102
|
+
const completions = lang.getCompletionsAtPosition(filePath, offset, {
|
|
103
|
+
...exports.completionOptions,
|
|
104
|
+
triggerCharacter: validTriggerCharacter,
|
|
105
|
+
});
|
|
106
|
+
if (completions === undefined || completions.entries.length === 0) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
const wordRange = completions.optionalReplacementSpan
|
|
110
|
+
? vscode_languageserver_1.Range.create(document.positionAt(completions.optionalReplacementSpan.start), document.positionAt(completions.optionalReplacementSpan.start + completions.optionalReplacementSpan.length))
|
|
111
|
+
: undefined;
|
|
112
|
+
const wordRangeStartPosition = wordRange?.start;
|
|
28
113
|
const fragment = await tsDoc.createFragment();
|
|
29
|
-
const
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
.map((entry) => this.toCompletionItem(fragment, entry,
|
|
33
|
-
.filter(
|
|
34
|
-
|
|
114
|
+
const existingImports = this.getExistingImports(document);
|
|
115
|
+
const completionItems = completions.entries
|
|
116
|
+
.filter(this.isValidCompletion)
|
|
117
|
+
.map((entry) => this.toCompletionItem(fragment, entry, filePath, offset, isCompletionInsideFrontmatter, existingImports))
|
|
118
|
+
.filter(utils_3.isNotNullOrUndefined)
|
|
119
|
+
.map((comp) => this.fixTextEditRange(wordRangeStartPosition, comp));
|
|
120
|
+
const completionList = vscode_languageserver_2.CompletionList.create(completionItems, true);
|
|
121
|
+
this.lastCompletion = { key: document.getFilePath() || '', position, completionList };
|
|
122
|
+
return completionList;
|
|
35
123
|
}
|
|
36
|
-
async resolveCompletion(document,
|
|
37
|
-
const { data: comp } = completionItem;
|
|
124
|
+
async resolveCompletion(document, item, cancellationToken) {
|
|
38
125
|
const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
|
|
39
|
-
|
|
40
|
-
if (!
|
|
41
|
-
return
|
|
126
|
+
const data = item.data;
|
|
127
|
+
if (!data || !data.filePath || cancellationToken?.isCancellationRequested) {
|
|
128
|
+
return item;
|
|
42
129
|
}
|
|
43
130
|
const fragment = await tsDoc.createFragment();
|
|
44
|
-
const detail = lang.getCompletionEntryDetails(filePath, // fileName
|
|
45
|
-
|
|
46
|
-
|
|
131
|
+
const detail = lang.getCompletionEntryDetails(data.filePath, // fileName
|
|
132
|
+
data.offset, // position
|
|
133
|
+
data.originalItem.name, // entryName
|
|
47
134
|
{}, // formatOptions
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
135
|
+
data.originalItem.source, // source
|
|
136
|
+
exports.completionOptions, // preferences
|
|
137
|
+
data.originalItem.data // data
|
|
51
138
|
);
|
|
52
139
|
if (detail) {
|
|
53
140
|
const { detail: itemDetail, documentation: itemDocumentation } = this.getCompletionDocument(detail);
|
|
54
|
-
|
|
55
|
-
|
|
141
|
+
item.detail = itemDetail;
|
|
142
|
+
item.documentation = itemDocumentation;
|
|
56
143
|
}
|
|
57
|
-
|
|
144
|
+
const actions = detail?.codeActions;
|
|
145
|
+
if (actions) {
|
|
146
|
+
const edit = [];
|
|
147
|
+
for (const action of actions) {
|
|
148
|
+
for (const change of action.changes) {
|
|
149
|
+
edit.push(...change.textChanges.map((textChange) => codeActionChangeToTextEdit(document, fragment, textChange)));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
item.additionalTextEdits = (item.additionalTextEdits ?? []).concat(edit);
|
|
153
|
+
}
|
|
154
|
+
return item;
|
|
58
155
|
}
|
|
59
|
-
toCompletionItem(fragment, comp,
|
|
156
|
+
toCompletionItem(fragment, comp, filePath, offset, insideFrontmatter, existingImports) {
|
|
157
|
+
let item = vscode_languageserver_protocol_1.CompletionItem.create(comp.name);
|
|
158
|
+
const isAstroComponent = this.isAstroComponentImport(comp.name);
|
|
159
|
+
const isImport = comp.insertText?.includes('import');
|
|
160
|
+
// Avoid showing completions for using components as functions
|
|
161
|
+
if (isAstroComponent && !isImport && insideFrontmatter) {
|
|
162
|
+
return null;
|
|
163
|
+
}
|
|
164
|
+
if (isAstroComponent) {
|
|
165
|
+
item.label = (0, utils_2.removeAstroComponentSuffix)(comp.name);
|
|
166
|
+
// Set component imports as file completion, that way we get cool icons
|
|
167
|
+
item.kind = vscode_languageserver_protocol_1.CompletionItemKind.File;
|
|
168
|
+
item.detail = comp.data?.moduleSpecifier;
|
|
169
|
+
}
|
|
170
|
+
else {
|
|
171
|
+
item.kind = (0, utils_2.scriptElementKindToCompletionItemKind)(comp.kind);
|
|
172
|
+
}
|
|
173
|
+
// TS may suggest another component even if there already exists an import with the same.
|
|
174
|
+
// This happens because internally, components get suffixed with __AstroComponent_
|
|
175
|
+
if (isAstroComponent && existingImports.has(item.label)) {
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
if (comp.kindModifiers) {
|
|
179
|
+
const kindModifiers = new Set(comp.kindModifiers.split(/,|\s+/g));
|
|
180
|
+
if (kindModifiers.has(typescript_1.ScriptElementKindModifier.deprecatedModifier)) {
|
|
181
|
+
item.tags = [vscode_languageserver_1.CompletionItemTag.Deprecated];
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
// Label details are currently unsupported, however, they'll be supported in the next version of LSP
|
|
185
|
+
if (comp.sourceDisplay) {
|
|
186
|
+
item.labelDetails = { description: typescript_1.default.displayPartsToString(comp.sourceDisplay) };
|
|
187
|
+
}
|
|
188
|
+
item.commitCharacters = (0, utils_2.getCommitCharactersForScriptElement)(comp.kind);
|
|
189
|
+
item.sortText = comp.sortText;
|
|
190
|
+
item.preselect = comp.isRecommended;
|
|
191
|
+
if (comp.replacementSpan) {
|
|
192
|
+
item.insertText = comp.insertText ? (0, utils_2.removeAstroComponentSuffix)(comp.insertText) : undefined;
|
|
193
|
+
item.insertTextFormat = comp.isSnippet ? vscode_languageserver_1.InsertTextFormat.Snippet : vscode_languageserver_1.InsertTextFormat.PlainText;
|
|
194
|
+
item.textEdit = comp.replacementSpan
|
|
195
|
+
? vscode_languageserver_1.TextEdit.replace((0, utils_2.convertRange)(fragment, comp.replacementSpan), item.insertText ?? item.label)
|
|
196
|
+
: undefined;
|
|
197
|
+
}
|
|
60
198
|
return {
|
|
61
|
-
|
|
62
|
-
insertText: comp.insertText,
|
|
63
|
-
kind: (0, utils_2.scriptElementKindToCompletionItemKind)(comp.kind),
|
|
64
|
-
commitCharacters: (0, utils_2.getCommitCharactersForScriptElement)(comp.kind),
|
|
65
|
-
// Make sure svelte component takes precedence
|
|
66
|
-
sortText: comp.sortText,
|
|
67
|
-
preselect: comp.isRecommended,
|
|
68
|
-
// pass essential data for resolving completion
|
|
199
|
+
...item,
|
|
69
200
|
data: {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
201
|
+
uri: fragment.getURL(),
|
|
202
|
+
filePath,
|
|
203
|
+
offset,
|
|
204
|
+
originalItem: comp,
|
|
73
205
|
},
|
|
74
206
|
};
|
|
75
207
|
}
|
|
208
|
+
isValidCompletion(completion) {
|
|
209
|
+
// Remove completion for default exported function
|
|
210
|
+
if (completion.name === 'default' && completion.kindModifiers == typescript_1.ScriptElementKindModifier.exportedModifier) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
return true;
|
|
214
|
+
}
|
|
76
215
|
getCompletionDocument(compDetail) {
|
|
77
|
-
const {
|
|
78
|
-
let detail = typescript_1.default.displayPartsToString(displayParts);
|
|
79
|
-
if (
|
|
80
|
-
const importPath = typescript_1.default.displayPartsToString(
|
|
81
|
-
detail =
|
|
82
|
-
}
|
|
83
|
-
const documentation =
|
|
84
|
-
|
|
85
|
-
:
|
|
216
|
+
const { sourceDisplay, documentation: tsDocumentation, displayParts } = compDetail;
|
|
217
|
+
let detail = (0, utils_2.removeAstroComponentSuffix)(typescript_1.default.displayPartsToString(displayParts));
|
|
218
|
+
if (sourceDisplay) {
|
|
219
|
+
const importPath = typescript_1.default.displayPartsToString(sourceDisplay);
|
|
220
|
+
detail = importPath;
|
|
221
|
+
}
|
|
222
|
+
const documentation = {
|
|
223
|
+
kind: 'markdown',
|
|
224
|
+
value: (0, previewer_1.getMarkdownDocumentation)(tsDocumentation, compDetail.tags),
|
|
225
|
+
};
|
|
86
226
|
return {
|
|
87
227
|
documentation,
|
|
88
228
|
detail,
|
|
89
229
|
};
|
|
90
230
|
}
|
|
231
|
+
/**
|
|
232
|
+
* If the textEdit is out of the word range of the triggered position
|
|
233
|
+
* vscode would refuse to show the completions
|
|
234
|
+
* split those edits into additionalTextEdit to fix it
|
|
235
|
+
*/
|
|
236
|
+
fixTextEditRange(wordRangePosition, completionItem) {
|
|
237
|
+
const { textEdit } = completionItem;
|
|
238
|
+
if (!textEdit || !vscode_languageserver_1.TextEdit.is(textEdit) || !wordRangePosition) {
|
|
239
|
+
return completionItem;
|
|
240
|
+
}
|
|
241
|
+
const { newText, range: { start }, } = textEdit;
|
|
242
|
+
const wordRangeStartCharacter = wordRangePosition.character;
|
|
243
|
+
if (wordRangePosition.line !== wordRangePosition.line || start.character > wordRangePosition.character) {
|
|
244
|
+
return completionItem;
|
|
245
|
+
}
|
|
246
|
+
textEdit.newText = newText.substring(wordRangeStartCharacter - start.character);
|
|
247
|
+
textEdit.range.start = {
|
|
248
|
+
line: start.line,
|
|
249
|
+
character: wordRangeStartCharacter,
|
|
250
|
+
};
|
|
251
|
+
completionItem.additionalTextEdits = [
|
|
252
|
+
vscode_languageserver_1.TextEdit.replace({
|
|
253
|
+
start,
|
|
254
|
+
end: {
|
|
255
|
+
line: start.line,
|
|
256
|
+
character: wordRangeStartCharacter,
|
|
257
|
+
},
|
|
258
|
+
}, newText.substring(0, wordRangeStartCharacter - start.character)),
|
|
259
|
+
];
|
|
260
|
+
return completionItem;
|
|
261
|
+
}
|
|
262
|
+
canReuseLastCompletion(lastCompletion, triggerKind, triggerCharacter, document, position) {
|
|
263
|
+
return (!!lastCompletion &&
|
|
264
|
+
lastCompletion.key === document.getFilePath() &&
|
|
265
|
+
lastCompletion.position.line === position.line &&
|
|
266
|
+
Math.abs(lastCompletion.position.character - position.character) < 2 &&
|
|
267
|
+
(triggerKind === vscode_languageserver_1.CompletionTriggerKind.TriggerForIncompleteCompletions ||
|
|
268
|
+
// Special case: `.` is a trigger character, but inside import path completions
|
|
269
|
+
// it shouldn't trigger another completion because we can reuse the old one
|
|
270
|
+
(triggerCharacter === '.' && (0, utils_4.isPartOfImportStatement)(document.getText(), position))));
|
|
271
|
+
}
|
|
272
|
+
getExistingImports(document) {
|
|
273
|
+
const rawImports = (0, utils_3.getRegExpMatches)(scriptImportRegex, document.getText()).map((match) => (match[1] ?? match[2]).split(','));
|
|
274
|
+
const tidiedImports = (0, lodash_1.flatten)(rawImports).map((match) => match.trim());
|
|
275
|
+
return new Set(tidiedImports);
|
|
276
|
+
}
|
|
277
|
+
isAstroComponentImport(className) {
|
|
278
|
+
return className.endsWith('__AstroComponent_');
|
|
279
|
+
}
|
|
91
280
|
}
|
|
92
281
|
exports.CompletionsProviderImpl = CompletionsProviderImpl;
|
|
282
|
+
function codeActionChangeToTextEdit(document, fragment, change) {
|
|
283
|
+
change.newText = (0, utils_2.removeAstroComponentSuffix)(change.newText);
|
|
284
|
+
// If we don't have a frontmatter already, create one with the import
|
|
285
|
+
const frontmatterState = document.astroMeta.frontmatter.state;
|
|
286
|
+
if (frontmatterState === null) {
|
|
287
|
+
return vscode_languageserver_1.TextEdit.replace(vscode_languageserver_1.Range.create(vscode_languageserver_1.Position.create(0, 0), vscode_languageserver_1.Position.create(0, 0)), `---${typescript_1.default.sys.newLine}${change.newText}---${typescript_1.default.sys.newLine}${typescript_1.default.sys.newLine}`);
|
|
288
|
+
}
|
|
289
|
+
const { span } = change;
|
|
290
|
+
let range;
|
|
291
|
+
const virtualRange = (0, utils_2.convertRange)(fragment, span);
|
|
292
|
+
range = (0, documents_1.mapRangeToOriginal)(fragment, virtualRange);
|
|
293
|
+
if (!(0, utils_1.isInsideFrontmatter)(document.getText(), document.offsetAt(range.start))) {
|
|
294
|
+
range = (0, utils_2.ensureFrontmatterInsert)(range, document);
|
|
295
|
+
}
|
|
296
|
+
// First import in a file will wrongly have a newline before it due to how the frontmatter is replaced by a comment
|
|
297
|
+
if (range.start.line === 1 && (change.newText.startsWith('\n') || change.newText.startsWith('\r\n'))) {
|
|
298
|
+
change.newText = change.newText.trimStart();
|
|
299
|
+
}
|
|
300
|
+
return vscode_languageserver_1.TextEdit.replace(range, change.newText);
|
|
301
|
+
}
|
|
302
|
+
exports.codeActionChangeToTextEdit = codeActionChangeToTextEdit;
|
|
@@ -13,24 +13,21 @@ class DiagnosticsProviderImpl {
|
|
|
13
13
|
this.languageServiceManager = languageServiceManager;
|
|
14
14
|
}
|
|
15
15
|
async getDiagnostics(document, _cancellationToken) {
|
|
16
|
-
var _a, _b, _c;
|
|
17
16
|
// Don't return diagnostics for files inside node_modules. These are considered read-only
|
|
18
17
|
// and they would pollute the output for astro check
|
|
19
|
-
if (
|
|
18
|
+
if (document.getFilePath()?.includes('/node_modules/') || document.getFilePath()?.includes('\\node_modules\\')) {
|
|
20
19
|
return [];
|
|
21
20
|
}
|
|
22
21
|
const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
|
|
23
22
|
const filePath = (0, utils_1.toVirtualAstroFilePath)(tsDoc.filePath);
|
|
24
|
-
const { script: scriptBoundaries
|
|
23
|
+
const { script: scriptBoundaries } = this.getTagBoundaries(lang, filePath);
|
|
25
24
|
const syntaxDiagnostics = lang.getSyntacticDiagnostics(filePath);
|
|
26
25
|
const suggestionDiagnostics = lang.getSuggestionDiagnostics(filePath);
|
|
27
26
|
const semanticDiagnostics = lang.getSemanticDiagnostics(filePath).filter((d) => {
|
|
28
|
-
return
|
|
27
|
+
return isNoWithinBoundary(scriptBoundaries, d);
|
|
29
28
|
});
|
|
30
29
|
const diagnostics = [...syntaxDiagnostics, ...suggestionDiagnostics, ...semanticDiagnostics];
|
|
31
30
|
const fragment = await tsDoc.createFragment();
|
|
32
|
-
const sourceFile = (_c = lang.getProgram()) === null || _c === void 0 ? void 0 : _c.getSourceFile(filePath);
|
|
33
|
-
const isNoFalsePositiveInst = isNoFalsePositive();
|
|
34
31
|
return diagnostics
|
|
35
32
|
.map((diagnostic) => ({
|
|
36
33
|
range: (0, utils_1.convertRange)(tsDoc, diagnostic),
|
|
@@ -43,20 +40,19 @@ class DiagnosticsProviderImpl {
|
|
|
43
40
|
.map(mapRange(fragment, document))
|
|
44
41
|
.filter((diag) => {
|
|
45
42
|
return (hasNoNegativeLines(diag) &&
|
|
46
|
-
isNoFalsePositiveInst(diag) &&
|
|
47
43
|
isNoJSXImplicitRuntimeWarning(diag) &&
|
|
48
44
|
isNoJSXMustHaveOneParent(diag) &&
|
|
49
|
-
isNoCantUseJSX(diag) &&
|
|
50
45
|
isNoCantEndWithTS(diag) &&
|
|
51
46
|
isNoSpreadExpected(diag) &&
|
|
52
47
|
isNoCantResolveJSONModule(diag) &&
|
|
53
|
-
|
|
48
|
+
isNoCantReturnOutsideFunction(diag) &&
|
|
49
|
+
isNoJsxCannotHaveMultipleAttrsError(diag));
|
|
54
50
|
})
|
|
55
51
|
.map(enhanceIfNecessary);
|
|
56
52
|
}
|
|
57
53
|
getTagBoundaries(lang, tsFilePath) {
|
|
58
54
|
const program = lang.getProgram();
|
|
59
|
-
const sourceFile = program
|
|
55
|
+
const sourceFile = program?.getSourceFile(tsFilePath);
|
|
60
56
|
const boundaries = {
|
|
61
57
|
script: [],
|
|
62
58
|
markdown: [],
|
|
@@ -64,7 +60,7 @@ class DiagnosticsProviderImpl {
|
|
|
64
60
|
if (!sourceFile) {
|
|
65
61
|
return boundaries;
|
|
66
62
|
}
|
|
67
|
-
function
|
|
63
|
+
function findTags(parent) {
|
|
68
64
|
typescript_1.default.forEachChild(parent, (node) => {
|
|
69
65
|
if (typescript_1.default.isJsxElement(node)) {
|
|
70
66
|
let tagName = node.openingElement.tagName.getText();
|
|
@@ -80,10 +76,10 @@ class DiagnosticsProviderImpl {
|
|
|
80
76
|
}
|
|
81
77
|
}
|
|
82
78
|
}
|
|
83
|
-
|
|
79
|
+
findTags(node);
|
|
84
80
|
});
|
|
85
81
|
}
|
|
86
|
-
|
|
82
|
+
findTags(sourceFile);
|
|
87
83
|
return boundaries;
|
|
88
84
|
}
|
|
89
85
|
}
|
|
@@ -116,33 +112,55 @@ function mapRange(fragment, _document) {
|
|
|
116
112
|
function hasNoNegativeLines(diagnostic) {
|
|
117
113
|
return diagnostic.range.start.line >= 0 && diagnostic.range.end.line >= 0;
|
|
118
114
|
}
|
|
119
|
-
function isNoFalsePositive() {
|
|
120
|
-
return (diagnostic) => {
|
|
121
|
-
return isNoJsxCannotHaveMultipleAttrsError(diagnostic);
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
115
|
/**
|
|
125
|
-
*
|
|
126
|
-
* but that's allowed for svelte
|
|
116
|
+
* Astro allows multiple attributes to have the same name
|
|
127
117
|
*/
|
|
128
118
|
function isNoJsxCannotHaveMultipleAttrsError(diagnostic) {
|
|
129
119
|
return diagnostic.code !== 17001;
|
|
130
120
|
}
|
|
131
|
-
|
|
132
|
-
return diagnostic.code !== 7016 && diagnostic.code !== 2792;
|
|
133
|
-
}
|
|
121
|
+
/** Astro allows component with multiple root elements */
|
|
134
122
|
function isNoJSXMustHaveOneParent(diagnostic) {
|
|
135
123
|
return diagnostic.code !== 2657;
|
|
136
124
|
}
|
|
137
|
-
|
|
138
|
-
return diagnostic.code !== 17004 && diagnostic.code !== 6142;
|
|
139
|
-
}
|
|
125
|
+
/** Astro allows `.ts` ending for imports, unlike TypeScript */
|
|
140
126
|
function isNoCantEndWithTS(diagnostic) {
|
|
141
127
|
return diagnostic.code !== 2691;
|
|
142
128
|
}
|
|
143
129
|
function isNoSpreadExpected(diagnostic) {
|
|
144
130
|
return diagnostic.code !== 1005;
|
|
145
131
|
}
|
|
132
|
+
function isNoJSXImplicitRuntimeWarning(diagnostic) {
|
|
133
|
+
return diagnostic.code !== 7016 && diagnostic.code !== 2792;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Ignore "Can't return outside of function body"
|
|
137
|
+
* This is technically a valid diagnostic, but due to how we format our TSX, the frontmatter is at top-level so we have
|
|
138
|
+
* to ignore this. It wasn't a problem before because users didn't need to return things but they can now with SSR
|
|
139
|
+
*/
|
|
140
|
+
function isNoCantReturnOutsideFunction(diagnostic) {
|
|
141
|
+
return diagnostic.code !== 1108;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Astro allows users to import JSON modules
|
|
145
|
+
*/
|
|
146
|
+
function isNoCantResolveJSONModule(diagnostic) {
|
|
147
|
+
return diagnostic.code !== 2732;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Some diagnostics have JSX-specific nomenclature or unclear description. Enhance them for more clarity.
|
|
151
|
+
*/
|
|
152
|
+
function enhanceIfNecessary(diagnostic) {
|
|
153
|
+
if (diagnostic.code === 2322) {
|
|
154
|
+
// For the rare case where an user might try to put a client directive on something that is not a component
|
|
155
|
+
if (diagnostic.message.includes("Property 'client:") && diagnostic.message.includes("to type 'HTMLProps")) {
|
|
156
|
+
return {
|
|
157
|
+
...diagnostic,
|
|
158
|
+
message: 'Client directives are only available on framework components',
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
return diagnostic;
|
|
163
|
+
}
|
|
146
164
|
function isWithinBoundaries(boundaries, start) {
|
|
147
165
|
for (let [bstart, bend] of boundaries) {
|
|
148
166
|
if (start > bstart && start < bend) {
|
|
@@ -163,39 +181,6 @@ function diagnosticIsWithinBoundaries(sourceFile, boundaries, diagnostic) {
|
|
|
163
181
|
let pos = typescript_1.default.getPositionOfLineAndCharacter(sourceFile, startRange.line, startRange.character);
|
|
164
182
|
return isWithinBoundaries(boundaries, pos);
|
|
165
183
|
}
|
|
166
|
-
function
|
|
184
|
+
function isNoWithinBoundary(boundaries, diagnostic) {
|
|
167
185
|
return !diagnosticIsWithinBoundaries(undefined, boundaries, diagnostic);
|
|
168
186
|
}
|
|
169
|
-
/**
|
|
170
|
-
* This allows us to have JSON module imports.
|
|
171
|
-
*/
|
|
172
|
-
function isNoCantResolveJSONModule(diagnostic) {
|
|
173
|
-
return diagnostic.code !== 2732;
|
|
174
|
-
}
|
|
175
|
-
/**
|
|
176
|
-
* This is for using > within a markdown component like:
|
|
177
|
-
* <Markdown>
|
|
178
|
-
* > Blockquote here.
|
|
179
|
-
* </Markdown>
|
|
180
|
-
*/
|
|
181
|
-
function isNoMarkdownBlockQuoteWithinMarkdown(sourceFile, boundaries, diagnostic) {
|
|
182
|
-
if (diagnostic.code !== 1382) {
|
|
183
|
-
return true;
|
|
184
|
-
}
|
|
185
|
-
return !diagnosticIsWithinBoundaries(sourceFile, boundaries, diagnostic);
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Some diagnostics have JSX-specific nomenclature. Enhance them for more clarity.
|
|
189
|
-
*/
|
|
190
|
-
function enhanceIfNecessary(diagnostic) {
|
|
191
|
-
if (diagnostic.code === 2322) {
|
|
192
|
-
// For the rare case where an user might try to put a client directive on something that is not a component
|
|
193
|
-
if (diagnostic.message.includes("Property 'client:") && diagnostic.message.includes("to type 'HTMLProps")) {
|
|
194
|
-
return {
|
|
195
|
-
...diagnostic,
|
|
196
|
-
message: 'Client directives are only available on framework components',
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return diagnostic;
|
|
201
|
-
}
|
|
@@ -11,7 +11,6 @@ class DocumentSymbolsProviderImpl {
|
|
|
11
11
|
this.languageServiceManager = languageServiceManager;
|
|
12
12
|
}
|
|
13
13
|
async getDocumentSymbols(document) {
|
|
14
|
-
var _a, _b, _c;
|
|
15
14
|
const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
|
|
16
15
|
const fragment = await tsDoc.createFragment();
|
|
17
16
|
const navTree = lang.getNavigationTree(tsDoc.filePath);
|
|
@@ -27,23 +26,23 @@ class DocumentSymbolsProviderImpl {
|
|
|
27
26
|
result.push(vscode_languageserver_types_1.SymbolInformation.create('Frontmatter', vscode_languageserver_types_1.SymbolKind.Namespace, vscode_languageserver_types_1.Range.create(document.positionAt(document.astroMeta.frontmatter.startOffset), document.positionAt(document.astroMeta.frontmatter.endOffset)), document.getURL()));
|
|
28
27
|
}
|
|
29
28
|
// Add a "Template" namespace for everything under the frontmatter
|
|
30
|
-
result.push(vscode_languageserver_types_1.SymbolInformation.create('Template', vscode_languageserver_types_1.SymbolKind.Namespace, vscode_languageserver_types_1.Range.create(document.positionAt(
|
|
29
|
+
result.push(vscode_languageserver_types_1.SymbolInformation.create('Template', vscode_languageserver_types_1.SymbolKind.Namespace, vscode_languageserver_types_1.Range.create(document.positionAt(document.astroMeta.frontmatter.endOffset ?? 0), document.positionAt(document.getTextLength())), document.getURL()));
|
|
31
30
|
for (let symbol of symbols.splice(1)) {
|
|
32
31
|
symbol = (0, documents_1.mapSymbolInformationToOriginal)(fragment, symbol);
|
|
33
|
-
if (document.offsetAt(symbol.location.range.end) >= (
|
|
32
|
+
if (document.offsetAt(symbol.location.range.end) >= (document.astroMeta.content.firstNonWhitespaceOffset ?? 0)) {
|
|
34
33
|
if (symbol.containerName === originalContainerName) {
|
|
35
34
|
symbol.containerName = 'Template';
|
|
36
35
|
}
|
|
37
36
|
// For some reason, it seems like TypeScript thinks that the "class" attribute is a real class, weird
|
|
38
37
|
if (symbol.kind === vscode_languageserver_types_1.SymbolKind.Class && symbol.name === '<class>') {
|
|
39
38
|
const node = document.html.findNodeAt(document.offsetAt(symbol.location.range.start));
|
|
40
|
-
if (
|
|
39
|
+
if (node.attributes?.class) {
|
|
41
40
|
continue;
|
|
42
41
|
}
|
|
43
42
|
}
|
|
44
43
|
}
|
|
45
|
-
// Remove the
|
|
46
|
-
if (symbol.
|
|
44
|
+
// Remove the exported function in our TSX output from the symbols
|
|
45
|
+
if (document.offsetAt(symbol.location.range.start) >= document.getTextLength()) {
|
|
47
46
|
continue;
|
|
48
47
|
}
|
|
49
48
|
result.push(symbol);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { FoldingRange } from 'vscode-languageserver';
|
|
2
|
+
import { AstroDocument } from '../../../core/documents';
|
|
3
|
+
import { FoldingRangesProvider } from '../../interfaces';
|
|
4
|
+
import { LanguageServiceManager } from '../LanguageServiceManager';
|
|
5
|
+
export declare class FoldingRangesProviderImpl implements FoldingRangesProvider {
|
|
6
|
+
private readonly languageServiceManager;
|
|
7
|
+
constructor(languageServiceManager: LanguageServiceManager);
|
|
8
|
+
getFoldingRanges(document: AstroDocument): Promise<FoldingRange[] | null>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.FoldingRangesProviderImpl = void 0;
|
|
7
|
+
const typescript_1 = __importDefault(require("typescript"));
|
|
8
|
+
const vscode_languageserver_1 = require("vscode-languageserver");
|
|
9
|
+
const utils_1 = require("../utils");
|
|
10
|
+
class FoldingRangesProviderImpl {
|
|
11
|
+
constructor(languageServiceManager) {
|
|
12
|
+
this.languageServiceManager = languageServiceManager;
|
|
13
|
+
}
|
|
14
|
+
async getFoldingRanges(document) {
|
|
15
|
+
const html = document.html;
|
|
16
|
+
const { lang, tsDoc } = await this.languageServiceManager.getLSAndTSDoc(document);
|
|
17
|
+
const filePath = (0, utils_1.toVirtualAstroFilePath)(tsDoc.filePath);
|
|
18
|
+
const outliningSpans = lang.getOutliningSpans(filePath);
|
|
19
|
+
const foldingRanges = [];
|
|
20
|
+
for (const span of outliningSpans) {
|
|
21
|
+
const node = html.findNodeAt(span.textSpan.start);
|
|
22
|
+
// Due to how our TSX output transform those tags into function calls or template literals
|
|
23
|
+
// TypeScript thinks of those as outlining spans, which is fine but we don't want folding ranges for those
|
|
24
|
+
if (node.tag === 'script' || node.tag === 'Markdown' || node.tag === 'style') {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const start = document.positionAt(span.textSpan.start);
|
|
28
|
+
const end = adjustFoldingEnd(start, document.positionAt(span.textSpan.start + span.textSpan.length), document);
|
|
29
|
+
// When using this method for generating folding ranges, TypeScript tend to return some
|
|
30
|
+
// one line / one character ones that we should be able to safely ignore
|
|
31
|
+
if (start.line === end.line && start.character === end.character) {
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
foldingRanges.push(vscode_languageserver_1.FoldingRange.create(start.line, end.line, start.character, end.character, transformFoldingRangeKind(span.kind)));
|
|
35
|
+
}
|
|
36
|
+
return foldingRanges;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.FoldingRangesProviderImpl = FoldingRangesProviderImpl;
|
|
40
|
+
function transformFoldingRangeKind(tsKind) {
|
|
41
|
+
switch (tsKind) {
|
|
42
|
+
case typescript_1.default.OutliningSpanKind.Comment:
|
|
43
|
+
return vscode_languageserver_1.FoldingRangeKind.Comment;
|
|
44
|
+
case typescript_1.default.OutliningSpanKind.Imports:
|
|
45
|
+
return vscode_languageserver_1.FoldingRangeKind.Imports;
|
|
46
|
+
case typescript_1.default.OutliningSpanKind.Region:
|
|
47
|
+
return vscode_languageserver_1.FoldingRangeKind.Region;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// https://github.com/microsoft/vscode/blob/bed61166fb604e519e82e4d1d1ed839bc45d65f8/extensions/typescript-language-features/src/languageFeatures/folding.ts#L61-L73
|
|
51
|
+
function adjustFoldingEnd(start, end, document) {
|
|
52
|
+
// workaround for #47240
|
|
53
|
+
if (end.character > 0) {
|
|
54
|
+
const foldEndCharacter = document.getText({
|
|
55
|
+
start: { line: end.line, character: end.character - 1 },
|
|
56
|
+
end,
|
|
57
|
+
});
|
|
58
|
+
if (['}', ']', ')', '`'].includes(foldEndCharacter)) {
|
|
59
|
+
const endOffset = Math.max(document.offsetAt({ line: end.line, character: 0 }) - 1, document.offsetAt(start));
|
|
60
|
+
return document.positionAt(endOffset);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return end;
|
|
64
|
+
}
|