Skip to content

Commit

Permalink
feat: Trivial source code validation
Browse files Browse the repository at this point in the history
  • Loading branch information
vain0x committed Jan 4, 2019
1 parent 549915b commit 2c463d3
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion lsp/src/curage-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

import {
InitializeResult,
TextDocumentSyncKind,
DidOpenTextDocumentParams,
DidChangeTextDocumentParams,
PublishDiagnosticsParams,
DiagnosticSeverity,
Diagnostic,
} from "vscode-languageserver-protocol"
import {
listenToLSPClient,
Expand All @@ -23,7 +29,19 @@ export const onMessage = (message: Message) => {
switch (method) {
case "initialize": {
sendResponse(id, {
capabilities: {},
capabilities: {
textDocumentSync: {
// Indicate the server want the client to send
// `textDocument/didOpen` and `textDocument/didClose` notifications
// whenever a document opens or get closed.
openClose: true,
// Indicate the server want the client to send
// `textDocument/didChange` notifications
// whenever an open document is modified,
// including the full text of the modified document.
change: TextDocumentSyncKind.Full,
},
},
} as InitializeResult)
break
}
Expand All @@ -40,7 +58,51 @@ export const onMessage = (message: Message) => {
process.exit(0)
break
}
case "textDocument/didOpen": {
const { textDocument: { uri, text } } = params as DidOpenTextDocumentParams
validateDocument(uri, text)
break
}
case "textDocument/didChange": {
const { textDocument: { uri }, contentChanges: [{ text }] } = params as DidChangeTextDocumentParams
validateDocument(uri, text)
break
}
default: {
// Pass.
break
}
}
}

/**
* Validates a document to publish diagnostics (warnings).
*/
const validateDocument = (uri: string, text: string) => {
const expected = `print "hello, world!"`

const diagnostics: Diagnostic[] = []

// If the text is not a hello world program, report a warning.
for (let i = 0; i < expected.length; i++) {
if (text[i] !== expected[i]) {
diagnostics.push({
message: `Expected '${expected[i]}'.`,
severity: DiagnosticSeverity.Warning,
range: {
start: { line: 0, character: i },
end: { line: 0, character: i + 1 },
},
})
break
}
}

// Report current diagnostics in the document identified by the `uri`.
sendNotify("textDocument/publishDiagnostics", {
uri,
diagnostics,
} as PublishDiagnosticsParams)
}

export const main = () => {
Expand Down

0 comments on commit 2c463d3

Please sign in to comment.