/
language-server.ts
154 lines (134 loc) · 5.17 KB
/
language-server.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
import uri2path from "file-uri-to-path";
import fileUrl from "file-url";
import _ from "lodash";
import * as path from "path";
import {
createConnection,
Diagnostic,
DiagnosticSeverity,
InitializeParams,
ProposedFeatures,
TextDocumentChangeEvent,
TextDocuments,
TextDocumentSyncKind
} from "vscode-languageserver";
import { ElmApp, Message, LogMessage } from "elm-analyse/ts/domain";
import * as fileLoadingPorts from "elm-analyse/ts/file-loading-ports";
// import * as dependencies from 'elm-analyse/ts/util/dependencies';
import * as dependencies from "elm-analyse/ts/util/dependencies";
const { Elm } = require("elm-analyse/dist/app/backend-elm.js");
// TODO: remove need for this config, the port, format, and open options
// should not be needed.
// elm-format will be needed to implement fixing issues with elm-analyse
const CONFIG = {
port: 3000,
elmFormatPath: "elm-format",
format: "json",
open: false
};
function start(project: {}) {
// Disable console logging while in language server mode
// otherwise in stdio mode we will not be sending valid JSON
console.log = console.warn = console.error = () => {};
let connection = createConnection(ProposedFeatures.all);
run(project, function(elm: ElmApp) {
let documents: TextDocuments = new TextDocuments();
let filesWithDiagnostics = new Set();
documents.listen(connection);
connection.listen();
connection.onInitialize((params: InitializeParams) => ({
capabilities: {
textDocumentSync: {
openClose: true,
willSave: true,
change: TextDocumentSyncKind.Full
},
textDocument: {
publishDiagnostics: {
relatedInformation: false
}
}
}
}));
// The content of a text document has changed. This event is emitted
// when the text document first opened or when its content has changed.
documents.onDidOpen(validateTextDocument);
documents.onDidChangeContent(validateTextDocument);
documents.onDidSave(validateTextDocument);
async function validateTextDocument(change: TextDocumentChangeEvent): Promise<void> {
elm.ports.fileWatch.send({
event: "update",
file: path.relative(process.cwd(), uri2path(change.document.uri)),
content: change.document.getText()
});
}
function publishDiagnostics(messages: Message[], uri: string) {
const messagesForFile = messages.filter(m =>
// Windows paths have a forward slash in the `message.file`, which won't
// match with the end of the file URI we have from the language server event,
// so this replaces backslashes before matching to get consistent behavior
uri.endsWith(m.file.replace("\\", "/"))
);
let diagnostics: Diagnostic[] = messagesForFile.map(messageToDiagnostic);
connection.sendDiagnostics({ uri: uri, diagnostics });
}
elm.ports.sendReportValue.subscribe(function(report) {
// When publishing diagnostics it looks like you have to publish
// for one URI at a time, so this groups all of the messages for
// each file and sends them as a batch
const messagesByFile = _.groupBy(report.messages, "file");
const filesInReport = new Set(_.map(_.keys(messagesByFile), fileUrl));
const filesThatAreNowFixed = new Set([...filesWithDiagnostics].filter(uriPath => !filesInReport.has(uriPath)));
filesWithDiagnostics = filesInReport;
// We you fix the last error in a file it no longer shows up in the report, but
// we still need to clear the error marker for it
filesThatAreNowFixed.forEach(file => publishDiagnostics([], file));
_.forEach(messagesByFile, (messages, file) => publishDiagnostics(messages, fileUrl(file)));
});
elm.ports.log.subscribe((data: LogMessage) => {
console.log(data.level + ":", data.message);
});
});
}
function messageToDiagnostic(message: Message): Diagnostic {
if (message.type === "FileLoadFailed") {
return {
severity: DiagnosticSeverity.Error,
range: { start: { line: 0, character: 0 }, end: { line: 1, character: 0 } },
code: "1",
message: "Error parsing file",
source: "elm-analyse"
};
}
let [lineStart, colStart, lineEnd, colEnd] = message.data.properties.range;
const range = {
start: { line: lineStart - 1, character: colStart - 1 },
end: { line: lineEnd - 1, character: colEnd - 1 }
};
return {
severity: DiagnosticSeverity.Warning,
range: range,
code: message.id,
// Clean up the error message a bit, removing the end of the line, e.g.
// "Record has only one field. Use the field's type or introduce a Type. At ((14,5),(14,20))"
message:
message.data.description.split(/at .+$/i)[0] +
"\n" +
`See https://stil4m.github.io/elm-analyse/#/messages/${message.type}`,
source: "elm-analyse"
};
}
function run(project: {}, onload: (app: ElmApp) => void) {
dependencies.getDependencies(function(registry) {
const app = Elm.Analyser.init({
flags: {
server: false,
registry: registry || [],
project: project
}
});
fileLoadingPorts.setup(app, CONFIG, process.cwd());
onload(app);
});
}
export default { start };