-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
207 lines (184 loc) · 4.92 KB
/
main.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
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env node
import {
CodeAction,
CodeActionKind,
Command,
createConnection,
Diagnostic,
DiagnosticSeverity,
// Position,
// Range,
TextDocumentEdit,
TextDocuments,
TextDocumentSyncKind,
TextEdit
} from "vscode-languageserver";
import { TextDocument } from "vscode-languageserver-textdocument";
import axios from "axios";
const queryString = require("query-string");
const he = require("he");
const connection = createConnection();
const documents: TextDocuments<TextDocument> = new TextDocuments(TextDocument);
documents.listen(connection);
connection.console.info(
`korean language server running in node ${process.version}`
);
connection.onInitialize(() => {
return {
capabilities: {
codeActionProvider: true,
textDocumentSync: {
openClose: true,
change: TextDocumentSyncKind.Incremental
},
executeCommandProvider: {
commands: ["korean.quickfix"]
}
}
};
});
const getErrorList = async (text: string): Promise<any[]> => {
connection.console.info(`getting error list`);
if(text == "") {
return [];
}
return axios
.post(
"http://speller.cs.pusan.ac.kr/results",
queryString.stringify({ text1: text.replace(/\n/g, "\r") })
)
.then(({ data }) => {
const startIndex = data.indexOf("data = [{");
const nextIndex = data.indexOf("}];");
const rawData = data.substring(startIndex + 7, nextIndex + 2);
let xxx: SpellerResponse[] = JSON.parse(rawData);
return xxx[0]?.errInfo?.map((match: ErrInfo) => {
return {
start: match.start,
end: match.end,
msg: `${match.candWord}\n${htmltoString(match.help)}`
};
});
})
.catch(error => {
connection.console.error(JSON.stringify(error));
return [];
});
};
const getDiagnostics = async (txt: any): Promise<any[]> => {
const errList = await getErrorList(txt.getText());
return new Promise(resolve => {
resolve(errList.map(errToDiagnostic(txt)));
});
};
const errToDiagnostic = txt => ({ start, end, msg }) => ({
severity: DiagnosticSeverity.Warning,
range: {
start: txt.positionAt(start),
end: txt.positionAt(end)
},
message: `${msg}`,
source: "korean"
});
async function validate(document: TextDocument) {
const diagnosticList = await getDiagnostics(document);
connection.sendDiagnostics({
uri: document.uri,
version: document.version,
diagnostics: diagnosticList
});
}
documents.onDidOpen(event => {
validate(event.document);
});
documents.onDidChangeContent(event => {
validate(event.document);
});
connection.onCodeAction(params => {
if (params.context.diagnostics.length == 0) {
return undefined;
}
const textDocument = documents.get(params.textDocument.uri);
if (textDocument === undefined) {
return undefined;
}
return params.context.diagnostics
.map((diagnosis: Diagnostic) => {
let msg = diagnosis.message;
let fixList = msg.substr(0, msg.indexOf("\n")).split("|");
return fixList.map(newStr => {
const orgStr = textDocument.getText(diagnosis.range);
return CodeAction.create(
`${orgStr} => ${newStr}`,
Command.create(
newStr,
"korean.quickfix",
textDocument.uri,
diagnosis,
newStr
),
CodeActionKind.QuickFix
);
});
})
.flat();
});
connection.onExecuteCommand(async params => {
if (params.command !== "korean.quickfix" || params.arguments === undefined) {
return;
}
const textDocument = documents.get(params.arguments[0]);
if (textDocument === undefined) {
return;
}
const diagnosis: Diagnostic = params.arguments[1];
const newStr = params.arguments[2];
connection.workspace.applyEdit({
documentChanges: [
TextDocumentEdit.create(
{ uri: textDocument.uri, version: textDocument.version },
[TextEdit.replace(diagnosis.range, newStr)]
)
]
});
});
connection.listen();
const htmltoString = (rawStr: string) => {
let msg = he.unescape(
rawStr.replace(/<br\/>/g, `\n`).replace("도움말 정보 없음", "")
);
msg = msg.replace(/^\s*$(?:\r\n?|\n)/gm, "");
return msg;
};
// Sample response from api
// [
// {
// "str": "안뇽",
// "errInfo": [
// {
// "help": "어린이들의 발음을 흉내내어 '안뇽'이라고 말하는 사람들이 종종 있습니다. 특히, 글을 쓸 때는 이러한 단어를 쓰지 않 도록 합시다.",
// "errorIdx": 0,
// "correctMethod": 2,
// "start": 0,
// "end": 2,
// "orgStr": "안뇽",
// "candWord": "안녕"
// }
// ],
// "idx": 0
// }
// ]
export interface SpellerResponse {
str: string;
errInfo: ErrInfo[];
idx: number;
}
export interface ErrInfo {
help: string;
errorIdx: number;
correctMethod: number;
start: number;
end: number;
orgStr: string;
candWord: string;
}