Use FilePosition everywhere

This commit is contained in:
Aleksey Kladov 2018-11-05 14:57:41 +03:00
parent 1e1c188cbf
commit 0192c116f7
7 changed files with 124 additions and 157 deletions

View file

@ -14,7 +14,7 @@ use crate::{
descriptors::module::{ModuleId, ModuleScope, ModuleTree, ModuleSource},
descriptors::DescriptorDatabase,
input::FilesDatabase,
Cancelable, FileId,
Cancelable, FilePosition,
};
#[derive(Debug)]
@ -29,21 +29,21 @@ pub struct CompletionItem {
pub(crate) fn resolve_based_completion(
db: &db::RootDatabase,
file_id: FileId,
offset: TextUnit,
position: FilePosition,
) -> Cancelable<Option<Vec<CompletionItem>>> {
let source_root_id = db.file_source_root(file_id);
let file = db.file_syntax(file_id);
let source_root_id = db.file_source_root(position.file_id);
let file = db.file_syntax(position.file_id);
let module_tree = db.module_tree(source_root_id)?;
let module_id = match module_tree.any_module_for_source(ModuleSource::File(file_id)) {
let module_id = match module_tree.any_module_for_source(ModuleSource::File(position.file_id)) {
None => return Ok(None),
Some(it) => it,
};
let file = {
let edit = AtomEdit::insert(offset, "intellijRulezz".to_string());
let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string());
file.reparse(&edit)
};
let target_module_id = match find_target_module(&module_tree, module_id, &file, offset) {
let target_module_id = match find_target_module(&module_tree, module_id, &file, position.offset)
{
None => return Ok(None),
Some(it) => it,
};
@ -99,18 +99,17 @@ fn crate_path(name_ref: ast::NameRef) -> Option<Vec<ast::NameRef>> {
pub(crate) fn scope_completion(
db: &db::RootDatabase,
file_id: FileId,
offset: TextUnit,
position: FilePosition,
) -> Option<Vec<CompletionItem>> {
let original_file = db.file_syntax(file_id);
let original_file = db.file_syntax(position.file_id);
// Insert a fake ident to get a valid parse tree
let file = {
let edit = AtomEdit::insert(offset, "intellijRulezz".to_string());
let edit = AtomEdit::insert(position.offset, "intellijRulezz".to_string());
original_file.reparse(&edit)
};
let mut has_completions = false;
let mut res = Vec::new();
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), offset) {
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
has_completions = true;
complete_name_ref(&file, name_ref, &mut res);
// special case, `trait T { fn foo(i_am_a_name_ref) {} }`
@ -129,7 +128,7 @@ pub(crate) fn scope_completion(
_ => (),
}
}
if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), offset) {
if let Some(name) = find_node_at_offset::<ast::Name>(file.syntax(), position.offset) {
if is_node::<ast::Param>(name.syntax()) {
has_completions = true;
param_completions(name.syntax(), &mut res);
@ -383,7 +382,7 @@ mod tests {
fn check_scope_completion(code: &str, expected_completions: &str) {
let (analysis, position) = single_file_with_position(code);
let completions = scope_completion(&analysis.imp.db, position.file_id, position.offset)
let completions = scope_completion(&analysis.imp.db, position)
.unwrap()
.into_iter()
.filter(|c| c.snippet.is_none())
@ -393,7 +392,7 @@ mod tests {
fn check_snippet_completion(code: &str, expected_completions: &str) {
let (analysis, position) = single_file_with_position(code);
let completions = scope_completion(&analysis.imp.db, position.file_id, position.offset)
let completions = scope_completion(&analysis.imp.db, position)
.unwrap()
.into_iter()
.filter(|c| c.snippet.is_some())

View file

@ -220,16 +220,13 @@ impl AnalysisImpl {
let source_root = self.db.file_source_root(file_id);
self.db.module_tree(source_root)
}
pub fn parent_module(
&self,
file_id: FileId,
offset: TextUnit,
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
let module_tree = self.module_tree(file_id)?;
let file = self.db.file_syntax(file_id);
let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), offset) {
Some(m) if !m.has_semi() => ModuleSource::new_inline(file_id, m),
_ => ModuleSource::File(file_id),
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> {
let module_tree = self.module_tree(position.file_id)?;
let file = self.db.file_syntax(position.file_id);
let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset)
{
Some(m) if !m.has_semi() => ModuleSource::new_inline(position.file_id, m),
_ => ModuleSource::File(position.file_id),
};
let res = module_tree
@ -269,18 +266,14 @@ impl AnalysisImpl {
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
self.db.crate_graph().crate_roots[&crate_id]
}
pub fn completions(
&self,
file_id: FileId,
offset: TextUnit,
) -> Cancelable<Option<Vec<CompletionItem>>> {
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
let mut res = Vec::new();
let mut has_completions = false;
if let Some(scope_based) = scope_completion(&self.db, file_id, offset) {
if let Some(scope_based) = scope_completion(&self.db, position) {
res.extend(scope_based);
has_completions = true;
}
if let Some(scope_based) = resolve_based_completion(&self.db, file_id, offset)? {
if let Some(scope_based) = resolve_based_completion(&self.db, position)? {
res.extend(scope_based);
has_completions = true;
}
@ -289,18 +282,19 @@ impl AnalysisImpl {
}
pub fn approximately_resolve_symbol(
&self,
file_id: FileId,
offset: TextUnit,
position: FilePosition,
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
let module_tree = self.module_tree(file_id)?;
let file = self.db.file_syntax(file_id);
let module_tree = self.module_tree(position.file_id)?;
let file = self.db.file_syntax(position.file_id);
let syntax = file.syntax();
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, offset) {
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
// First try to resolve the symbol locally
return if let Some((name, range)) = resolve_local_name(&self.db, file_id, name_ref) {
return if let Some((name, range)) =
resolve_local_name(&self.db, position.file_id, name_ref)
{
let mut vec = vec![];
vec.push((
file_id,
position.file_id,
FileSymbol {
name,
node_range: range,
@ -313,10 +307,10 @@ impl AnalysisImpl {
self.index_resolve(name_ref)
};
}
if let Some(name) = find_node_at_offset::<ast::Name>(syntax, offset) {
if let Some(name) = find_node_at_offset::<ast::Name>(syntax, position.offset) {
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
if module.has_semi() {
let file_ids = self.resolve_module(&*module_tree, file_id, module);
let file_ids = self.resolve_module(&*module_tree, position.file_id, module);
let res = file_ids
.into_iter()
@ -341,16 +335,17 @@ impl AnalysisImpl {
Ok(vec![])
}
pub fn find_all_refs(&self, file_id: FileId, offset: TextUnit) -> Vec<(FileId, TextRange)> {
let file = self.db.file_syntax(file_id);
pub fn find_all_refs(&self, position: FilePosition) -> Vec<(FileId, TextRange)> {
let file = self.db.file_syntax(position.file_id);
let syntax = file.syntax();
// Find the binding associated with the offset
let maybe_binding = find_node_at_offset::<ast::BindPat>(syntax, offset).or_else(|| {
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, offset)?;
let resolved = resolve_local_name(&self.db, file_id, name_ref)?;
find_node_at_offset::<ast::BindPat>(syntax, resolved.1.end())
});
let maybe_binding =
find_node_at_offset::<ast::BindPat>(syntax, position.offset).or_else(|| {
let name_ref = find_node_at_offset::<ast::NameRef>(syntax, position.offset)?;
let resolved = resolve_local_name(&self.db, position.file_id, name_ref)?;
find_node_at_offset::<ast::BindPat>(syntax, resolved.1.end())
});
let binding = match maybe_binding {
None => return Vec::new(),
@ -359,11 +354,11 @@ impl AnalysisImpl {
let decl = DeclarationDescriptor::new(binding);
let mut ret = vec![(file_id, decl.range)];
let mut ret = vec![(position.file_id, decl.range)];
ret.extend(
decl.find_all_refs()
.into_iter()
.map(|ref_desc| (file_id, ref_desc.range)),
.map(|ref_desc| (position.file_id, ref_desc.range)),
);
ret
@ -457,14 +452,13 @@ impl AnalysisImpl {
pub fn resolve_callable(
&self,
file_id: FileId,
offset: TextUnit,
position: FilePosition,
) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> {
let file = self.db.file_syntax(file_id);
let file = self.db.file_syntax(position.file_id);
let syntax = file.syntax();
// Find the calling expression and it's NameRef
let calling_node = match FnCallNode::with_node(syntax, offset) {
let calling_node = match FnCallNode::with_node(syntax, position.offset) {
Some(node) => node,
None => return Ok(None),
};
@ -499,7 +493,7 @@ impl AnalysisImpl {
if let Some(ref arg_list) = calling_node.arg_list() {
let start = arg_list.syntax().range().start();
let range_search = TextRange::from_to(start, offset);
let range_search = TextRange::from_to(start, position.offset);
let mut commas: usize = arg_list
.syntax()
.text()

View file

@ -119,7 +119,7 @@ impl AnalysisHost {
}
}
#[derive(Debug)]
#[derive(Clone, Copy, Debug)]
pub struct FilePosition {
pub file_id: FileId,
pub offset: TextUnit,
@ -224,18 +224,18 @@ impl Analysis {
let file = self.imp.file_syntax(file_id);
SourceChange::from_local_edit(file_id, "join lines", ra_editor::join_lines(&file, range))
}
pub fn on_enter(&self, file_id: FileId, offset: TextUnit) -> Option<SourceChange> {
let file = self.imp.file_syntax(file_id);
let edit = ra_editor::on_enter(&file, offset)?;
let res = SourceChange::from_local_edit(file_id, "on enter", edit);
pub fn on_enter(&self, position: FilePosition) -> Option<SourceChange> {
let file = self.imp.file_syntax(position.file_id);
let edit = ra_editor::on_enter(&file, position.offset)?;
let res = SourceChange::from_local_edit(position.file_id, "on enter", edit);
Some(res)
}
pub fn on_eq_typed(&self, file_id: FileId, offset: TextUnit) -> Option<SourceChange> {
let file = self.imp.file_syntax(file_id);
pub fn on_eq_typed(&self, position: FilePosition) -> Option<SourceChange> {
let file = self.imp.file_syntax(position.file_id);
Some(SourceChange::from_local_edit(
file_id,
position.file_id,
"add semicolon",
ra_editor::on_eq_typed(&file, offset)?,
ra_editor::on_eq_typed(&file, position.offset)?,
))
}
pub fn file_structure(&self, file_id: FileId) -> Vec<StructureNode> {
@ -251,24 +251,15 @@ impl Analysis {
}
pub fn approximately_resolve_symbol(
&self,
file_id: FileId,
offset: TextUnit,
position: FilePosition,
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
self.imp.approximately_resolve_symbol(file_id, offset)
self.imp.approximately_resolve_symbol(position)
}
pub fn find_all_refs(
&self,
file_id: FileId,
offset: TextUnit,
) -> Cancelable<Vec<(FileId, TextRange)>> {
Ok(self.imp.find_all_refs(file_id, offset))
pub fn find_all_refs(&self, position: FilePosition) -> Cancelable<Vec<(FileId, TextRange)>> {
Ok(self.imp.find_all_refs(position))
}
pub fn parent_module(
&self,
file_id: FileId,
offset: TextUnit,
) -> Cancelable<Vec<(FileId, FileSymbol)>> {
self.imp.parent_module(file_id, offset)
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> {
self.imp.parent_module(position)
}
pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> {
self.imp.crate_for(file_id)
@ -284,12 +275,8 @@ impl Analysis {
let file = self.imp.file_syntax(file_id);
Ok(ra_editor::highlight(&file))
}
pub fn completions(
&self,
file_id: FileId,
offset: TextUnit,
) -> Cancelable<Option<Vec<CompletionItem>>> {
self.imp.completions(file_id, offset)
pub fn completions(&self, position: FilePosition) -> Cancelable<Option<Vec<CompletionItem>>> {
self.imp.completions(position)
}
pub fn assists(&self, file_id: FileId, range: TextRange) -> Cancelable<Vec<SourceChange>> {
Ok(self.imp.assists(file_id, range))
@ -299,10 +286,9 @@ impl Analysis {
}
pub fn resolve_callable(
&self,
file_id: FileId,
offset: TextUnit,
position: FilePosition,
) -> Cancelable<Option<(FnDescriptor, Option<usize>)>> {
self.imp.resolve_callable(file_id, offset)
self.imp.resolve_callable(position)
}
}

View file

@ -1,16 +1,9 @@
use std::sync::Arc;
use ra_syntax::TextUnit;
use relative_path::{RelativePath, RelativePathBuf};
use test_utils::{extract_offset, parse_fixture, CURSOR_MARKER};
use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FileResolver};
#[derive(Debug)]
pub struct FilePosition {
pub file_id: FileId,
pub offset: TextUnit,
}
use crate::{Analysis, AnalysisChange, AnalysisHost, FileId, FileResolver, FilePosition};
/// Mock analysis is used in test to bootstrap an AnalysisHost/Analysis
/// from a set of in-memory files.

View file

@ -15,10 +15,7 @@ use ra_analysis::{
fn get_signature(text: &str) -> (FnDescriptor, Option<usize>) {
let (analysis, position) = single_file_with_position(text);
analysis
.resolve_callable(position.file_id, position.offset)
.unwrap()
.unwrap()
analysis.resolve_callable(position).unwrap().unwrap()
}
#[test]
@ -32,9 +29,7 @@ fn test_resolve_module() {
",
);
let symbols = analysis
.approximately_resolve_symbol(pos.file_id, pos.offset)
.unwrap();
let symbols = analysis.approximately_resolve_symbol(pos).unwrap();
assert_eq_dbg(
r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
&symbols,
@ -49,9 +44,7 @@ fn test_resolve_module() {
",
);
let symbols = analysis
.approximately_resolve_symbol(pos.file_id, pos.offset)
.unwrap();
let symbols = analysis.approximately_resolve_symbol(pos).unwrap();
assert_eq_dbg(
r#"[(FileId(2), FileSymbol { name: "foo", node_range: [0; 0), kind: MODULE })]"#,
&symbols,
@ -92,7 +85,7 @@ fn test_resolve_parent_module() {
<|>// empty
",
);
let symbols = analysis.parent_module(pos.file_id, pos.offset).unwrap();
let symbols = analysis.parent_module(pos).unwrap();
assert_eq_dbg(
r#"[(FileId(1), FileSymbol { name: "foo", node_range: [4; 7), kind: MODULE })]"#,
&symbols,
@ -111,7 +104,7 @@ fn test_resolve_parent_module_for_inline() {
}
",
);
let symbols = analysis.parent_module(pos.file_id, pos.offset).unwrap();
let symbols = analysis.parent_module(pos).unwrap();
assert_eq_dbg(
r#"[(FileId(1), FileSymbol { name: "bar", node_range: [18; 21), kind: MODULE })]"#,
&symbols,
@ -397,9 +390,7 @@ By default this method stops actor's `Context`."#
fn get_all_refs(text: &str) -> Vec<(FileId, TextRange)> {
let (analysis, position) = single_file_with_position(text);
analysis
.find_all_refs(position.file_id, position.offset)
.unwrap()
analysis.find_all_refs(position).unwrap()
}
#[test]
@ -454,10 +445,7 @@ fn test_complete_crate_path() {
use crate::Sp<|>
",
);
let completions = analysis
.completions(position.file_id, position.offset)
.unwrap()
.unwrap();
let completions = analysis.completions(position).unwrap().unwrap();
assert_eq_dbg(
r#"[CompletionItem { label: "foo", lookup: None, snippet: None },
CompletionItem { label: "Spam", lookup: None, snippet: None }]"#,