update cdl/def diagnostic

This commit is contained in:
Christoph Brandau
2025-01-13 13:05:10 +01:00
parent 11af66a267
commit 40f6f41667
4 changed files with 82 additions and 20 deletions
+1
View File
@@ -2,3 +2,4 @@ node_modules
out
dist
*.vsix
target
+26
View File
@@ -35,6 +35,32 @@ export function formatDefFile(content: string): string {
}
return formattedLines.join("\n")
}
export function isFirstLineMachine(content: string): boolean {
const firstLine = content.split("\n")[0].trim()
const machineRegex = /^MACHINE\s+\S+/
return machineRegex.test(firstLine)
}
export function diagnosticHandler(document: vscode.TextDocument) {
if (document.languageId === "cdl" || document.languageId === "def") {
const diagnostics: vscode.Diagnostic[] = []
if (!isFirstLineMachine(document.getText())) {
const range = new vscode.Range(
document.positionAt(0),
document.positionAt(document.getText().length)
)
const diagnostic = new vscode.Diagnostic(
range,
"The first line of the CDL file should contain 'MACHINE'.",
vscode.DiagnosticSeverity.Error
)
diagnostics.push(diagnostic)
}
return diagnostics
}
}
export function completionHandlerCdl(document: vscode.TextDocument, position: vscode.Position) {
const linePrefix = document.lineAt(position).text.substring(0, position.character)
const categories = ["MILL", "LATHE", "DRILL"]
+38 -3
View File
@@ -1,11 +1,21 @@
import * as vscode from "vscode"
import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
Executable
} from "vscode-languageclient/node"
import {
formatCdlFile,
completionHandlerCdl,
hoverCdlHandler,
formatDefFile
formatDefFile,
isFirstLineMachine,
diagnosticHandler
} from "./common/handlers"
let client: LanguageClient
export function activate(context: vscode.ExtensionContext) {
const formatCdlProvider = vscode.languages.registerDocumentFormattingEditProvider(
{ scheme: "file", language: "cdl" },
@@ -70,6 +80,31 @@ export function activate(context: vscode.ExtensionContext) {
)
context.subscriptions.push(formatDefProvider)
}
export function deactivate() {}
// Diagnostics collection
const diagnosticCollectionCdl = vscode.languages.createDiagnosticCollection("cdl")
const diagnosticCollectionDef = vscode.languages.createDiagnosticCollection("def")
context.subscriptions.push(diagnosticCollectionCdl, diagnosticCollectionDef)
// Check if the first line of the CDL file contains "MACHINE"
vscode.workspace.onDidOpenTextDocument((document) => {
if (document.languageId === "cdl" || document.languageId === "def") {
if (document.languageId === "cdl") {
diagnosticCollectionCdl.set(document.uri, diagnosticHandler(document))
} else if (document.languageId === "def") {
diagnosticCollectionDef.set(document.uri, diagnosticHandler(document))
}
}
})
vscode.workspace.onDidChangeTextDocument((event) => {
const document = event.document
if (document.languageId === "cdl" || document.languageId === "def") {
if (document.languageId === "cdl") {
diagnosticCollectionCdl.set(document.uri, diagnosticHandler(document))
} else if (document.languageId === "def") {
diagnosticCollectionDef.set(document.uri, diagnosticHandler(document))
}
}
})
}
+1 -1
View File
@@ -1,4 +1,4 @@
MACHINE DEFAULT
MACHINE
EVENT dummy_event_start
{
UI_LABEL "------------SAMSON UDES------------"