-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
executable file
·123 lines (123 loc) · 4.45 KB
/
main.js
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
#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const vscode_languageserver_1 = require("vscode-languageserver");
const vscode_languageserver_textdocument_1 = require("vscode-languageserver-textdocument");
const axios_1 = require("axios");
const queryString = require("query-string");
const he = require("he");
const connection = vscode_languageserver_1.createConnection();
const documents = new vscode_languageserver_1.TextDocuments(vscode_languageserver_textdocument_1.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: vscode_languageserver_1.TextDocumentSyncKind.Incremental
},
executeCommandProvider: {
commands: ["korean.quickfix"]
}
}
};
});
const getErrorList = async (text) => {
connection.console.info(`getting error list`);
if (text == "") {
return [];
}
return axios_1.default
.post("http://speller.cs.pusan.ac.kr/results", queryString.stringify({ text1: text.replace(/\n/g, "\r") }))
.then(({ data }) => {
var _a, _b;
const startIndex = data.indexOf("data = [{");
const nextIndex = data.indexOf("}];");
const rawData = data.substring(startIndex + 7, nextIndex + 2);
let xxx = JSON.parse(rawData);
return (_b = (_a = xxx[0]) === null || _a === void 0 ? void 0 : _a.errInfo) === null || _b === void 0 ? void 0 : _b.map((match) => {
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) => {
const errList = await getErrorList(txt.getText());
return new Promise(resolve => {
resolve(errList.map(errToDiagnostic(txt)));
});
};
const errToDiagnostic = txt => ({ start, end, msg }) => ({
severity: vscode_languageserver_1.DiagnosticSeverity.Warning,
range: {
start: txt.positionAt(start),
end: txt.positionAt(end)
},
message: `${msg}`,
source: "korean"
});
async function validate(document) {
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) => {
let msg = diagnosis.message;
let fixList = msg.substr(0, msg.indexOf("\n")).split("|");
return fixList.map(newStr => {
const orgStr = textDocument.getText(diagnosis.range);
return vscode_languageserver_1.CodeAction.create(`${orgStr} => ${newStr}`, vscode_languageserver_1.Command.create(newStr, "korean.quickfix", textDocument.uri, diagnosis, newStr), vscode_languageserver_1.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 = params.arguments[1];
const newStr = params.arguments[2];
connection.workspace.applyEdit({
documentChanges: [
vscode_languageserver_1.TextDocumentEdit.create({ uri: textDocument.uri, version: textDocument.version }, [vscode_languageserver_1.TextEdit.replace(diagnosis.range, newStr)])
]
});
});
connection.listen();
const htmltoString = (rawStr) => {
let msg = he.unescape(rawStr.replace(/<br\/>/g, `\n`).replace("도움말 정보 없음", ""));
msg = msg.replace(/^\s*$(?:\r\n?|\n)/gm, "");
return msg;
};
//# sourceMappingURL=main.js.map