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

fix(vscode): vscode rename accidentally renames keyword arguments #7017

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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"
);
}
52 changes: 41 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,48 @@ 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 {
return;
};
tsuf239 marked this conversation as resolved.
Show resolved Hide resolved
// 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