Merge pull request 'develop_def_syntax' (#4) from develop_def_syntax into main
/ build_and_publish (push) Successful in 23s

Reviewed-on: Christoph/vscode-cdl#4
This commit was merged in pull request #4.
This commit is contained in:
2025-01-13 15:23:56 +00:00
4 changed files with 123 additions and 20 deletions
+1
View File
@@ -2,3 +2,4 @@ node_modules
out out
dist dist
*.vsix *.vsix
target
+45
View File
@@ -12,10 +12,55 @@ export function formatCdlFile(content: string): string {
if (strippedLine.endsWith("{")) { if (strippedLine.endsWith("{")) {
indentLevel += 1 indentLevel += 1
} }
if (strippedLine.startsWith("#")) {
formattedLines[formattedLines.length - 1] =
" ".repeat(indentLevel) + strippedLine.replace(/^#\s*/, "# ")
}
} }
return formattedLines.join("\n") return formattedLines.join("\n")
} }
export function formatDefFile(content: string): string {
let indentLevel = 0
const formattedLines = []
for (const line of content.split("\n")) {
const strippedLine = line.trim()
if (strippedLine.endsWith("}")) {
indentLevel = Math.max(indentLevel - 1, 0)
}
formattedLines.push(" ".repeat(indentLevel) + strippedLine)
if (strippedLine.endsWith("{")) {
indentLevel += 1
}
}
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) { export function completionHandlerCdl(document: vscode.TextDocument, position: vscode.Position) {
const linePrefix = document.lineAt(position).text.substring(0, position.character) const linePrefix = document.lineAt(position).text.substring(0, position.character)
const categories = ["MILL", "LATHE", "DRILL"] const categories = ["MILL", "LATHE", "DRILL"]
+60 -3
View File
@@ -1,5 +1,20 @@
import * as vscode from "vscode" import * as vscode from "vscode"
import { formatCdlFile, completionHandlerCdl, hoverCdlHandler } from "./common/handlers" import {
LanguageClient,
LanguageClientOptions,
ServerOptions,
Executable
} from "vscode-languageclient/node"
import {
formatCdlFile,
completionHandlerCdl,
hoverCdlHandler,
formatDefFile,
isFirstLineMachine,
diagnosticHandler
} from "./common/handlers"
let client: LanguageClient
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
const formatCdlProvider = vscode.languages.registerDocumentFormattingEditProvider( const formatCdlProvider = vscode.languages.registerDocumentFormattingEditProvider(
@@ -48,6 +63,48 @@ export function activate(context: vscode.ExtensionContext) {
} }
) )
context.subscriptions.push(hoverCdlProvider) context.subscriptions.push(hoverCdlProvider)
}
export function deactivate() {} const formatDefProvider = vscode.languages.registerDocumentFormattingEditProvider(
{ scheme: "file", language: "def" },
{
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {
const text = document.getText()
const formattedText = formatDefFile(text)
const fullRange = new vscode.Range(
document.positionAt(0),
document.positionAt(text.length)
)
return [vscode.TextEdit.replace(fullRange, formattedText)]
}
}
)
context.subscriptions.push(formatDefProvider)
// 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 EVENT dummy_event_start
{ {
UI_LABEL "------------SAMSON UDES------------" UI_LABEL "------------SAMSON UDES------------"