forked from withastro/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
76 lines (67 loc) · 3.01 KB
/
index.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
import type { ServicePlugin, ServicePluginInstance } from '@volar/language-server';
import { create as createTypeScriptServices } from 'volar-service-typescript';
import { AstroVirtualCode } from '../../core/index.js';
import { enhancedProvideCodeActions, enhancedResolveCodeAction } from './codeActions.js';
import { enhancedProvideCompletionItems, enhancedResolveCompletionItem } from './completions.js';
import { enhancedProvideSemanticDiagnostics } from './diagnostics.js';
export const create = (ts: typeof import('typescript')): ServicePlugin[] => {
const tsServicePlugins = createTypeScriptServices(ts as typeof import('typescript'), {});
return tsServicePlugins.map<ServicePlugin>(plugin => {
if (plugin.name === 'typescript-semantic') {
return {
...plugin,
create(context): ServicePluginInstance {
const typeScriptPlugin = plugin.create(context);
return {
...typeScriptPlugin,
async provideCompletionItems(document, position, completionContext, token) {
const originalCompletions = await typeScriptPlugin.provideCompletionItems!(
document,
position,
completionContext,
token
);
if (!originalCompletions) return null;
return enhancedProvideCompletionItems(originalCompletions);
},
async resolveCompletionItem(item, token) {
const resolvedCompletionItem = await typeScriptPlugin.resolveCompletionItem!(item, token);
if (!resolvedCompletionItem) return item;
return enhancedResolveCompletionItem(resolvedCompletionItem, context);
},
async provideCodeActions(document, range, codeActionContext, token) {
const originalCodeActions = await typeScriptPlugin.provideCodeActions!(
document,
range,
codeActionContext,
token
);
if (!originalCodeActions) return null;
return enhancedProvideCodeActions(originalCodeActions, context);
},
async resolveCodeAction(codeAction, token) {
const resolvedCodeAction = await typeScriptPlugin.resolveCodeAction!(codeAction, token);
if (!resolvedCodeAction) return codeAction;
return enhancedResolveCodeAction(resolvedCodeAction, context);
},
async provideSemanticDiagnostics(document, token) {
const [_, source] = context.documents.getVirtualCodeByUri(document.uri);
const code = source?.generated?.code;
let tsxLineCount = undefined;
if (code instanceof AstroVirtualCode) {
// If we have compiler errors, our TSX isn't valid so don't bother showing TS errors
if (code.hasCompilationErrors) return null;
// We'll use this to filter out diagnostics that are outside the mapped range of the TSX
tsxLineCount = code.astroMeta.tsxRanges.body.end.line;
}
const diagnostics = await typeScriptPlugin.provideSemanticDiagnostics!(document, token);
if (!diagnostics) return null;
return enhancedProvideSemanticDiagnostics(diagnostics, tsxLineCount);
},
};
},
};
}
return plugin;
});
};