-
-
Notifications
You must be signed in to change notification settings - Fork 1
Implement language server protocol (LSP) and VS Code extension client #81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
56befb9
lsp
ehwan d6ecefa
Merge branch 'main' into lsp
ehwan 611ca4f
test working
ehwan 52ab222
Merge branch 'main' into lsp
ehwan 3dcbc99
WIP completion support
ehwan 4aacd2b
suggestion details
ehwan b5efae1
Hovering
ehwan c02fe0a
inlay hint
ehwan dbda29d
fix hovering details for plain identifier
ehwan 5e3205a
%allow suggestion
ehwan f5419b0
formatting
ehwan e798a20
formatting comments
ehwan 961f04b
semantic tokens and VSCode extension settings
ehwan 912a59c
show userdata definition
ehwan 7ecfce1
hover detection at '@'
ehwan 01f27af
semantic and hover support for %prec defined identifiers
ehwan 9f75395
fix AGENTS.md to sync for lsp projects
ehwan 20dc9c6
hover, inlay support for reduce action
ehwan 7669b09
fixed wording ruletype to production type
ehwan 0532e7a
support for Find References/Goto Definitino
ehwan d2817bc
support for VSCode extension
ehwan dee290a
fix for comment inside formattable
ehwan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| node_modules/ | ||
| *.vsix |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to the "RustyLR" extension will be documented in this file. | ||
|
|
||
| ## 0.1.0 | ||
|
|
||
| - First public release of RustyLR language support! | ||
| - Fully integrated with the `rusty_lr_lsp` server: | ||
| - **Syntax Highlighting (Semantic Tokens):** Distinct syntax coloring for terminals, non-terminals, directives, bindings, location bindings, and variables. | ||
| - **Diagnostics:** Inline warning and error reporting directly in the editor. | ||
| - **Code Actions:** Quick-fix actions to suppress warnings with `%allow` directives. | ||
| - **Formatting:** Code formatting and indentation support for rule definitions and reduce actions. | ||
| - **Go to Definition:** Jump directly to token declarations, production definitions, and precedence definitions. | ||
| - **Find References:** Find all usages of terminals, non-terminals, and precedence symbols across the grammar document. | ||
| - **Hover Tooltips:** Interactive documentation tooltips for keywords, patterns, and variables. | ||
| - **Inlay Hints:** Inline type hints for grammar patterns and reduce actions. | ||
| - **Auto-Completion:** Intelligent suggestions for symbols, directives, variables, and locations. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # RustyLR Language Support | ||
|
|
||
| This extension provides rich language support for the [RustyLR](https://github.com/ehwan/RustyLR) parser generator grammar files (`*.rustylr` and `rustylr.rs`). | ||
|
|
||
| ## Features | ||
|
|
||
| - **Diagnostics & Error Reporting:** Real-time diagnostics for grammar syntax errors, unused symbols, conflict resolutions, and more. | ||
| - **Go to Definition:** Quickly navigate to rule definitions, terminal declarations, and precedence rules. | ||
| - **Find References:** Find all occurrences and usages of terminals, non-terminals, and precedence symbols. | ||
| - **Syntax Highlighting (Semantic Tokens):** Distinct, theme-aligned colors for terminal names, non-terminal rules, directives, bindings, location bindings (`@loc`), and variables (`$var`). | ||
| - **Formatting:** Automatic document formatter that standardizes directives, separates rules, and indents rule lines and reduce-action bodies. | ||
| - **Code Actions (Quick Fixes):** Fast diagnostic suppression actions using the `%allow` directive. | ||
| - **Hover tooltips:** Documented explanations and types for terminal tokens, non-terminal rules, keywords, and patterns. | ||
| - **Inlay Hints:** Inline type annotations and reduce action indicators. | ||
| - **Auto-Completion:** Intelligent suggestions for directives, symbols, locations, variables, and diagnostics. | ||
|
|
||
| ## Extension Settings | ||
|
|
||
| This extension contributes the following settings to control the language server behavior: | ||
|
|
||
| * `rustylr.server.command`: Path to the `rusty_lr_lsp` server binary. Leave empty to automatically detect or run from Cargo. | ||
| * `rustylr.server.args`: Arguments passed to the language server command. | ||
| * `rustylr.server.cwd`: Working directory for the language server. | ||
| * `rustylr.semanticTokens.enabled`: Toggle semantic token syntax highlighting. | ||
|
|
||
| ## Requirements | ||
|
|
||
| The language features require the `rusty_lr_lsp` server, which is part of the RustyLR cargo workspace. You can build it from the repository root: | ||
|
|
||
| ```bash | ||
| cargo build -p rusty_lr_lsp | ||
| ``` | ||
|
|
||
| By default, the extension will attempt to auto-detect the built binary in your workspace target folder or run it dynamically using Cargo. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| const fs = require("fs"); | ||
| const path = require("path"); | ||
| const vscode = require("vscode"); | ||
| const { LanguageClient, TransportKind } = require("vscode-languageclient/node"); | ||
|
|
||
| let client; | ||
| let outputChannel; | ||
| let startingClient; | ||
|
|
||
| async function activate(context) { | ||
| outputChannel = vscode.window.createOutputChannel("RustyLR LSP"); | ||
| context.subscriptions.push(outputChannel); | ||
|
|
||
| context.subscriptions.push( | ||
| vscode.commands.registerCommand("rustylr.restartServer", async () => { | ||
| await stopClient(); | ||
| try { | ||
| await startClient(context); | ||
| vscode.window.showInformationMessage("RustyLR language server restarted."); | ||
| } catch (error) { | ||
| reportStartError(error); | ||
| } | ||
| }) | ||
| ); | ||
|
|
||
| startClient(context).catch(reportStartError); | ||
| } | ||
|
|
||
| async function deactivate() { | ||
| await stopClient(); | ||
| } | ||
|
|
||
| async function startClient(context) { | ||
| if (startingClient) { | ||
| return startingClient; | ||
| } | ||
| if (client) { | ||
| return; | ||
| } | ||
|
|
||
| startingClient = doStartClient(context); | ||
| try { | ||
| await startingClient; | ||
| } finally { | ||
| startingClient = undefined; | ||
| } | ||
| } | ||
|
|
||
| async function doStartClient(context) { | ||
| const config = vscode.workspace.getConfiguration("rustylr.server"); | ||
| const workspaceFolder = | ||
| vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 | ||
| ? vscode.workspace.workspaceFolders[0].uri.fsPath | ||
| : undefined; | ||
| const repoRoot = findRustyLrRoot(workspaceFolder) || findRustyLrRoot(context.extensionPath); | ||
|
|
||
| const configuredCwd = config.get("cwd", ""); | ||
| const cwd = configuredCwd | ||
| ? expandPath(configuredCwd, { workspaceFolder, extensionPath: context.extensionPath, repoRoot }) | ||
| : repoRoot || workspaceFolder || context.extensionPath; | ||
|
|
||
| const configuredCommand = config.get("command", ""); | ||
| const configuredArgs = config.get("args", []); | ||
| const server = resolveServerCommand(configuredCommand, configuredArgs, { | ||
| workspaceFolder, | ||
| extensionPath: context.extensionPath, | ||
| repoRoot, | ||
| cwd, | ||
| }); | ||
|
|
||
| const patterns = config.get("documentPatterns", [ | ||
| "**/*.rustylr", | ||
| "**/rustylr.rs", | ||
| ]); | ||
|
|
||
| const documentSelector = [ | ||
| { scheme: "file", language: "rustylr" }, | ||
| ...patterns.map((pattern) => ({ scheme: "file", pattern })), | ||
| ]; | ||
|
|
||
| outputChannel.appendLine(`Starting RustyLR LSP: ${server.command} ${server.args.join(" ")}`); | ||
| outputChannel.appendLine(`RustyLR LSP cwd: ${cwd}`); | ||
|
|
||
| client = new LanguageClient( | ||
| "rustylr", | ||
| "RustyLR Language Server", | ||
| { | ||
| command: server.command, | ||
| args: server.args, | ||
| options: { cwd }, | ||
| transport: TransportKind.stdio, | ||
| }, | ||
| { | ||
| documentSelector, | ||
| outputChannel, | ||
| synchronize: { | ||
| configurationSection: "rustylr", | ||
| }, | ||
| } | ||
| ); | ||
|
|
||
| await client.start(); | ||
| } | ||
|
|
||
| async function stopClient() { | ||
| if (startingClient) { | ||
| try { | ||
| await startingClient; | ||
| } catch (_error) { | ||
| // The start failure will already be reported by the original caller. | ||
| } | ||
| } | ||
|
|
||
| if (!client) { | ||
| return; | ||
| } | ||
|
|
||
| const activeClient = client; | ||
| client = undefined; | ||
| try { | ||
| await activeClient.stop(); | ||
| } catch (error) { | ||
| const message = error && error.message ? error.message : String(error); | ||
| if (outputChannel) { | ||
| outputChannel.appendLine(`Ignoring RustyLR LSP stop error: ${message}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function expandPath(value, vars) { | ||
| return value | ||
| .split("${workspaceFolder}") | ||
| .join(vars.workspaceFolder || "") | ||
| .split("${extensionPath}") | ||
| .join(vars.extensionPath || "") | ||
| .split("${repoRoot}") | ||
| .join(vars.repoRoot || ""); | ||
| } | ||
|
|
||
| function resolveServerCommand(configuredCommand, configuredArgs, vars) { | ||
| if (configuredCommand) { | ||
| return { | ||
| command: expandPath(configuredCommand, vars), | ||
| args: configuredArgs.map((arg) => expandPath(arg, vars)), | ||
| }; | ||
| } | ||
|
|
||
| const binaryName = process.platform === "win32" ? "rusty_lr_lsp.exe" : "rusty_lr_lsp"; | ||
| const candidates = [ | ||
| vars.repoRoot && path.join(vars.repoRoot, "target", "debug", binaryName), | ||
| vars.repoRoot && path.join(vars.repoRoot, "target", "release", binaryName), | ||
| ].filter(Boolean); | ||
|
|
||
| for (const candidate of candidates) { | ||
| if (fs.existsSync(candidate)) { | ||
| return { command: candidate, args: [] }; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| command: "cargo", | ||
| args: ["run", "--quiet", "--package", "rusty_lr_lsp"], | ||
| }; | ||
| } | ||
|
|
||
| function findRustyLrRoot(startPath) { | ||
| if (!startPath) { | ||
| return undefined; | ||
| } | ||
|
|
||
| let current = fs.statSync(startPath).isDirectory() ? startPath : path.dirname(startPath); | ||
| while (true) { | ||
| if ( | ||
| fs.existsSync(path.join(current, "Cargo.toml")) && | ||
| fs.existsSync(path.join(current, "rusty_lr_lsp", "Cargo.toml")) | ||
| ) { | ||
| return current; | ||
| } | ||
|
|
||
| const parent = path.dirname(current); | ||
| if (parent === current) { | ||
| return undefined; | ||
| } | ||
| current = parent; | ||
| } | ||
| } | ||
|
|
||
| function reportStartError(error) { | ||
| const message = error && error.stack ? error.stack : String(error); | ||
| if (outputChannel) { | ||
| outputChannel.appendLine("Failed to start RustyLR LSP."); | ||
| outputChannel.appendLine(message); | ||
| outputChannel.show(true); | ||
| } | ||
| vscode.window.showErrorMessage("Failed to start RustyLR language server. See Output: RustyLR LSP."); | ||
| } | ||
|
|
||
| module.exports = { | ||
| activate, | ||
| deactivate, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| { | ||
| "comments": { | ||
| "lineComment": "//", | ||
| "blockComment": [ | ||
| "/*", | ||
| "*/" | ||
| ] | ||
| }, | ||
| "brackets": [ | ||
| [ | ||
| "{", | ||
| "}" | ||
| ], | ||
| [ | ||
| "[", | ||
| "]" | ||
| ], | ||
| [ | ||
| "(", | ||
| ")" | ||
| ] | ||
| ], | ||
| "autoClosingPairs": [ | ||
| { | ||
| "open": "{", | ||
| "close": "}" | ||
| }, | ||
| { | ||
| "open": "[", | ||
| "close": "]" | ||
| }, | ||
| { | ||
| "open": "(", | ||
| "close": ")" | ||
| }, | ||
| { | ||
| "open": "\"", | ||
| "close": "\"", | ||
| "notIn": [ | ||
| "string" | ||
| ] | ||
| }, | ||
| { | ||
| "open": "'", | ||
| "close": "'", | ||
| "notIn": [ | ||
| "string", | ||
| "comment" | ||
| ] | ||
| } | ||
| ], | ||
| "surroundingPairs": [ | ||
| [ | ||
| "{", | ||
| "}" | ||
| ], | ||
| [ | ||
| "[", | ||
| "]" | ||
| ], | ||
| [ | ||
| "(", | ||
| ")" | ||
| ], | ||
| [ | ||
| "\"", | ||
| "\"" | ||
| ], | ||
| [ | ||
| "'", | ||
| "'" | ||
| ] | ||
| ] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
findRustyLrRoot,fs.statSync(startPath)can throw an exception (e.g.,ENOENTor permission errors) if the path does not exist or is inaccessible. This is especially common in virtual or remote workspaces in VS Code. An unhandled exception here will prevent the extension from activating properly.We should wrap
fs.statSyncin atry-catchblock to handle this gracefully.