-
Notifications
You must be signed in to change notification settings - Fork 382
feat: suggest removing ! from macro call that doesn't return Quoted
#6384
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
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3f0f8f3
Fix small typo in error message
asterite 6e52e22
Suggest to remove `!` at the end of the call
asterite 17ae2d5
Code action to remove `!` from call
asterite cb22862
Revert "Fix small typo in error message"
asterite b52b28e
Better hint
asterite 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
97 changes: 97 additions & 0 deletions
97
tooling/lsp/src/requests/code_action/remove_bang_from_call.rs
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,97 @@ | ||
| use lsp_types::TextEdit; | ||
| use noirc_errors::{Location, Span}; | ||
| use noirc_frontend::{node_interner::ReferenceId, QuotedType, Type}; | ||
|
|
||
| use crate::byte_span_to_range; | ||
|
|
||
| use super::CodeActionFinder; | ||
|
|
||
| impl<'a> CodeActionFinder<'a> { | ||
| pub(super) fn remove_bang_from_call(&mut self, span: Span) { | ||
| // If we can't find the referenced function, there's nothing we can do | ||
| let Some(ReferenceId::Function(func_id)) = | ||
| self.interner.find_referenced(Location::new(span, self.file)) | ||
| else { | ||
| return; | ||
| }; | ||
|
|
||
| // If the return type is Quoted, all is good | ||
| let func_meta = self.interner.function_meta(&func_id); | ||
| if let Type::Quoted(QuotedType::Quoted) = func_meta.return_type() { | ||
| return; | ||
| } | ||
|
|
||
| // The `!` comes right after the name | ||
| let byte_span = span.end() as usize..span.end() as usize + 1; | ||
| let Some(range) = byte_span_to_range(self.files, self.file, byte_span) else { | ||
| return; | ||
| }; | ||
|
|
||
| let title = "Remove `!` from call".to_string(); | ||
| let text_edit = TextEdit { range, new_text: "".to_string() }; | ||
|
|
||
| let code_action = self.new_quick_fix(title, text_edit); | ||
| self.code_actions.push(code_action); | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use tokio::test; | ||
|
|
||
| use crate::requests::code_action::tests::assert_code_action; | ||
|
|
||
| #[test] | ||
| async fn test_removes_bang_from_call() { | ||
| let title = "Remove `!` from call"; | ||
|
|
||
| let src = r#" | ||
| fn foo() {} | ||
|
|
||
| fn main() { | ||
| fo>|<o!(); | ||
| } | ||
| "#; | ||
|
|
||
| let expected = r#" | ||
| fn foo() {} | ||
|
|
||
| fn main() { | ||
| foo(); | ||
| } | ||
| "#; | ||
|
|
||
| assert_code_action(title, src, expected).await; | ||
| } | ||
|
|
||
| #[test] | ||
| async fn test_removes_bang_from_method_call() { | ||
| let title = "Remove `!` from call"; | ||
|
|
||
| let src = r#" | ||
| struct Foo {} | ||
|
|
||
| impl Foo { | ||
| fn foo(self) {} | ||
| } | ||
|
|
||
| fn bar(foo: Foo) { | ||
| foo.fo>|<o!(); | ||
| } | ||
| "#; | ||
|
|
||
| let expected = r#" | ||
| struct Foo {} | ||
|
|
||
| impl Foo { | ||
| fn foo(self) {} | ||
| } | ||
|
|
||
| fn bar(foo: Foo) { | ||
| foo.foo(); | ||
| } | ||
| "#; | ||
|
|
||
| assert_code_action(title, src, expected).await; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.