aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/nameres/mod_resolution.rs
blob: 94c9946ffdd9022604226ffb4c82647e6130e323 (plain)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use std::{borrow::Cow, sync::Arc};

use ra_db::{FileId, SourceRoot};
use ra_syntax::SmolStr;
use relative_path::RelativePathBuf;

use crate::{DefDatabase, HirFileId, Name};

#[derive(Clone, Copy)]
pub(super) struct ParentModule<'a> {
    pub(super) name: &'a Name,
    pub(super) attr_path: Option<&'a SmolStr>,
}

impl<'a> ParentModule<'a> {
    fn attribute_path(&self) -> Option<&SmolStr> {
        self.attr_path.filter(|p| !p.is_empty())
    }
}

pub(super) fn resolve_submodule(
    db: &impl DefDatabase,
    file_id: HirFileId,
    name: &Name,
    is_root: bool,
    attr_path: Option<&SmolStr>,
    parent_module: Option<ParentModule<'_>>,
) -> Result<FileId, RelativePathBuf> {
    let file_id = file_id.original_file(db);
    let source_root_id = db.file_source_root(file_id);
    let path = db.file_relative_path(file_id);
    let root = RelativePathBuf::default();
    let dir_path = path.parent().unwrap_or(&root);
    let mod_name = path.file_stem().unwrap_or("unknown");

    let resolve_mode = match (attr_path.filter(|p| !p.is_empty()), parent_module) {
        (Some(file_path), Some(parent_module)) => {
            let file_path = normalize_attribute_path(file_path);
            match parent_module.attribute_path() {
                Some(parent_module_attr_path) => {
                    let path = dir_path
                        .join(format!(
                            "{}/{}",
                            normalize_attribute_path(parent_module_attr_path),
                            file_path
                        ))
                        .normalize();
                    ResolutionMode::InlineModuleWithAttributePath(
                        InsideInlineModuleMode::WithAttributePath(path),
                    )
                }
                None => {
                    let path =
                        dir_path.join(format!("{}/{}", parent_module.name, file_path)).normalize();
                    ResolutionMode::InsideInlineModule(InsideInlineModuleMode::WithAttributePath(
                        path,
                    ))
                }
            }
        }
        (None, Some(parent_module)) => match parent_module.attribute_path() {
            Some(parent_module_attr_path) => {
                let path = dir_path.join(format!(
                    "{}/{}.rs",
                    normalize_attribute_path(parent_module_attr_path),
                    name
                ));
                ResolutionMode::InlineModuleWithAttributePath(InsideInlineModuleMode::File(path))
            }
            None => {
                let path = dir_path.join(format!("{}/{}.rs", parent_module.name, name));
                ResolutionMode::InsideInlineModule(InsideInlineModuleMode::File(path))
            }
        },
        (Some(file_path), None) => {
            let file_path = normalize_attribute_path(file_path);
            let path = dir_path.join(file_path.as_ref()).normalize();
            ResolutionMode::OutOfLine(OutOfLineMode::WithAttributePath(path))
        }
        _ => {
            let is_dir_owner = is_root || mod_name == "mod";
            if is_dir_owner {
                let file_mod = dir_path.join(format!("{}.rs", name));
                let dir_mod = dir_path.join(format!("{}/mod.rs", name));
                ResolutionMode::OutOfLine(OutOfLineMode::RootOrModRs {
                    file: file_mod,
                    directory: dir_mod,
                })
            } else {
                let path = dir_path.join(format!("{}/{}.rs", mod_name, name));
                ResolutionMode::OutOfLine(OutOfLineMode::FileInDirectory(path))
            }
        }
    };

    resolve_mode.resolve(db.source_root(source_root_id))
}

fn normalize_attribute_path(file_path: &SmolStr) -> Cow<str> {
    let current_dir = "./";
    let windows_path_separator = r#"\"#;
    let current_dir_normalize = if file_path.starts_with(current_dir) {
        &file_path[current_dir.len()..]
    } else {
        file_path.as_str()
    };
    if current_dir_normalize.contains(windows_path_separator) {
        Cow::Owned(current_dir_normalize.replace(windows_path_separator, "/"))
    } else {
        Cow::Borrowed(current_dir_normalize)
    }
}

enum OutOfLineMode {
    RootOrModRs { file: RelativePathBuf, directory: RelativePathBuf },
    FileInDirectory(RelativePathBuf),
    WithAttributePath(RelativePathBuf),
}

impl OutOfLineMode {
    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
        match self {
            OutOfLineMode::RootOrModRs { file, directory } => match source_root.files.get(file) {
                None => resolve_simple_path(source_root, directory).map_err(|_| file.clone()),
                file_id => resolve_find_result(file_id, file),
            },
            OutOfLineMode::FileInDirectory(path) => resolve_simple_path(source_root, path),
            OutOfLineMode::WithAttributePath(path) => resolve_simple_path(source_root, path),
        }
    }
}

enum InsideInlineModuleMode {
    File(RelativePathBuf),
    WithAttributePath(RelativePathBuf),
}

impl InsideInlineModuleMode {
    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
        match self {
            InsideInlineModuleMode::File(path) => resolve_simple_path(source_root, path),
            InsideInlineModuleMode::WithAttributePath(path) => {
                resolve_simple_path(source_root, path)
            }
        }
    }
}

enum ResolutionMode {
    OutOfLine(OutOfLineMode),
    InsideInlineModule(InsideInlineModuleMode),
    InlineModuleWithAttributePath(InsideInlineModuleMode),
}

impl ResolutionMode {
    pub fn resolve(&self, source_root: Arc<SourceRoot>) -> Result<FileId, RelativePathBuf> {
        use self::ResolutionMode::*;

        match self {
            OutOfLine(mode) => mode.resolve(source_root),
            InsideInlineModule(mode) => mode.resolve(source_root),
            InlineModuleWithAttributePath(mode) => mode.resolve(source_root),
        }
    }
}

fn resolve_simple_path(
    source_root: Arc<SourceRoot>,
    path: &RelativePathBuf,
) -> Result<FileId, RelativePathBuf> {
    resolve_find_result(source_root.files.get(path), path)
}

fn resolve_find_result(
    file_id: Option<&FileId>,
    path: &RelativePathBuf,
) -> Result<FileId, RelativePathBuf> {
    match file_id {
        Some(file_id) => Ok(file_id.clone()),
        None => Err(path.clone()),
    }
}