-
Notifications
You must be signed in to change notification settings - Fork 23
/
syntactic.ts
175 lines (154 loc) · 5.67 KB
/
syntactic.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
import type {
ProviderResult,
ServiceContext,
LanguageServicePlugin,
LanguageServicePluginInstance
} from '@volar/language-service';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { getFormatCodeSettings } from '../configs/getFormatCodeSettings';
import { getConfigTitle, isTsDocument, safeCall } from '../shared';
import {
convertNavTree,
convertOutliningSpan,
convertTextChange
} from '../utils/lspConverters';
import { createSyntaxOnlyService } from '../syntaxOnlyService';
import type * as ts from 'typescript';
const snapshots = new WeakMap<TextDocument, [number, ts.IScriptSnapshot]>();
let created: ReturnType<typeof createSyntaxOnlyService> | undefined;
export function getLanguageServiceByDocument(ts: typeof import('typescript'), document: TextDocument) {
if (!created) {
created = createSyntaxOnlyService(ts, true);
}
let cache = snapshots.get(document);
if (!cache || cache[0] !== document.version) {
const snapshot = ts.ScriptSnapshot.fromString(document.getText());
cache = [document.version, snapshot];
snapshots.set(document, cache);
created.updateFile(
document.uri,
cache[1],
document.languageId === 'javascript'
? ts.ScriptKind.JS
: document.languageId === 'javascriptreact'
? ts.ScriptKind.JSX
: document.languageId === 'typescriptreact'
? ts.ScriptKind.TSX
: ts.ScriptKind.TS
);
}
return {
languageService: created.languageService,
fileName: document.uri,
};
}
export function create(
ts: typeof import('typescript'),
{
isFormattingEnabled = async (document, context) => {
return await context.env.getConfiguration?.<boolean>(getConfigTitle(document) + '.format.enable') ?? true;
},
isAutoClosingTagsEnabled = async (document, context) => {
return await context.env.getConfiguration?.<boolean>(getConfigTitle(document) + '.autoClosingTags') ?? true;
},
}: {
isFormattingEnabled?(document: TextDocument, context: ServiceContext): ProviderResult<boolean>;
isAutoClosingTagsEnabled?(document: TextDocument, context: ServiceContext): ProviderResult<boolean>;
} = {},
): LanguageServicePlugin {
return {
name: 'typescript-syntactic',
// https://github.com/microsoft/vscode/blob/ce119308e8fd4cd3f992d42b297588e7abe33a0c/extensions/typescript-language-features/src/languageFeatures/formatting.ts#L99
autoFormatTriggerCharacters: [';', '}', '\n'],
create(context): LanguageServicePluginInstance {
return {
async provideAutoInsertionEdit(document, selection, change) {
// selection must at end of change
if (document.offsetAt(selection) !== change.rangeOffset + change.text.length) {
return;
}
if (
(document.languageId === 'javascriptreact' || document.languageId === 'typescriptreact')
&& change.text.endsWith('>')
&& await isAutoClosingTagsEnabled(document, context)
) {
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const close = languageService.getJsxClosingTagAtPosition(fileName, document.offsetAt(selection));
if (close) {
return '$0' + close.newText;
}
}
},
provideFoldingRanges(document) {
if (!isTsDocument(document)) {
return;
}
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const outliningSpans = safeCall(() => languageService.getOutliningSpans(fileName));
if (!outliningSpans) {
return [];
}
return outliningSpans.map(span => convertOutliningSpan(span, document));
},
provideDocumentSymbols(document) {
if (!isTsDocument(document)) {
return;
}
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const barItems = safeCall(() => languageService.getNavigationTree(fileName));
if (!barItems) {
return [];
}
// The root represents the file. Ignore this when showing in the UI
return barItems.childItems
?.map(item => convertNavTree(item, document))
.flat()
?? [];
},
async provideDocumentFormattingEdits(document, range, options, codeOptions) {
if (!isTsDocument(document)) {
return;
}
if (!await isFormattingEnabled(document, context)) {
return;
}
const tsOptions = await getFormatCodeSettings(context, document, options);
if (codeOptions) {
tsOptions.baseIndentSize = codeOptions.initialIndentLevel * options.tabSize;
}
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const scriptEdits = range
? safeCall(() => languageService.getFormattingEditsForRange(
fileName,
document.offsetAt(range.start),
document.offsetAt(range.end),
tsOptions,
))
: safeCall(() => languageService.getFormattingEditsForDocument(fileName, tsOptions));
if (!scriptEdits) {
return [];
}
return scriptEdits.map(edit => convertTextChange(edit, document));
},
async provideOnTypeFormattingEdits(document, position, key, options, codeOptions) {
if (!isTsDocument(document)) {
return;
}
if (!await isFormattingEnabled(document, context)) {
return;
}
const tsOptions = await getFormatCodeSettings(context, document, options);
if (codeOptions) {
tsOptions.baseIndentSize = codeOptions.initialIndentLevel * options.tabSize;
}
const { languageService, fileName } = getLanguageServiceByDocument(ts, document);
const scriptEdits = safeCall(() => languageService.getFormattingEditsAfterKeystroke(fileName, document.offsetAt(position), key, tsOptions));
if (!scriptEdits) {
return [];
}
return scriptEdits.map(edit => convertTextChange(edit, document));
},
};
},
};
}