handle def file formatter
This commit is contained in:
@@ -12,10 +12,29 @@ export function formatCdlFile(content: string): string {
|
||||
if (strippedLine.endsWith("{")) {
|
||||
indentLevel += 1
|
||||
}
|
||||
if (strippedLine.startsWith("#")) {
|
||||
formattedLines[formattedLines.length - 1] =
|
||||
" ".repeat(indentLevel) + strippedLine.replace(/^#\s*/, "# ")
|
||||
}
|
||||
}
|
||||
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 completionHandlerCdl(document: vscode.TextDocument, position: vscode.Position) {
|
||||
const linePrefix = document.lineAt(position).text.substring(0, position.character)
|
||||
const categories = ["MILL", "LATHE", "DRILL"]
|
||||
|
||||
+23
-1
@@ -1,5 +1,10 @@
|
||||
import * as vscode from "vscode"
|
||||
import { formatCdlFile, completionHandlerCdl, hoverCdlHandler } from "./common/handlers"
|
||||
import {
|
||||
formatCdlFile,
|
||||
completionHandlerCdl,
|
||||
hoverCdlHandler,
|
||||
formatDefFile
|
||||
} from "./common/handlers"
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
const formatCdlProvider = vscode.languages.registerDocumentFormattingEditProvider(
|
||||
@@ -48,6 +53,23 @@ export function activate(context: vscode.ExtensionContext) {
|
||||
}
|
||||
)
|
||||
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)
|
||||
}
|
||||
|
||||
export function deactivate() {}
|
||||
|
||||
Reference in New Issue
Block a user