add Hover feature
/ build_and_publish (push) Failing after 19s

This commit is contained in:
Christoph Brandau
2025-01-08 15:53:19 +01:00
parent 793effd85d
commit a318cf6e46
8 changed files with 213 additions and 93 deletions
+71 -29
View File
@@ -1,36 +1,78 @@
import * as vscode from "vscode";
import * as vscode from "vscode"
export function formatCdlFile(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);
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
}
}
formattedLines.push(" ".repeat(indentLevel) + strippedLine);
if (strippedLine.endsWith("{")) {
indentLevel += 1;
}
}
return formattedLines.join("\n");
return formattedLines.join("\n")
}
export function completionHandler(
document: vscode.TextDocument,
position: vscode.Position
) {
const linePrefix = document
.lineAt(position)
.text.substr(0, position.character);
if (!linePrefix.endsWith("TYPE ")) {
return undefined;
}
export function completionHandler(document: vscode.TextDocument, position: vscode.Position) {
const linePrefix = document.lineAt(position).text.substring(0, position.character)
const categories = ["MILL", "LATHE", "DRILL"]
return [
new vscode.CompletionItem("o", vscode.CompletionItemKind.Keyword),
new vscode.CompletionItem("b", vscode.CompletionItemKind.Keyword),
new vscode.CompletionItem("i", vscode.CompletionItemKind.Keyword),
new vscode.CompletionItem("d", vscode.CompletionItemKind.Keyword),
];
if (linePrefix.endsWith("TYPE ")) {
return [
new vscode.CompletionItem("o", vscode.CompletionItemKind.TypeParameter),
new vscode.CompletionItem("b", vscode.CompletionItemKind.TypeParameter),
new vscode.CompletionItem("i", vscode.CompletionItemKind.TypeParameter),
new vscode.CompletionItem("d", vscode.CompletionItemKind.TypeParameter),
new vscode.CompletionItem("g", vscode.CompletionItemKind.TypeParameter),
new vscode.CompletionItem("s", vscode.CompletionItemKind.TypeParameter)
]
}
if (linePrefix.endsWith("CATEGORY ")) {
return categories.map(
(cat) => new vscode.CompletionItem(cat, vscode.CompletionItemKind.TypeParameter)
)
}
const categoryPrefix = linePrefix.match(/CATEGORY (.*) /)
if (categoryPrefix) {
const usedCategories = categoryPrefix[1].split(" ")
const remainingCategories = categories.filter((cat) => !usedCategories.includes(cat))
return remainingCategories.map(
(cat) => new vscode.CompletionItem(cat, vscode.CompletionItemKind.TypeParameter)
)
}
return undefined
}
export function hoverHandler(document: vscode.TextDocument, position: vscode.Position) {
const wordRange = document.getWordRangeAtPosition(position)
const word = document.getText(wordRange)
const text = document.getText()
const lines = text.split("\n")
let hoverText: string | undefined
for (const line of lines) {
const words = line.split(/\s+/)
const wordIndex = words.indexOf(word)
if (wordIndex > 0) {
if (words[wordIndex - 1] === "EVENT") {
hoverText = `MOM_${word}`
} else if (words[wordIndex - 1] === "PARAM") {
hoverText = `mom_${word}`
}
break
}
}
if (hoverText) {
return new vscode.Hover(hoverText)
}
return undefined
}
+46 -34
View File
@@ -1,41 +1,53 @@
import * as vscode from "vscode";
import { formatCdlFile, completionHandler } from "./common/handlers";
import * as vscode from "vscode"
import { formatCdlFile, completionHandler, hoverHandler } from "./common/handlers"
export function activate(context: vscode.ExtensionContext) {
const disposable = 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)];
},
}
);
const formatProvider = 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(disposable);
context.subscriptions.push(formatProvider)
const completionProvider = vscode.languages.registerCompletionItemProvider(
{ scheme: "file", language: "cdl" },
{
provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken,
context: vscode.CompletionContext
) {
return completionHandler(document, position);
},
},
" " // Trigger completion on space
);
context.subscriptions.push(completionProvider);
const completionProvider = vscode.languages.registerCompletionItemProvider(
{ scheme: "file", language: "cdl" },
{
provideCompletionItems(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken,
context: vscode.CompletionContext
) {
return completionHandler(document, position)
}
},
" " // Trigger completion on space
)
context.subscriptions.push(completionProvider)
const hoverProvider = vscode.languages.registerHoverProvider(
{ scheme: "file", language: "cdl" },
{
provideHover(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
) {
return hoverHandler(document, position)
}
}
)
context.subscriptions.push(hoverProvider)
}
export function deactivate() {}