1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
use std::{
fs::{self},
path::{Path, PathBuf},
};
use base_db::{
deps::{self, ProjectRoot},
util, DocumentData, FeatureParams,
};
use rowan::{ast::AstNode, TextLen, TextRange};
use syntax::latex;
use crate::{
util::{find_curly_group_word_list, CompletionBuilder},
CompletionItem, CompletionItemData, CompletionParams,
};
pub fn complete_includes<'a>(
params: &'a CompletionParams<'a>,
builder: &mut CompletionBuilder<'a>,
) -> Option<()> {
params.feature.document.path.as_ref()?;
let (cursor, group) = find_curly_group_word_list(params)?;
let include = group.syntax().parent()?;
let (include_extension, extensions): (bool, &[&str]) = match include.kind() {
latex::PACKAGE_INCLUDE => (false, &["sty"]),
latex::CLASS_INCLUDE => (false, &["cls"]),
latex::LATEX_INCLUDE => {
let include = latex::Include::cast(include.clone())?;
(
matches!(include.command()?.text(), "\\input" | "\\subfile"),
&["tex"],
)
}
latex::BIBLATEX_INCLUDE => (true, &["bib"]),
latex::BIBTEX_INCLUDE => (false, &["bib"]),
latex::GRAPHICS_INCLUDE => (true, &["pdf", "png", "jpg", "jpeg", "bmp"]),
latex::SVG_INCLUDE => (true, &["svg"]),
latex::INKSCAPE_INCLUDE => (true, &["pdf", "eps", "ps", "png"]),
latex::VERBATIM_INCLUDE => (true, &[]),
_ => return None,
};
let segment_range = if cursor.text.is_empty() {
cursor.range
} else {
let start = cursor.range.end() - cursor.text.split('/').last()?.text_len();
TextRange::new(start, cursor.range.end())
};
let segment_text = ¶ms.feature.document.text[std::ops::Range::from(segment_range)];
let mut dirs = vec![current_dir(¶ms.feature, &cursor.text, None)];
if include.kind() == latex::GRAPHICS_INCLUDE {
for document in ¶ms.feature.project.documents {
let DocumentData::Tex(data) = &document.data else {
continue;
};
for graphics_path in &data.semantics.graphics_paths {
dirs.push(current_dir(
¶ms.feature,
&cursor.text,
Some(graphics_path),
));
}
}
}
for entry in dirs
.into_iter()
.flatten()
.filter_map(|dir| fs::read_dir(dir).ok())
.flatten()
.flatten()
{
let mut path = entry.path();
let file_type = entry.file_type().ok()?;
if file_type.is_file() && is_included(&path, extensions) {
if !include_extension {
remove_extension(&mut path);
}
let name = String::from(path.file_name()?.to_str()?);
if let Some(score) = builder.matcher.score(&name, segment_text) {
builder.items.push(CompletionItem::new_simple(
score,
segment_range,
CompletionItemData::File(name),
));
}
} else if file_type.is_dir() {
let name = String::from(path.file_name()?.to_str()?);
if let Some(score) = builder.matcher.score(&name, segment_text) {
builder.items.push(CompletionItem::new_simple(
score,
segment_range,
CompletionItemData::Directory(name),
));
}
}
}
Some(())
}
fn current_dir(
params: &FeatureParams,
path_text: &str,
graphics_path: Option<&str>,
) -> Option<PathBuf> {
let workspace = ¶ms.workspace;
let parent = deps::parents(workspace, params.document)
.iter()
.next()
.map_or(params.document, Clone::clone);
let root = ProjectRoot::walk_and_find(workspace, &parent.dir);
let mut path = PathBuf::new();
if let Some(graphics_path) = graphics_path {
path.push(graphics_path);
}
if !path_text.is_empty() {
path.push(path_text);
if !path_text.ends_with('/') {
path.pop();
}
}
let current_dir =
util::expand_relative_path(path.to_str()?, &root.src_dir, workspace.folders()).ok()?;
current_dir.to_file_path().ok()
}
fn is_included(file: &Path, allowed_extensions: &[&str]) -> bool {
allowed_extensions.is_empty()
|| file
.extension()
.and_then(std::ffi::OsStr::to_str)
.map(str::to_lowercase)
.map(|ext| allowed_extensions.contains(&ext.as_str()))
.unwrap_or_default()
}
fn remove_extension(path: &mut PathBuf) {
if let Some(stem) = path
.file_stem()
.and_then(std::ffi::OsStr::to_str)
.map(ToOwned::to_owned)
{
path.pop();
path.push(stem);
}
}
|