Merge #2855
2855: More fluent API r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
2f1df3cd74
5 changed files with 48 additions and 48 deletions
|
|
@ -35,7 +35,7 @@ fn find_path_inner(
|
|||
let def_map = db.crate_def_map(from.krate);
|
||||
let from_scope: &crate::item_scope::ItemScope = &def_map.modules[from.local_id].scope;
|
||||
if let Some((name, _)) = from_scope.name_of(item) {
|
||||
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
|
||||
return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
|
||||
}
|
||||
|
||||
// - if the item is the crate root, return `crate`
|
||||
|
|
@ -45,12 +45,12 @@ fn find_path_inner(
|
|||
local_id: def_map.root,
|
||||
}))
|
||||
{
|
||||
return Some(ModPath::from_simple_segments(PathKind::Crate, Vec::new()));
|
||||
return Some(ModPath::from_segments(PathKind::Crate, Vec::new()));
|
||||
}
|
||||
|
||||
// - if the item is the module we're in, use `self`
|
||||
if item == ItemInNs::Types(from.into()) {
|
||||
return Some(ModPath::from_simple_segments(PathKind::Super(0), Vec::new()));
|
||||
return Some(ModPath::from_segments(PathKind::Super(0), Vec::new()));
|
||||
}
|
||||
|
||||
// - if the item is the parent module, use `super` (this is not used recursively, since `super::super` is ugly)
|
||||
|
|
@ -61,14 +61,14 @@ fn find_path_inner(
|
|||
local_id: parent_id,
|
||||
}))
|
||||
{
|
||||
return Some(ModPath::from_simple_segments(PathKind::Super(1), Vec::new()));
|
||||
return Some(ModPath::from_segments(PathKind::Super(1), Vec::new()));
|
||||
}
|
||||
}
|
||||
|
||||
// - if the item is the crate root of a dependency crate, return the name from the extern prelude
|
||||
for (name, def_id) in &def_map.extern_prelude {
|
||||
if item == ItemInNs::Types(*def_id) {
|
||||
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
|
||||
return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -79,7 +79,7 @@ fn find_path_inner(
|
|||
&prelude_def_map.modules[prelude_module.local_id].scope;
|
||||
if let Some((name, vis)) = prelude_scope.name_of(item) {
|
||||
if vis.is_visible_from(db, from) {
|
||||
return Some(ModPath::from_simple_segments(PathKind::Plain, vec![name.clone()]));
|
||||
return Some(ModPath::from_segments(PathKind::Plain, vec![name.clone()]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -39,10 +39,7 @@ impl ModPath {
|
|||
lower::lower_path(path, hygiene).map(|it| it.mod_path)
|
||||
}
|
||||
|
||||
pub fn from_simple_segments(
|
||||
kind: PathKind,
|
||||
segments: impl IntoIterator<Item = Name>,
|
||||
) -> ModPath {
|
||||
pub fn from_segments(kind: PathKind, segments: impl IntoIterator<Item = Name>) -> ModPath {
|
||||
let segments = segments.into_iter().collect::<Vec<_>>();
|
||||
ModPath { kind, segments }
|
||||
}
|
||||
|
|
@ -240,7 +237,7 @@ impl From<Name> for Path {
|
|||
fn from(name: Name) -> Path {
|
||||
Path {
|
||||
type_anchor: None,
|
||||
mod_path: ModPath::from_simple_segments(PathKind::Plain, iter::once(name)),
|
||||
mod_path: ModPath::from_segments(PathKind::Plain, iter::once(name)),
|
||||
generic_args: vec![None],
|
||||
}
|
||||
}
|
||||
|
|
@ -248,7 +245,7 @@ impl From<Name> for Path {
|
|||
|
||||
impl From<Name> for ModPath {
|
||||
fn from(name: Name) -> ModPath {
|
||||
ModPath::from_simple_segments(PathKind::Plain, iter::once(name))
|
||||
ModPath::from_segments(PathKind::Plain, iter::once(name))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -311,7 +308,7 @@ macro_rules! __known_path {
|
|||
macro_rules! __path {
|
||||
($start:ident $(:: $seg:ident)*) => ({
|
||||
$crate::__known_path!($start $(:: $seg)*);
|
||||
$crate::path::ModPath::from_simple_segments($crate::path::PathKind::Abs, vec![
|
||||
$crate::path::ModPath::from_segments($crate::path::PathKind::Abs, vec![
|
||||
$crate::path::__name![$start], $($crate::path::__name![$seg],)*
|
||||
])
|
||||
});
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
|
|||
res
|
||||
}
|
||||
Either::Right(crate_id) => {
|
||||
return Some(ModPath::from_simple_segments(
|
||||
return Some(ModPath::from_segments(
|
||||
PathKind::DollarCrate(crate_id),
|
||||
iter::empty(),
|
||||
))
|
||||
|
|
@ -95,19 +95,19 @@ fn convert_path(prefix: Option<ModPath>, path: ast::Path, hygiene: &Hygiene) ->
|
|||
if prefix.is_some() {
|
||||
return None;
|
||||
}
|
||||
ModPath::from_simple_segments(PathKind::Crate, iter::empty())
|
||||
ModPath::from_segments(PathKind::Crate, iter::empty())
|
||||
}
|
||||
ast::PathSegmentKind::SelfKw => {
|
||||
if prefix.is_some() {
|
||||
return None;
|
||||
}
|
||||
ModPath::from_simple_segments(PathKind::Super(0), iter::empty())
|
||||
ModPath::from_segments(PathKind::Super(0), iter::empty())
|
||||
}
|
||||
ast::PathSegmentKind::SuperKw => {
|
||||
if prefix.is_some() {
|
||||
return None;
|
||||
}
|
||||
ModPath::from_simple_segments(PathKind::Super(1), iter::empty())
|
||||
ModPath::from_segments(PathKind::Super(1), iter::empty())
|
||||
}
|
||||
ast::PathSegmentKind::Type { .. } => {
|
||||
// not allowed in imports
|
||||
|
|
|
|||
|
|
@ -22,9 +22,8 @@ impl ast::BinExpr {
|
|||
#[must_use]
|
||||
pub fn replace_op(&self, op: SyntaxKind) -> Option<ast::BinExpr> {
|
||||
let op_node: SyntaxElement = self.op_details()?.0.into();
|
||||
let to_insert: Option<SyntaxElement> = Some(tokens::op(op).into());
|
||||
let replace_range = RangeInclusive::new(op_node.clone(), op_node);
|
||||
Some(replace_children(self, replace_range, to_insert.into_iter()))
|
||||
let to_insert: Option<SyntaxElement> = Some(make::token(op).into());
|
||||
Some(replace_children(self, single_node(op_node), to_insert))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -40,11 +39,10 @@ impl ast::FnDef {
|
|||
} else {
|
||||
to_insert.push(make::tokens::single_space().into());
|
||||
to_insert.push(body.syntax().clone().into());
|
||||
return insert_children(self, InsertPosition::Last, to_insert.into_iter());
|
||||
return insert_children(self, InsertPosition::Last, to_insert);
|
||||
};
|
||||
to_insert.push(body.syntax().clone().into());
|
||||
let replace_range = RangeInclusive::new(old_body_or_semi.clone(), old_body_or_semi);
|
||||
replace_children(self, replace_range, to_insert.into_iter())
|
||||
replace_children(self, single_node(old_body_or_semi), to_insert)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -77,7 +75,7 @@ impl ast::ItemList {
|
|||
let ws = tokens::WsBuilder::new(&format!("\n{}", indent));
|
||||
let to_insert: ArrayVec<[SyntaxElement; 2]> =
|
||||
[ws.ws().into(), item.syntax().clone().into()].into();
|
||||
insert_children(self, position, to_insert.into_iter())
|
||||
insert_children(self, position, to_insert)
|
||||
}
|
||||
|
||||
fn l_curly(&self) -> Option<SyntaxElement> {
|
||||
|
|
@ -109,9 +107,7 @@ impl ast::ItemList {
|
|||
let to_insert = iter::once(ws.ws().into());
|
||||
match existing_ws {
|
||||
None => insert_children(self, InsertPosition::After(l_curly), to_insert),
|
||||
Some(ws) => {
|
||||
replace_children(self, RangeInclusive::new(ws.clone().into(), ws.into()), to_insert)
|
||||
}
|
||||
Some(ws) => replace_children(self, single_node(ws), to_insert),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -188,7 +184,7 @@ impl ast::RecordFieldList {
|
|||
InsertPosition::After(anchor) => after_field!(anchor),
|
||||
};
|
||||
|
||||
insert_children(self, position, to_insert.iter().cloned())
|
||||
insert_children(self, position, to_insert)
|
||||
}
|
||||
|
||||
fn l_curly(&self) -> Option<SyntaxElement> {
|
||||
|
|
@ -207,7 +203,7 @@ impl ast::TypeParam {
|
|||
Some(it) => it.syntax().clone().into(),
|
||||
None => colon.clone().into(),
|
||||
};
|
||||
replace_children(self, RangeInclusive::new(colon.into(), end), iter::empty())
|
||||
replace_children(self, colon.into()..=end, iter::empty())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -224,7 +220,7 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
|
|||
Some(el) if el.kind() == WHITESPACE => el.clone(),
|
||||
Some(_) | None => start.clone(),
|
||||
};
|
||||
node = algo::replace_children(&node, RangeInclusive::new(start, end), &mut iter::empty());
|
||||
node = algo::replace_children(&node, start..=end, &mut iter::empty());
|
||||
}
|
||||
node
|
||||
}
|
||||
|
|
@ -232,9 +228,10 @@ fn strip_attrs_and_docs_inner(mut node: SyntaxNode) -> SyntaxNode {
|
|||
#[must_use]
|
||||
pub fn replace_descendants<N: AstNode, D: AstNode>(
|
||||
parent: &N,
|
||||
replacement_map: impl Iterator<Item = (D, D)>,
|
||||
replacement_map: impl IntoIterator<Item = (D, D)>,
|
||||
) -> N {
|
||||
let map = replacement_map
|
||||
.into_iter()
|
||||
.map(|(from, to)| (from.syntax().clone().into(), to.syntax().clone().into()))
|
||||
.collect::<FxHashMap<SyntaxElement, _>>();
|
||||
let new_syntax = algo::replace_descendants(parent.syntax(), &|n| map.get(n).cloned());
|
||||
|
|
@ -348,19 +345,25 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
|
|||
fn insert_children<N: AstNode>(
|
||||
parent: &N,
|
||||
position: InsertPosition<SyntaxElement>,
|
||||
mut to_insert: impl Iterator<Item = SyntaxElement>,
|
||||
to_insert: impl IntoIterator<Item = SyntaxElement>,
|
||||
) -> N {
|
||||
let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert);
|
||||
let new_syntax = algo::insert_children(parent.syntax(), position, &mut to_insert.into_iter());
|
||||
N::cast(new_syntax).unwrap()
|
||||
}
|
||||
|
||||
fn single_node(element: impl Into<SyntaxElement>) -> RangeInclusive<SyntaxElement> {
|
||||
let element = element.into();
|
||||
element.clone()..=element
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
fn replace_children<N: AstNode>(
|
||||
parent: &N,
|
||||
to_replace: RangeInclusive<SyntaxElement>,
|
||||
mut to_insert: impl Iterator<Item = SyntaxElement>,
|
||||
to_insert: impl IntoIterator<Item = SyntaxElement>,
|
||||
) -> N {
|
||||
let new_syntax = algo::replace_children(parent.syntax(), to_replace, &mut to_insert);
|
||||
let new_syntax =
|
||||
algo::replace_children(parent.syntax(), to_replace, &mut to_insert.into_iter());
|
||||
N::cast(new_syntax).unwrap()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
//! of smaller pieces.
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::{algo, ast, AstNode, SourceFile};
|
||||
use crate::{algo, ast, AstNode, SourceFile, SyntaxKind, SyntaxToken};
|
||||
|
||||
pub fn name(text: &str) -> ast::Name {
|
||||
ast_from_text(&format!("mod {};", text))
|
||||
|
|
@ -181,28 +181,28 @@ pub fn let_stmt(pattern: ast::Pat, initializer: Option<ast::Expr>) -> ast::LetSt
|
|||
ast_from_text(&format!("fn f() {{ {} }}", text))
|
||||
}
|
||||
|
||||
pub fn token(kind: SyntaxKind) -> SyntaxToken {
|
||||
tokens::SOURCE_FILE
|
||||
.tree()
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == kind)
|
||||
.unwrap_or_else(|| panic!("unhandled token: {:?}", kind))
|
||||
}
|
||||
|
||||
fn ast_from_text<N: AstNode>(text: &str) -> N {
|
||||
let parse = SourceFile::parse(text);
|
||||
parse.tree().syntax().descendants().find_map(N::cast).unwrap()
|
||||
}
|
||||
|
||||
pub mod tokens {
|
||||
use crate::{AstNode, Parse, SourceFile, SyntaxKind, SyntaxKind::*, SyntaxToken, T};
|
||||
use crate::{AstNode, Parse, SourceFile, SyntaxKind::*, SyntaxToken, T};
|
||||
use once_cell::sync::Lazy;
|
||||
|
||||
static SOURCE_FILE: Lazy<Parse<SourceFile>> =
|
||||
pub(super) static SOURCE_FILE: Lazy<Parse<SourceFile>> =
|
||||
Lazy::new(|| SourceFile::parse("const C: () = (1 != 1, 2 == 2)\n;"));
|
||||
|
||||
pub fn op(op: SyntaxKind) -> SyntaxToken {
|
||||
SOURCE_FILE
|
||||
.tree()
|
||||
.syntax()
|
||||
.descendants_with_tokens()
|
||||
.filter_map(|it| it.into_token())
|
||||
.find(|it| it.kind() == op)
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
pub fn comma() -> SyntaxToken {
|
||||
SOURCE_FILE
|
||||
.tree()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue