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: add api rename code #928

Merged
merged 4 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 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/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ indexmap = "1.0"
fslock = "0.2.1"
libloading = "0.7.3"
chrono = "0.4.19"
maplit = "1.0.2"
anyhow = { version = "1.0.70", features = ["backtrace"] }
compiler_base_session = {path = "../compiler_base/session"}

Expand Down
4 changes: 3 additions & 1 deletion kclvm/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ tokio = {version = "1.25.0", features = ["full"] }
futures = "0.3.28"
jsonrpc-stdio-server = "18.0.0"
tempfile = "3.5.0"

maplit = "1.0.2"
prost = "0.11.8"
prost-types = "0.11.8"
serde_json = "1.0"
Expand All @@ -17,6 +17,8 @@ anyhow = "1.0.70"
serde = { version = "1", features = ["derive"] }
indexmap = "1.0"
once_cell = "1.5.2"
ra_ap_vfs = "0.0.149"
ra_ap_vfs-notify = "0.0.149"

prost-wkt = {path = "../third-party/prost-wkt", version = "0.4.1"}
prost-wkt-types = {path = "../third-party/prost-wkt/wkt-types", version = "0.4.1"}
Expand Down
1 change: 1 addition & 0 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::gpyrpc::*;
use crate::service::capi::*;
use once_cell::sync::Lazy;
use prost::Message;
use ra_ap_vfs::Vfs;
use serde::de::DeserializeOwned;
use std::default::Default;
use std::ffi::{CStr, CString};
Expand Down
15 changes: 9 additions & 6 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,19 +592,22 @@ impl KclvmServiceImpl {
///
/// let serv = KclvmServiceImpl::default();
/// let result = serv.rename_code(&RenameCodeArgs {
/// package_root: "./src/testdata/rename".to_string(),
/// package_root: "/mock/path".to_string(),
/// symbol_path: "a".to_string(),
/// source_codes: vec![("main.k".to_string(), "a = 1\nb = a".to_string())].into_iter().collect(),
/// source_codes: vec![("/mock/path/main.k".to_string(), "a = 1\nb = a".to_string())].into_iter().collect(),
/// new_name: "a2".to_string(),
/// }).unwrap();
/// assert_eq!(result.changed_codes.len(), 1);
/// assert_eq!(result.changed_codes.get("/mock/path/main.k").unwrap(), "a2 = 1\nb = a2");
/// ```
pub fn rename_code(&self, args: &RenameCodeArgs) -> anyhow::Result<RenameCodeResult> {
let symbol_path = args.symbol_path.clone();
let source_codes = args.source_codes.clone();
let new_name = args.new_name.clone();
Ok(RenameCodeResult {
changed_codes: source_codes,
changed_codes: rename::rename_symbol_on_code(
&args.package_root,
&args.symbol_path,
args.source_codes.clone(),
args.new_name.clone(),
)?,
})
}

Expand Down
3 changes: 2 additions & 1 deletion kclvm/api/src/testdata/rename-code.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"source_codes": {"./src/testdata/rename/main.k": "a = 1\nb = a"},
"package_root": "/mock/path",
"source_codes": {"/mock/path/main.k": "a = 1\nb = a"},
"symbol_path": "a",
"new_name": "a2"
}
2 changes: 1 addition & 1 deletion kclvm/api/src/testdata/rename-code.response.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"changed_codes": {"./src/testdata/rename/main.k": "a = 1\nb = a"}
"changed_codes": {"/mock/path/main.k": "a2 = 1\nb = a2"}
}
2 changes: 1 addition & 1 deletion kclvm/api/src/testdata/rename.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"package_root": "./src/testdata/rename",
"package_root": "./src/testdata/rename/",
"file_paths": ["./src/testdata/rename/main.k"],
"symbol_path": "a",
"new_name": "a2"
Expand Down
7 changes: 5 additions & 2 deletions kclvm/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ pub use crate::session::ParseSession;
use compiler_base_macros::bug;
use compiler_base_session::Session;
use compiler_base_span::span::new_byte_pos;
use entry::Entries;
use indexmap::IndexMap;
use kclvm_ast::ast;
use kclvm_config::modfile::{get_vendor_home, KCL_FILE_EXTENSION, KCL_FILE_SUFFIX, KCL_MOD_FILE};
use kclvm_config::path::ModRelativePath;
use kclvm_error::diagnostic::Range;
use kclvm_error::{ErrorKind, Message, Style};
use kclvm_sema::plugin::PLUGIN_MODULE_PREFIX;
Expand All @@ -27,7 +29,7 @@ use kclvm_utils::pkgpath::rm_external_pkg_name;

use lexer::parse_token_streams;
use parser::Parser;
use std::collections::HashMap;
use std::collections::{HashMap, VecDeque};
use std::path::PathBuf;
use std::sync::{Arc, RwLock};

Expand Down Expand Up @@ -344,7 +346,8 @@ impl Loader {
let mut pkgs = HashMap::new();
let workdir = compile_entries.get_root_path().to_string();

debug_assert_eq!(compile_entries.len(), self.paths.len());
// debug_assert_eq!(compile_entries.len(), self.paths.len());
// todo: need to be removed after support vfs

let mut pkg_files = Vec::new();
for entry in compile_entries.iter() {
Expand Down
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"
maplit = "1.0.2"

kclvm-tools = { path = "../../../tools" }
kclvm-error = { path = "../../../error" }
Expand Down
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/find_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ pub(crate) fn find_refs_from_def<F: Fn(String) -> Result<(), anyhow::Error>>(
));
locs = locs[0..20].to_vec();
}

let matched_locs: Vec<Location> = locs
.into_iter()
.filter(|ref_loc| {
Expand Down
Loading
Loading