Skip to content

Commit

Permalink
Update server.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vrognas committed Oct 6, 2023
1 parent e08a75f commit 8865bec
Showing 1 changed file with 49 additions and 2 deletions.
51 changes: 49 additions & 2 deletions server/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
createConnection,
CodeAction,
CodeActionKind,
TextDocuments,
Diagnostic,
DiagnosticSeverity,
Expand Down Expand Up @@ -37,7 +39,8 @@ connection.onInitialize((params: InitializeParams): InitializeResult => {
return {
capabilities: {
textDocumentSync: TextDocumentSyncKind.Full,
hoverProvider: true
hoverProvider: true,
codeActionProvider: true
}
};
});
Expand Down Expand Up @@ -85,6 +88,11 @@ function findControlRecordsInText(text: string): RegExpExecArray[] {
const matches: RegExpExecArray[] = [];
let match: RegExpExecArray | null;

// Remove lines that start with a semicolon (comments)
const filteredText = text.split('\n')
.filter(line => !line.trim().startsWith(';'))
.join('\n');

while ((match = controlRecordPattern.exec(text)) !== null) {
matches.push(match);
}
Expand All @@ -105,6 +113,7 @@ function createDiagnosticForControlRecord(match: RegExpExecArray, textDocument:
end: textDocument.positionAt(match.index + match[0].length)
},
message: `Did you mean ${closestMatch}?`,
code: "replace-abbreviation",
source: 'NMTRAN Language Server'
};
}
Expand All @@ -128,7 +137,7 @@ function createDiagnosticForControlRecord(match: RegExpExecArray, textDocument:

// ------------ Main Functionalities -------------

// Implement hover logic
// Implement Hover logic
connection.onHover(({ textDocument, position }) => {
const uri = textDocument.uri;
const document = documents.get(uri);
Expand Down Expand Up @@ -164,6 +173,44 @@ connection.onHover(({ textDocument, position }) => {
return null;
});

// Implement CodeAction logic
connection.onCodeAction(({ textDocument, range, context }) => {
const uri = textDocument.uri;
const document = documents.get(uri);
if (!document) {
return null;
}

const codeActions: CodeAction[] = [];

for (const diagnostic of context.diagnostics) {
// Check if the diagnostic is related to an abbreviation
if (diagnostic.message.startsWith("Did you mean")) {
const fullControlRecord = diagnostic.message.replace("Did you mean ", "").replace("?", "");

const replaceAbbreviationAction: CodeAction = {
title: `Replace with ${fullControlRecord}`,
kind: CodeActionKind.QuickFix,
diagnostics: [diagnostic],
edit: {
changes: {
[uri]: [
{
range: diagnostic.range,
newText: fullControlRecord
}
]
}
}
};

codeActions.push(replaceAbbreviationAction);
}
}

return codeActions;
});

async function validateNMTRANDocument(textDocument: TextDocument): Promise<void> {
const text = textDocument.getText();
const controlRecords = findControlRecordsInText(text);
Expand Down

0 comments on commit 8865bec

Please sign in to comment.