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

[move][move-ide] Swap move-analyzer to use loc #18345

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Replace most things with Loc in the IDE
  • Loading branch information
cgswords committed Jun 20, 2024
commit 28a09d8dcddfb67dfc3c5ac2cb2db07587857426
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use crate::{
compiler_info::CompilerInfo,
symbols::{
add_fun_use_def, add_struct_use_def, def_info_to_type_def_loc,
expansion_mod_ident_to_map_key, type_def_loc, DefInfo, LocalDef, ModuleDefs,
UseDef, UseDefMap, UseLoc, References, DefMap,
expansion_mod_ident_to_map_key, type_def_loc, DefInfo, DefMap, LocalDef, ModuleDefs,
References, UseDef, UseDefMap, UseLoc,
},
utils::{ignored_function, loc_start_to_lsp_position_opt},
};
Expand All @@ -16,7 +16,11 @@ use move_compiler::{
expansion::ast::{self as E, ModuleIdent},
naming::ast as N,
parser::ast as P,
shared::{files::{self, FilePosition, MappedFiles}, ide::MacroCallInfo, Identifier, Name},
shared::{
files::{self, FilePosition, MappedFiles},
ide::MacroCallInfo,
Identifier, Name,
},
typing::{
ast as T,
visitor::{LValueKind, TypingVisitorContext},
Expand Down Expand Up @@ -145,10 +149,7 @@ impl TypingAnalysisContext<'_> {
};
if let Some(const_def) = mod_defs.constants.get(use_name) {
let def_fhash = self.mod_outer_defs.get(&mod_ident_str).unwrap().fhash;
let const_info = self
.def_info
.get(&const_def.name_loc)
.unwrap();
let const_info = self.def_info.get(&const_def.name_loc).unwrap();
let ident_type_def_loc = def_info_to_type_def_loc(self.mod_outer_defs, const_info);
self.use_defs.insert(
name_start.position.line_offset() as u32,
Expand Down Expand Up @@ -206,10 +207,8 @@ impl TypingAnalysisContext<'_> {
ident_type_def_loc,
),
);
self.def_info.insert(
*loc,
DefInfo::Local(*name, def_type, with_let, mutable),
);
self.def_info
.insert(*loc, DefInfo::Local(*name, def_type, with_let, mutable));
}

/// Add a use for and identifier whose definition is expected to be local to a function, and
Expand Down Expand Up @@ -384,10 +383,7 @@ impl TypingAnalysisContext<'_> {
for fdef in &def.field_defs {
if fdef.name == *use_name {
let def_fhash = self.mod_outer_defs.get(&mod_ident_str).unwrap().fhash;
let field_info = self
.def_info
.get(&fdef.loc)
.unwrap();
let field_info = self.def_info.get(&fdef.loc).unwrap();
let ident_type_def_loc =
def_info_to_type_def_loc(self.mod_outer_defs, field_info);
self.use_defs.insert(
Expand Down Expand Up @@ -473,10 +469,7 @@ impl<'a> TypingVisitorContext for TypingAnalysisContext<'a> {
let file_hash = struct_name.loc().file_hash();
// enter self-definition for struct name (unwrap safe - done when inserting def)
let name_start = self.file_start_position(&struct_name.loc());
let struct_info = self
.def_info
.get(&struct_name.loc())
.unwrap();
let struct_info = self.def_info.get(&struct_name.loc()).unwrap();
let struct_type_def = def_info_to_type_def_loc(self.mod_outer_defs, struct_info);
self.use_defs.insert(
name_start.line,
Expand Down Expand Up @@ -556,10 +549,7 @@ impl<'a> TypingVisitorContext for TypingAnalysisContext<'a> {
let loc = constant_name.loc();
// enter self-definition for const name (unwrap safe - done when inserting def)
let name_start = self.file_start_position(&loc);
let const_info = self
.def_info
.get(&loc)
.unwrap();
let const_info = self.def_info.get(&loc).unwrap();
let ident_type_def_loc = def_info_to_type_def_loc(self.mod_outer_defs, const_info);
self.use_defs.insert(
name_start.line,
Expand Down Expand Up @@ -589,10 +579,7 @@ impl<'a> TypingVisitorContext for TypingAnalysisContext<'a> {
let loc = function_name.loc();
// first, enter self-definition for function name (unwrap safe - done when inserting def)
let name_start = self.file_start_position(&loc);
let fun_info = self
.def_info
.get(&loc)
.unwrap();
let fun_info = self.def_info.get(&loc).unwrap();
let fun_type_def = def_info_to_type_def_loc(self.mod_outer_defs, fun_info);
let use_def = UseDef::new(
self.references,
Expand Down
13 changes: 11 additions & 2 deletions external-crates/move/crates/move-analyzer/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,8 +441,17 @@ fn context_specific_no_trigger(
}

/// Checks if a use at a given position is also a definition.
fn is_definition(symbols: &Symbols, use_line: u32, use_col: u32, use_fhash: FileHash, def_loc: Loc) -> bool {
if let Some(use_loc) = symbols.files.line_char_offset_to_loc_opt(use_fhash, use_line, use_col) {
fn is_definition(
symbols: &Symbols,
use_line: u32,
use_col: u32,
use_fhash: FileHash,
def_loc: Loc,
) -> bool {
if let Some(use_loc) = symbols
.files
.line_char_offset_to_loc_opt(use_fhash, use_line, use_col)
{
// TODO: is overlapping better?
def_loc.contains(&use_loc)
} else {
Expand Down
7 changes: 5 additions & 2 deletions external-crates/move/crates/move-analyzer/src/inlay_hints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ use lsp_types::{
InlayHintTooltip, Position,
};

use move_compiler::{naming::ast as N, shared::{files::FilePosition, Identifier}};
use move_compiler::{
naming::ast as N,
shared::{files::FilePosition, Identifier},
};

/// Handles inlay hints request of the language server
pub fn on_inlay_hint_request(context: &Context, request: &Request, symbols: &Symbols) {
Expand All @@ -33,7 +36,7 @@ pub fn on_inlay_hint_request(context: &Context, request: &Request, symbols: &Sym
if let Some(DefInfo::Local(n, t, _, _)) = symbols.def_info(untyped_def_loc) {
let position = Position {
line: start_position.line_offset() as u32,
character: start_position.column_offset() as u32 + n.len() as u32,
character: start_position.column_offset() as u32 + n.len() as u32,
};
let colon_label = InlayHintLabelPart {
value: ": ".to_string(),
Expand Down
91 changes: 50 additions & 41 deletions external-crates/move/crates/move-analyzer/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ use move_compiler::{
naming::ast::{StructFields, Type, TypeName_, Type_},
parser::ast::{self as P},
shared::{
files::{self, FilePosition, MappedFiles, FileId}, unique_map::UniqueMap, Identifier, Name, NamedAddressMap,
NamedAddressMaps,
files::{self, FileId, FilePosition, MappedFiles},
unique_map::UniqueMap,
Identifier, Name, NamedAddressMap, NamedAddressMaps,
},
typing::{
ast::{Exp, ExpListItem, ModuleDefinition, SequenceItem, SequenceItem_, UnannotatedExp_},
Expand Down Expand Up @@ -1721,7 +1722,7 @@ fn get_mod_outer_defs(
} else {
Visibility::Internal
};
let doc_string = extract_doc_string( files, file_id_to_lines, &name_loc);
let doc_string = extract_doc_string(files, file_id_to_lines, &name_loc);
def_info.insert(
name_loc,
DefInfo::Struct(
Expand Down Expand Up @@ -1750,7 +1751,7 @@ fn get_mod_outer_defs(

for (name_loc, name, c) in &mod_def.constants {
constants.insert(*name, ConstDef { name_loc });
let doc_string = extract_doc_string( files, file_id_to_lines, &name_loc,);
let doc_string = extract_doc_string(files, file_id_to_lines, &name_loc);
def_info.insert(
name_loc,
DefInfo::Const(
Expand All @@ -1774,7 +1775,7 @@ fn get_mod_outer_defs(
} else {
FunType::Regular
};
let doc_string = extract_doc_string( files, file_id_to_lines, &name_loc);
let doc_string = extract_doc_string(files, file_id_to_lines, &name_loc);
let fun_info = DefInfo::Function(
mod_ident.value,
fun.visibility,
Expand Down Expand Up @@ -2396,9 +2397,7 @@ pub fn add_fun_use_def(
};
if let Some(func_def) = mod_defs.functions.get(fun_def_name) {
let def_fhash = mod_outer_defs.get(&mod_ident_str).unwrap().fhash;
let fun_info = def_info
.get(&func_def.name_loc)
.unwrap();
let fun_info = def_info.get(&func_def.name_loc).unwrap();
let ident_type_def_loc = def_info_to_type_def_loc(mod_outer_defs, fun_info);
let ud = UseDef::new(
references,
Expand Down Expand Up @@ -2434,9 +2433,7 @@ pub fn add_struct_use_def(
};
if let Some(def) = mod_defs.structs.get(use_name) {
let def_fhash = mod_outer_defs.get(&mod_ident_str).unwrap().fhash;
let struct_info = def_info
.get(&def.name_loc)
.unwrap();
let struct_info = def_info.get(&def.name_loc).unwrap();
let ident_type_def_loc = def_info_to_type_def_loc(mod_outer_defs, struct_info);
let ud = UseDef::new(
references,
Expand Down Expand Up @@ -2681,36 +2678,37 @@ pub fn on_references_request(context: &Context, request: &Request, symbols: &Sym
|u| {
let def_posn = symbols.files.file_start_position_opt(&u.def_loc)?;
match symbols.references.get(&u.def_loc) {
Some(s) => {
let mut locs = vec![];

for ref_loc in s {
if include_decl
|| !(Into::<Position>::into(def_posn.position) == ref_loc.start && def_posn.file_hash == ref_loc.fhash)
{
let end_pos = Position {
line: ref_loc.start.line,
character: ref_loc.col_end,
};
let range = Range {
start: ref_loc.start,
end: end_pos,
};
let path = symbols.files.file_path(&ref_loc.fhash);
locs.push(Location {
uri: Url::from_file_path(path).unwrap(),
range,
});
Some(s) => {
let mut locs = vec![];

for ref_loc in s {
if include_decl
|| !(Into::<Position>::into(def_posn.position) == ref_loc.start
&& def_posn.file_hash == ref_loc.fhash)
{
let end_pos = Position {
line: ref_loc.start.line,
character: ref_loc.col_end,
};
let range = Range {
start: ref_loc.start,
end: end_pos,
};
let path = symbols.files.file_path(&ref_loc.fhash);
locs.push(Location {
uri: Url::from_file_path(path).unwrap(),
range,
});
}
}
if locs.is_empty() {
Some(serde_json::to_value(Option::<lsp_types::Location>::None).unwrap())
} else {
Some(serde_json::to_value(locs).unwrap())
}
}
if locs.is_empty() {
Some(serde_json::to_value(Option::<lsp_types::Location>::None).unwrap())
} else {
Some(serde_json::to_value(locs).unwrap())
}
None => Some(serde_json::to_value(Option::<lsp_types::Location>::None).unwrap()),
}
None => Some(serde_json::to_value(Option::<lsp_types::Location>::None).unwrap()),
}
},
);
}
Expand Down Expand Up @@ -2913,7 +2911,11 @@ pub fn on_document_symbol_request(context: &Context, request: &Request, symbols:

/// Helper function to handle struct fields
#[allow(deprecated)]
fn handle_struct_fields(struct_def: StructDef, fields: &mut Vec<DocumentSymbol>, symbols: &Symbols) {
fn handle_struct_fields(
struct_def: StructDef,
fields: &mut Vec<DocumentSymbol>,
symbols: &Symbols,
) {
let cloned_fileds = struct_def.field_defs;

for field_def in cloned_fileds {
Expand Down Expand Up @@ -2971,9 +2973,16 @@ fn assert_use_def_with_doc_string(
def_line
);
assert!(
symbols.files.start_position(&use_def.def_loc).column_offset() as u32 == def_col,
symbols
.files
.start_position(&use_def.def_loc)
.column_offset() as u32
== def_col,
"'{}' != '{}' for use in column {use_col} of line {use_line} in file {use_file}",
symbols.files.start_position(&use_def.def_loc).column_offset(),
symbols
.files
.start_position(&use_def.def_loc)
.column_offset(),
def_col
);
assert!(
Expand Down