mirror of
https://github.com/open-goal/opengoal-vscode.git
synced 2024-10-19 20:47:37 -04:00
languages: implement custom folder for IR2 files (#37)
This commit is contained in:
parent
bbc8940d0f
commit
aa6cdb2739
38154
samples/sample_ir2.asm
generated
vendored
38154
samples/sample_ir2.asm
generated
vendored
File diff suppressed because it is too large
Load diff
|
@ -8,6 +8,7 @@ import { PdfCustomProvider } from "./vendor/vscode-pdfviewer/pdfProvider";
|
||||||
import { switchFile } from "./utils/FileUtils";
|
import { switchFile } from "./utils/FileUtils";
|
||||||
import { activateDecompTools } from "./decomp/decomp-tools";
|
import { activateDecompTools } from "./decomp/decomp-tools";
|
||||||
import { initContext } from "./context";
|
import { initContext } from "./context";
|
||||||
|
import { IRFoldingRangeProvider } from "./languages/ir2-folder";
|
||||||
|
|
||||||
export async function activate(context: vscode.ExtensionContext) {
|
export async function activate(context: vscode.ExtensionContext) {
|
||||||
// Init Context
|
// 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
|
// Start the LSP
|
||||||
lsp.activate(context);
|
lsp.activate(context);
|
||||||
}
|
}
|
||||||
|
|
91
src/languages/ir2-folder.ts
Normal file
91
src/languages/ir2-folder.ts
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,11 +9,5 @@
|
||||||
["(", ")"],
|
["(", ")"],
|
||||||
["\"", "\""]
|
["\"", "\""]
|
||||||
],
|
],
|
||||||
"folding": {
|
|
||||||
"markers": {
|
|
||||||
"start": "^; \\.function.*",
|
|
||||||
"end": "^;; \\.endfunction\\b"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
"wordPattern": "[^\\s,\\(\\)]+"
|
"wordPattern": "[^\\s,\\(\\)]+"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue