Skip to content
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

LSP: Allow unqualifying constructor when hovering over the module name bit #4197

Merged
merged 2 commits into from
Jan 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,17 @@

([Surya Rose](https://github.com/GearsDatapacks))

- `Unqualify` Action now get triggered when hovering over the module name
for record value constructor.
For example:

```gleam
pub fn main() {
let my_option = option.Some(1)
// ^ would trigger "Unqualify option.Some"
}
```
([Jiangda Wang](https://github.com/Frank-III))

### Formatter

Expand Down
9 changes: 8 additions & 1 deletion compiler-core/src/language_server/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1367,7 +1367,14 @@ impl<'ast> ast::visit::Visit<'ast> for QualifiedToUnqualifiedImportFirstPass<'as
module_alias: &'ast EcoString,
constructor: &'ast ModuleValueConstructor,
) {
let range = src_span_to_lsp_range(*location, &self.line_numbers);
// When hovering over a Record Value Constructor, we want to expand the source span to
// include the module name:
// option.Some
// ↑
// This allows us to offer a code action when hovering over the module name.
let expanded_location =
SrcSpan::new(location.start - module_name.len() as u32, location.end);
let range = src_span_to_lsp_range(expanded_location, &self.line_numbers);
if overlaps(self.params.range, range) {
if let ModuleValueConstructor::Record {
name: constructor_name,
Expand Down
17 changes: 17 additions & 0 deletions compiler-core/src/language_server/tests/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2107,6 +2107,23 @@ pub fn identity(x: wobble.Wobble) -> wobble.Wobble {
);
}

#[test]
fn test_qualified_to_unqualified_record_value_constructor_module_name() {
let src = r#"
import option

pub fn main() {
option.Some(1)
}
"#;
assert_code_action!(
"Unqualify option.Some",
TestProject::for_source(src)
.add_hex_module("option", "pub type Option(v) { Some(v) None }"),
find_position_of("option").nth_occurrence(2).to_selection()
);
}

#[test]
fn test_qualified_to_unqualified_import_basic_multiple() {
let src = r#"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: compiler-core/src/language_server/tests/action.rs
expression: "\nimport option\n\npub fn main() {\n option.Some(1)\n}\n"
---
----- BEFORE ACTION

import option

pub fn main() {
option.Some(1)
}


----- AFTER ACTION

import option.{Some}

pub fn main() {
Some(1)
}
Loading