111 lines
4.0 KiB
TypeScript
111 lines
4.0 KiB
TypeScript
import * as vscode from "vscode"
|
|
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) {
|
|
const formatCdlProvider = vscode.languages.registerDocumentFormattingEditProvider(
|
|
{ scheme: "file", language: "cdl" },
|
|
{
|
|
provideDocumentFormattingEdits(document: vscode.TextDocument): vscode.TextEdit[] {
|
|
const text = document.getText()
|
|
const formattedText = formatCdlFile(text)
|
|
const fullRange = new vscode.Range(
|
|
document.positionAt(0),
|
|
document.positionAt(text.length)
|
|
)
|
|
return [vscode.TextEdit.replace(fullRange, formattedText)]
|
|
}
|
|
}
|
|
)
|
|
|
|
context.subscriptions.push(formatCdlProvider)
|
|
|
|
const completionCdlProvider = vscode.languages.registerCompletionItemProvider(
|
|
{ scheme: "file", language: "cdl" },
|
|
{
|
|
provideCompletionItems(
|
|
document: vscode.TextDocument,
|
|
position: vscode.Position,
|
|
token: vscode.CancellationToken,
|
|
context: vscode.CompletionContext
|
|
) {
|
|
return completionHandlerCdl(document, position)
|
|
}
|
|
},
|
|
" " // Trigger completion on space
|
|
)
|
|
context.subscriptions.push(completionCdlProvider)
|
|
|
|
const hoverCdlProvider = vscode.languages.registerHoverProvider(
|
|
{ scheme: "file", language: "cdl" },
|
|
{
|
|
provideHover(
|
|
document: vscode.TextDocument,
|
|
position: vscode.Position,
|
|
token: vscode.CancellationToken
|
|
) {
|
|
return hoverCdlHandler(document, position)
|
|
}
|
|
}
|
|
)
|
|
context.subscriptions.push(hoverCdlProvider)
|
|
|
|
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))
|
|
}
|
|
}
|
|
})
|
|
}
|