/
index.ts
191 lines (182 loc) · 6.66 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
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
import { commands, ExtensionContext, listManager, CompletionContext, window, workspace, languages } from 'coc.nvim';
import { Position, CancellationToken, CompletionList, Range } from 'vscode-languageserver-protocol';
import { TextDocument } from 'vscode-languageserver-textdocument';
import SchemaList from './lists';
import { Rime } from './rime';
import { Config } from './config';
let rime: Rime;
export async function activate(context: ExtensionContext): Promise<void> {
const userConfig = new Config(context);
rime = new Rime(await userConfig.traits);
rime.setCompletionStatus(userConfig.enabled);
const statusBarItem = window.createStatusBarItem(0, { progress: false });
rime.getSchema().then((schemaId) => {
if (schemaId !== userConfig.schemaId && userConfig.schemaId !== '') {
rime.setSchema(userConfig.schemaId);
schemaId = userConfig.schemaId;
}
rime.getSchemaList().then((schemaList) => {
statusBarItem.text =
userConfig.shortcut +
' ' +
schemaList.filter((schema) => {
return schema.schema_id === schemaId;
})[0].name;
});
});
if (userConfig.enabled) {
statusBarItem.show();
}
context.subscriptions.push(
// Commands
commands.registerCommand('rime.enable', async () => {
rime.setCompletionStatus(true);
statusBarItem.show();
}),
commands.registerCommand('rime.disable', async () => {
rime.setCompletionStatus(false);
statusBarItem.hide();
}),
commands.registerCommand('rime.toggle', async () => {
rime.toggleCompletionStatus();
if (rime.getCompletionStatus()) {
statusBarItem.show();
} else {
statusBarItem.hide();
}
}),
// Completion Source
languages.registerCompletionItemProvider(
'rime',
userConfig.shortcut,
null,
{
async provideCompletionItems(
document: TextDocument,
position: Position,
_token: CancellationToken,
_context: CompletionContext,
): Promise<CompletionList> {
return new Promise<CompletionList>((resolve, reject) => {
const emptyResponse: CompletionList = {
items: [],
isIncomplete: false,
};
const punctMap: Map<string, string[]> = new Map([
[',', [',']],
['.', ['。']],
['?', ['?']],
['!', ['!']],
[':', [':']],
[';', [';']],
['\\', ['、']],
['"', ['“', '”']],
["'", ['‘', '’']],
['<', ['《', '〈', '«', '‹', '˂']],
['>', ['》', '〉', '»', '›', '˃']],
['(', ['(']],
[')', [')']],
['[', ['「', '【', '〔', '[', '〚']],
[']', ['」', '】', '〕', ']', '〛']],
['{', ['{']],
['}', ['}']],
['$', ['¥']],
['|', ['|']],
['^', ['……']],
['~', ['~']],
['-', ['——']],
]);
let offset = document.offsetAt(position);
if (offset !== 0 && rime.getCompletionStatus()) {
let inputString = '';
let inputRange: Range = { start: position, end: position };
const getPrevSingleChar = (offset: number): string => {
return document.getText({
start: document.positionAt(offset - 1),
end: document.positionAt(offset),
});
};
let singleChar = getPrevSingleChar(offset);
if (punctMap.has(singleChar)) {
let punctResponse = emptyResponse;
for (const fullWidthPunct of punctMap.get(singleChar)) {
punctResponse.items.push({
label: fullWidthPunct,
sortText: singleChar,
filterText: singleChar,
textEdit: {
range: { start: document.positionAt(offset - 1), end: position },
newText: fullWidthPunct,
},
});
}
resolve(punctResponse);
} else {
while (/[!-@\[-~]/.test(singleChar) && offset !== 0) {
inputString = singleChar + inputString;
offset -= 1;
singleChar = getPrevSingleChar(offset);
}
// Special treat for camelCase naming
if (/[A-Z]/.test(singleChar)) {
inputString = '';
}
inputRange.start = document.positionAt(offset);
rime
.getContext(inputString)
.then((res) => {
if (
res !== null &&
'composition' in res &&
res.composition !== null &&
'preedit' in res.composition &&
res.composition.preedit !== null
) {
window.showInformationMessage(res.composition.preedit);
}
if (
res !== null &&
'menu' in res &&
res.menu !== null &&
'candidates' in res.menu &&
res.menu.candidates !== null
) {
resolve({
items: res.menu.candidates.map((candidate, order) => {
return {
label: candidate.text,
sortText: String.fromCharCode(order),
filterText: inputString,
documentation: candidate?.comment ?? '',
textEdit: { range: inputRange, newText: candidate.text },
};
}),
isIncomplete: true,
});
} else {
resolve(emptyResponse);
}
})
.catch((e) => {
console.log(`Error getting Rime Context: ${e}`);
reject(e);
});
}
} else {
// offset === 0
resolve(emptyResponse);
}
});
},
},
[],
userConfig.priority,
[],
),
);
// Schema List
listManager.registerList(new SchemaList(workspace.nvim, rime, statusBarItem, userConfig.shortcut));
}
export async function deactivate(): Promise<void> {
rime.destroy();
}