mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-15 01:17:27 +00:00
remove Canceled from API impl
This commit is contained in:
parent
02c3d2f78e
commit
05ba45c667
8 changed files with 61 additions and 76 deletions
|
@ -21,8 +21,6 @@ pub struct Canceled {
|
|||
_private: (),
|
||||
}
|
||||
|
||||
pub type Cancelable<T> = Result<T, Canceled>;
|
||||
|
||||
impl Canceled {
|
||||
pub(crate) fn new() -> Canceled {
|
||||
Canceled { _private: () }
|
||||
|
|
|
@ -10,7 +10,7 @@ use std::panic;
|
|||
use ra_syntax::{TextUnit, TextRange, SourceFile, TreeArc};
|
||||
|
||||
pub use crate::{
|
||||
cancellation::{Canceled, Cancelable},
|
||||
cancellation::Canceled,
|
||||
syntax_ptr::LocalSyntaxPtr,
|
||||
input::{
|
||||
FilesDatabase, FileId, CrateId, SourceRoot, SourceRootId, CrateGraph, Dependency,
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use std::cmp::{max, min};
|
||||
|
||||
use ra_db::{SyntaxDatabase, Cancelable};
|
||||
use ra_db::SyntaxDatabase;
|
||||
use ra_syntax::{
|
||||
AstNode, SyntaxNode, TextUnit, TextRange,
|
||||
SyntaxKind::FN_DEF,
|
||||
|
@ -11,21 +11,23 @@ use ra_syntax::{
|
|||
use crate::{FilePosition, CallInfo, db::RootDatabase};
|
||||
|
||||
/// Computes parameter information for the given call expression.
|
||||
pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Cancelable<Option<CallInfo>> {
|
||||
pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option<CallInfo> {
|
||||
let file = db.source_file(position.file_id);
|
||||
let syntax = file.syntax();
|
||||
|
||||
// Find the calling expression and it's NameRef
|
||||
let calling_node = ctry!(FnCallNode::with_node(syntax, position.offset));
|
||||
let name_ref = ctry!(calling_node.name_ref());
|
||||
let calling_node = FnCallNode::with_node(syntax, position.offset)?;
|
||||
let name_ref = calling_node.name_ref()?;
|
||||
|
||||
// Resolve the function's NameRef (NOTE: this isn't entirely accurate).
|
||||
let file_symbols = db.index_resolve(name_ref);
|
||||
let symbol = ctry!(file_symbols.into_iter().find(|it| it.ptr.kind() == FN_DEF));
|
||||
let symbol = file_symbols
|
||||
.into_iter()
|
||||
.find(|it| it.ptr.kind() == FN_DEF)?;
|
||||
let fn_file = db.source_file(symbol.file_id);
|
||||
let fn_def = symbol.ptr.resolve(&fn_file);
|
||||
let fn_def = ast::FnDef::cast(&fn_def).unwrap();
|
||||
let mut call_info = ctry!(CallInfo::new(fn_def));
|
||||
let mut call_info = CallInfo::new(fn_def)?;
|
||||
// If we have a calling expression let's find which argument we are on
|
||||
let num_params = call_info.parameters.len();
|
||||
let has_self = fn_def.param_list().and_then(|l| l.self_param()).is_some();
|
||||
|
@ -61,7 +63,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Cancelable
|
|||
}
|
||||
}
|
||||
|
||||
Ok(Some(call_info))
|
||||
Some(call_info)
|
||||
}
|
||||
|
||||
enum FnCallNode<'a> {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use ra_db::{FileId, Cancelable, SyntaxDatabase};
|
||||
use ra_db::{FileId, SyntaxDatabase};
|
||||
use ra_syntax::{
|
||||
AstNode, ast,
|
||||
algo::find_node_at_offset,
|
||||
|
@ -9,21 +9,18 @@ use crate::{FilePosition, NavigationTarget, db::RootDatabase, RangeInfo};
|
|||
pub(crate) fn goto_definition(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
||||
) -> Option<RangeInfo<Vec<NavigationTarget>>> {
|
||||
let file = db.source_file(position.file_id);
|
||||
let syntax = file.syntax();
|
||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(syntax, position.offset) {
|
||||
let navs = reference_definition(db, position.file_id, name_ref)?.to_vec();
|
||||
return Ok(Some(RangeInfo::new(
|
||||
name_ref.syntax().range(),
|
||||
navs.to_vec(),
|
||||
)));
|
||||
let navs = reference_definition(db, position.file_id, name_ref).to_vec();
|
||||
return Some(RangeInfo::new(name_ref.syntax().range(), navs.to_vec()));
|
||||
}
|
||||
if let Some(name) = find_node_at_offset::<ast::Name>(syntax, position.offset) {
|
||||
let navs = ctry!(name_definition(db, position.file_id, name)?);
|
||||
return Ok(Some(RangeInfo::new(name.syntax().range(), navs)));
|
||||
let navs = name_definition(db, position.file_id, name)?;
|
||||
return Some(RangeInfo::new(name.syntax().range(), navs));
|
||||
}
|
||||
Ok(None)
|
||||
None
|
||||
}
|
||||
|
||||
pub(crate) enum ReferenceResult {
|
||||
|
@ -45,7 +42,7 @@ pub(crate) fn reference_definition(
|
|||
db: &RootDatabase,
|
||||
file_id: FileId,
|
||||
name_ref: &ast::NameRef,
|
||||
) -> Cancelable<ReferenceResult> {
|
||||
) -> ReferenceResult {
|
||||
use self::ReferenceResult::*;
|
||||
if let Some(function) =
|
||||
hir::source_binder::function_from_child_node(db, file_id, name_ref.syntax())
|
||||
|
@ -54,7 +51,7 @@ pub(crate) fn reference_definition(
|
|||
// First try to resolve the symbol locally
|
||||
if let Some(entry) = scope.resolve_local_name(name_ref) {
|
||||
let nav = NavigationTarget::from_scope_entry(file_id, &entry);
|
||||
return Ok(Exact(nav));
|
||||
return Exact(nav);
|
||||
};
|
||||
|
||||
// Next check if it is a method
|
||||
|
@ -71,7 +68,7 @@ pub(crate) fn reference_definition(
|
|||
.and_then(|it| infer_result.method_resolution(it))
|
||||
{
|
||||
if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
|
||||
return Ok(Exact(target));
|
||||
return Exact(target);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -88,7 +85,7 @@ pub(crate) fn reference_definition(
|
|||
let resolved = module.resolve_path(db, &path);
|
||||
if let Some(def_id) = resolved.take_types().or(resolved.take_values()) {
|
||||
if let Some(target) = NavigationTarget::from_def(db, def_id.resolve(db)) {
|
||||
return Ok(Exact(target));
|
||||
return Exact(target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -99,25 +96,25 @@ pub(crate) fn reference_definition(
|
|||
.into_iter()
|
||||
.map(NavigationTarget::from_symbol)
|
||||
.collect();
|
||||
Ok(Approximate(navs))
|
||||
Approximate(navs)
|
||||
}
|
||||
|
||||
fn name_definition(
|
||||
db: &RootDatabase,
|
||||
file_id: FileId,
|
||||
name: &ast::Name,
|
||||
) -> Cancelable<Option<Vec<NavigationTarget>>> {
|
||||
) -> Option<Vec<NavigationTarget>> {
|
||||
if let Some(module) = name.syntax().parent().and_then(ast::Module::cast) {
|
||||
if module.has_semi() {
|
||||
if let Some(child_module) =
|
||||
hir::source_binder::module_from_declaration(db, file_id, module)
|
||||
{
|
||||
let nav = NavigationTarget::from_module(db, child_module);
|
||||
return Ok(Some(vec![nav]));
|
||||
return Some(vec![nav]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
None
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use ra_db::{Cancelable, SyntaxDatabase};
|
||||
use ra_db::{SyntaxDatabase};
|
||||
use ra_syntax::{
|
||||
AstNode, SyntaxNode, TreeArc,
|
||||
ast::self,
|
||||
|
@ -7,19 +7,16 @@ use ra_syntax::{
|
|||
|
||||
use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange, NavigationTarget};
|
||||
|
||||
pub(crate) fn hover(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Option<RangeInfo<String>>> {
|
||||
pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeInfo<String>> {
|
||||
let file = db.source_file(position.file_id);
|
||||
let mut res = Vec::new();
|
||||
|
||||
let mut range = None;
|
||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
|
||||
use crate::goto_definition::{ReferenceResult::*, reference_definition};
|
||||
let ref_result = reference_definition(db, position.file_id, name_ref)?;
|
||||
let ref_result = reference_definition(db, position.file_id, name_ref);
|
||||
match ref_result {
|
||||
Exact(nav) => res.extend(doc_text_for(db, nav)?),
|
||||
Exact(nav) => res.extend(doc_text_for(db, nav)),
|
||||
Approximate(navs) => {
|
||||
let mut msg = String::from("Failed to exactly resolve the symbol. This is probably because rust_analyzer does not yet support glob imports or traits.");
|
||||
if !navs.is_empty() {
|
||||
|
@ -27,7 +24,7 @@ pub(crate) fn hover(
|
|||
}
|
||||
res.push(msg);
|
||||
for nav in navs {
|
||||
res.extend(doc_text_for(db, nav)?)
|
||||
res.extend(doc_text_for(db, nav))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -39,25 +36,24 @@ pub(crate) fn hover(
|
|||
let node = find_leaf_at_offset(file.syntax(), position.offset).find_map(|leaf| {
|
||||
leaf.ancestors()
|
||||
.find(|n| ast::Expr::cast(*n).is_some() || ast::Pat::cast(*n).is_some())
|
||||
});
|
||||
let node = ctry!(node);
|
||||
})?;
|
||||
let frange = FileRange {
|
||||
file_id: position.file_id,
|
||||
range: node.range(),
|
||||
};
|
||||
res.extend(type_of(db, frange)?.map(Into::into));
|
||||
res.extend(type_of(db, frange).map(Into::into));
|
||||
range = Some(node.range());
|
||||
};
|
||||
|
||||
let range = ctry!(range);
|
||||
let range = range?;
|
||||
if res.is_empty() {
|
||||
return Ok(None);
|
||||
return None;
|
||||
}
|
||||
let res = RangeInfo::new(range, res.join("\n\n---\n"));
|
||||
Ok(Some(res))
|
||||
Some(res)
|
||||
}
|
||||
|
||||
pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Cancelable<Option<String>> {
|
||||
pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Option<String> {
|
||||
let file = db.source_file(frange.file_id);
|
||||
let syntax = file.syntax();
|
||||
let leaf_node = find_covering_node(syntax, frange.range);
|
||||
|
@ -67,34 +63,28 @@ pub(crate) fn type_of(db: &RootDatabase, frange: FileRange) -> Cancelable<Option
|
|||
.take_while(|it| it.range() == leaf_node.range())
|
||||
.find(|&it| ast::Expr::cast(it).is_some() || ast::Pat::cast(it).is_some())
|
||||
.unwrap_or(leaf_node);
|
||||
let parent_fn = ctry!(node.ancestors().find_map(ast::FnDef::cast));
|
||||
let function = ctry!(hir::source_binder::function_from_source(
|
||||
db,
|
||||
frange.file_id,
|
||||
parent_fn
|
||||
));
|
||||
let parent_fn = node.ancestors().find_map(ast::FnDef::cast)?;
|
||||
let function = hir::source_binder::function_from_source(db, frange.file_id, parent_fn)?;
|
||||
let infer = function.infer(db);
|
||||
let syntax_mapping = function.body_syntax_mapping(db);
|
||||
if let Some(expr) = ast::Expr::cast(node).and_then(|e| syntax_mapping.node_expr(e)) {
|
||||
Ok(Some(infer[expr].to_string()))
|
||||
Some(infer[expr].to_string())
|
||||
} else if let Some(pat) = ast::Pat::cast(node).and_then(|p| syntax_mapping.node_pat(p)) {
|
||||
Ok(Some(infer[pat].to_string()))
|
||||
Some(infer[pat].to_string())
|
||||
} else {
|
||||
Ok(None)
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: this should not really use navigation target. Rather, approximatelly
|
||||
// resovled symbol should return a `DefId`.
|
||||
fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Cancelable<Option<String>> {
|
||||
let result = match (nav.description(db), nav.docs(db)) {
|
||||
fn doc_text_for(db: &RootDatabase, nav: NavigationTarget) -> Option<String> {
|
||||
match (nav.description(db), nav.docs(db)) {
|
||||
(Some(desc), Some(docs)) => Some("```rust\n".to_string() + &*desc + "\n```\n\n" + &*docs),
|
||||
(Some(desc), None) => Some("```rust\n".to_string() + &*desc + "\n```"),
|
||||
(None, Some(docs)) => Some(docs),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
impl NavigationTarget {
|
||||
|
|
|
@ -58,9 +58,11 @@ pub use ra_ide_api_light::{
|
|||
LineIndex, LineCol, translate_offset_with_edit,
|
||||
};
|
||||
pub use ra_db::{
|
||||
Cancelable, Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId
|
||||
Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId
|
||||
};
|
||||
|
||||
pub type Cancelable<T> = Result<T, Canceled>;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct AnalysisChange {
|
||||
new_roots: Vec<(SourceRootId, bool)>,
|
||||
|
@ -393,7 +395,7 @@ impl Analysis {
|
|||
position: FilePosition,
|
||||
) -> Cancelable<Option<RangeInfo<Vec<NavigationTarget>>>> {
|
||||
self.db
|
||||
.catch_canceled(|db| goto_definition::goto_definition(db, position))?
|
||||
.catch_canceled(|db| goto_definition::goto_definition(db, position))
|
||||
}
|
||||
|
||||
/// Finds all usages of the reference at point.
|
||||
|
@ -403,18 +405,18 @@ impl Analysis {
|
|||
|
||||
/// Returns a short text descrbing element at position.
|
||||
pub fn hover(&self, position: FilePosition) -> Cancelable<Option<RangeInfo<String>>> {
|
||||
self.with_db(|db| hover::hover(db, position))?
|
||||
self.with_db(|db| hover::hover(db, position))
|
||||
}
|
||||
|
||||
/// Computes parameter information for the given call expression.
|
||||
pub fn call_info(&self, position: FilePosition) -> Cancelable<Option<CallInfo>> {
|
||||
self.db
|
||||
.catch_canceled(|db| call_info::call_info(db, position))?
|
||||
.catch_canceled(|db| call_info::call_info(db, position))
|
||||
}
|
||||
|
||||
/// Returns a `mod name;` declaration which created the current module.
|
||||
pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<NavigationTarget>> {
|
||||
self.with_db(|db| parent_module::parent_module(db, position))?
|
||||
self.with_db(|db| parent_module::parent_module(db, position))
|
||||
}
|
||||
|
||||
/// Returns crates this file belongs too.
|
||||
|
@ -430,7 +432,7 @@ impl Analysis {
|
|||
/// Returns the set of possible targets to run for the current file.
|
||||
pub fn runnables(&self, file_id: FileId) -> Cancelable<Vec<Runnable>> {
|
||||
self.db
|
||||
.catch_canceled(|db| runnables::runnables(db, file_id))?
|
||||
.catch_canceled(|db| runnables::runnables(db, file_id))
|
||||
}
|
||||
|
||||
/// Computes syntax highlighting for the given file.
|
||||
|
@ -460,7 +462,7 @@ impl Analysis {
|
|||
|
||||
/// Computes the type of the expression at the given position.
|
||||
pub fn type_of(&self, frange: FileRange) -> Cancelable<Option<String>> {
|
||||
self.with_db(|db| hover::type_of(db, frange))?
|
||||
self.with_db(|db| hover::type_of(db, frange))
|
||||
}
|
||||
|
||||
/// Returns the edit required to rename reference at the position to the new
|
||||
|
|
|
@ -1,19 +1,16 @@
|
|||
use ra_db::{Cancelable, FilePosition};
|
||||
use ra_db::FilePosition;
|
||||
|
||||
use crate::{NavigationTarget, db::RootDatabase};
|
||||
|
||||
/// This returns `Vec` because a module may be included from several places. We
|
||||
/// don't handle this case yet though, so the Vec has length at most one.
|
||||
pub(crate) fn parent_module(
|
||||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
) -> Cancelable<Vec<NavigationTarget>> {
|
||||
pub(crate) fn parent_module(db: &RootDatabase, position: FilePosition) -> Vec<NavigationTarget> {
|
||||
let module = match hir::source_binder::module_from_position(db, position) {
|
||||
None => return Ok(Vec::new()),
|
||||
None => return Vec::new(),
|
||||
Some(it) => it,
|
||||
};
|
||||
let nav = NavigationTarget::from_module_to_decl(db, module);
|
||||
Ok(vec![nav])
|
||||
vec![nav]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -3,7 +3,7 @@ use ra_syntax::{
|
|||
TextRange, SyntaxNode,
|
||||
ast::{self, AstNode, NameOwner, ModuleItemOwner},
|
||||
};
|
||||
use ra_db::{Cancelable, SyntaxDatabase};
|
||||
use ra_db::SyntaxDatabase;
|
||||
|
||||
use crate::{db::RootDatabase, FileId};
|
||||
|
||||
|
@ -21,14 +21,13 @@ pub enum RunnableKind {
|
|||
Bin,
|
||||
}
|
||||
|
||||
pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Cancelable<Vec<Runnable>> {
|
||||
pub(crate) fn runnables(db: &RootDatabase, file_id: FileId) -> Vec<Runnable> {
|
||||
let source_file = db.source_file(file_id);
|
||||
let res = source_file
|
||||
source_file
|
||||
.syntax()
|
||||
.descendants()
|
||||
.filter_map(|i| runnable(db, file_id, i))
|
||||
.collect();
|
||||
Ok(res)
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn runnable(db: &RootDatabase, file_id: FileId, item: &SyntaxNode) -> Option<Runnable> {
|
||||
|
|
Loading…
Reference in a new issue