-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathWxmlAutoCompletion.ts
More file actions
65 lines (58 loc) · 2.26 KB
/
Copy pathWxmlAutoCompletion.ts
File metadata and controls
65 lines (58 loc) · 2.26 KB
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
/******************************************************************
MIT License http://www.opensource.org/licenses/mit-license.php
Author Mora <qiuzhongleiabc@126.com> (https://github.com/qiu8310)
*******************************************************************/
import {
Position,
CancellationToken,
CompletionItemProvider,
TextDocument,
CompletionItem,
CompletionContext,
} from 'vscode'
import AutoCompletion from './AutoCompletion'
import { getLanguage, getLastChar } from './lib/helper'
export default class extends AutoCompletion implements CompletionItemProvider {
id = 'wxml' as const
provideCompletionItems(
document: TextDocument,
position: Position,
token: CancellationToken,
context: CompletionContext
): Promise<CompletionItem[]> {
if (token.isCancellationRequested) {
return Promise.resolve([])
}
const language = getLanguage(document, position)
if (!language) return [] as any
const char = context.triggerCharacter || getLastChar(document, position)
switch (char) {
case '<':
return this.createComponentSnippetItems(language, document, position)
case '\n': // 换行
case ' ': // 空格
// 如果后面紧跟字母数字或_不触发自动提示
// (常用于手动调整缩进位置)
if (/[\w\d$_]/.test(getLastChar(document, new Position(position.line, position.character + 1)))) {
return Promise.resolve([])
}
return this.createComponentAttributeSnippetItems(language, document, position)
case '"':
case "'":
return this.createComponentAttributeSnippetItems(language, document, position)
case ':': // 绑定变量 (也可以是原生小程序的控制语句或事件,如 wx:for, bind:tap)
case '@': // 绑定事件
case '-': // v-if
case '.': // 变量或事件的修饰符
return this.createSpecialAttributeSnippetItems(language, document, position)
case '/': // 闭合标签
return this.createCloseTagCompletionItem(document, position)
default:
if (char >= 'a' && char <= 'z') {
// 输入属性时自动提示
return this.createComponentAttributeSnippetItems(language, document, position)
}
return [] as any
}
}
}