diff --git a/.gitignore b/.gitignore index 79184c8..9e72bd2 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ node_modules out dist *.vsix +target \ No newline at end of file diff --git a/client/src/common/handlers.ts b/client/src/common/handlers.ts index 542f899..9fa1db1 100644 --- a/client/src/common/handlers.ts +++ b/client/src/common/handlers.ts @@ -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"] diff --git a/client/src/extension.ts b/client/src/extension.ts index a98b6ca..e8ae0ba 100644 --- a/client/src/extension.ts +++ b/client/src/extension.ts @@ -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)) + } + } + }) +} diff --git a/test/file.cdl b/test/file.cdl index 9660e9a..e98a153 100644 --- a/test/file.cdl +++ b/test/file.cdl @@ -1,4 +1,4 @@ -MACHINE DEFAULT +MACHINE EVENT dummy_event_start { UI_LABEL "------------SAMSON UDES------------" @@ -282,11 +282,11 @@ EVENT lib_event_cycle_60 { # - #Engraving - CYCLE60 - #CYCLE60(STRING[200] _TEXT, REAL _RTP, REAL _RFP, REAL _SDIS, REAL _DP, REAL _DPR, REAL _PA, REAL _PO, REAL _STA, REAL _CP1, REAL _CP2,REAL _WID, REAL _DF, REAL _FFD, REAL _FFP1, INT _VARI, INT _CODEP,INT _UMODE, INT _GMODE, INT _DMODE, INT _AMODE) + # Engraving - CYCLE60 + # CYCLE60(STRING[200] _TEXT, REAL _RTP, REAL _RFP, REAL _SDIS, REAL _DP, REAL _DPR, REAL _PA, REAL _PO, REAL _STA, REAL _CP1, REAL _CP2,REAL _WID, REAL _DF, REAL _FFD, REAL _FFP1, INT _VARI, INT _CODEP,INT _UMODE, INT _GMODE, INT _DMODE, INT _AMODE) # - #Gravurzyklus - CYCLE60 - #CYCLE60(STRING[200] _TEXT, REAL _RTP, REAL _RFP, REAL _SDIS, REAL _DP, REAL _DPR, REAL _PA, REAL _PO, REAL _STA, REAL _CP1, REAL _CP2,REAL _WID, REAL _DF, REAL _FFD, REAL _FFP1, INT _VARI, INT _CODEP,INT _UMODE, INT _GMODE, INT _DMODE, INT _AMODE) + # Gravurzyklus - CYCLE60 + # CYCLE60(STRING[200] _TEXT, REAL _RTP, REAL _RFP, REAL _SDIS, REAL _DP, REAL _DPR, REAL _PA, REAL _PO, REAL _STA, REAL _CP1, REAL _CP2,REAL _WID, REAL _DF, REAL _FFD, REAL _FFP1, INT _VARI, INT _CODEP,INT _UMODE, INT _GMODE, INT _DMODE, INT _AMODE) UI_LABEL "SAMSON: Gravieren (CYCLE60)" UI_HELP "Detailierte Zyklus Beschrieb" "file://${POST_LIB_CURRENT_BMP_DIR}/CYCL60_DE.pdf" @@ -364,18 +364,18 @@ EVENT lib_event_cycle_60 UI_LABEL "Zeichenabstand, Breite, Winkel _DF" } - # # - # # Text direction - # # - # # Textrichtung - # - # PARAM cycle60_engraving_text_direction - # { - # TYPE o - # DEFVAL "1-Normal" - # OPTIONS "0-Spiegel","1-Normal" - # UI_LABEL "Text direction" - # } + # # + # # Text direction + # # + # # Textrichtung + # + # PARAM cycle60_engraving_text_direction + # { + # TYPE o + # DEFVAL "1-Normal" + # OPTIONS "0-Spiegel","1-Normal" + # UI_LABEL "Text direction" + # } # # Text orientation