Skip to content

Commit

Permalink
enhance: build word index prune word in comments (#903)
Browse files Browse the repository at this point in the history
* enhance: build word index prune word in comments

Signed-off-by: xiarui.xr <[email protected]>

* enhance: build word index prune word in docstrings

Signed-off-by: xiarui.xr <[email protected]>

* rename prune_comments to prune

Signed-off-by: xiarui.xr <[email protected]>

* enhance: build word index prune preserved words

Signed-off-by: xiarui.xr <[email protected]>

* minor: fix test cases

Signed-off-by: xiarui.xr <[email protected]>

---------

Signed-off-by: xiarui.xr <[email protected]>
  • Loading branch information
amyXia1994 authored Nov 22, 2023
1 parent 1fbbcc6 commit 971555f
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 69 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.

10 changes: 10 additions & 0 deletions kclvm/macros/src/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
let mut keyword_stream = quote! {};
let mut symbols_stream = quote! {};
let mut prefill_stream = quote! {};
let mut reserved_word_stream = quote! {};
let mut counter = 0u32;
let mut keys =
HashMap::<String, Span>::with_capacity(input.keywords.len() + input.symbols.len() + 10);
Expand Down Expand Up @@ -151,6 +152,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
let value = &keyword.value;
let value_string = value.value();
check_dup(keyword.name.span(), &value_string, &mut errors);
reserved_word_stream.extend(quote! {#value_string,});
prefill_stream.extend(quote! {
#value,
});
Expand All @@ -170,6 +172,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
check_dup(symbol.name.span(), &value, &mut errors);
check_order(symbol.name.span(), &name.to_string(), &mut errors);

reserved_word_stream.extend(quote! {#value,});
prefill_stream.extend(quote! {
#value,
});
Expand All @@ -178,6 +181,7 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
});
counter += 1;
}
let reserved_count = counter as usize;

// Generate symbols for the strings "0", "1", ..., "9".
let digits_base = counter;
Expand Down Expand Up @@ -208,6 +212,12 @@ fn symbols_with_errors(input: TokenStream) -> (TokenStream, Vec<syn::Error>) {
#symbols_stream
}

#[doc(hidden)]
#[allow(non_upper_case_globals)]
pub mod reserved_word {
pub const reserved_words : [&str; #reserved_count] = [#reserved_word_stream];
}

impl Interner {
pub(crate) fn fresh() -> Self {
Interner::prefill(&[
Expand Down
9 changes: 9 additions & 0 deletions kclvm/span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ pub mod sym {
}
}

pub mod reserved {

pub use super::reserved_word;

pub fn is_reserved_word(word: &str) -> bool {
reserved_word::reserved_words.contains(&word)
}
}

/// Special symbols related to KCL keywords.
impl Symbol {
/// Returns `true` if the symbol is `true` or `false`.
Expand Down
3 changes: 2 additions & 1 deletion kclvm/tools/src/LSP/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ kclvm-ast = { path = "../../../ast" }
kclvm-utils = { path = "../../../utils" }
kclvm-version = { path = "../../../version" }
compiler_base_session = { path = "../../../../compiler_base/session" }
kclvm-query = {path = "../../../query"}
kclvm-query = { path = "../../../query" }
kclvm-span = { path = "../../../span" }

lsp-server = { version = "0.6.0", default-features = false }
anyhow = { version = "1.0", default-features = false, features = ["std"] }
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/src/find_refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ mod tests {
fn setup_word_index_map(root: &str) -> HashMap<Url, HashMap<String, Vec<Location>>> {
HashMap::from([(
Url::from_file_path(root).unwrap(),
build_word_index(root.to_string()).unwrap(),
build_word_index(root.to_string(), true).unwrap(),
)])
}

Expand Down
5 changes: 3 additions & 2 deletions kclvm/tools/src/LSP/src/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ impl LanguageServerState {
vfs.set_file_contents(path.into(), Some(text.clone().into_bytes()));

// update word index
let old_word_index = build_word_index_for_file_content(old_text, &text_document.uri);
let new_word_index = build_word_index_for_file_content(text.clone(), &text_document.uri);
let old_word_index = build_word_index_for_file_content(old_text, &text_document.uri, true);
let new_word_index =
build_word_index_for_file_content(text.clone(), &text_document.uri, true);
let binding = text_document.uri.path();
let file_path = Path::new(binding); //todo rename
let word_index_map = &mut *self.word_index_map.write();
Expand Down
2 changes: 1 addition & 1 deletion kclvm/tools/src/LSP/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn rename_symbol(
match select_symbol(&symbol_spec) {
Some((name, range)) => {
// 3. build word index on file_paths, find refs within file_paths scope
let word_index = build_word_index_for_file_paths(file_paths)?;
let word_index = build_word_index_for_file_paths(file_paths, true)?;
if let Some(locations) = word_index.get(&name) {
// 4. filter out the matched refs
// 4.1 collect matched words(names) and remove Duplicates of the file paths
Expand Down
7 changes: 4 additions & 3 deletions kclvm/tools/src/LSP/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl LanguageServerState {
let word_index_map = state.word_index_map.clone();
state
.thread_pool
.execute(move || build_word_index_map(word_index_map, initialize_params));
.execute(move || build_word_index_map(word_index_map, initialize_params, true));

state
}
Expand Down Expand Up @@ -334,17 +334,18 @@ pub(crate) fn log_message(message: String, sender: &Sender<Task>) -> anyhow::Res
fn build_word_index_map(
word_index_map: Arc<RwLock<HashMap<Url, HashMap<String, Vec<Location>>>>>,
initialize_params: InitializeParams,
prune: bool,
) {
if let Some(workspace_folders) = initialize_params.workspace_folders {
for folder in workspace_folders {
let path = folder.uri.path();
if let Ok(word_index) = build_word_index(path.to_string()) {
if let Ok(word_index) = build_word_index(path.to_string(), prune) {
word_index_map.write().insert(folder.uri, word_index);
}
}
} else if let Some(root_uri) = initialize_params.root_uri {
let path = root_uri.path();
if let Ok(word_index) = build_word_index(path.to_string()) {
if let Ok(word_index) = build_word_index(path.to_string(), prune) {
word_index_map.write().insert(root_uri, word_index);
}
}
Expand Down
Loading

0 comments on commit 971555f

Please sign in to comment.