mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-26 13:03:31 +00:00
initial mod resolve
This commit is contained in:
parent
55e87e0b74
commit
081c16c776
6 changed files with 111 additions and 23 deletions
|
@ -16,7 +16,7 @@ use rayon::prelude::*;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fmt,
|
fmt,
|
||||||
path::Path,
|
path::{Path, PathBuf},
|
||||||
sync::{
|
sync::{
|
||||||
Arc,
|
Arc,
|
||||||
atomic::{AtomicUsize, Ordering::SeqCst},
|
atomic::{AtomicUsize, Ordering::SeqCst},
|
||||||
|
@ -26,8 +26,9 @@ use std::{
|
||||||
};
|
};
|
||||||
|
|
||||||
use libsyntax2::{
|
use libsyntax2::{
|
||||||
TextUnit,
|
TextUnit, TextRange, SyntaxRoot,
|
||||||
ast::{self, AstNode},
|
ast::{self, AstNode, NameOwner},
|
||||||
|
SyntaxKind::*,
|
||||||
};
|
};
|
||||||
use libeditor::{LineIndex, FileSymbol, find_node};
|
use libeditor::{LineIndex, FileSymbol, find_node};
|
||||||
|
|
||||||
|
@ -119,33 +120,58 @@ impl World {
|
||||||
Ok(index.clone())
|
Ok(index.clone())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn world_symbols<'a>(&'a self, mut query: Query) -> impl Iterator<Item=(FileId, &'a FileSymbol)> + 'a {
|
pub fn world_symbols(&self, mut query: Query) -> Vec<(FileId, FileSymbol)> {
|
||||||
self.reindex();
|
self.reindex();
|
||||||
self.data.file_map.iter()
|
self.data.file_map.iter()
|
||||||
.flat_map(move |(id, data)| {
|
.flat_map(move |(id, data)| {
|
||||||
let symbols = data.symbols();
|
let symbols = data.symbols();
|
||||||
query.process(symbols).into_iter().map(move |s| (*id, s))
|
query.process(symbols).into_iter().map(move |s| (*id, s))
|
||||||
})
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn approximately_resolve_symbol<'a>(
|
pub fn approximately_resolve_symbol(
|
||||||
&'a self,
|
&self,
|
||||||
id: FileId,
|
id: FileId,
|
||||||
offset: TextUnit,
|
offset: TextUnit,
|
||||||
) -> Result<Vec<(FileId, &'a FileSymbol)>> {
|
) -> Result<Vec<(FileId, FileSymbol)>> {
|
||||||
let file = self.file_syntax(id)?;
|
let file = self.file_syntax(id)?;
|
||||||
let syntax = file.syntax();
|
let syntax = file.syntax_ref();
|
||||||
let syntax = syntax.as_ref();
|
if let Some(name_ref) = find_node::<ast::NameRef<_>>(syntax, offset) {
|
||||||
let name_ref = find_node::<ast::NameRef<_>>(syntax, offset);
|
return Ok(self.index_resolve(name_ref));
|
||||||
let name = match name_ref {
|
}
|
||||||
None => return Ok(vec![]),
|
if let Some(name) = find_node::<ast::Name<_>>(syntax, offset) {
|
||||||
Some(name_ref) => name_ref.text(),
|
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
|
||||||
};
|
if module.has_semi() {
|
||||||
|
return Ok(self.resolve_module(id, module));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(vec![])
|
||||||
|
}
|
||||||
|
|
||||||
|
fn index_resolve(&self, name_ref: ast::NameRef<&SyntaxRoot>) -> Vec<(FileId, FileSymbol)> {
|
||||||
|
let name = name_ref.text();
|
||||||
let mut query = Query::new(name.to_string());
|
let mut query = Query::new(name.to_string());
|
||||||
query.exact();
|
query.exact();
|
||||||
query.limit(4);
|
query.limit(4);
|
||||||
Ok(self.world_symbols(query).collect())
|
self.world_symbols(query)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn resolve_module(&self, id: FileId, module: ast::Module<&SyntaxRoot>) -> Vec<(FileId, FileSymbol)> {
|
||||||
|
let name = match module.name() {
|
||||||
|
Some(name) => name.text(),
|
||||||
|
None => return Vec::new(),
|
||||||
|
};
|
||||||
|
let id = match self.resolve_relative_path(id, &PathBuf::from(format!("../{}.rs", name))) {
|
||||||
|
Some(id) => id,
|
||||||
|
None => return Vec::new(),
|
||||||
|
};
|
||||||
|
vec![(id, FileSymbol {
|
||||||
|
name: name.clone(),
|
||||||
|
node_range: TextRange::offset_len(0.into(), 0.into()),
|
||||||
|
kind: MODULE,
|
||||||
|
})]
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_relative_path(&self, id: FileId, path: &Path) -> Option<FileId> {
|
fn resolve_relative_path(&self, id: FileId, path: &Path) -> Option<FileId> {
|
||||||
|
|
|
@ -62,10 +62,10 @@ impl Query {
|
||||||
self.limit = limit
|
self.limit = limit
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn process<'a>(
|
pub(crate) fn process(
|
||||||
&mut self,
|
&mut self,
|
||||||
file: &'a FileSymbols,
|
file: &FileSymbols,
|
||||||
) -> Vec<&'a FileSymbol> {
|
) -> Vec<FileSymbol> {
|
||||||
fn is_type(kind: SyntaxKind) -> bool {
|
fn is_type(kind: SyntaxKind) -> bool {
|
||||||
match kind {
|
match kind {
|
||||||
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_DEF => true,
|
STRUCT_DEF | ENUM_DEF | TRAIT_DEF | TYPE_DEF => true,
|
||||||
|
@ -87,7 +87,7 @@ impl Query {
|
||||||
if self.exact && symbol.name != self.query {
|
if self.exact && symbol.name != self.query {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
res.push(symbol);
|
res.push(symbol.clone());
|
||||||
self.limit -= 1;
|
self.limit -= 1;
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
|
|
|
@ -9,7 +9,7 @@ use libsyntax2::{
|
||||||
};
|
};
|
||||||
use TextRange;
|
use TextRange;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct StructureNode {
|
pub struct StructureNode {
|
||||||
pub parent: Option<usize>,
|
pub parent: Option<usize>,
|
||||||
pub label: String,
|
pub label: String,
|
||||||
|
@ -18,7 +18,7 @@ pub struct StructureNode {
|
||||||
pub kind: SyntaxKind,
|
pub kind: SyntaxKind,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct FileSymbol {
|
pub struct FileSymbol {
|
||||||
pub name: SmolStr,
|
pub name: SmolStr,
|
||||||
pub node_range: TextRange,
|
pub node_range: TextRange,
|
||||||
|
|
|
@ -118,3 +118,12 @@ impl <R: TreeRoot> ImplItem<R> {
|
||||||
(first, second)
|
(first, second)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl <R: TreeRoot> Module<R> {
|
||||||
|
pub fn has_semi(&self) -> bool {
|
||||||
|
match self.syntax_ref().last_child() {
|
||||||
|
None => false,
|
||||||
|
Some(node) => node.kind() == SEMI,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -89,7 +89,15 @@ impl<R: TreeRoot> SyntaxNode<R> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn first_child(&self) -> Option<SyntaxNode<R>> {
|
pub fn first_child(&self) -> Option<SyntaxNode<R>> {
|
||||||
self.children().next()
|
let red = self.red().get_child(0)?;
|
||||||
|
Some(SyntaxNode { root: self.root.clone(), red })
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last_child(&self) -> Option<SyntaxNode<R>> {
|
||||||
|
let n = self.red().n_children();
|
||||||
|
let n = n.checked_sub(1)?;
|
||||||
|
let red = self.red().get_child(n)?;
|
||||||
|
Some(SyntaxNode { root: self.root.clone(), red })
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn next_sibling(&self) -> Option<SyntaxNode<R>> {
|
pub fn next_sibling(&self) -> Option<SyntaxNode<R>> {
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
use std::path::{PathBuf, Path};
|
use std::path::{PathBuf, Path, Component};
|
||||||
use im;
|
use im;
|
||||||
use libanalysis::{FileId};
|
use libanalysis::{FileId};
|
||||||
|
|
||||||
|
@ -36,6 +36,7 @@ impl PathMap {
|
||||||
|
|
||||||
pub fn resolve(&self, id: FileId, relpath: &Path) -> Option<FileId> {
|
pub fn resolve(&self, id: FileId, relpath: &Path) -> Option<FileId> {
|
||||||
let path = self.get_path(id).join(relpath);
|
let path = self.get_path(id).join(relpath);
|
||||||
|
let path = normalize(&path);
|
||||||
self.get_id(&path)
|
self.get_id(&path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,3 +51,47 @@ impl PathMap {
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn normalize(path: &Path) -> PathBuf {
|
||||||
|
let mut components = path.components().peekable();
|
||||||
|
let mut ret = if let Some(c @ Component::Prefix(..)) = components.peek().cloned() {
|
||||||
|
components.next();
|
||||||
|
PathBuf::from(c.as_os_str())
|
||||||
|
} else {
|
||||||
|
PathBuf::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
for component in components {
|
||||||
|
match component {
|
||||||
|
Component::Prefix(..) => unreachable!(),
|
||||||
|
Component::RootDir => {
|
||||||
|
ret.push(component.as_os_str());
|
||||||
|
}
|
||||||
|
Component::CurDir => {}
|
||||||
|
Component::ParentDir => {
|
||||||
|
ret.pop();
|
||||||
|
}
|
||||||
|
Component::Normal(c) => {
|
||||||
|
ret.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_resolve() {
|
||||||
|
let mut m = PathMap::new();
|
||||||
|
let id1 = m.get_or_insert(PathBuf::from("/foo"));
|
||||||
|
let id2 = m.get_or_insert(PathBuf::from("/foo/bar.rs"));
|
||||||
|
assert_eq!(
|
||||||
|
m.resolve(id1, &PathBuf::from("bar.rs")),
|
||||||
|
Some(id2),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue