aboutsummaryrefslogtreecommitdiff
path: root/crates/libanalysis
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-08-27 18:58:38 +0100
committerAleksey Kladov <[email protected]>2018-08-27 18:58:38 +0100
commitaaca7d003bd969785be53d8f312b67bfa26f6272 (patch)
tree9e3ab4d75d631fb1055fb6ab5c05f0a78153fadd /crates/libanalysis
parent846114a6e95da696deb6a0f3243ad23c45074a00 (diff)
move scopes to file
Diffstat (limited to 'crates/libanalysis')
-rw-r--r--crates/libanalysis/src/lib.rs36
-rw-r--r--crates/libanalysis/src/module_map.rs5
-rw-r--r--crates/libanalysis/tests/tests.rs13
3 files changed, 49 insertions, 5 deletions
diff --git a/crates/libanalysis/src/lib.rs b/crates/libanalysis/src/lib.rs
index a3f721cc8..49c39e1f3 100644
--- a/crates/libanalysis/src/lib.rs
+++ b/crates/libanalysis/src/lib.rs
@@ -12,12 +12,9 @@ extern crate rayon;
12mod symbol_index; 12mod symbol_index;
13mod module_map; 13mod module_map;
14 14
15use once_cell::sync::OnceCell;
16use rayon::prelude::*;
17
18use std::{ 15use std::{
19 fmt, 16 fmt,
20 path::{Path}, 17 path::{Path, PathBuf},
21 sync::{ 18 sync::{
22 Arc, 19 Arc,
23 atomic::{AtomicBool, Ordering::SeqCst}, 20 atomic::{AtomicBool, Ordering::SeqCst},
@@ -26,13 +23,16 @@ use std::{
26 time::Instant, 23 time::Instant,
27}; 24};
28 25
26use once_cell::sync::OnceCell;
27use rayon::prelude::*;
28
29use libsyntax2::{ 29use libsyntax2::{
30 File, 30 File,
31 TextUnit, TextRange, SmolStr, 31 TextUnit, TextRange, SmolStr,
32 ast::{self, AstNode, NameOwner}, 32 ast::{self, AstNode, NameOwner},
33 SyntaxKind::*, 33 SyntaxKind::*,
34}; 34};
35use libeditor::{LineIndex, FileSymbol, find_node_at_offset}; 35use libeditor::{Diagnostic, LineIndex, FileSymbol, find_node_at_offset};
36 36
37use self::{ 37use self::{
38 symbol_index::FileSymbols, 38 symbol_index::FileSymbols,
@@ -130,6 +130,9 @@ impl WorldState {
130 } 130 }
131} 131}
132 132
133pub enum QuickFix {
134 CreateFile(PathBuf),
135}
133 136
134impl World { 137impl World {
135 pub fn file_syntax(&self, file_id: FileId) -> Result<File> { 138 pub fn file_syntax(&self, file_id: FileId) -> Result<File> {
@@ -210,6 +213,29 @@ impl World {
210 Ok(vec![]) 213 Ok(vec![])
211 } 214 }
212 215
216 pub fn diagnostics(&self, file_id: FileId) -> Result<Vec<(Diagnostic, Option<QuickFix>)>> {
217 let syntax = self.file_syntax(file_id)?;
218 let mut res = libeditor::diagnostics(&syntax)
219 .into_iter()
220 .map(|d| (d, None))
221 .collect::<Vec<_>>();
222 for module in syntax.ast().modules() {
223 if module.has_semi() && self.resolve_module(file_id, module).is_empty() {
224 if let Some(name) = module.name() {
225 let d = Diagnostic {
226 range: name.syntax().range(),
227 msg: "unresolved module".to_string(),
228 };
229 let quick_fix = self.data.module_map.suggested_child_mod_path(module)
230 .map(QuickFix::CreateFile);
231
232 res.push((d, quick_fix))
233 }
234 }
235 }
236 Ok(res)
237 }
238
213 fn index_resolve(&self, name_ref: ast::NameRef) -> Vec<(FileId, FileSymbol)> { 239 fn index_resolve(&self, name_ref: ast::NameRef) -> Vec<(FileId, FileSymbol)> {
214 let name = name_ref.text(); 240 let name = name_ref.text();
215 let mut query = Query::new(name.to_string()); 241 let mut query = Query::new(name.to_string());
diff --git a/crates/libanalysis/src/module_map.rs b/crates/libanalysis/src/module_map.rs
index 6a9104d84..4f480591e 100644
--- a/crates/libanalysis/src/module_map.rs
+++ b/crates/libanalysis/src/module_map.rs
@@ -93,6 +93,11 @@ impl ModuleMap {
93 res 93 res
94 } 94 }
95 95
96 pub fn suggested_child_mod_path(&self, m: ast::Module) -> Option<PathBuf> {
97 let name = m.name()?;
98 Some(PathBuf::from(format!("../{}.rs", name.text())))
99 }
100
96 fn links( 101 fn links(
97 &self, 102 &self,
98 file_resolver: &FileResolver, 103 file_resolver: &FileResolver,
diff --git a/crates/libanalysis/tests/tests.rs b/crates/libanalysis/tests/tests.rs
index 32abf9152..b3c357b02 100644
--- a/crates/libanalysis/tests/tests.rs
+++ b/crates/libanalysis/tests/tests.rs
@@ -45,6 +45,19 @@ fn test_resolve_module() {
45} 45}
46 46
47#[test] 47#[test]
48fn test_unresolved_module_diagnostic() {
49 let mut world = WorldState::new();
50 world.change_file(FileId(1), Some("mod foo;".to_string()));
51
52 let snap = world.snapshot(|_id, _path| None);
53 let diagnostics = snap.diagnostics(FileId(1)).unwrap();
54 assert_eq_dbg(
55 r#"[Diagnostic { range: [4; 7), msg: "unresolved module" }]"#,
56 &diagnostics,
57 );
58}
59
60#[test]
48fn test_resolve_parent_module() { 61fn test_resolve_parent_module() {
49 let mut world = WorldState::new(); 62 let mut world = WorldState::new();
50 world.change_file(FileId(1), Some("mod foo;".to_string())); 63 world.change_file(FileId(1), Some("mod foo;".to_string()));