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 API support overwrite changes to files #898

Merged
merged 9 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
17 changes: 9 additions & 8 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/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ kclvm-ast-pretty = {path = "../ast_pretty"}
kclvm-runtime = {path = "../runtime"}
kclvm-tools = {path = "../tools" }
kclvm-query = {path = "../query"}
kcl-language-server = {path = "../tools/src/LSP"}

[dev-dependencies]
criterion = "0.4.0"
Expand Down
26 changes: 24 additions & 2 deletions kclvm/api/src/capi_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use serde::de::DeserializeOwned;
use std::default::Default;
use std::ffi::{CStr, CString};
use std::fs;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::sync::Mutex;
const TEST_DATA_PATH: &str = "./src/testdata";
static TEST_MUTEX: Lazy<Mutex<i32>> = Lazy::new(|| Mutex::new(0i32));
Expand Down Expand Up @@ -159,11 +159,33 @@ fn test_c_api_load_settings_files() {

#[test]
fn test_c_api_rename() {
test_c_api_without_wrapper::<RenameArgs, RenameResult>(
// before test, load template from .bak
let path = Path::new(TEST_DATA_PATH).join("rename").join("main.k");
let backup_path = path.with_extension("bak");
let content = fs::read_to_string(backup_path.clone()).unwrap();
fs::write(path.clone(), content).unwrap();

test_c_api::<RenameArgs, RenameResult, _>(
"KclvmService.Rename",
"rename.json",
"rename.response.json",
|r| {
r.changed_files = r
.changed_files
.iter()
.map(|f| {
PathBuf::from(f)
.canonicalize()
.unwrap()
.display()
.to_string()
})
.collect();
},
);

// after test, restore template from .bak
fs::remove_file(path.clone()).unwrap();
}

#[test]
Expand Down
38 changes: 32 additions & 6 deletions kclvm/api/src/service/service_impl.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::collections::HashMap;
use std::io::Write;
use std::path::PathBuf;
use std::string::String;
use std::sync::Arc;

use crate::gpyrpc::*;

use anyhow::anyhow;
use kcl_language_server::rename;
use kclvm_config::settings::build_settings_pathbuf;
use kclvm_driver::canonicalize_input_files;
use kclvm_parser::ParseSession;
Expand Down Expand Up @@ -534,22 +536,45 @@ impl KclvmServiceImpl {
/// ```
/// use kclvm_api::service::service_impl::KclvmServiceImpl;
/// use kclvm_api::gpyrpc::*;
/// # use std::path::PathBuf;
/// # use std::fs;
/// #
/// # let serv = KclvmServiceImpl::default();
/// # // before test, load template from .bak
/// # let path = PathBuf::from(".").join("src").join("testdata").join("rename_doc").join("main.k");
/// # let backup_path = path.with_extension("bak");
/// # let content = fs::read_to_string(backup_path.clone()).unwrap();
/// # fs::write(path.clone(), content).unwrap();
///
/// let serv = KclvmServiceImpl::default();
/// let result = serv.rename(&RenameArgs {
/// package_root: "./src/testdata/rename_doc".to_string(),
/// symbol_path: "a".to_string(),
/// file_paths: vec!["./src/testdata/rename/main.k".to_string()],
/// file_paths: vec!["./src/testdata/rename_doc/main.k".to_string()],
/// new_name: "a2".to_string(),
/// }).unwrap();
/// assert_eq!(result.changed_files.len(), 1);
///
/// # // after test, restore template from .bak
/// # fs::remove_file(path.clone()).unwrap();
/// ```
pub fn rename(&self, args: &RenameArgs) -> anyhow::Result<RenameResult> {
let pkg_root = PathBuf::from(args.package_root.clone())
.canonicalize()?
.display()
.to_string();
let symbol_path = args.symbol_path.clone();
let file_paths = args.file_paths.clone();
let mut file_paths = vec![];
for path in args.file_paths.iter() {
file_paths.push(PathBuf::from(path).canonicalize()?.display().to_string());
}
let new_name = args.new_name.clone();
Ok(RenameResult {
//todo: mock implementation
changed_files: file_paths,
changed_files: rename::rename_symbol_on_file(
&pkg_root,
&symbol_path,
&file_paths,
new_name,
)?,
})
}

Expand All @@ -564,8 +589,9 @@ impl KclvmServiceImpl {
///
/// let serv = KclvmServiceImpl::default();
/// let result = serv.rename_code(&RenameCodeArgs {
/// package_root: "./src/testdata/rename".to_string(),
/// symbol_path: "a".to_string(),
/// source_codes: vec![("./src/testdata/rename/main.k".to_string(), "a = 1\nb = a".to_string())].into_iter().collect(),
/// source_codes: vec![("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);
Expand Down
3 changes: 2 additions & 1 deletion kclvm/api/src/testdata/rename.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"file_paths": ["./testdata/rename/main.k"],
"package_root": "./src/testdata/rename",
"file_paths": ["./src/testdata/rename/main.k"],
"symbol_path": "a",
"new_name": "a2"
}
2 changes: 1 addition & 1 deletion kclvm/api/src/testdata/rename.response.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"changed_files": ["./testdata/rename/main.k"]
"changed_files": ["./src/testdata/rename/main.k"]
}
2 changes: 2 additions & 0 deletions kclvm/api/src/testdata/rename_doc/main.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
a = 1
b = a
14 changes: 8 additions & 6 deletions kclvm/spec/gpyrpc/gpyrpc.proto
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,10 @@ message KeyValuePair {
// ---------------------------------------------------------------------------------

message Rename_Args {
string symbol_path = 1; // the path to the target symbol to be renamed. The symbol path should conform to format: `<pkgpath>:<field_path>` When the pkgpath is '__main__', `<pkgpath>:` can be omitted.
repeated string file_paths = 2; // the paths to the source code files
string new_name = 3; // the new name of the symbol
string package_root = 1; // the file path to the package root
string symbol_path = 2; // the path to the target symbol to be renamed. The symbol path should conform to format: `<pkgpath>:<field_path>` When the pkgpath is '__main__', `<pkgpath>:` can be omitted.
repeated string file_paths = 3; // the paths to the source code files
string new_name = 4; // the new name of the symbol
}

message Rename_Result {
Expand All @@ -323,9 +324,10 @@ message Rename_Result {
// ---------------------------------------------------------------------------------

message RenameCode_Args {
string symbol_path = 1; // the path to the target symbol to be renamed. The symbol path should conform to format: `<pkgpath>:<field_path>` When the pkgpath is '__main__', `<pkgpath>:` can be omitted.
map<string, string> source_codes = 2; // the source code. a <filename>:<code> map
string new_name = 3; // the new name of the symbol
string package_root = 1; // the file path to the package root
string symbol_path = 2; // the path to the target symbol to be renamed. The symbol path should conform to format: `<pkgpath>:<field_path>` When the pkgpath is '__main__', `<pkgpath>:` can be omitted.
map<string, string> source_codes = 3; // the source code. a <filename>:<code> map
string new_name = 4; // the new name of the symbol
}

message RenameCode_Result {
Expand Down
Loading
Loading