Skip to content

Commit

Permalink
feat: implement rename api
Browse files Browse the repository at this point in the history
Signed-off-by: xiarui.xr <xiarui1994@gmail.com>
  • Loading branch information
amyXia1994 committed Nov 16, 2023
1 parent e61efe8 commit 25465a4
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 16 deletions.
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.

8 changes: 8 additions & 0 deletions kclvm/ast/src/ast.rs
Expand Up @@ -304,6 +304,14 @@ pub enum OverrideAction {
CreateOrUpdate,
}

/// KCL API symbol selector Spec, eg: `pkgpath:path.to.field`
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct SymbolSelectorSpec {
pub pkg_root: String,
pub pkgpath: String,
pub field_path: String,
}

/// Program is the AST collection of all files of the running KCL program.
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq)]
pub struct Program {
Expand Down
1 change: 1 addition & 0 deletions kclvm/query/src/lib.rs
Expand Up @@ -5,6 +5,7 @@
//! AST, recursively modifying or deleting the values of the nodes in the AST.
pub mod r#override;
pub mod query;
pub mod selector;

#[cfg(test)]
mod tests;
Expand Down
37 changes: 37 additions & 0 deletions kclvm/query/src/selector.rs
@@ -0,0 +1,37 @@
use super::util::{invalid_symbol_selector_spec_error, split_field_path};
use anyhow::Result;
use kclvm_ast::ast;

/// Parse symbol selector string to symbol selector spec
///
/// # Examples
///
/// ```
/// use kclvm_query::selector::parse_symbol_selector_spec;
///
/// if let Ok(spec) = parse_symbol_selector_spec("", "alice.age") {
/// assert_eq!(spec.pkgpath, "".to_string());
/// assert_eq!(spec.field_path, "alice.age".to_string());
/// }
/// ```
pub fn parse_symbol_selector_spec(
pkg_root: &str,
symbol_path: &str,
) -> Result<ast::SymbolSelectorSpec> {
if let Ok((pkgpath, field_path)) = split_field_path(symbol_path) {
Ok(ast::SymbolSelectorSpec {
pkg_root: pkg_root.to_string(),
pkgpath,
field_path,
})
} else {
Err(invalid_symbol_selector_spec_error(symbol_path))
}
}

#[test]
fn test_symbol_path_selector() {
let spec = parse_symbol_selector_spec("", "pkg_name:alice.age").unwrap();
assert_eq!(spec.pkgpath, "pkg_name".to_string());
assert_eq!(spec.field_path, "alice.age".to_string());
}
9 changes: 9 additions & 0 deletions kclvm/query/src/util.rs
Expand Up @@ -27,3 +27,12 @@ pub(crate) fn split_field_path(path: &str) -> Result<(String, String)> {
pub(crate) fn invalid_spec_error(spec: &str) -> anyhow::Error {
anyhow!("Invalid spec format '{}', expected <pkgpath>:<field_path>=<filed_value> or <pkgpath>:<field_path>-", spec)
}

/// Get the invalid symbol selector spec error message.
#[inline]
pub(crate) fn invalid_symbol_selector_spec_error(spec: &str) -> anyhow::Error {
anyhow!(
"Invalid spec format '{}', expected <pkgpath>:<field_path>",
spec
)
}
4 changes: 2 additions & 2 deletions kclvm/spec/gpyrpc/gpyrpc.proto
Expand Up @@ -302,7 +302,7 @@ 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 newName = 3; // the new name of the symbol
string new_name = 3; // the new name of the symbol
}

message Rename_Result {
Expand All @@ -317,7 +317,7 @@ 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 newName = 3; // the new name of the symbol
string new_name = 3; // the new name of the symbol
}

message RenameCode_Result {
Expand Down
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/Cargo.toml
Expand Up @@ -30,6 +30,7 @@ kclvm-ast = { path = "../../../ast" }
kclvm-utils = { path = "../../../utils" }
kclvm-version = { path = "../../../version" }
compiler_base_session = { path = "../../../../compiler_base/session" }
kclvm-query = {path = "../../../query"}

lsp-server = { version = "0.6.0", default-features = false }
anyhow = { version = "1.0", default-features = false, features = ["std"] }
Expand Down
1 change: 1 addition & 0 deletions kclvm/tools/src/LSP/src/lib.rs
Expand Up @@ -13,6 +13,7 @@ mod hover;
mod main_loop;
mod notification;
mod quick_fix;
mod rename;
mod request;
mod state;
#[cfg(test)]
Expand Down

0 comments on commit 25465a4

Please sign in to comment.