forked from vuejs/language-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-autoinsert-dotvalue.ts
196 lines (181 loc) · 5.63 KB
/
vue-autoinsert-dotvalue.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
import type { LanguageServiceContext, LanguageServicePlugin } from '@volar/language-service';
import { hyphenateAttr, VueVirtualCode } from '@vue/language-core';
import type * as ts from 'typescript';
import type { TextDocument } from 'vscode-languageserver-textdocument';
import { URI } from 'vscode-uri';
import { isTsDocument, sleep } from './utils';
export function create(
ts: typeof import('typescript'),
getTsPluginClient?: (context: LanguageServiceContext) => typeof import('@vue/typescript-plugin/lib/client') | undefined
): LanguageServicePlugin {
return {
name: 'vue-autoinsert-dotvalue',
capabilities: {
autoInsertionProvider: {
triggerCharacters: ['\\w'],
configurationSections: ['vue.autoInsert.dotValue'],
},
},
create(context) {
const tsPluginClient = getTsPluginClient?.(context);
let currentReq = 0;
return {
async provideAutoInsertSnippet(document, selection, change) {
// selection must at end of change
if (document.offsetAt(selection) !== change.rangeOffset + change.text.length) {
return;
}
if (!isTsDocument(document)) {
return;
}
if (!isCharacterTyping(document, change)) {
return;
}
const req = ++currentReq;
// Wait for tsserver to sync
await sleep(250);
if (req !== currentReq) {
return;
}
const enabled = await context.env.getConfiguration?.<boolean>('vue.autoInsert.dotValue') ?? true;
if (!enabled) {
return;
}
const uri = URI.parse(document.uri);
const decoded = context.decodeEmbeddedDocumentUri(uri);
const sourceScript = decoded && context.language.scripts.get(decoded[0]);
const virtualCode = decoded && sourceScript?.generated?.embeddedCodes.get(decoded[1]);
if (!sourceScript?.generated || !virtualCode) {
return;
}
const root = sourceScript.generated.root;
if (!(root instanceof VueVirtualCode)) {
return;
}
const { sfc } = root;
const blocks = [sfc.script, sfc.scriptSetup].filter(block => !!block);
if (!blocks.length) {
return;
}
let sourceCodeOffset = document.offsetAt(selection);
let mapped = false;
for (const [, map] of context.language.maps.forEach(virtualCode)) {
for (const [sourceOffset] of map.toSourceLocation(sourceCodeOffset)) {
sourceCodeOffset = sourceOffset;
mapped = true;
break;
}
if (mapped) {
break;
}
}
if (!mapped) {
return;
}
for (const { ast, startTagEnd, endTagStart } of blocks) {
if (sourceCodeOffset < startTagEnd || sourceCodeOffset > endTagStart) {
continue;
}
if (isBlacklistNode(ts, ast, sourceCodeOffset - startTagEnd, false)) {
return;
}
}
const props = await tsPluginClient?.getPropertiesAtLocation(root.fileName, sourceCodeOffset) ?? [];
if (props.some(prop => prop === 'value')) {
return '${1:.value}';
}
},
};
},
};
}
const charReg = /\w/;
function isCharacterTyping(document: TextDocument, change: { text: string; rangeOffset: number; rangeLength: number; }) {
const lastCharacter = change.text[change.text.length - 1];
const nextCharacter = document.getText().slice(
change.rangeOffset + change.text.length,
change.rangeOffset + change.text.length + 1
);
if (lastCharacter === undefined) { // delete text
return false;
}
if (change.text.includes('\n')) { // multi-line change
return false;
}
return charReg.test(lastCharacter) && !charReg.test(nextCharacter);
}
function isBlacklistNode(ts: typeof import('typescript'), node: ts.Node, pos: number, allowAccessDotValue: boolean) {
if (ts.isVariableDeclaration(node) && pos >= node.name.getFullStart() && pos <= node.name.getEnd()) {
return true;
}
else if (ts.isFunctionDeclaration(node) && node.name && pos >= node.name.getFullStart() && pos <= node.name.getEnd()) {
return true;
}
else if (ts.isParameter(node) && pos >= node.name.getFullStart() && pos <= node.name.getEnd()) {
return true;
}
else if (ts.isPropertyAssignment(node) && pos >= node.name.getFullStart() && pos <= node.name.getEnd()) {
return true;
}
else if (ts.isShorthandPropertyAssignment(node)) {
return true;
}
else if (ts.isImportDeclaration(node)) {
return true;
}
else if (ts.isLiteralTypeNode(node)) {
return true;
}
else if (ts.isTypeReferenceNode(node)) {
return true;
}
else if (!allowAccessDotValue && ts.isPropertyAccessExpression(node) && node.expression.end === pos && node.name.text === 'value') {
return true;
}
else if (
ts.isCallExpression(node)
&& ts.isIdentifier(node.expression)
&& isWatchOrUseFunction(node.expression.text)
&& isTopLevelArgOrArrayTopLevelItemItem(node)
) {
return true;
}
else {
let _isBlacklistNode = false;
node.forEachChild(node => {
if (_isBlacklistNode) {
return;
}
if (pos >= node.getFullStart() && pos <= node.getEnd()) {
if (isBlacklistNode(ts, node, pos, allowAccessDotValue)) {
_isBlacklistNode = true;
}
}
});
return _isBlacklistNode;
}
function isWatchOrUseFunction(fnName: string) {
return fnName === 'watch'
|| fnName === 'unref'
|| fnName === 'triggerRef'
|| fnName === 'isRef'
|| hyphenateAttr(fnName).startsWith('use-');
}
function isTopLevelArgOrArrayTopLevelItemItem(node: ts.CallExpression) {
for (const arg of node.arguments) {
if (pos >= arg.getFullStart() && pos <= arg.getEnd()) {
if (ts.isIdentifier(arg)) {
return true;
}
if (ts.isArrayLiteralExpression(arg)) {
for (const el of arg.elements) {
if (pos >= el.getFullStart() && pos <= el.getEnd()) {
return ts.isIdentifier(el);
}
}
}
return false;
}
}
}
}