-
Notifications
You must be signed in to change notification settings - Fork 219
Add code fix for double literal without dot #3352
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
Open
amcasey
wants to merge
7
commits into
main
Choose a base branch
from
amcasey/i2d
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+312
−20
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c68a2eb
Add code fix for double literal without dot
amcasey 70c6193
Apply copilot suggestions
amcasey e656c2c
Handle unary operators
amcasey d105ace
Also check ranges in wrap_in_array tests
amcasey e363c92
Handle multiple unary operators
amcasey c24ca09
Use some rust syntax I didn't know about
amcasey e516c8d
Correct comment
amcasey 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
121 changes: 121 additions & 0 deletions
121
source/language_service/src/code_action/int_to_double.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,121 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| //! Code action: "Convert integer literal to double" | ||
| //! Detects when an integer literal is passed where a double is expected and | ||
| //! offers to add a trailing `.` to make it into a double literal. | ||
|
|
||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
||
| use qsc::{ | ||
| Span, | ||
| ast::{self, Expr, ExprKind, UnOp}, | ||
| compile::{ErrorKind, TyInfoKind}, | ||
| hir::ty::Prim, | ||
| line_column::Encoding, | ||
| }; | ||
|
|
||
| use super::is_error_relevant; | ||
| use crate::{ | ||
| compilation::Compilation, | ||
| protocol::{CodeAction, CodeActionKind, TextEdit, WorkspaceEdit}, | ||
| qsc_utils::into_range, | ||
| }; | ||
|
|
||
| pub(crate) fn int_to_double_fixes( | ||
| compilation: &Compilation, | ||
| source_name: &str, | ||
| span: Span, | ||
| encoding: Encoding, | ||
| ) -> Vec<CodeAction> { | ||
| let mut code_actions = Vec::new(); | ||
|
|
||
| let unit = compilation.user_unit(); | ||
| let package = &unit.ast.package; | ||
| let source_map = &unit.sources; | ||
|
|
||
| let ty_mismatches = compilation | ||
| .compile_errors | ||
| .iter() | ||
| .filter(|error| is_error_relevant(error, span)) | ||
| .filter_map(|error| match error.error() { | ||
| ErrorKind::Frontend(frontend_error) => frontend_error.ty_mismatch(), | ||
| _ => None, | ||
| }); | ||
|
|
||
| for (expected, actual, error_span) in ty_mismatches { | ||
| // Check if expected is Double and actual is Int. | ||
| if matches!(&expected.kind, TyInfoKind::Prim(Prim::Double)) | ||
| && matches!(&actual.kind, TyInfoKind::Prim(Prim::Int)) | ||
| { | ||
| // Confirm that it's a literal and not just some expression of type int | ||
| let Some(mut expr) = find_expr_at(package, error_span) else { | ||
| continue; | ||
| }; | ||
|
|
||
| // Strip off any + or - unary operators | ||
| while let ExprKind::UnOp(UnOp::Pos | UnOp::Neg, inner) = expr.kind.as_ref() { | ||
| expr = inner; | ||
| } | ||
|
|
||
| if !matches!(expr.kind.as_ref(), ExprKind::Lit(_)) { | ||
| continue; | ||
| } | ||
|
|
||
| // Generate the fix: add a trailing `.` | ||
| // Note that this depends on the error span excluding surrounding parens | ||
| // so we don't end up with something like `(q).`. | ||
| let dot_range = into_range( | ||
| encoding, | ||
| Span { | ||
| lo: error_span.hi, | ||
| hi: error_span.hi, | ||
| }, | ||
| source_map, | ||
| ); | ||
|
|
||
| code_actions.push(CodeAction { | ||
| title: "Convert to double literal".to_string(), | ||
| edit: Some(WorkspaceEdit { | ||
| changes: vec![( | ||
| source_name.to_string(), | ||
| vec![TextEdit { | ||
| new_text: ".".to_string(), | ||
| range: dot_range, | ||
| }], | ||
| )], | ||
| }), | ||
| kind: Some(CodeActionKind::QuickFix), | ||
| is_preferred: Some(true), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| code_actions | ||
| } | ||
|
|
||
| /// Finds the AST expression whose span exactly matches `target`. | ||
| fn find_expr_at(package: &ast::Package, target: Span) -> Option<&ast::Expr> { | ||
| let mut finder = ExprSpanFinder { | ||
| target, | ||
| found: None, | ||
| }; | ||
| ast::visit::Visitor::visit_package(&mut finder, package); | ||
| finder.found | ||
| } | ||
|
|
||
| struct ExprSpanFinder<'a> { | ||
| target: Span, | ||
| found: Option<&'a ast::Expr>, | ||
| } | ||
|
|
||
| impl<'a> ast::visit::Visitor<'a> for ExprSpanFinder<'a> { | ||
| fn visit_expr(&mut self, expr: &'a Expr) { | ||
| if expr.span == self.target { | ||
| self.found = Some(expr); | ||
| } else if self.target.intersection(&expr.span).is_some() { | ||
| ast::visit::walk_expr(self, expr); | ||
| } | ||
| } | ||
| } | ||
157 changes: 157 additions & 0 deletions
157
source/language_service/src/code_action/int_to_double/tests.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,157 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| use crate::{ | ||
| code_action, | ||
| test_utils::{compile_project_with_markers_no_cursor, whole_document_range}, | ||
| }; | ||
| use qsc::{line_column::Encoding, location::Location}; | ||
|
|
||
| fn expect_single<T: std::fmt::Debug>(items: &[T]) -> &T { | ||
| let [item] = items else { | ||
| panic!("expected a single item, got: {items:?}"); | ||
| }; | ||
| item | ||
| } | ||
|
|
||
| fn get_int_to_double_actions(source: &str) -> (Vec<Location>, Vec<crate::protocol::CodeAction>) { | ||
| let (compilation, targets) = | ||
| compile_project_with_markers_no_cursor(&[("<source>", source)], false); | ||
| let range = whole_document_range(source); | ||
| let actions = code_action::get_code_actions(&compilation, "<source>", range, Encoding::Utf8); | ||
| ( | ||
| targets, | ||
| actions | ||
| .into_iter() | ||
| .filter(|a| a.title == "Convert to double literal") | ||
| .collect(), | ||
| ) | ||
| } | ||
|
|
||
| #[test] | ||
| fn int_literal_to_double() { | ||
| let source = "namespace A { | ||
| function Foo(d: Double) : Unit { | ||
| Foo(1◉◉); | ||
| } | ||
| } | ||
| "; | ||
| let (locations, actions) = get_int_to_double_actions(source); | ||
| let action = expect_single(&actions); | ||
| let edit = action.edit.as_ref().expect("expected edit"); | ||
| let (_, text_edits) = expect_single(&edit.changes); | ||
| let text_edit = expect_single(text_edits); | ||
| let location = expect_single(&locations); | ||
| assert_eq!(text_edit.range, location.range); | ||
| assert_eq!(text_edit.new_text, "."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn int_literal_to_double_with_parens() { | ||
| let source = "namespace A { | ||
| function Foo(d: Double) : Unit { | ||
| Foo((1◉◉)); | ||
| } | ||
| } | ||
| "; | ||
| let (locations, actions) = get_int_to_double_actions(source); | ||
| let action = expect_single(&actions); | ||
| let edit = action.edit.as_ref().expect("expected edit"); | ||
| let (_, text_edits) = expect_single(&edit.changes); | ||
| let text_edit = expect_single(text_edits); | ||
| let location = expect_single(&locations); | ||
| assert_eq!(text_edit.range, location.range); | ||
| assert_eq!(text_edit.new_text, "."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn int_literal_to_double_with_pos() { | ||
| let source = "namespace A { | ||
| function Foo(d: Double) : Unit { | ||
| Foo((+1◉◉)); | ||
| } | ||
| } | ||
| "; | ||
| let (locations, actions) = get_int_to_double_actions(source); | ||
| let action = expect_single(&actions); | ||
| let edit = action.edit.as_ref().expect("expected edit"); | ||
| let (_, text_edits) = expect_single(&edit.changes); | ||
| let text_edit = expect_single(text_edits); | ||
| let location = expect_single(&locations); | ||
| assert_eq!(text_edit.range, location.range); | ||
| assert_eq!(text_edit.new_text, "."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn int_literal_to_double_with_neg() { | ||
| let source = "namespace A { | ||
| function Foo(d: Double) : Unit { | ||
| Foo((-1◉◉)); | ||
| } | ||
| } | ||
| "; | ||
| let (locations, actions) = get_int_to_double_actions(source); | ||
| let action = expect_single(&actions); | ||
| let edit = action.edit.as_ref().expect("expected edit"); | ||
| let (_, text_edits) = expect_single(&edit.changes); | ||
| let text_edit = expect_single(text_edits); | ||
| let location = expect_single(&locations); | ||
| assert_eq!(text_edit.range, location.range); | ||
| assert_eq!(text_edit.new_text, "."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn int_literal_to_double_with_neg_neg() { | ||
| let source = "namespace A { | ||
| function Foo(d: Double) : Unit { | ||
| Foo((--1◉◉)); | ||
| } | ||
| } | ||
| "; | ||
| let (locations, actions) = get_int_to_double_actions(source); | ||
| let action = expect_single(&actions); | ||
| let edit = action.edit.as_ref().expect("expected edit"); | ||
| let (_, text_edits) = expect_single(&edit.changes); | ||
| let text_edit = expect_single(text_edits); | ||
| let location = expect_single(&locations); | ||
| assert_eq!(text_edit.range, location.range); | ||
| assert_eq!(text_edit.new_text, "."); | ||
| } | ||
|
|
||
| #[test] | ||
| fn int_literal_to_double_with_notb() { | ||
| let source = "namespace A { | ||
| function Foo(d: Double) : Unit { | ||
| Foo((~~~1)); | ||
| } | ||
| } | ||
| "; | ||
| let (_, actions) = get_int_to_double_actions(source); | ||
| assert_eq!(actions.len(), 0, "Expected 0 actions, got: {actions:?}"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn int_local_to_double() { | ||
| let source = "namespace A { | ||
| function Foo(d: Double) : Unit { | ||
| let x = 1; | ||
| Foo((x)); | ||
| } | ||
| } | ||
| "; | ||
| let (_, actions) = get_int_to_double_actions(source); | ||
| assert_eq!(actions.len(), 0, "Expected 0 actions, got: {actions:?}"); | ||
| } | ||
|
|
||
| // We'd like this to work but the TyMismatch flags the whole array, not the elements | ||
| #[test] | ||
| fn int_array_to_double() { | ||
| let source = "namespace A { | ||
| function Foo(d: Double[]) : Unit { | ||
| Foo([1,2,3]); | ||
| } | ||
| } | ||
| "; | ||
| let (_, actions) = get_int_to_double_actions(source); | ||
| assert_eq!(actions.len(), 0, "Expected 0 actions, got: {actions:?}"); | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.