languages: implement custom folder for IR2 files (#37)

This commit is contained in:
Tyler Wilding 2022-07-31 23:53:36 -04:00 committed by GitHub
parent bbc8940d0f
commit aa6cdb2739
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 8816 additions and 29442 deletions

38154
samples/sample_ir2.asm generated vendored

File diff suppressed because it is too large Load diff

View file

@ -8,6 +8,7 @@ import { PdfCustomProvider } from "./vendor/vscode-pdfviewer/pdfProvider";
import { switchFile } from "./utils/FileUtils";
import { activateDecompTools } from "./decomp/decomp-tools";
import { initContext } from "./context";
import { IRFoldingRangeProvider } from "./languages/ir2-folder";
export async function activate(context: vscode.ExtensionContext) {
// Init Context
@ -40,6 +41,12 @@ export async function activate(context: vscode.ExtensionContext) {
)
);
// Language Customizations
vscode.languages.registerFoldingRangeProvider(
{ scheme: "file", language: "opengoal-ir" },
new IRFoldingRangeProvider()
);
// Start the LSP
lsp.activate(context);
}

View file

@ -0,0 +1,91 @@
import * as vscode from "vscode";
export class IRFoldingRangeProvider implements vscode.FoldingRangeProvider {
provideFoldingRanges(document: vscode.TextDocument): vscode.FoldingRange[] {
const ranges: vscode.FoldingRange[] = [];
const functionStart = /^; \.function/;
const functionEnd = /^;; \.endfunction/;
const label = /L\d+:/;
const branch = /B\d+:/;
let currFunctionStart = -1;
let currLabelStart = -1;
let currBranchStart = -1;
for (let i = 0; i < document.lineCount; i++) {
const line = document.lineAt(i).text;
// The start of a new function
if (functionStart.test(line)) {
currFunctionStart = i;
}
// The end of the current function
if (functionEnd.test(line)) {
if (currFunctionStart != -1) {
// End existing function
ranges.push(
new vscode.FoldingRange(
currFunctionStart,
i - 1,
vscode.FoldingRangeKind.Region
)
);
}
}
// The start of a label or new section of data
if (label.test(line)) {
if (currLabelStart != -1) {
// End existing label
ranges.push(
new vscode.FoldingRange(
currLabelStart,
i - 1,
vscode.FoldingRangeKind.Region
)
);
}
currLabelStart = i;
} else if (
currLabelStart != -1 &&
(line.trim().startsWith("(") || line.trim().startsWith(";"))
) {
// End existing label
ranges.push(
new vscode.FoldingRange(
currLabelStart,
i - 1,
vscode.FoldingRangeKind.Region
)
);
currLabelStart = -1;
}
if (branch.test(line)) {
if (currBranchStart != -1) {
// End existing function
ranges.push(
new vscode.FoldingRange(
currBranchStart,
i - 1,
vscode.FoldingRangeKind.Region
)
);
}
currBranchStart = i;
} else if (
currBranchStart != -1 &&
(line.trim().startsWith("(") || line.trim().startsWith(";"))
) {
// End existing branch
ranges.push(
new vscode.FoldingRange(
currBranchStart,
i - 1,
vscode.FoldingRangeKind.Region
)
);
currBranchStart = -1;
}
}
return ranges;
}
}

View file

@ -9,11 +9,5 @@
["(", ")"],
["\"", "\""]
],
"folding": {
"markers": {
"start": "^; \\.function.*",
"end": "^;; \\.endfunction\\b"
}
},
"wordPattern": "[^\\s,\\(\\)]+"
}