move node at offset to aglo already

This commit is contained in:
Aleksey Kladov 2019-01-08 20:44:31 +03:00
parent 1b82084a5f
commit 702bdacb03
11 changed files with 19 additions and 22 deletions

View file

@ -5,8 +5,8 @@ use ra_syntax::{
AstNode, SyntaxNode, TextUnit, TextRange, AstNode, SyntaxNode, TextUnit, TextRange,
SyntaxKind::FN_DEF, SyntaxKind::FN_DEF,
ast::{self, ArgListOwner, DocCommentsOwner}, ast::{self, ArgListOwner, DocCommentsOwner},
algo::find_node_at_offset,
}; };
use ra_editor::find_node_at_offset;
use crate::{FilePosition, CallInfo, db::RootDatabase}; use crate::{FilePosition, CallInfo, db::RootDatabase};

View file

@ -1,9 +1,8 @@
use ra_editor::find_node_at_offset;
use ra_text_edit::AtomTextEdit; use ra_text_edit::AtomTextEdit;
use ra_syntax::{ use ra_syntax::{
AstNode, SyntaxNode, SourceFile, TextUnit, TextRange, AstNode, SyntaxNode, SourceFile, TextUnit, TextRange,
ast, ast,
algo::{find_leaf_at_offset, find_covering_node}, algo::{find_leaf_at_offset, find_covering_node, find_node_at_offset},
SyntaxKind::*, SyntaxKind::*,
}; };
use hir::source_binder; use hir::source_binder;

View file

@ -1,7 +1,8 @@
use ra_db::{FileId, Cancelable, SyntaxDatabase}; use ra_db::{FileId, Cancelable, SyntaxDatabase};
use ra_syntax::{TextRange, AstNode, ast, SyntaxKind::{NAME, MODULE}}; use ra_syntax::{
TextRange, AstNode, ast, SyntaxKind::{NAME, MODULE},
use ra_editor::find_node_at_offset; algo::find_node_at_offset,
};
use crate::{FilePosition, NavigationTarget, db::RootDatabase}; use crate::{FilePosition, NavigationTarget, db::RootDatabase};

View file

@ -1,9 +1,8 @@
use ra_db::{Cancelable, SyntaxDatabase}; use ra_db::{Cancelable, SyntaxDatabase};
use ra_editor::find_node_at_offset;
use ra_syntax::{ use ra_syntax::{
AstNode, SyntaxNode, TreePtr, AstNode, SyntaxNode, TreePtr,
ast::{self, NameOwner}, ast::{self, NameOwner},
algo::{find_covering_node, find_leaf_at_offset, visit::{visitor, Visitor}}, algo::{find_covering_node, find_node_at_offset, find_leaf_at_offset, visit::{visitor, Visitor}},
}; };
use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange, NavigationTarget}; use crate::{db::RootDatabase, RangeInfo, FilePosition, FileRange, NavigationTarget};

View file

@ -6,10 +6,11 @@ use hir::{
self, Problem, source_binder, self, Problem, source_binder,
}; };
use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase}; use ra_db::{FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase};
use ra_editor::{self, find_node_at_offset, assists, LocalEdit, Severity}; use ra_editor::{self, assists, LocalEdit, Severity};
use ra_syntax::{ use ra_syntax::{
TextRange, AstNode, SourceFile, TextRange, AstNode, SourceFile,
ast::{self, NameOwner}, ast::{self, NameOwner},
algo::find_node_at_offset,
SyntaxKind::*, SyntaxKind::*,
}; };

View file

@ -14,13 +14,11 @@ mod replace_if_let_with_match;
use ra_text_edit::{TextEdit, TextEditBuilder}; use ra_text_edit::{TextEdit, TextEditBuilder};
use ra_syntax::{ use ra_syntax::{
Direction, SyntaxNode, TextUnit, TextRange, SourceFile, AstNode, Direction, SyntaxNode, TextUnit, TextRange, SourceFile, AstNode,
algo::{find_leaf_at_offset, find_covering_node, LeafAtOffset}, algo::{find_leaf_at_offset, find_node_at_offset, find_covering_node, LeafAtOffset},
ast::{self, AstToken}, ast::{self, AstToken},
}; };
use itertools::Itertools; use itertools::Itertools;
use crate::find_node_at_offset;
pub use self::{ pub use self::{
flip_comma::flip_comma, flip_comma::flip_comma,
add_derive::add_derive, add_derive::add_derive,

View file

@ -120,10 +120,6 @@ pub fn syntax_tree(file: &SourceFile) -> String {
::ra_syntax::utils::dump_tree(file.syntax()) ::ra_syntax::utils::dump_tree(file.syntax())
} }
pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> {
find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ra_syntax::AstNode; use ra_syntax::AstNode;

View file

@ -2,7 +2,7 @@ use std::mem;
use itertools::Itertools; use itertools::Itertools;
use ra_syntax::{ use ra_syntax::{
algo::{find_covering_node, find_leaf_at_offset, LeafAtOffset}, algo::{find_node_at_offset, find_covering_node, find_leaf_at_offset, LeafAtOffset},
ast, ast,
text_utils::intersect, text_utils::intersect,
AstNode, Direction, SourceFile, SyntaxKind, AstNode, Direction, SourceFile, SyntaxKind,
@ -11,7 +11,7 @@ use ra_syntax::{
}; };
use ra_text_edit::text_utils::contains_offset_nonstrict; use ra_text_edit::text_utils::contains_offset_nonstrict;
use crate::{find_node_at_offset, LocalEdit, TextEditBuilder}; use crate::{LocalEdit, TextEditBuilder};
pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit { pub fn join_lines(file: &SourceFile, range: TextRange) -> LocalEdit {
let range = if range.is_empty() { let range = if range.is_empty() {

View file

@ -308,8 +308,7 @@ pub struct ReferenceDescriptor {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use ra_editor::find_node_at_offset; use ra_syntax::{SourceFile, algo::find_node_at_offset};
use ra_syntax::SourceFile;
use test_utils::{extract_offset, assert_eq_text}; use test_utils::{extract_offset, assert_eq_text};
use crate::expr; use crate::expr;

View file

@ -6,10 +6,10 @@
/// So, this modules should not be used during hir construction, it exists /// So, this modules should not be used during hir construction, it exists
/// purely for "IDE needs". /// purely for "IDE needs".
use ra_db::{FileId, FilePosition, Cancelable}; use ra_db::{FileId, FilePosition, Cancelable};
use ra_editor::find_node_at_offset;
use ra_syntax::{ use ra_syntax::{
SmolStr, TextRange, SyntaxNode, SmolStr, TextRange, SyntaxNode,
ast::{self, AstNode, NameOwner}, ast::{self, AstNode, NameOwner},
algo::find_node_at_offset,
}; };
use crate::{ use crate::{

View file

@ -2,7 +2,7 @@ pub mod visit;
use rowan::TransparentNewType; use rowan::TransparentNewType;
use crate::{SyntaxNode, TextRange, TextUnit}; use crate::{SyntaxNode, TextRange, TextUnit, AstNode};
pub use rowan::LeafAtOffset; pub use rowan::LeafAtOffset;
@ -16,6 +16,10 @@ pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<
} }
} }
pub fn find_node_at_offset<N: AstNode>(syntax: &SyntaxNode, offset: TextUnit) -> Option<&N> {
find_leaf_at_offset(syntax, offset).find_map(|leaf| leaf.ancestors().find_map(N::cast))
}
pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode { pub fn find_covering_node(root: &SyntaxNode, range: TextRange) -> &SyntaxNode {
SyntaxNode::from_repr(root.0.covering_node(range)) SyntaxNode::from_repr(root.0.covering_node(range))
} }