✨ feat(lsp): improve goto definition to navigate to correct source files #1221
Merged
✨ feat(lsp): improve goto definition to navigate to correct source files #1221
Conversation
- Add `url_by_source()` method to HIR for retrieving URLs by source ID - Add `get_module_path()` to ModuleLoader and `get_path()` to ModuleResolver trait - Update goto_definition to navigate to the symbol's actual source file - Return None for builtin symbols (no file to navigate to) - Add `-M/--module-path` CLI option to LSP for custom module search paths - Add comprehensive tests for the new functionality
Contributor
There was a problem hiding this comment.
Pull request overview
This PR enhances the LSP's goto definition feature to correctly navigate to the actual source files where symbols are defined, including support for module imports. The implementation adds module path resolution and properly handles builtin symbols that have no file to navigate to.
Changes:
- Added
get_pathmethod toModuleResolvertrait to retrieve the absolute file path of a module - Updated goto definition to resolve the correct target URL based on the symbol's source file instead of always using the current file
- Added CLI support for configuring module search paths via the
--module-pathflag
Reviewed changes
Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| crates/mq-lang/src/module/resolver.rs | Adds get_path method to ModuleResolver trait for retrieving module file paths |
| crates/mq-lang/src/module.rs | Exposes get_module_path method in ModuleLoader to access module paths |
| crates/mq-wasm/src/script.rs | Implements get_path for WASM module resolver (returns module name as-is) |
| crates/mq-hir/src/hir.rs | Adds url_by_source method and updates module resolution to use actual file paths |
| crates/mq-lsp/src/goto_definition.rs | Updates goto definition to navigate to the correct source file using symbol's source_id |
| crates/mq-lsp/src/main.rs | Adds CLI argument parsing for module paths using clap |
| crates/mq-lsp/Cargo.toml | Adds clap dependency with derive feature |
| crates/mq-formatter/src/formatter.rs | Changes unused token variable to underscore |
Co-authored-by: Copilot <[email protected]>
|
|
||
| fn get_path(&self, module_name: &str) -> Result<String, ModuleError> { | ||
| let file_path = search(module_name, &self.paths)?; | ||
| Ok(file_path.to_str().unwrap_or_default().to_string()) |
There was a problem hiding this comment.
When to_str() returns None (invalid UTF-8 path), this silently returns an empty string. Consider returning a more descriptive error that explains the path could not be converted to UTF-8, which would help users debug path-related issues.
Suggested change
| Ok(file_path.to_str().unwrap_or_default().to_string()) | |
| let path_str = file_path.to_str().ok_or_else(|| { | |
| ModuleError::IOError(Cow::Owned(format!( | |
| "Module path is not valid UTF-8: {:?}", | |
| file_path | |
| ))) | |
| })?; | |
| Ok(path_str.to_string()) |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
#1204