Skip to content

Commit

Permalink
Merge pull request #5 from vrognas/feature/quick-fix
Browse files Browse the repository at this point in the history
Feature/quick fix
  • Loading branch information
vrognas authored Oct 6, 2023
2 parents 37b8ae8 + 219bf0a commit 77093a9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
2 changes: 1 addition & 1 deletion server/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,4 @@ export const validControlRecords = [
'$TOL',
'$TTDF',
'$WARNINGS'
];
];
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
8 changes: 8 additions & 0 deletions test/maximal.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ $INVALID

$VERYINVALID

$ANN

$ANNEAL

$DES

$DESIGN

$EST

$ESTIMATE METHOD=FO
Expand Down

0 comments on commit 77093a9

Please sign in to comment.