Compare commits
No commits in common. "trunk" and "0.1.1" have entirely different histories.
|
@ -1,13 +0,0 @@
|
|||
# ---> VisualStudioCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Local History for Visual Studio Code
|
||||
.history/
|
||||
|
||||
# Built Visual Studio Code Extensions
|
||||
*.vsix
|
|
@ -4,11 +4,8 @@ syntax highlighting for hblang in vscode
|
|||
|
||||
# Usage guide:
|
||||
|
||||
1. `cargo +nightly install hblang --git https://git.ablecorp.us/ableos/holey-bytes`
|
||||
> remember to keep this updated
|
||||
2.
|
||||
- add `$HOME/.cargo/bin` (or wherever you installed hblang) to `$PATH`
|
||||
1. run `cargo install hblang --git https://git.ablecorp.us/ableos/holey-bytes`
|
||||
2. add `$HOME/.cargo/bin` (or wherever you installed hblang) to `$PATH`
|
||||
> in bash, `export PATH=$PATH:$HOME/.cargo/bin`<br>
|
||||
> in other shells, there may be other ways
|
||||
- alternatively, add the path you installed hblang to the extension settings
|
||||
3. log out and log back in (or something, idk)
|
|
@ -68,8 +68,8 @@
|
|||
],
|
||||
"folding": {
|
||||
"markers": {
|
||||
"start": "{\n",
|
||||
"end": "}"
|
||||
"start": "^\\s*//\\s*#region\\b",
|
||||
"end": "^\\s*//\\s*#endregion\\b"
|
||||
}
|
||||
},
|
||||
"indentationRules": {
|
||||
|
|
2
package-lock.json
generated
2
package-lock.json
generated
|
@ -16,7 +16,7 @@
|
|||
"typescript": "^4.0.0",
|
||||
"vscode": "^1.1.37",
|
||||
"webpack": "^5.0.0",
|
||||
"webpack-cli": "^4.10.0"
|
||||
"webpack-cli": "^4.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"vscode": "^1.75.0"
|
||||
|
|
20
package.json
20
package.json
|
@ -6,15 +6,14 @@
|
|||
"publisher": "koniifer",
|
||||
"displayName": "hblang",
|
||||
"description": "Syntax highlighter and formatter for holey-bytes lang",
|
||||
"version": "0.2.3",
|
||||
"version": "0.1.0",
|
||||
"engines": {
|
||||
"vscode": "^1.75.0"
|
||||
},
|
||||
"main": "./out/main.js",
|
||||
"categories": [
|
||||
"Programming Languages",
|
||||
"Formatters",
|
||||
"Linters"
|
||||
"Formatters"
|
||||
],
|
||||
"activationEvents": [
|
||||
"onCommand:extension.formatDocument"
|
||||
|
@ -48,18 +47,7 @@
|
|||
}
|
||||
}
|
||||
}
|
||||
],
|
||||
"configuration": {
|
||||
"title": "hblang",
|
||||
"properties": {
|
||||
"hblang.compilerPath": {
|
||||
"type": "string",
|
||||
"default": "PATH",
|
||||
"description": "Set the optional path for the hbc compiler. If set to 'PATH', it will rely on the system to provide hbc.",
|
||||
"scope": "machine-overridable"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"vscode:prepublish": "webpack",
|
||||
|
@ -75,6 +63,6 @@
|
|||
"typescript": "^4.0.0",
|
||||
"vscode": "^1.1.37",
|
||||
"webpack": "^5.0.0",
|
||||
"webpack-cli": "^4.10.0"
|
||||
"webpack-cli": "^4.0.0"
|
||||
}
|
||||
}
|
166
src/main.ts
166
src/main.ts
|
@ -1,120 +1,66 @@
|
|||
import * as vscode from "vscode";
|
||||
import { execFile } from "child_process";
|
||||
import * as fs from "fs";
|
||||
import * as path from "path";
|
||||
|
||||
const DEFAULT_EXECUTABLE = "hbc";
|
||||
let diagnosticCollection: vscode.DiagnosticCollection;
|
||||
|
||||
function getExecutablePath(): string {
|
||||
const config = vscode.workspace.getConfiguration("hblang");
|
||||
return config.get<string>("compilerPath") === "PATH"
|
||||
? DEFAULT_EXECUTABLE
|
||||
: config.get<string>("compilerPath")!;
|
||||
}
|
||||
|
||||
async function runCommand(filePath: string, args: string[]): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
execFile(getExecutablePath(), args, (error, stdout, stderr) => {
|
||||
if (error && error.code === "ENOENT") {
|
||||
vscode.window.showErrorMessage(
|
||||
"hblang compiler not found. Ensure 'hbc' is installed and available in PATH."
|
||||
);
|
||||
return reject(new Error(`Compiler not found: ${stderr || stdout}`));
|
||||
}
|
||||
return error
|
||||
? reject(new Error(stderr || `Exit code: ${error.code}`))
|
||||
: resolve(stdout);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
async function provideDocumentFormattingEdits(
|
||||
document: vscode.TextDocument
|
||||
): Promise<vscode.TextEdit[]> {
|
||||
const tempFilePath = path.join(
|
||||
path.dirname(document.uri.fsPath),
|
||||
`temp_${path.basename(document.uri.fsPath)}`
|
||||
);
|
||||
await fs.promises.writeFile(tempFilePath, document.getText());
|
||||
|
||||
try {
|
||||
const stdout = await runCommand(tempFilePath, [
|
||||
"--fmt-stdout",
|
||||
tempFilePath,
|
||||
]);
|
||||
const fullRange = new vscode.Range(
|
||||
document.positionAt(0),
|
||||
document.positionAt(document.getText().length)
|
||||
);
|
||||
|
||||
diagnosticCollection.delete(document.uri);
|
||||
|
||||
return [vscode.TextEdit.replace(fullRange, stdout)];
|
||||
} catch (error) {
|
||||
return [];
|
||||
} finally {
|
||||
fs.promises.unlink(tempFilePath).catch(console.error);
|
||||
}
|
||||
}
|
||||
|
||||
async function lintDocument(document: vscode.TextDocument) {
|
||||
try {
|
||||
const stderr = await runCommand(document.uri.fsPath, [document.uri.fsPath]);
|
||||
diagnosticCollection.set(document.uri, parseLintingErrors(stderr));
|
||||
} catch (error) {
|
||||
if (error instanceof Error) {
|
||||
diagnosticCollection.set(document.uri, parseLintingErrors(error.message));
|
||||
} else {
|
||||
vscode.window.showErrorMessage("Error linting hblang document.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function parseLintingErrors(stderr: string): vscode.Diagnostic[] {
|
||||
return stderr
|
||||
.split("\n")
|
||||
.filter((line) => line && !line.includes("missing main function"))
|
||||
.map((lineText) => {
|
||||
const match = lineText.match(/^([^:]+):(\d+):(\d+):\s*(.+)$/);
|
||||
if (!match) return null;
|
||||
|
||||
const [, , lineStr, columnStr, message] = match;
|
||||
const lineNum = parseInt(lineStr, 10) - 1;
|
||||
const columnNum = parseInt(columnStr, 10) - 1;
|
||||
|
||||
return new vscode.Diagnostic(
|
||||
new vscode.Range(
|
||||
lineNum,
|
||||
columnNum,
|
||||
lineNum,
|
||||
columnNum + message.length
|
||||
),
|
||||
message,
|
||||
vscode.DiagnosticSeverity.Error
|
||||
);
|
||||
})
|
||||
.filter(Boolean) as vscode.Diagnostic[];
|
||||
}
|
||||
import * as vscode from 'vscode';
|
||||
import { spawn } from 'child_process';
|
||||
import * as fs from 'fs';
|
||||
import * as os from 'os';
|
||||
import * as path from 'path';
|
||||
|
||||
export function activate(context: vscode.ExtensionContext) {
|
||||
diagnosticCollection = vscode.languages.createDiagnosticCollection("hblang");
|
||||
context.subscriptions.push(diagnosticCollection);
|
||||
// Register a document formatting edit provider for the language
|
||||
context.subscriptions.push(
|
||||
vscode.languages.registerDocumentFormattingEditProvider('hblang', {
|
||||
async provideDocumentFormattingEdits(document: vscode.TextDocument): Promise<vscode.TextEdit[]> {
|
||||
|
||||
vscode.workspace.onDidSaveTextDocument(async (document) => {
|
||||
if (document.languageId === "hblang") {
|
||||
await lintDocument(document);
|
||||
try {
|
||||
const formattedText = await formatDocument(document.fileName);
|
||||
|
||||
if (diagnosticCollection.get(document.uri)?.length === 0) {
|
||||
vscode.commands.executeCommand(
|
||||
"editor.action.formatDocument",
|
||||
document
|
||||
const edit = new vscode.TextEdit(
|
||||
new vscode.Range(0, 0, document.lineCount, 0),
|
||||
formattedText
|
||||
);
|
||||
|
||||
return [edit];
|
||||
} catch (error: unknown) {
|
||||
if (error instanceof Error) {
|
||||
vscode.window.showErrorMessage(`Formatting failed: ${error.message}`);
|
||||
} else {
|
||||
vscode.window.showErrorMessage(`Formatting failed: ${String(error)}`);
|
||||
}
|
||||
return []; // Return an empty array if formatting fails
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function deactivate() { }
|
||||
|
||||
async function formatDocument(path: string): Promise<string> {
|
||||
|
||||
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const child = spawn('hbc', ['--fmt-stdout', path], { shell: true });
|
||||
|
||||
let formattedText = '';
|
||||
|
||||
// Capture the output
|
||||
child.stdout.on('data', (data) => {
|
||||
formattedText += data.toString();
|
||||
});
|
||||
|
||||
vscode.languages.registerDocumentFormattingEditProvider("hblang", {
|
||||
provideDocumentFormattingEdits,
|
||||
// Handle errors
|
||||
child.stderr.on('data', (data) => {
|
||||
reject(new Error(data.toString()));
|
||||
});
|
||||
|
||||
// Handle process exit
|
||||
child.on('exit', (code) => {
|
||||
|
||||
if (code === 0) {
|
||||
resolve(formattedText);
|
||||
} else {
|
||||
reject(new Error(`Formatter exited with code ${code}`));
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -18,29 +18,29 @@
|
|||
{
|
||||
"include": "#string"
|
||||
},
|
||||
{
|
||||
"include": "#struct"
|
||||
},
|
||||
{
|
||||
"include": "#keyword"
|
||||
},
|
||||
{
|
||||
"include": "#number"
|
||||
},
|
||||
{
|
||||
"include": "#punctuation"
|
||||
},
|
||||
{
|
||||
"include": "#function"
|
||||
},
|
||||
{
|
||||
"include": "#operator"
|
||||
"include": "#number"
|
||||
},
|
||||
{
|
||||
"include": "#struct"
|
||||
},
|
||||
{
|
||||
"include": "#type"
|
||||
},
|
||||
{
|
||||
"include": "#variable"
|
||||
},
|
||||
{
|
||||
"include": "#type"
|
||||
"include": "#operator"
|
||||
},
|
||||
{
|
||||
"include": "#punctuation"
|
||||
},
|
||||
{
|
||||
"include": "#array"
|
||||
|
@ -51,31 +51,19 @@
|
|||
"patterns": [
|
||||
{
|
||||
"name": "variable.parameter.hblang",
|
||||
"match": "\\b\\w+\\s*:\\s*(?:\\^)?[\\w\\$]+\\b"
|
||||
"match": "\\b\\w+\\b"
|
||||
}
|
||||
]
|
||||
},
|
||||
"number": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "constant.numeric.float.hblang",
|
||||
"match": "\\b\\d+\\.\\d+\\b"
|
||||
"name": "constant.numeric.hblang",
|
||||
"match": "\\b\\d+(\\.\\d+)?\\b"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.hex.hblang",
|
||||
"match": "0x[0-9A-Fa-f]+"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.binary.hblang",
|
||||
"match": "0b[0-1]+"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.octal.hblang",
|
||||
"match": "0o[0-7]+"
|
||||
},
|
||||
{
|
||||
"name": "constant.numeric.decimal.hblang",
|
||||
"match": "\\b\\d+\\b"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -99,10 +87,20 @@
|
|||
"name": "string.quoted.double.hblang",
|
||||
"begin": "\"",
|
||||
"end": "\"",
|
||||
"beginCaptures": {
|
||||
"0": {
|
||||
"name": "punctuation.definition.string.begin.hblang"
|
||||
}
|
||||
},
|
||||
"endCaptures": {
|
||||
"0": {
|
||||
"name": "punctuation.definition.string.end.hblang"
|
||||
}
|
||||
},
|
||||
"patterns": [
|
||||
{
|
||||
"name": "constant.character.escape.hblang",
|
||||
"match": "\\\\(?:[\"'\\\\bfnrt]|[{\\[]\\w+[}\\]]|[a-zA-Z])"
|
||||
"match": "\\\\[\"'\\\\bfnrt]|\\\\(\\{[^}]+\\}|\\[[^]]+\\]|[a-zA-Z])"
|
||||
},
|
||||
{
|
||||
"name": "invalid.illegal.escaped.hblang",
|
||||
|
@ -116,7 +114,7 @@
|
|||
"patterns": [
|
||||
{
|
||||
"name": "keyword.control.hblang",
|
||||
"match": "\\b(fn|loop|break|if|else|return|packed|continue|true|false|struct|idk|die|null)\\b"
|
||||
"match": "\\b(loop|break|if|else|return|packed|continue|true|false|struct|idk)\\b"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
@ -124,42 +122,77 @@
|
|||
"patterns": [
|
||||
{
|
||||
"name": "storage.type.hblang",
|
||||
"match": "\\b(f(32|64))|(uint|int|(u|i)(8|16|32)|void|bool|type|never)\\b"
|
||||
"match": "\\b(uint|int|(u|i)(8|16|32)|void|bool|type|never|([A-Z]\\w+))\\b"
|
||||
}
|
||||
]
|
||||
},
|
||||
"variable": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "variable.declaration.hblang",
|
||||
"match": "\\b\\w+\\b(?=\\s*:=)"
|
||||
"name": "variable.parameter.hblang",
|
||||
"match": "\\b\\w+\\s*:=\\b"
|
||||
},
|
||||
{
|
||||
"name": "variable.assignment.hblang",
|
||||
"match": "\\b\\w+\\b(?=\\s*=(?!=))"
|
||||
"name": "variable.parameter.hblang",
|
||||
"match": "\\b\\w+\\s*=\\b"
|
||||
},
|
||||
{
|
||||
"name": "variable.member.hblang",
|
||||
"match": "(?<=\\.)\\w+\\b"
|
||||
"name": "variable.parameter.hblang",
|
||||
"match": "(?<=\\w+\\.)(\\w+)\\b"
|
||||
}
|
||||
]
|
||||
},
|
||||
"operator": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "keyword.operator.assignment.hblang",
|
||||
"match": ":="
|
||||
},
|
||||
{
|
||||
"name": "keyword.operator.assignment.hblang",
|
||||
"match": "=(?!=)"
|
||||
},
|
||||
{
|
||||
"name": "keyword.operator.hblang",
|
||||
"match": "(\\$|\\?|@|<<=|>>=|>=|<=|\\^=|\\+=|-=|\\*=|\\/=|%=|\\|=|&=|\\^|\\*|&|<<|>>|\\+|\\-|\\/|%|\\||!|==|!=|<|>)"
|
||||
"match": "(@|:=|<<=|>>=|>=|<=|^=|\\+=|-=|\\*=|\\/=|%=|\\|=|&=|\\^|\\*|&|&&|\\|\\||<<|>>|\\+|\\-|\\/|%|\\||!|==|!=|<|>|=)"
|
||||
}
|
||||
]
|
||||
},
|
||||
"function": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "entity.name.function.hblang",
|
||||
"begin": "(?<!\\w)fn\\s*\\(",
|
||||
"end": "\\)",
|
||||
"captures": {
|
||||
"0": {
|
||||
"name": "keyword.hblang"
|
||||
}
|
||||
},
|
||||
"patterns": [
|
||||
{
|
||||
"include": "#all"
|
||||
},
|
||||
{
|
||||
"name": "variable.parameter.hblang",
|
||||
"match": "\\b\\w+\\b(\\s*,\\s*\\b\\w+\\b)*"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "entity.name.function.hblang",
|
||||
"begin": "(?:@)?\\b\\w+\\s*\\(",
|
||||
"end": "\\)",
|
||||
"captures": {
|
||||
"0": {
|
||||
"name": "entity.name.function.hblang"
|
||||
}
|
||||
},
|
||||
"patterns": [
|
||||
{
|
||||
"include": "#all"
|
||||
},
|
||||
{
|
||||
"name": "variable.parameter.hblang",
|
||||
"match": "\\b\\w+\\b(\\s*,\\s*\\b\\w+\\b)*"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"struct": {
|
||||
"patterns": [
|
||||
{
|
||||
|
@ -168,67 +201,21 @@
|
|||
}
|
||||
]
|
||||
},
|
||||
"array": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "storage.type.array.hblang",
|
||||
"begin": "\\.\\(",
|
||||
"end": "\\)"
|
||||
}
|
||||
]
|
||||
},
|
||||
"punctuation": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "punctuation.terminator.statement.hblang",
|
||||
"match": ";|\\.|,|(?<!:):(?!=)"
|
||||
}
|
||||
]
|
||||
},
|
||||
"function": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "meta.function.declaration.hblang",
|
||||
"begin": "(\\b\\w+\\b)\\s*(:=)\\s*(fn)\\s*\\(",
|
||||
"end": "\\)",
|
||||
"beginCaptures": {
|
||||
"1": {
|
||||
"name": "entity.name.function.hblang"
|
||||
},
|
||||
"2": {
|
||||
"name": "keyword.operator.assignment.hblang"
|
||||
},
|
||||
"3": {
|
||||
"name": "keyword.control.hblang"
|
||||
}
|
||||
},
|
||||
"patterns": [
|
||||
{
|
||||
"include": "#all"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "meta.function.call.hblang",
|
||||
"begin": "@?\\w+\\s*\\(",
|
||||
"end": "\\)",
|
||||
"beginCaptures": {
|
||||
"0": {
|
||||
"name": "entity.name.function.hblang"
|
||||
}
|
||||
},
|
||||
"patterns": [
|
||||
{
|
||||
"include": "#all"
|
||||
"match": ";|\\.|,|:"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
},
|
||||
"array": {
|
||||
"patterns": [
|
||||
{
|
||||
"name": "meta.array.initialization.hblang",
|
||||
"begin": "\\.\\(",
|
||||
"end": "\\)",
|
||||
"patterns": [
|
||||
{
|
||||
"include": "#all"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,3 +0,0 @@
|
|||
main := fn(): void {
|
||||
undefined_identifier
|
||||
}
|
|
@ -6,19 +6,20 @@ module.exports = {
|
|||
output: {
|
||||
path: path.resolve(__dirname, 'out'),
|
||||
filename: 'main.js',
|
||||
libraryTarget: 'commonjs2'
|
||||
libraryTarget: 'commonjs2' // This is correct for VS Code extensions
|
||||
},
|
||||
resolve: {
|
||||
extensions: ['.ts', '.js'],
|
||||
fallback: {
|
||||
"child_process": false,
|
||||
"fs": false,
|
||||
"os": require.resolve("os-browserify/browser"),
|
||||
"path": require.resolve("path-browserify"),
|
||||
// For Node.js core modules not available in browser context, you set them to false
|
||||
"child_process": false, // Not used in browser context
|
||||
"fs": false, // Not used in browser context
|
||||
"os": require.resolve("os-browserify/browser"), // Polyfill for os
|
||||
"path": require.resolve("path-browserify"), // Polyfill for path
|
||||
},
|
||||
},
|
||||
externals: {
|
||||
vscode: 'commonjs vscode'
|
||||
vscode: 'commonjs vscode' // Externalize the vscode module
|
||||
},
|
||||
module: {
|
||||
rules: [
|
||||
|
@ -29,5 +30,5 @@ module.exports = {
|
|||
}
|
||||
]
|
||||
},
|
||||
target: 'node'
|
||||
target: 'node' // Specify the target environment as Node.js
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue