1 //! This module resolves `mod foo;` declaration to file.
2 use hir_expand::name::Name;
4 use ra_syntax::SmolStr;
6 use crate::{db::DefDatabase, HirFileId};
8 #[derive(Clone, Debug)]
9 pub(super) struct ModDir {
10 /// `` for `mod.rs`, `lib.rs`
11 /// `foo/` for `foo.rs`
12 /// `foo/bar/` for `mod bar { mod x; }` nested in `foo.rs`
13 /// Invariant: path.is_empty() || path.ends_with('/')
15 /// inside `./foo.rs`, mods with `#[path]` should *not* be relative to `./foo/`
16 root_non_dir_owner: bool,
20 pub(super) fn root() -> ModDir {
21 ModDir { dir_path: DirPath::empty(), root_non_dir_owner: false }
24 pub(super) fn descend_into_definition(
27 attr_path: Option<&SmolStr>,
29 let path = match attr_path.map(|it| it.as_str()) {
31 let mut path = self.dir_path.clone();
32 path.push(&name.to_string());
36 let mut path = self.dir_path.join_attr(attr_path, self.root_non_dir_owner);
37 if !(path.is_empty() || path.ends_with('/')) {
43 ModDir { dir_path: path, root_non_dir_owner: false }
46 pub(super) fn resolve_declaration(
51 attr_path: Option<&SmolStr>,
52 ) -> Result<(FileId, bool, ModDir), String> {
53 let file_id = file_id.original_file(db.upcast());
55 let mut candidate_files = Vec::new();
58 candidate_files.push(self.dir_path.join_attr(attr_path, self.root_non_dir_owner))
61 candidate_files.push(format!("{}{}.rs", self.dir_path.0, name));
62 candidate_files.push(format!("{}{}/mod.rs", self.dir_path.0, name));
66 for candidate in candidate_files.iter() {
67 if let Some(file_id) = db.resolve_path(file_id, candidate.as_str()) {
68 let is_mod_rs = candidate.ends_with("mod.rs");
70 let (dir_path, root_non_dir_owner) = if is_mod_rs || attr_path.is_some() {
71 (DirPath::empty(), false)
73 (DirPath::new(format!("{}/", name)), true)
75 return Ok((file_id, is_mod_rs, ModDir { dir_path, root_non_dir_owner }));
78 Err(candidate_files.remove(0))
82 #[derive(Clone, Debug)]
83 struct DirPath(String);
86 fn assert_invariant(&self) {
87 assert!(self.0.is_empty() || self.0.ends_with('/'));
89 fn new(repr: String) -> DirPath {
90 let res = DirPath(repr);
91 res.assert_invariant();
94 fn empty() -> DirPath {
95 DirPath::new(String::new())
97 fn push(&mut self, name: &str) {
98 self.0.push_str(name);
100 self.assert_invariant();
102 fn parent(&self) -> Option<&str> {
103 if self.0.is_empty() {
107 self.0[..self.0.len() - '/'.len_utf8()].rfind('/').map_or(0, |it| it + '/'.len_utf8());
110 /// So this is the case which doesn't really work I think if we try to be
111 /// 100% platform agnostic:
115 /// #[path="C://sad/face"]
120 /// Here, we need to join logical dir path to a string path from an
121 /// attribute. Ideally, we should somehow losslessly communicate the whole
122 /// construction to `FileLoader`.
123 fn join_attr(&self, mut attr: &str, relative_to_parent: bool) -> String {
124 let base = if relative_to_parent { self.parent().unwrap() } else { &self.0 };
126 if attr.starts_with("./") {
127 attr = &attr["./".len()..];
130 let attr = if attr.contains('\\') {
131 tmp = attr.replace('\\', "/");
136 let res = format!("{}{}", base, attr);