Skip to content

Commit

Permalink
support goto definition for typepara fun
Browse files Browse the repository at this point in the history
  • Loading branch information
hapeeeeee committed Jan 22, 2024
1 parent 294efba commit 705649a
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 24 deletions.
8 changes: 4 additions & 4 deletions language/move-analyzer/editors/code/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { Context } from './context';
import { Extension } from './extension';
import { log } from './log';
import { Reg } from './reg';
import * as commands from './commands';
// import * as commands from './commands';

import * as vscode from 'vscode';

Expand Down Expand Up @@ -47,9 +47,9 @@ export async function activate(
}

// context.registerCommand('textDocumentDocumentSymbol', commands.textDocumentDocumentSymbol);
context.registerCommand('textDocumentHover', commands.textDocumentHover);
context.registerCommand('textDocumentCompletion', commands.textDocumentCompletion);
context.registerCommand('textDocumentDefinition', commands.textDocumentDefinition);
// context.registerCommand('textDocumentHover', commands.textDocumentHover);
// context.registerCommand('textDocumentCompletion', commands.textDocumentCompletion);
// context.registerCommand('textDocumentDefinition', commands.textDocumentDefinition);

const d = vscode.languages.registerInlayHintsProvider(
{ scheme: 'file', language: 'move' },
Expand Down
61 changes: 43 additions & 18 deletions language/move-analyzer/src/goto_definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ pub fn on_go_to_def_request(context: &Context, request: &Request) -> lsp_server:
handler.addrname_2_addrnum = project.addrname_2_addrnum.clone();
// handler.addrname_2_addrnum = project.addrname_2_addrnum.clone();
project.run_visitor_for_file(&mut handler, &fpath, String::default());

handler.remove_not_in_loc(&project.global_env);

let locations = handler.convert_to_locations();

let r = Response::new_ok(
request.id.clone(),
serde_json::to_value(GotoDefinitionResponse::Array(locations)).unwrap(),
Expand Down Expand Up @@ -198,6 +202,43 @@ impl Handler {
self.mouse_span = codespan::Span::new(mouse_line_first_col.span().start(), mouse_line_last_col.span().start());
}

fn remove_not_in_loc(&mut self, env: &GlobalEnv) {
let mut res_capture_items_span = vec![];
let mut res_result_candidates = vec![];
let mut indexes_to_retain = vec![];
for index in 0..self.capture_items_span.len() {
if let Some(file_id) = crate::utils::get_file_id_by_fpath_in_all_modules(env, &self.filepath) {
let capture_span = self.capture_items_span.get(index).unwrap();
let span_loc = move_model::model::Loc::new(
file_id,
codespan::Span::new(
capture_span.start(),
capture_span.end(),
),
);
if crate::move_generate_spec_sel::ReqParametersPath::is_linecol_in_loc(
self.line, self.col, &span_loc, env
) {
indexes_to_retain.push(index)
}
}
}

for (index, span) in self.capture_items_span.iter().enumerate() {
if indexes_to_retain.contains(&index) {
res_capture_items_span.push(*span);
}
}
self.capture_items_span = res_capture_items_span;

for (index, file_range) in self.result_candidates.iter().enumerate() {
if indexes_to_retain.contains(&index) {
res_result_candidates.push(file_range.clone());
}
}
self.result_candidates = res_result_candidates;
}

fn process_use_decl(&mut self, env: &GlobalEnv) {
log::trace!("process_use_decl =======================================\n\n");
let target_module = env.get_module(self.target_module_id);
Expand Down Expand Up @@ -979,23 +1020,7 @@ impl Handler {
_speck_block: &Spec,
) {
log::trace!("\n\n");
// let target_module = env.get_module(self.target_module_id);
// for spec_block_info in target_module.get_spec_block_infos() {
// log::info!("lll >> process_spec_block loc = {:?}", env.get_location(capture_items_loc));
// log::info!("lll >> spec_block_info.loc = {:?}", env.get_location(&spec_block_info.loc));
// if spec_block_info.loc == *capture_items_loc {
// self.print_spec_block_info(spec_block_info);
// }
// if let SpecBlockTarget::Function(..) = spec_block_info.target {
// let mut spec_source = env.get_source(capture_items_loc);
// log::info!("lll >> capture_items_loc spec_source = {:?}", spec_source);

// spec_source = env.get_source(&spec_block_info.loc);
// log::info!("lll >> spec_block_info spec_source = {:?}", spec_source);
// // self.print_spec_block_info(spec_block_info);
// }
// log::info!("lll >> spec_block_info loc = {:?}", env.get_file_and_location(&spec_block_info.loc));
// }

log::trace!("lll >> process_spec_block loc = {:?}", env.get_file_and_location(capture_items_loc));
log::trace!("lll << process_spec_block loc");
}
Expand Down Expand Up @@ -1171,7 +1196,7 @@ impl std::fmt::Display for Handler {
pub fn find_smallest_length_index(spans: &[codespan::Span]) -> Option<usize> {
let mut smallest_length = i64::MAX;
let mut smallest_index = None;

for (index, span) in spans.iter().enumerate() {
let length = span.end() - span.start();
if length.0 < smallest_length {
Expand Down
2 changes: 1 addition & 1 deletion language/move-analyzer/src/move_generate_spec_sel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl GetPosition for ReqParametersPath {
}

impl ReqParametersPath {
fn is_linecol_in_loc(line :u32, col :u32, loc : &Loc, env:&GlobalEnv) -> bool {
pub fn is_linecol_in_loc(line :u32, col :u32, loc : &Loc, env:&GlobalEnv) -> bool {
let start_location = match env.get_location(loc) {
Some(x) => x,
None => return false,
Expand Down
19 changes: 18 additions & 1 deletion language/move-analyzer/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

use codespan_reporting::files::{Files, SimpleFiles};
use codespan::FileId;
use lsp_types::{Command, Location, Position};
use move_command_line_common::files::FileHash;
use move_ir_types::location::*;
Expand Down Expand Up @@ -430,6 +431,21 @@ pub fn get_modules_by_fpath_in_all_modules<'a>(env: &'a GlobalEnv, fpath: &Path)
result_vec_modules
}

pub fn get_file_id_by_fpath_in_all_modules<'a>(env: &'a GlobalEnv, fpath: &Path) -> Option<FileId> {
let mut result_file_id = Default::default();
for module_env in env.get_modules() {
if fpath_str_is_equal(
&env.get_file(module_env.get_loc().file_id()).to_string_lossy().to_string(),
&fpath.to_string_lossy().to_string()
) {
result_file_id = Some(module_env.get_loc().file_id());
}

}
result_file_id
}


pub fn collect_use_decl(addrname_2_addrnum :&std::collections::HashMap<String, String>, module_env: &ModuleEnv, global_env: &GlobalEnv) -> HashMap<ModuleName, Vec<SpecSymbol>> {
let mut result: HashMap<ModuleName, Vec<SpecSymbol>> = Default::default();
for using_decl in module_env.get_use_decls() {
Expand Down Expand Up @@ -462,4 +478,5 @@ pub fn collect_use_decl(addrname_2_addrnum :&std::collections::HashMap<String, S
result.insert(addr_addrnum_with_module_name, using_decl_member);
}
result
}
}

0 comments on commit 705649a

Please sign in to comment.