diff --git a/kclvm/tools/src/LSP/src/find_refs.rs b/kclvm/tools/src/LSP/src/find_refs.rs index a5828f47a..e8413be42 100644 --- a/kclvm/tools/src/LSP/src/find_refs.rs +++ b/kclvm/tools/src/LSP/src/find_refs.rs @@ -72,7 +72,7 @@ mod tests { use super::find_refs; use lsp_types::{Location, Position, Range}; use proc_macro_crate::bench_test; - use std::{path::PathBuf, vec}; + use std::path::PathBuf; fn logger(msg: String) -> Result<(), anyhow::Error> { println!("{}", msg); @@ -174,55 +174,8 @@ mod tests { Location { uri: url.clone(), range: Range { - start: Position::new(8, 4), - end: Position::new(8, 8), - }, - }, - Location { - uri: url.clone(), - range: Range { - start: Position::new(11, 7), - end: Position::new(11, 11), - }, - }, - ]; - check_locations_match( - expect, - find_refs(def_loc, "Name".to_string(), path.to_string(), logger), - ); - } - Err(_) => assert!(false, "file not found"), - } - } - #[test] - #[bench_test] - fn find_refs_from_schema_name_test() { - let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); - let mut path = root.clone(); - path.push("src/test_data/find_refs_test/main.k"); - let path = path.to_str().unwrap(); - match lsp_types::Url::from_file_path(path) { - Ok(url) => { - let def_loc = Location { - uri: url.clone(), - range: Range { - start: Position::new(4, 0), - end: Position::new(7, 0), - }, - }; - let expect = vec![ - Location { - uri: url.clone(), - range: Range { - start: Position::new(4, 7), - end: Position::new(4, 11), - }, - }, - Location { - uri: url.clone(), - range: Range { - start: Position::new(8, 4), - end: Position::new(8, 8), + start: Position::new(8, 7), + end: Position::new(8, 11), }, }, Location { diff --git a/kclvm/tools/src/LSP/src/test_data/find_refs_test/main.k b/kclvm/tools/src/LSP/src/test_data/find_refs_test/main.k index ad2c3f8d9..496868958 100644 --- a/kclvm/tools/src/LSP/src/test_data/find_refs_test/main.k +++ b/kclvm/tools/src/LSP/src/test_data/find_refs_test/main.k @@ -6,7 +6,7 @@ schema Name: name: str schema Person: - n: Name + n: Name p2 = Person { n: Name{ diff --git a/kclvm/tools/src/LSP/src/util.rs b/kclvm/tools/src/LSP/src/util.rs index 45ebe9ad5..f7a84562f 100644 --- a/kclvm/tools/src/LSP/src/util.rs +++ b/kclvm/tools/src/LSP/src/util.rs @@ -834,7 +834,7 @@ fn line_to_words(text: String) -> HashMap> { } for w in words { - result.entry(w.word.clone()).or_insert(vec![w.clone()]); + result.entry(w.word.clone()).or_insert(Vec::new()).push(w); } result } @@ -842,70 +842,307 @@ fn line_to_words(text: String) -> HashMap> { #[cfg(test)] mod tests { use super::{build_word_index, line_to_words, Word}; - // todo assert + use lsp_types::{Location, Position, Range}; + use std::collections::HashMap; + use std::path::PathBuf; #[test] fn test_build_word_index() { - let result = build_word_index("/Users/amy/work/open/catalog".to_string()); - println!("{:?}", result) + let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let mut path = root.clone(); + path.push("src/test_data/find_refs_test/main.k"); + + let url = lsp_types::Url::from_file_path(path.clone()).unwrap(); + let path = path.to_str().unwrap(); + let expect: HashMap> = vec![ + ( + "a".to_string(), + vec![ + Location { + uri: url.clone(), + range: Range { + start: Position::new(0, 0), + end: Position::new(0, 1), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(1, 4), + end: Position::new(1, 5), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(2, 4), + end: Position::new(2, 5), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(12, 14), + end: Position::new(12, 15), + }, + }, + ], + ), + ( + "c".to_string(), + vec![Location { + uri: url.clone(), + range: Range { + start: Position::new(2, 0), + end: Position::new(2, 1), + }, + }], + ), + ( + "b".to_string(), + vec![Location { + uri: url.clone(), + range: Range { + start: Position::new(1, 0), + end: Position::new(1, 1), + }, + }], + ), + ( + "n".to_string(), + vec![ + Location { + uri: url.clone(), + range: Range { + start: Position::new(8, 4), + end: Position::new(8, 5), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(11, 4), + end: Position::new(11, 5), + }, + }, + ], + ), + ( + "schema".to_string(), + vec![ + Location { + uri: url.clone(), + range: Range { + start: Position::new(4, 0), + end: Position::new(4, 6), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(7, 0), + end: Position::new(7, 6), + }, + }, + ], + ), + ( + "b".to_string(), + vec![Location { + uri: url.clone(), + range: Range { + start: Position::new(1, 0), + end: Position::new(1, 1), + }, + }], + ), + ( + "Name".to_string(), + vec![ + Location { + uri: url.clone(), + range: Range { + start: Position::new(4, 7), + end: Position::new(4, 11), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(8, 7), + end: Position::new(8, 11), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(11, 7), + end: Position::new(11, 11), + }, + }, + ], + ), + ( + "name".to_string(), + vec![ + Location { + uri: url.clone(), + range: Range { + start: Position::new(5, 4), + end: Position::new(5, 8), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(12, 8), + end: Position::new(12, 12), + }, + }, + ], + ), + ( + "demo".to_string(), + vec![Location { + uri: url.clone(), + range: Range { + start: Position::new(0, 5), + end: Position::new(0, 9), + }, + }], + ), + ( + "str".to_string(), + vec![Location { + uri: url.clone(), + range: Range { + start: Position::new(5, 10), + end: Position::new(5, 13), + }, + }], + ), + ( + "Person".to_string(), + vec![ + Location { + uri: url.clone(), + range: Range { + start: Position::new(7, 7), + end: Position::new(7, 13), + }, + }, + Location { + uri: url.clone(), + range: Range { + start: Position::new(10, 5), + end: Position::new(10, 11), + }, + }, + ], + ), + ( + "p2".to_string(), + vec![Location { + uri: url.clone(), + range: Range { + start: Position::new(10, 0), + end: Position::new(10, 2), + }, + }], + ), + ] + .into_iter() + .collect(); + match build_word_index(path.to_string()) { + Ok(actual) => { + assert_eq!(expect, actual) + } + Err(_) => assert!(false, "build word index failed. expect: {:?}", expect), + } } - // todo assert #[test] fn test_line_to_words() { - let datas = [ - "alice_first_name = \"alice\"", - "0lice_first_name = \"alic0\"", - "alice = p.Parent { name: \"alice\" }", - ]; - let expect = vec![ + let lines = ["schema Person:", "name. name again", "some_word word !word"]; + + let expects: Vec>> = vec![ vec![ - Word { - start_col: 0, - end_col: 16, - word: "alice_first_name".to_string(), - }, - Word { - start_col: 20, - end_col: 25, - word: "alice".to_string(), - }, - ], - vec![Word { - start_col: 20, - end_col: 25, - word: "alic0".to_string(), - }], + ( + "schema".to_string(), + vec![Word { + start_col: 0, + end_col: 6, + word: "schema".to_string(), + }], + ), + ( + "Person".to_string(), + vec![Word { + start_col: 7, + end_col: 13, + word: "Person".to_string(), + }], + ), + ] + .into_iter() + .collect(), vec![ - Word { - start_col: 0, - end_col: 5, - word: "alice".to_string(), - }, - Word { - start_col: 8, - end_col: 9, - word: "p".to_string(), - }, - Word { - start_col: 10, - end_col: 16, - word: "Parent".to_string(), - }, - Word { - start_col: 19, - end_col: 23, - word: "name".to_string(), - }, - Word { - start_col: 26, - end_col: 31, - word: "alice".to_string(), - }, - ], + ( + "name".to_string(), + vec![ + Word { + start_col: 0, + end_col: 4, + word: "name".to_string(), + }, + Word { + start_col: 6, + end_col: 10, + word: "name".to_string(), + }, + ], + ), + ( + "again".to_string(), + vec![Word { + start_col: 11, + end_col: 16, + word: "again".to_string(), + }], + ), + ] + .into_iter() + .collect(), + vec![ + ( + "some_word".to_string(), + vec![Word { + start_col: 0, + end_col: 9, + word: "some_word".to_string(), + }], + ), + ( + "word".to_string(), + vec![ + Word { + start_col: 10, + end_col: 14, + word: "word".to_string(), + }, + Word { + start_col: 16, + end_col: 20, + word: "word".to_string(), + }, + ], + ), + ] + .into_iter() + .collect(), ]; - for i in 0..datas.len() { - // assert_eq!(line_to_words(datas[i].to_string()), expect[i].clone()); - let _ = line_to_words(datas[i].to_string()); + for i in 0..lines.len() { + let got = line_to_words(lines[i].to_string()); + assert_eq!(expects[i], got) } } }