Skip to content

Commit 0cd1a7c

Browse files
committed
resolve angle-bracket includes under include/ and enable Ctrl+Click nav
1 parent 17c81db commit 0cd1a7c

File tree

3 files changed

+99
-1
lines changed

3 files changed

+99
-1
lines changed

crates/base-db/src/include.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,38 @@ pub(crate) fn file_includes_query(
171171
}
172172
_ => continue,
173173
};
174+
// Try resolving relative to the current file's directory first
175+
if let Some(include_file_id) = db.resolve_path(AnchoredPath::new(file_id, &path)) {
176+
res.push(Include::new(include_file_id, kind, type_, ext));
177+
continue;
178+
}
179+
// Try resolving relative to the file's `include/` subdirectory
180+
if kind == IncludeKind::Chevrons {
181+
let anchored_with_include = format!("include/{}", path);
182+
if let Some(include_file_id) =
183+
db.resolve_path(AnchoredPath::new(file_id, &anchored_with_include))
184+
{
185+
res.push(Include::new(include_file_id, kind, type_, ext));
186+
continue;
187+
}
188+
}
189+
// Try resolving relative to the configured source roots
174190
match db.resolve_path_relative_to_roots(&path) {
175191
Some(include_file_id) => {
176192
res.push(Include::new(include_file_id, kind, type_, ext));
177193
continue;
178194
}
179195
None => {
196+
// Try common include subdirectory fallback for chevron includes
197+
if kind == IncludeKind::Chevrons {
198+
let fallback = format!("include/{}", path);
199+
if let Some(include_file_id) =
200+
db.resolve_path_relative_to_roots(&fallback)
201+
{
202+
res.push(Include::new(include_file_id, kind, type_, ext));
203+
continue;
204+
}
205+
}
180206
if type_ == IncludeType::Include {
181207
// TODO: Optional diagnostic for tryinclude ?
182208
// FIXME: Emit the diagnostics in the preprocessor. This would make more sense as some

crates/hir-def/src/db.rs

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,62 @@ pub fn resolve_include_node(
218218
};
219219
let extension = infer_include_ext(&mut text);
220220

221+
// Try resolving relative to current file directory first
222+
if let Some(file_id) = db.resolve_path(AnchoredPath::new(file_id, &text)) {
223+
return Some((
224+
Some(file_id),
225+
kind,
226+
type_,
227+
text.to_string(),
228+
NodePtr::from(&path_node),
229+
extension,
230+
));
231+
}
232+
233+
// Try resolving relative to the file's include/ subdirectory for chevrons
234+
if kind == IncludeKind::Chevrons {
235+
let text_with_include = format!("include/{}", text);
236+
if let Some(file_id) = db.resolve_path(AnchoredPath::new(file_id, &text_with_include)) {
237+
return Some((
238+
Some(file_id),
239+
kind,
240+
type_,
241+
text.to_string(),
242+
NodePtr::from(&path_node),
243+
extension,
244+
));
245+
}
246+
}
247+
248+
// Try resolving relative to roots
249+
if let Some(file_id) = db.resolve_path_relative_to_roots(&text) {
250+
return Some((
251+
Some(file_id),
252+
kind,
253+
type_,
254+
text.to_string(),
255+
NodePtr::from(&path_node),
256+
extension,
257+
));
258+
}
259+
260+
// Try resolving include/<path> relative to roots for chevrons
261+
if kind == IncludeKind::Chevrons {
262+
let text_with_include = format!("include/{}", text);
263+
if let Some(file_id) = db.resolve_path_relative_to_roots(&text_with_include) {
264+
return Some((
265+
Some(file_id),
266+
kind,
267+
type_,
268+
text.to_string(),
269+
NodePtr::from(&path_node),
270+
extension,
271+
));
272+
}
273+
}
274+
221275
(
222-
db.resolve_path_relative_to_roots(&text),
276+
None,
223277
kind,
224278
type_,
225279
text,

crates/preprocessor/src/db.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ pub(crate) fn _preprocess_file_params_query(
103103
if inc_file_id.is_none() {
104104
inc_file_id = db.resolve_path_relative_to_roots(&path);
105105
}
106+
if inc_file_id.is_none() {
107+
inc_file_id = db.resolve_path(AnchoredPath::new(file_id, &path));
108+
}
109+
if inc_file_id.is_none() && !quoted {
110+
let path_with_include = format!("include/{}", path);
111+
inc_file_id = db
112+
.resolve_path_relative_to_roots(&path_with_include)
113+
.or_else(|| db.resolve_path(AnchoredPath::new(file_id, &path_with_include)));
114+
}
106115
let inc_file_id = inc_file_id.ok_or_else(|| anyhow::anyhow!("Include not found"))?;
107116
if being_preprocessed.contains(&inc_file_id) {
108117
// Avoid cyclic deps
@@ -170,6 +179,15 @@ pub(crate) fn _preprocess_file_data_query(
170179
if inc_file_id.is_none() {
171180
inc_file_id = db.resolve_path_relative_to_roots(&path);
172181
}
182+
if inc_file_id.is_none() {
183+
inc_file_id = db.resolve_path(AnchoredPath::new(file_id, &path));
184+
}
185+
if inc_file_id.is_none() && !quoted {
186+
let path_with_include = format!("include/{}", path);
187+
inc_file_id = db
188+
.resolve_path_relative_to_roots(&path_with_include)
189+
.or_else(|| db.resolve_path(AnchoredPath::new(file_id, &path_with_include)));
190+
}
173191
let inc_file_id = inc_file_id.ok_or_else(|| anyhow::anyhow!("Include not found"))?;
174192
macros.extend(
175193
params

0 commit comments

Comments
 (0)