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
+8
View File
@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/prettierrc",
"semi": false,
"tabWidth": 4,
"singleQuote": false,
"printWidth": 100,
"trailingComma": "none"
}
+65 -23
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 = [];
let indentLevel = 0
const formattedLines = []
for (const line of content.split("\n")) {
const strippedLine = line.trim();
const strippedLine = line.trim()
if (strippedLine.endsWith("}")) {
indentLevel = Math.max(indentLevel - 1, 0);
indentLevel = Math.max(indentLevel - 1, 0)
}
formattedLines.push(" ".repeat(indentLevel) + strippedLine);
formattedLines.push(" ".repeat(indentLevel) + strippedLine)
if (strippedLine.endsWith("{")) {
indentLevel += 1;
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"]
if (linePrefix.endsWith("TYPE ")) {
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),
];
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
}
+29 -17
View File
@@ -1,25 +1,23 @@
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(
const formatProvider = vscode.languages.registerDocumentFormattingEditProvider(
{ scheme: "file", language: "cdl" },
{
provideDocumentFormattingEdits(
document: vscode.TextDocument
): vscode.TextEdit[] {
const text = document.getText();
const formattedText = formatCdlFile(text);
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)];
},
)
return [vscode.TextEdit.replace(fullRange, formattedText)]
}
);
}
)
context.subscriptions.push(disposable);
context.subscriptions.push(formatProvider)
const completionProvider = vscode.languages.registerCompletionItemProvider(
{ scheme: "file", language: "cdl" },
@@ -30,12 +28,26 @@ export function activate(context: vscode.ExtensionContext) {
token: vscode.CancellationToken,
context: vscode.CompletionContext
) {
return completionHandler(document, position);
},
return completionHandler(document, position)
}
},
" " // Trigger completion on space
);
context.subscriptions.push(completionProvider);
)
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() {}
+21 -4
View File
@@ -1,15 +1,16 @@
{
"name": "cdl",
"version": "0.0.1",
"name": "vscode-cdl-support",
"version": "0.1.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "cdl",
"version": "0.0.1",
"name": "vscode-cdl-support",
"version": "0.1.0",
"devDependencies": {
"@types/vscode": "^1.96.0",
"@vscode/vsce": "^3.2.1",
"prettier": "^3.4.2",
"typescript": "^5.7.2",
"vite": "^6.0.7"
},
@@ -2766,6 +2767,22 @@
"node": ">=10"
}
},
"node_modules/prettier": {
"version": "3.4.2",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.4.2.tgz",
"integrity": "sha512-e9MewbtFo+Fevyuxn/4rrcDAaq0IYxPGLvObpQjiZBMAzB9IGmzlnG9RZy3FFas+eBMu2vA0CszMeduow5dIuQ==",
"dev": true,
"license": "MIT",
"bin": {
"prettier": "bin/prettier.cjs"
},
"engines": {
"node": ">=14"
},
"funding": {
"url": "https://github.com/prettier/prettier?sponsor=1"
}
},
"node_modules/pump": {
"version": "3.0.2",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.2.tgz",
+2 -1
View File
@@ -2,7 +2,7 @@
"name": "vscode-cdl-support",
"displayName": "vscode-cdl-support",
"description": "",
"version": "0.0.5",
"version": "0.1.0",
"publisher": "Christoph",
"repository": {
"type": "git",
@@ -50,6 +50,7 @@
"devDependencies": {
"@types/vscode": "^1.96.0",
"@vscode/vsce": "^3.2.1",
"prettier": "^3.4.2",
"typescript": "^5.7.2",
"vite": "^6.0.7"
}
+40
View File
@@ -5,6 +5,9 @@
{
"include": "#comment"
},
{
"include": "#function"
},
{
"include": "#variables"
},
@@ -14,6 +17,9 @@
{
"include": "#keywords"
},
{
"include": "#numbers"
},
{
"include": "#strings"
}
@@ -38,6 +44,27 @@
}
]
},
"numbers": {
"name": "constant.numeric",
"patterns": [
{
"name": "constant.numeric",
"match": "\"\\d+\""
},
{
"name": "constant.numeric",
"match": "\"\\d+.\\d+\""
},
{
"name": "constant.numeric",
"match": "\\d+"
},
{
"name": "constant.numeric",
"match": "\\d+.\\d+"
}
]
},
"types": {
"patterns": [
{
@@ -82,6 +109,19 @@
}
]
},
"function": {
"patterns": [
{
"name": "storage.type.function.cdl",
"match": "\\bEVENT\\s+([a-zA-Z_]\\w*)\\b",
"captures": {
"1": {
"name": "entity.name.function"
}
}
}
]
},
"comment": {
"patterns": [
{
+2 -2
View File
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es6",
"lib": ["es6"],
"target": "es2023",
"lib": ["es2023", "dom"],
"module": "commonjs",
"outDir": "client/out",
"rootDir": "client",
+6 -6
View File
@@ -1,17 +1,17 @@
import { defineConfig } from "vite";
import { defineConfig } from "vite"
export default defineConfig({
build: {
lib: {
entry: "./client/src/extension.ts",
formats: ["cjs", "es"],
fileName: "extension",
fileName: "extension"
},
rollupOptions: {
external: ["vscode"],
external: ["vscode"]
},
sourcemap: true,
outDir: "client/out/src",
outDir: "client/out/src"
},
plugins: [],
});
plugins: []
})