Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
Signed-off-by: xiarui.xr <[email protected]>
  • Loading branch information
amyXia1994 committed Oct 17, 2023
1 parent ed39272 commit ea4c169
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
21 changes: 12 additions & 9 deletions kclvm/tools/src/LSP/src/find_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,24 @@ pub(crate) fn find_refs<F: Fn(String) -> Result<(), anyhow::Error>>(
));
}
let def = def.unwrap();
if def.get_positions().len() != 1 {
if def.get_positions().len() > 1 {
return Err(String::from(
"Found more than one definitions, reference not supported",
));
}
let (start, end) = def.get_positions().iter().next().unwrap().clone();
let def_loc = lsp_location(start.filename.clone(), &start, &end);
// find all the refs of the def
Ok(find_refs_from_def(
vfs,
word_index_map,
def_loc,
def.get_name(),
logger,
))
if let Some(def_loc) = lsp_location(start.filename.clone(), &start, &end) {
Ok(find_refs_from_def(
vfs,
word_index_map,
def_loc,
def.get_name(),
logger,
))
} else {
Err(format!("Invalid file path: {0}", start.filename))
}
}

pub(crate) fn find_refs_from_def<F: Fn(String) -> Result<(), anyhow::Error>>(
Expand Down
10 changes: 4 additions & 6 deletions kclvm/tools/src/LSP/src/goto_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,16 +407,14 @@ fn positions_to_goto_def_resp(
0 => None,
1 => {
let (start, end) = positions.iter().next().unwrap().clone();
Some(lsp_types::GotoDefinitionResponse::Scalar(lsp_location(
start.filename.clone(),
&start,
&end,
)))
let loc = lsp_location(start.filename.clone(), &start, &end)?;
Some(lsp_types::GotoDefinitionResponse::Scalar(loc))
}
_ => {
let mut res = vec![];
for (start, end) in positions {
res.push(lsp_location(start.filename.clone(), &start, &end))
let loc = lsp_location(start.filename.clone(), &start, &end)?;
res.push(loc)
}
Some(lsp_types::GotoDefinitionResponse::Array(res))
}
Expand Down
42 changes: 19 additions & 23 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,31 +246,27 @@ pub(crate) fn handle_rename(
);
match references {
Result::Ok(locations) => {
match locations.len() {
0 => {
let _ = log("Symbol not found".to_string());
anyhow::Ok(None)
}
_ => {
// 3. return the workspaceEdit to rename all the references with the new name
let mut workspace_edit = lsp_types::WorkspaceEdit::default();
if locations.is_empty() {
let _ = log("Symbol not found".to_string());
anyhow::Ok(None)
} else {
// 3. return the workspaceEdit to rename all the references with the new name
let mut workspace_edit = lsp_types::WorkspaceEdit::default();

let changes =
locations
.into_iter()
.fold(HashMap::new(), |mut map, location| {
let uri = location.uri;
map.entry(uri.clone())
.or_insert_with(Vec::new)
.push(TextEdit {
range: location.range,
new_text: new_name.clone(),
});
map
let changes = locations
.into_iter()
.fold(HashMap::new(), |mut map, location| {
let uri = location.uri;
map.entry(uri.clone())
.or_insert_with(Vec::new)
.push(TextEdit {
range: location.range,
new_text: new_name.clone(),
});
workspace_edit.changes = Some(changes);
anyhow::Ok(Some(workspace_edit))
}
map
});
workspace_edit.changes = Some(changes);
anyhow::Ok(Some(workspace_edit))
}
}
Err(msg) => {
Expand Down
9 changes: 5 additions & 4 deletions kclvm/tools/src/LSP/src/to_lsp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ pub fn lsp_pos(pos: &KCLPos) -> Position {
}
}

pub fn lsp_location(file_path: String, start: &KCLPos, end: &KCLPos) -> Location {
Location {
uri: Url::from_file_path(file_path).unwrap(),
pub fn lsp_location(file_path: String, start: &KCLPos, end: &KCLPos) -> Option<Location> {
let uri = Url::from_file_path(file_path).ok()?;
Some(Location {
uri,
range: Range {
start: lsp_pos(start),
end: lsp_pos(end),
},
}
})
}

/// Convert KCL Message to LSP Diagnostic
Expand Down

0 comments on commit ea4c169

Please sign in to comment.