You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.3 KiB

2 years ago
#!/usr/bin/env node
const {
createConnection,
TextDocuments,
DiagnosticSeverity,
CompletionItemKind,
InsertTextFormat
} = require('vscode-languageserver/node');
const {
TextDocument
} = require('vscode-languageserver-textdocument')
const diagnostics = new Map();
diagnostics.set('ReplyTo', {
severity: DiagnosticSeverity.Hint,
message: 'Detta är ett test'
});
diagnostics.set('Apache', {
severity: DiagnosticSeverity.Hint,
message: 'Apache Software Foundation (ASF)'
});
diagnostics.set('Camel', {
severity: DiagnosticSeverity.Information,
message: 'A species in the Camelidae family from the Animalia kingdom'
});
diagnostics.set('error', {
severity: DiagnosticSeverity.Error,
message: 'This is an error message... Probably a bug 🦠'
});
const getMatchingWords = (text) => {
const regex = new RegExp(`\\b(${Array.from(diagnostics.keys()).join('|')})\\b`, 'gi')
const results = []
while ((matches = regex.exec(text)) && results.length < 100) {
results.push({
value: matches[0],
index: matches.index,
})
}
return results
}
const wordListToDiagnostic = (textDocument) => ({ index, value }) => ({
severity: diagnostics.get(value).severity,
range: {
start: textDocument.positionAt(index),
end: textDocument.positionAt(index + value.length),
},
message: `${value}: ${diagnostics.get(value).message}`,
source: 'LSP-Server',
})
const getDiagnostics = (textDocument) =>
getMatchingWords(textDocument.getText())
.map(wordListToDiagnostic(textDocument))
const connection = createConnection()
const documents = new TextDocuments(TextDocument)
/* CompletionItemKind:
Text Method Function Constructor Field Variable Class Interface
Module Property Unit Value Enum Keyword Snippet Color File
Reference Folder EnumMember Constant Struct Event TypeParameter
*/
connection.onCompletion(() => {
return [{
label: 'Apache Camel',
kind: CompletionItemKind.Reference,
detail: 'Apache Camel Framework',
documentation: 'Camel is an Open Source integration framework that empowers you to quickly and easily integrate various systems consuming or producing data.',
}, {
label: 'Apache Camel Hackathon',
kind: CompletionItemKind.Event,
detail: 'Event',
documentation: 'A programming event to learn more about the capabilities of the Apache Camel framework.',
}, {
label: 'note metadata',
kind: CompletionItemKind.Snippet,
insertTextFormat: InsertTextFormat.Snippet,
detail: 'Note Template',
documentation: 'Template for notetaking',
insertText: '# ${1:Header}\n\nDate: ' + new Date().toJSON().slice(0, 10) + ' \nAuthor: ${2:Nils Gerstner} \nCompany: ${3:Example Corp} \n\n$0',
}];
});
connection.onInitialize(() => ({
capabilities: {
textDocumentSync: documents.syncKind,
completionProvider: {
resolveProvider: true,
}
},
}))
documents.onDidChangeContent(change => {
connection.sendDiagnostics({
uri: change.document.uri,
diagnostics: getDiagnostics(change.document),
})
})
documents.listen(connection)
connection.listen()