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

feat: rename symbol #773

Merged
merged 4 commits into from
Oct 17, 2023
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
1 change: 1 addition & 0 deletions kclvm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ log = "0.4.14"
im-rc = "15.0.0"
rustc_lexer = "0.1.0"
clap = "4.3.0"
regex = "1.4"

kclvm-tools = { path = "../../../tools" }
kclvm-error = { path = "../../../error" }
Expand Down
23 changes: 14 additions & 9 deletions kclvm/tools/src/LSP/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Ok;
use anyhow::{anyhow, Ok};
use crossbeam_channel::Sender;
use lsp_types::{Location, TextEdit};
use ra_ap_vfs::VfsPath;
Expand All @@ -16,6 +16,7 @@ use crate::{
goto_def::goto_definition,
hover, quick_fix,
state::{log_message, LanguageServerSnapshot, LanguageServerState, Task},
util,
};

impl LanguageServerState {
Expand Down Expand Up @@ -223,8 +224,11 @@ pub(crate) fn handle_rename(
params: lsp_types::RenameParams,
sender: Sender<Task>,
) -> anyhow::Result<Option<lsp_types::WorkspaceEdit>> {
// 1. check the new name validity, todo
// let new_name = params.new_name;
// 1. check the new name validity
let new_name = params.new_name;
if !util::is_valid_kcl_name(new_name.clone()) {
return Err(anyhow!("Can not rename to: {new_name}, invalid name"));
}

// 2. find all the references of the symbol
let file = file_path_from_url(&params.text_document_position.text_document.uri)?;
Expand All @@ -241,11 +245,11 @@ pub(crate) fn handle_rename(
log,
);
match references {
core::result::Result::Ok(locations) => {
Result::Ok(locations) => {
match locations.len() {
0 => {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
let _ = log("Symbol not found".to_string());
Ok(None)
Peefy marked this conversation as resolved.
Show resolved Hide resolved
anyhow::Ok(None)
}
_ => {
// 3. return the workspaceEdit to rename all the references with the new name
Expand All @@ -260,18 +264,19 @@ pub(crate) fn handle_rename(
.or_insert_with(Vec::new)
.push(TextEdit {
range: location.range,
new_text: params.new_name.clone(),
new_text: new_name.clone(),
});
map
});
workspace_edit.changes = Some(changes);
Ok(Some(workspace_edit))
anyhow::Ok(Some(workspace_edit))
}
}
}
Err(msg) => {
log(format!("Can not rename symbol: {msg}"))?;
Ok(None)
let err_msg = format!("Can not rename symbol: {msg}");
log(err_msg.clone())?;
return Err(anyhow!(err_msg));
}
}
}
6 changes: 6 additions & 0 deletions kclvm/tools/src/LSP/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use kclvm_utils::pkgpath::rm_external_pkg_name;
use lsp_types::{Location, Position, Range, Url};
use parking_lot::{RwLock, RwLockReadGuard};
use ra_ap_vfs::{FileId, Vfs};
use regex::Regex;
use serde::{de::DeserializeOwned, Serialize};
use std::cell::RefCell;
use std::collections::HashMap;
Expand Down Expand Up @@ -894,6 +895,11 @@ fn line_to_words(text: String) -> HashMap<String, Vec<Word>> {
result
}

pub fn is_valid_kcl_name(name: String) -> bool {
Peefy marked this conversation as resolved.
Show resolved Hide resolved
let re = Regex::new(r#"^[a-zA-Z_][a-zA-Z0-9_]*$"#).unwrap();
re.is_match(&name)
}

#[cfg(test)]
mod tests {
use super::{build_word_index, line_to_words, word_index_add, word_index_subtract, Word};
Expand Down
Loading