Skip to content

Commit

Permalink
Split parts of ide_db::call_info off into ide
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Nov 22, 2021
1 parent 54b2de4 commit 77f08d0
Show file tree
Hide file tree
Showing 9 changed files with 765 additions and 752 deletions.
679 changes: 679 additions & 0 deletions crates/ide/src/call_info.rs

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions crates/ide/src/lib.rs
Expand Up @@ -24,6 +24,7 @@ mod navigation_target;

mod annotations;
mod call_hierarchy;
mod call_info;
mod doc_links;
mod highlight_related;
mod expand_macro;
Expand Down Expand Up @@ -73,6 +74,7 @@ use crate::navigation_target::{ToNav, TryToNav};
pub use crate::{
annotations::{Annotation, AnnotationConfig, AnnotationKind},
call_hierarchy::CallItem,
call_info::CallInfo,
expand_macro::ExpandedMacro,
file_structure::{StructureNode, StructureNodeKind},
folding_ranges::{Fold, FoldKind},
Expand Down Expand Up @@ -106,7 +108,6 @@ pub use ide_db::{
Cancelled, Change, CrateGraph, CrateId, Edition, FileId, FilePosition, FileRange,
SourceRoot, SourceRootId,
},
call_info::CallInfo,
label::Label,
line_index::{LineCol, LineColUtf16, LineIndex},
search::{ReferenceCategory, SearchScope},
Expand Down Expand Up @@ -434,7 +435,7 @@ impl Analysis {

/// Computes parameter information for the given call expression.
pub fn call_info(&self, position: FilePosition) -> Cancellable<Option<CallInfo>> {
self.with_db(|db| ide_db::call_info::call_info(db, position))
self.with_db(|db| call_info::call_info(db, position))
}

/// Computes call hierarchy candidates for the given file position.
Expand Down
18 changes: 9 additions & 9 deletions crates/ide/src/static_index.rs
Expand Up @@ -3,18 +3,18 @@

use std::collections::HashMap;

use hir::Semantics;
use hir::{db::HirDatabase, Crate, Module};
use ide_db::base_db::{FileId, FileRange, SourceDatabaseExt};
use ide_db::defs::Definition;
use ide_db::RootDatabase;
use hir::{db::HirDatabase, Crate, Module, Semantics};
use ide_db::{
base_db::{FileId, FileRange, SourceDatabaseExt},
defs::Definition,
RootDatabase,
};
use rustc_hash::FxHashSet;
use syntax::{AstNode, SyntaxKind::*, T};
use syntax::{SyntaxToken, TextRange};
use syntax::{AstNode, SyntaxKind::*, SyntaxToken, TextRange, T};

use crate::hover::hover_for_definition;
use crate::{
Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult, InlayHint, InlayHintsConfig, TryToNav,
hover::hover_for_definition, Analysis, Fold, HoverConfig, HoverDocFormat, HoverResult,
InlayHint, InlayHintsConfig, TryToNav,
};

/// A static representation of fully analyzed source code.
Expand Down
3 changes: 2 additions & 1 deletion crates/ide/src/syntax_highlighting/inject.rs
Expand Up @@ -5,7 +5,8 @@ use std::mem;
use either::Either;
use hir::{InFile, Semantics};
use ide_db::{
call_info::ActiveParameter, defs::Definition, helpers::rust_doc::is_rust_fence, SymbolKind,
active_parameter::ActiveParameter, defs::Definition, helpers::rust_doc::is_rust_fence,
SymbolKind,
};
use syntax::{
ast::{self, AstNode, IsString},
Expand Down
2 changes: 1 addition & 1 deletion crates/ide_completion/src/context.rs
Expand Up @@ -3,8 +3,8 @@
use base_db::SourceDatabaseExt;
use hir::{Local, Name, ScopeDef, Semantics, SemanticsScope, Type, TypeInfo};
use ide_db::{
active_parameter::ActiveParameter,
base_db::{FilePosition, SourceDatabase},
call_info::ActiveParameter,
RootDatabase,
};
use syntax::{
Expand Down
70 changes: 70 additions & 0 deletions crates/ide_db/src/active_parameter.rs
@@ -0,0 +1,70 @@
//! This module provides functionality for querying callable information about a token.

use either::Either;
use hir::{Semantics, Type};
use syntax::{
ast::{self, HasArgList, HasName},
AstNode, SyntaxToken,
};

use crate::RootDatabase;

#[derive(Debug)]
pub struct ActiveParameter {
pub ty: Type,
pub pat: Either<ast::SelfParam, ast::Pat>,
}

impl ActiveParameter {
/// Returns information about the call argument this token is part of.
pub fn at_token(sema: &Semantics<RootDatabase>, token: SyntaxToken) -> Option<Self> {
let (signature, active_parameter) = callable_for_token(sema, token)?;

let idx = active_parameter?;
let mut params = signature.params(sema.db);
if !(idx < params.len()) {
cov_mark::hit!(too_many_arguments);
return None;
}
let (pat, ty) = params.swap_remove(idx);
pat.map(|pat| ActiveParameter { ty, pat })
}

pub fn ident(&self) -> Option<ast::Name> {
self.pat.as_ref().right().and_then(|param| match param {
ast::Pat::IdentPat(ident) => ident.name(),
_ => None,
})
}
}

/// Returns a [`hir::Callable`] this token is a part of and its argument index of said callable.
pub fn callable_for_token(
sema: &Semantics<RootDatabase>,
token: SyntaxToken,
) -> Option<(hir::Callable, Option<usize>)> {
// Find the calling expression and it's NameRef
let parent = token.parent()?;
let calling_node = parent.ancestors().filter_map(ast::CallableExpr::cast).find(|it| {
it.arg_list()
.map_or(false, |it| it.syntax().text_range().contains(token.text_range().start()))
})?;

let callable = match &calling_node {
ast::CallableExpr::Call(call) => {
let expr = call.expr()?;
sema.type_of_expr(&expr)?.adjusted().as_callable(sema.db)
}
ast::CallableExpr::MethodCall(call) => sema.resolve_method_call_as_callable(call),
}?;
let active_param = if let Some(arg_list) = calling_node.arg_list() {
let param = arg_list
.args()
.take_while(|arg| arg.syntax().text_range().end() <= token.text_range().start())
.count();
Some(param)
} else {
None
};
Some((callable, active_param))
}
174 changes: 0 additions & 174 deletions crates/ide_db/src/call_info.rs

This file was deleted.

0 comments on commit 77f08d0

Please sign in to comment.