Skip to content

Commit

Permalink
fix(vscode): vscode rename accidentally renames keyword arguments (#7017
Browse files Browse the repository at this point in the history
)

fixed #7016
and added tests :) 

@eladb FYI

## Checklist

- [ ] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [ ] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
tsuf239 authored Sep 11, 2024
1 parent 3528054 commit fb736d5
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
54 changes: 54 additions & 0 deletions packages/@winglang/wingc/src/lsp/rename_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,4 +627,58 @@ test "a cat scares b cat" {
"#,
"t1"
);

test_rename_request!(
named_args,
r#"
struct A {
option: str;
//------
}
let foo = (opts: A) => {};
let option = "myOption";
foo(option: "bla");
//-----^
log(option);
"#,
"t1"
);

test_rename_request!(
named_args_renaming_struct,
r#"
struct A {
option: str;
//-----^
}
let foo = (opts: A) => {};
let option = "myOption";
foo(option: "bla");
//------
log(option);
"#,
"t1"
);

test_rename_request!(
same_name_as_named_arg,
r#"
struct A {
option: str;
}
let foo = (opts: A) => {};
let option = "myOption";
//------
foo(option: "bla");
log(option);
//-----^
"#,
"t1"
);
}
54 changes: 43 additions & 11 deletions packages/@winglang/wingc/src/lsp/rename_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use lsp_types::{Position, PrepareRenameResponse, Range, TextEdit};

use crate::diagnostic::WingLocation;
use crate::type_check::symbol_env::{LookupResult, SymbolEnv};
use crate::type_check::{SymbolKind, Types, UnsafeRef, CLASS_INFLIGHT_INIT_NAME, CLASS_INIT_NAME};
use crate::type_check::{
resolve_super_method, SymbolKind, Types, UnsafeRef, CLASS_INFLIGHT_INIT_NAME, CLASS_INIT_NAME,
};
use crate::visit::{visit_scope, Visit};
use crate::visit_context::{VisitContext, VisitorWithContext};
use crate::{ast::*, visit_context};
Expand Down Expand Up @@ -159,20 +161,50 @@ impl<'a> Visit<'a> for RenameVisitor<'a> {
}

fn visit_expr(&mut self, node: &'a Expr) {
if let ExprKind::JsonMapLiteral { fields, .. } = &node.kind {
let type_ = self.types.maybe_unwrap_inference(self.types.get_expr_type(node));
let type_ = *if let Some(type_) = self.types.get_type_from_json_cast(node.id) {
*type_
} else {
type_
match &node.kind {
ExprKind::JsonMapLiteral { fields, .. } => {
let type_ = self.types.maybe_unwrap_inference(self.types.get_expr_type(node));
let type_ = *if let Some(type_) = self.types.get_type_from_json_cast(node.id) {
*type_
} else {
type_
}
.maybe_unwrap_option();

if let Some(c) = type_.as_struct() {
for (field, ..) in fields {
self.add_reference_symbol(field, Some(&UnsafeRef::from(&c.env)));
}
}
}
.maybe_unwrap_option();
ExprKind::Call { arg_list, callee } => {
let Some(env) = self.ctx.current_env() else {
// usually the env will exist, if not- we cannot use it to resolve the super method.
// it's the same as used here: https://github.com/winglang/wing/blob/main/packages/@winglang/wingc/src/lsp/symbol_locator.rs#L374-L376
return;
};
// we need to get the struct from the callee - to get the right env
let callee_type = match callee {
CalleeKind::Expr(expr) => self.types.get_expr_type(expr),
CalleeKind::SuperCall(method) => resolve_super_method(method, &env, &self.types)
.ok()
.map_or(self.types.error(), |t| t.0),
}
.maybe_unwrap_option()
.to_owned();

if let Some(c) = type_.as_struct() {
for (field, ..) in fields {
self.add_reference_symbol(field, Some(&UnsafeRef::from(&c.env)));
if let Some(func) = callee_type.as_function_sig() {
if let Some(arg) = func.parameters.last() {
let struct_type = arg.typeref.maybe_unwrap_option().as_struct();
if let Some(s) = struct_type {
for (arg, ..) in &arg_list.named_args {
self.add_reference_symbol(arg, Some(&UnsafeRef::from(&s.env)));
}
}
}
}
}
_ => {}
}
crate::visit::visit_expr(self, node);
}
Expand Down

0 comments on commit fb736d5

Please sign in to comment.