Skip to content

Commit 94cec42

Browse files
committed
use folder type for folders
1 parent 48e7a2a commit 94cec42

File tree

2 files changed

+62
-26
lines changed

2 files changed

+62
-26
lines changed

src/basecode_lsp/file.rs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,35 @@ use std::ffi::OsStr;
22
use std::fs;
33
use std::path::Path;
44
use std::path::PathBuf;
5+
use std::cmp::Ordering;
56

6-
pub fn list_all_file_items(path: &Path) -> Vec<String> {
7+
pub struct FileItem {
8+
pub filename: String,
9+
pub pos: usize,
10+
pub is_dir: bool,
11+
}
12+
13+
impl Ord for FileItem {
14+
fn cmp(&self, other: &Self) -> Ordering {
15+
self.filename.cmp(&other.filename)
16+
}
17+
}
18+
19+
impl PartialEq for FileItem {
20+
fn eq(&self, other: &Self) -> bool {
21+
self.filename == other.filename && self.pos == other.pos
22+
}
23+
}
24+
25+
impl Eq for FileItem {}
26+
27+
impl PartialOrd for FileItem {
28+
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
29+
Some(self.cmp(other))
30+
}
31+
}
32+
33+
pub fn list_all_file_items(path: &Path, pos: usize) -> Vec<FileItem> {
734
let read_result = fs::read_dir(path);
835
let mut result = Vec::new();
936
if let Ok(entries) = read_result {
@@ -12,7 +39,11 @@ pub fn list_all_file_items(path: &Path) -> Vec<String> {
1239
let current = entry.path();
1340
let filename = current.file_name().unwrap_or(OsStr::new("")).to_str();
1441
if let Some(f) = filename {
15-
result.push(f.to_string());
42+
result.push(FileItem {
43+
filename: f.to_string(),
44+
pos,
45+
is_dir: current.is_dir(),
46+
});
1647
}
1748
}
1849
}
@@ -22,7 +53,7 @@ pub fn list_all_file_items(path: &Path) -> Vec<String> {
2253

2354
const MAX_LINE_LENGTH: usize = 600;
2455

25-
pub fn get_file_items(current_line: &str, root_folder: &str) -> Vec<(String, usize)> {
56+
pub fn get_file_items(current_line: &str, root_folder: &str) -> Vec<FileItem> {
2657
if current_line.len() > MAX_LINE_LENGTH {
2758
return Vec::new();
2859
}
@@ -41,11 +72,7 @@ pub fn get_file_items(current_line: &str, root_folder: &str) -> Vec<(String, usi
4172

4273
for base in [root_folder, ""].iter().map(PathBuf::from) {
4374
let path = base.join(p);
44-
file_items.extend(
45-
list_all_file_items(&path)
46-
.into_iter()
47-
.map(|file_path| (file_path, j)),
48-
);
75+
file_items.extend(list_all_file_items(&path, j));
4976
}
5077
}
5178
}
@@ -61,21 +88,21 @@ mod tests {
6188

6289
#[test]
6390
fn test_list_all_files() {
64-
let items = list_all_file_items(Path::new("./"));
91+
let items = list_all_file_items(Path::new("./"), 0);
6592
for item in items.iter() {
66-
println!("item = {}", item);
93+
println!("item = {}", item.filename);
6794
}
68-
assert!(items.iter().any(|s| s == "src"));
69-
assert!(items.iter().any(|s| s == ".git"));
70-
assert!(items.iter().any(|s| s == "README.md"));
71-
assert!(items.iter().any(|s| s == ".gitignore"));
72-
assert!(items.iter().any(|s| s == "Cargo.toml"));
73-
assert!(items.iter().any(|s| s == "Cargo.lock"));
95+
assert!(items.iter().any(|s| s.filename == "src"));
96+
assert!(items.iter().any(|s| s.filename == ".git"));
97+
assert!(items.iter().any(|s| s.filename == "README.md"));
98+
assert!(items.iter().any(|s| s.filename == ".gitignore"));
99+
assert!(items.iter().any(|s| s.filename == "Cargo.toml"));
100+
assert!(items.iter().any(|s| s.filename == "Cargo.lock"));
74101

75-
let items = list_all_file_items(Path::new("./src"));
76-
assert!(items.iter().any(|s| s == "main.rs"));
102+
let items = list_all_file_items(Path::new("./src"), 0);
103+
assert!(items.iter().any(|s| s.filename == "main.rs"));
77104

78-
let items = list_all_file_items(Path::new("doesnt_exist"));
105+
let items = list_all_file_items(Path::new("doesnt_exist"), 0);
79106
assert_eq!(0, items.len());
80107
}
81108

@@ -90,7 +117,11 @@ mod tests {
90117
let root_folder = "./";
91118
let items = get_file_items(line, root_folder);
92119

93-
assert!(items.contains(&("file2.txt".to_string(), 15)));
120+
assert!(items.contains(&FileItem {
121+
filename: "file2.txt".to_string(),
122+
pos: 15,
123+
is_dir: false,
124+
}));
94125

95126
// Clean up the dummy directory structure
96127
fs::remove_dir_all("./test_dir").unwrap();

src/basecode_lsp/util.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use super::file::FileItem;
12
use super::snippet::Snippet;
23
use tower_lsp::lsp_types::*;
34

@@ -102,7 +103,7 @@ pub fn snippets_to_completion_items(snippets: Vec<Snippet>, completions: &mut Ve
102103
}
103104

104105
pub fn file_items_to_completion_items(
105-
file_items: Vec<(String, usize)>,
106+
file_items: Vec<FileItem>,
106107
params: &CompletionParams,
107108
completions: &mut Vec<CompletionItem>,
108109
) {
@@ -111,21 +112,25 @@ pub fn file_items_to_completion_items(
111112
let mut items: Vec<CompletionItem> = Vec::new();
112113
for file_item in file_items.iter() {
113114
let text_edit = TextEdit {
114-
new_text: file_item.0.clone(),
115+
new_text: file_item.filename.clone(),
115116
range: Range {
116117
start: Position {
117118
line,
118-
character: file_item.1 as u32 + 1,
119+
character: file_item.pos as u32 + 1,
119120
},
120121
end: Position {
121122
line,
122-
character: (file_item.1 + file_item.0.len()) as u32,
123+
character: (file_item.pos + file_item.filename.len()) as u32,
123124
},
124125
},
125126
};
126127
let completion_item = CompletionItem {
127-
label: file_item.0.clone(),
128-
kind: Some(CompletionItemKind::FILE),
128+
label: file_item.filename.clone(),
129+
kind: Some(if file_item.is_dir {
130+
CompletionItemKind::FOLDER
131+
} else {
132+
CompletionItemKind::FILE
133+
}),
129134
text_edit: Some(CompletionTextEdit::Edit(text_edit)),
130135
..CompletionItem::default()
131136
};

0 commit comments

Comments
 (0)