mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-12 21:28:51 +00:00
Merge #468
468: decouple ra_editor from other stuff r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
c9e42fcf24
21 changed files with 61 additions and 52 deletions
3
Cargo.lock
generated
3
Cargo.lock
generated
|
@ -679,7 +679,6 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ra_arena 0.1.0",
|
"ra_arena 0.1.0",
|
||||||
"ra_editor 0.1.0",
|
|
||||||
"ra_syntax 0.1.0",
|
"ra_syntax 0.1.0",
|
||||||
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -712,7 +711,6 @@ dependencies = [
|
||||||
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ra_arena 0.1.0",
|
"ra_arena 0.1.0",
|
||||||
"ra_db 0.1.0",
|
"ra_db 0.1.0",
|
||||||
"ra_editor 0.1.0",
|
|
||||||
"ra_syntax 0.1.0",
|
"ra_syntax 0.1.0",
|
||||||
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
@ -736,7 +734,6 @@ dependencies = [
|
||||||
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
"ra_analysis 0.1.0",
|
"ra_analysis 0.1.0",
|
||||||
"ra_editor 0.1.0",
|
|
||||||
"ra_syntax 0.1.0",
|
"ra_syntax 0.1.0",
|
||||||
"ra_text_edit 0.1.0",
|
"ra_text_edit 0.1.0",
|
||||||
"ra_vfs 0.1.0",
|
"ra_vfs 0.1.0",
|
||||||
|
|
|
@ -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};
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
use std::{fmt, sync::Arc};
|
use std::{fmt, sync::Arc};
|
||||||
use salsa::{self, Database};
|
|
||||||
use ra_db::{LocationIntener, BaseDatabase};
|
|
||||||
|
|
||||||
use crate::{
|
use salsa::{self, Database};
|
||||||
symbol_index,
|
use ra_db::{LocationIntener, BaseDatabase, FileId};
|
||||||
};
|
|
||||||
|
use crate::{symbol_index, LineIndex};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct RootDatabase {
|
pub(crate) struct RootDatabase {
|
||||||
|
@ -71,6 +70,19 @@ impl AsRef<LocationIntener<hir::MacroCallLoc, hir::MacroCallId>> for RootDatabas
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
salsa::query_group! {
|
||||||
|
pub(crate) trait LineIndexDatabase: ra_db::FilesDatabase + BaseDatabase {
|
||||||
|
fn line_index(file_id: FileId) -> Arc<LineIndex> {
|
||||||
|
type LineIndexQuery;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn line_index(db: &impl ra_db::FilesDatabase, file_id: FileId) -> Arc<LineIndex> {
|
||||||
|
let text = db.file_text(file_id);
|
||||||
|
Arc::new(LineIndex::new(&*text))
|
||||||
|
}
|
||||||
|
|
||||||
salsa::database_storage! {
|
salsa::database_storage! {
|
||||||
pub(crate) struct RootDatabaseStorage for RootDatabase {
|
pub(crate) struct RootDatabaseStorage for RootDatabase {
|
||||||
impl ra_db::FilesDatabase {
|
impl ra_db::FilesDatabase {
|
||||||
|
@ -84,7 +96,9 @@ salsa::database_storage! {
|
||||||
}
|
}
|
||||||
impl ra_db::SyntaxDatabase {
|
impl ra_db::SyntaxDatabase {
|
||||||
fn source_file() for ra_db::SourceFileQuery;
|
fn source_file() for ra_db::SourceFileQuery;
|
||||||
fn file_lines() for ra_db::FileLinesQuery;
|
}
|
||||||
|
impl LineIndexDatabase {
|
||||||
|
fn line_index() for LineIndexQuery;
|
||||||
}
|
}
|
||||||
impl symbol_index::SymbolsDatabase {
|
impl symbol_index::SymbolsDatabase {
|
||||||
fn file_symbols() for symbol_index::FileSymbolsQuery;
|
fn file_symbols() for symbol_index::FileSymbolsQuery;
|
||||||
|
|
|
@ -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};
|
||||||
|
|
||||||
|
|
|
@ -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};
|
||||||
|
|
|
@ -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::*,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -29,22 +29,27 @@ use std::{fmt, sync::Arc};
|
||||||
|
|
||||||
use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, TextRange, TextUnit};
|
use ra_syntax::{SmolStr, SourceFile, TreePtr, SyntaxKind, TextRange, TextUnit};
|
||||||
use ra_text_edit::TextEdit;
|
use ra_text_edit::TextEdit;
|
||||||
|
use ra_db::{SyntaxDatabase, FilesDatabase, LocalSyntaxPtr};
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use relative_path::RelativePathBuf;
|
use relative_path::RelativePathBuf;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
use salsa::ParallelDatabase;
|
use salsa::ParallelDatabase;
|
||||||
|
|
||||||
use crate::symbol_index::{FileSymbol, SymbolIndex};
|
use crate::{
|
||||||
|
symbol_index::{FileSymbol, SymbolIndex},
|
||||||
|
db::LineIndexDatabase,
|
||||||
|
};
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
completion::{CompletionItem, CompletionItemKind, InsertText},
|
completion::{CompletionItem, CompletionItemKind, InsertText},
|
||||||
runnables::{Runnable, RunnableKind},
|
runnables::{Runnable, RunnableKind},
|
||||||
};
|
};
|
||||||
pub use ra_editor::{Fold, FoldKind, HighlightedRange, LineIndex, Severity, StructureNode};
|
pub use ra_editor::{
|
||||||
|
Fold, FoldKind, HighlightedRange, Severity, StructureNode,
|
||||||
|
LineIndex, LineCol, translate_offset_with_edit,
|
||||||
|
};
|
||||||
pub use ra_db::{
|
pub use ra_db::{
|
||||||
Cancelable, Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, FilesDatabase,
|
Cancelable, Canceled, CrateGraph, CrateId, FileId, FilePosition, FileRange, SourceRootId
|
||||||
LocalSyntaxPtr, SourceRootId, SyntaxDatabase,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
|
@ -322,7 +327,7 @@ impl Analysis {
|
||||||
/// Gets the file's `LineIndex`: data structure to convert between absolute
|
/// Gets the file's `LineIndex`: data structure to convert between absolute
|
||||||
/// offsets and line/column representation.
|
/// offsets and line/column representation.
|
||||||
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
|
pub fn file_line_index(&self, file_id: FileId) -> Arc<LineIndex> {
|
||||||
self.db.file_lines(file_id)
|
self.db.line_index(file_id)
|
||||||
}
|
}
|
||||||
/// Selects the next syntactic nodes encopasing the range.
|
/// Selects the next syntactic nodes encopasing the range.
|
||||||
pub fn extend_selection(&self, frange: FileRange) -> TextRange {
|
pub fn extend_selection(&self, frange: FileRange) -> TextRange {
|
||||||
|
|
|
@ -11,5 +11,4 @@ rustc-hash = "1.0"
|
||||||
parking_lot = "0.7.0"
|
parking_lot = "0.7.0"
|
||||||
ra_arena = { path = "../ra_arena" }
|
ra_arena = { path = "../ra_arena" }
|
||||||
ra_syntax = { path = "../ra_syntax" }
|
ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_editor = { path = "../ra_editor" }
|
|
||||||
test_utils = { path = "../test_utils" }
|
test_utils = { path = "../test_utils" }
|
||||||
|
|
|
@ -5,9 +5,6 @@ mod input;
|
||||||
mod loc2id;
|
mod loc2id;
|
||||||
pub mod mock;
|
pub mod mock;
|
||||||
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
use ra_editor::LineIndex;
|
|
||||||
use ra_syntax::{TextUnit, TextRange, SourceFile, TreePtr};
|
use ra_syntax::{TextUnit, TextRange, SourceFile, TreePtr};
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
@ -36,9 +33,6 @@ salsa::query_group! {
|
||||||
fn source_file(file_id: FileId) -> TreePtr<SourceFile> {
|
fn source_file(file_id: FileId) -> TreePtr<SourceFile> {
|
||||||
type SourceFileQuery;
|
type SourceFileQuery;
|
||||||
}
|
}
|
||||||
fn file_lines(file_id: FileId) -> Arc<LineIndex> {
|
|
||||||
type FileLinesQuery;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,10 +40,6 @@ fn source_file(db: &impl SyntaxDatabase, file_id: FileId) -> TreePtr<SourceFile>
|
||||||
let text = db.file_text(file_id);
|
let text = db.file_text(file_id);
|
||||||
SourceFile::parse(&*text)
|
SourceFile::parse(&*text)
|
||||||
}
|
}
|
||||||
fn file_lines(db: &impl SyntaxDatabase, file_id: FileId) -> Arc<LineIndex> {
|
|
||||||
let text = db.file_text(file_id);
|
|
||||||
Arc::new(LineIndex::new(&*text))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug)]
|
#[derive(Clone, Copy, Debug)]
|
||||||
pub struct FilePosition {
|
pub struct FilePosition {
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
|
@ -14,7 +14,6 @@ parking_lot = "0.7.0"
|
||||||
ena = "0.11"
|
ena = "0.11"
|
||||||
ra_syntax = { path = "../ra_syntax" }
|
ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_arena = { path = "../ra_arena" }
|
ra_arena = { path = "../ra_arena" }
|
||||||
ra_editor = { path = "../ra_editor" }
|
|
||||||
ra_db = { path = "../ra_db" }
|
ra_db = { path = "../ra_db" }
|
||||||
test_utils = { path = "../test_utils" }
|
test_utils = { path = "../test_utils" }
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -215,7 +215,6 @@ salsa::database_storage! {
|
||||||
}
|
}
|
||||||
impl ra_db::SyntaxDatabase {
|
impl ra_db::SyntaxDatabase {
|
||||||
fn source_file() for ra_db::SourceFileQuery;
|
fn source_file() for ra_db::SourceFileQuery;
|
||||||
fn file_lines() for ra_db::FileLinesQuery;
|
|
||||||
}
|
}
|
||||||
impl db::HirDatabase {
|
impl db::HirDatabase {
|
||||||
fn hir_source_file() for db::HirSourceFileQuery;
|
fn hir_source_file() for db::HirSourceFileQuery;
|
||||||
|
|
|
@ -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::{
|
||||||
|
|
|
@ -1049,6 +1049,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn infer(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<InferenceResult>> {
|
pub fn infer(db: &impl HirDatabase, def_id: DefId) -> Cancelable<Arc<InferenceResult>> {
|
||||||
|
db.check_canceled()?;
|
||||||
let function = Function::new(def_id); // TODO: consts also need inference
|
let function = Function::new(def_id); // TODO: consts also need inference
|
||||||
let body = function.body(db)?;
|
let body = function.body(db)?;
|
||||||
let scopes = db.fn_scopes(def_id)?;
|
let scopes = db.fn_scopes(def_id)?;
|
||||||
|
|
|
@ -28,7 +28,6 @@ parking_lot = "0.7.0"
|
||||||
|
|
||||||
thread_worker = { path = "../thread_worker" }
|
thread_worker = { path = "../thread_worker" }
|
||||||
ra_syntax = { path = "../ra_syntax" }
|
ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_editor = { path = "../ra_editor" }
|
|
||||||
ra_text_edit = { path = "../ra_text_edit" }
|
ra_text_edit = { path = "../ra_text_edit" }
|
||||||
ra_analysis = { path = "../ra_analysis" }
|
ra_analysis = { path = "../ra_analysis" }
|
||||||
gen_lsp_server = { path = "../gen_lsp_server" }
|
gen_lsp_server = { path = "../gen_lsp_server" }
|
||||||
|
|
|
@ -7,8 +7,8 @@ use languageserver_types::{
|
||||||
use ra_analysis::{
|
use ra_analysis::{
|
||||||
CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
|
CompletionItem, CompletionItemKind, FileId, FilePosition, FileRange, FileSystemEdit,
|
||||||
InsertText, NavigationTarget, SourceChange, SourceFileEdit,
|
InsertText, NavigationTarget, SourceChange, SourceFileEdit,
|
||||||
|
LineCol, LineIndex, translate_offset_with_edit
|
||||||
};
|
};
|
||||||
use ra_editor::{translate_offset_with_edit, LineCol, LineIndex};
|
|
||||||
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
|
use ra_syntax::{SyntaxKind, TextRange, TextUnit};
|
||||||
use ra_text_edit::{AtomTextEdit, TextEdit};
|
use ra_text_edit::{AtomTextEdit, TextEdit};
|
||||||
|
|
||||||
|
|
|
@ -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,19 @@ pub fn find_leaf_at_offset(node: &SyntaxNode, offset: TextUnit) -> LeafAtOffset<
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Finds a node of specific Ast type at offset. Note that this is slightly
|
||||||
|
/// impercise: if the cursor is strictly betwen two nodes of the desired type,
|
||||||
|
/// as in
|
||||||
|
///
|
||||||
|
/// ```no-run
|
||||||
|
/// struct Foo {}|struct Bar;
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// then the left node will be silently prefered.
|
||||||
|
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))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue