Skip to content

Refactoring formatter - #15

Merged
abco20 merged 6 commits into
mainfrom
refactor/formatter
Jan 5, 2026
Merged

Refactoring formatter#15
abco20 merged 6 commits into
mainfrom
refactor/formatter

Conversation

@abco20

@abco20 abco20 commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello @abco20, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors the BT DSL formatter by migrating its underlying parsing technology. It transitions from a web-tree-sitter based grammar to a C++ parser compiled into WebAssembly, which is now part of the core project. This change simplifies the project structure by removing a separate grammar package, improves maintainability by consolidating parsing logic, and enhances the formatter's resilience to malformed input by preserving original text in problematic areas.

Highlights

  • Formatter Backend Migration: The BT DSL formatter has been refactored to utilize a C++ parser compiled to WebAssembly (WASM) from the core project, replacing the previous web-tree-sitter based implementation.
  • Removal of Tree-sitter Component: The dedicated tree-sitter-bt-dsl package, including its grammar definition, external scanner, and related build/test artifacts, has been entirely removed from the project.
  • WASM Integration & Configuration: New infrastructure (formatter/src/core-wasm.ts) has been introduced to manage the loading and configuration of the C++ WASM parser, including support for relocating WASM files in different environments (e.g., VS Code extensions).
  • Enhanced Malformed Input Handling: The formatter now includes robust handling for malformed or incomplete input, ensuring that valid portions are formatted while preserving all original characters in error-prone sections, preventing data loss.
  • Updated Build System & Dependencies: Project build scripts (package.json, vscode/package.json) and dependency lock files (pnpm-lock.yaml) have been updated to reflect the removal of Tree-sitter dependencies and the new WASM build process.
Ignored Files
  • Ignored by pattern: .github/workflows/** (1)
    • .github/workflows/ci.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request is a significant refactoring that replaces the Tree-sitter based parser in the formatter with a new one based on the core C++ parser compiled to WASM. This is a great move for consolidating the language grammar and likely improving performance. The changes are extensive, touching everything from the parser logic and AST structure to build scripts and documentation. The new implementation in prettier-plugin-bt-dsl.ts is impressive, with robust error handling, UTF-8 to UTF-16 offset conversion, and careful comment preservation. The addition of new tests for malformed input and other edge cases is also excellent.

I've added a few suggestions to improve the maintainability of the build and test scripts, and to clean up some leftover debug code in one of the new tests. Overall, this is a very well-executed and substantial improvement to the formatter.

Comment thread formatter/package.json Outdated
"lint:fix": "pnpm -w exec eslint formatter/src formatter/test --max-warnings 0 --fix",
"pretest": "pnpm build",
"test": "node dist/test/test-format.js && node dist/test/test-standard-nodes.js && node dist/test/test-params.js && node dist/test/test-comment-preservation.js && node dist/test/test-extern-parens.js"
"test": "node dist/test/test-format.js && node dist/test/test-standard-nodes.js && node dist/test/test-params.js && node dist/test/test-comment-preservation.js && node dist/test/test-extern-parens.js && node dist/test/test-wasm-relocation.js && node dist/test/test-malformed-input.js && node dist/test/test-trailing-comments.js && node dist/test/test-argument-comments.js"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test script is becoming long as it manually lists every test file. This can be brittle and harder to maintain as more tests are added.

Consider using a test runner that can automatically discover and run tests. Since other parts of the project use node --test, you could adopt it here as well. This would likely involve renaming your test files to follow a pattern like *.test.js or *.test.mjs so you can use a glob pattern.

For example, if you rename the test files, you could simplify the script to something like:

"test": "node --test dist/test/"

This would make the test execution more robust and easier to manage.

Comment thread formatter/test/test-wasm-relocation.ts Outdated
Comment on lines +57 to +60
console.log('DEBUG: input =', JSON.stringify(input));
console.log('DEBUG: output =', JSON.stringify(out));
console.log('DEBUG: 日本語コメント count =', countOccurrences(out, '日本語コメント'));
console.log('DEBUG: AlwaysSuccess(); count =', countOccurrences(out, 'AlwaysSuccess();'));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

These console.log statements appear to be for debugging. It's best to remove them to keep the test output clean.

Comment thread vscode/package.json Outdated
"scripts": {
"prebuild": "(if [ -f ../core/build-lsp/bt_dsl_lsp_server ]; then mkdir -p server && cp -f ../core/build-lsp/bt_dsl_lsp_server server/; elif [ -f ../core/build/bt_dsl_lsp_server ]; then mkdir -p server && cp -f ../core/build/bt_dsl_lsp_server server/; elif [ -f server/bt_dsl_lsp_server ]; then :; else echo 'Warning: LSP server not found (checked ../core/build-lsp/bt_dsl_lsp_server, ../core/build/bt_dsl_lsp_server, server/bt_dsl_lsp_server). Set BT_DSL_LSP_PATH for dev or build bt_dsl_lsp_server.'; fi) && if [ -d ../core/std ]; then cp -rf ../core/std .; else echo 'Warning: std/ not found'; fi && if [ -f ../tree-sitter-bt-dsl/tree-sitter-bt_dsl.wasm ]; then mkdir -p out && cp ../tree-sitter-bt-dsl/tree-sitter-bt_dsl.wasm out/; else echo 'Warning: tree-sitter WASM not found for formatter.'; fi",
"build": "tsc -b && esbuild src/extension.ts --bundle --outfile=out/extension.js --external:vscode --external:vscode-languageclient --external:vscode-languageclient/* --external:@bt-dsl/formatter --external:web-tree-sitter --external:prettier --format=esm --platform=node",
"prebuild": "(if [ -f ../core/build-lsp/bt_dsl_lsp_server ]; then mkdir -p server && cp -f ../core/build-lsp/bt_dsl_lsp_server server/; elif [ -f ../core/build/bt_dsl_lsp_server ]; then mkdir -p server && cp -f ../core/build/bt_dsl_lsp_server server/; elif [ -f server/bt_dsl_lsp_server ]; then :; else echo 'Warning: LSP server not found (checked ../core/build-lsp/bt_dsl_lsp_server, ../core/build/bt_dsl_lsp_server, server/bt_dsl_lsp_server). Set BT_DSL_LSP_PATH for dev or build bt_dsl_lsp_server.'; fi) && if [ -d ../core/std ]; then cp -rf ../core/std .; else echo 'Warning: std/ not found'; fi && pnpm --filter @bt-dsl/core build:wasm && mkdir -p out && cp -f ../core/build-wasm/formatter_wasm.js out/ && cp -f ../core/build-wasm/formatter_wasm.wasm out/",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The prebuild script is quite long and contains complex shell logic, which can be difficult to read and maintain within a JSON file.
For better readability and maintainability, consider moving this logic into a separate shell script file, for example, scripts/prebuild.sh.

You could create a scripts/prebuild.sh file with the logic and then call it from package.json:

"prebuild": "./scripts/prebuild.sh"

This would make the package.json cleaner and the script easier to manage.

@abco20
abco20 merged commit 4f2a114 into main Jan 5, 2026
17 checks passed
@abco20
abco20 deleted the refactor/formatter branch January 5, 2026 04:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant