Skip to content

Syntax ptr #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/ra_analysis/src/descriptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ pub(crate) mod module;

use ra_syntax::{
ast::{self, AstNode, NameOwner},
text_utils::is_subrange,
};

#[derive(Debug, Clone)]
Expand All @@ -23,7 +22,7 @@ impl FnDescriptor {
let label: String = node
.syntax()
.children()
.filter(|child| !is_subrange(body_range, child.range()))
.filter(|child| !child.range().is_subrange(&body_range))
.map(|node| node.text().to_string())
.collect();
label
Expand Down
1 change: 1 addition & 0 deletions crates/ra_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod descriptors;
mod imp;
mod symbol_index;
mod completion;
mod syntax_ptr;

use std::{
fmt,
Expand Down
67 changes: 67 additions & 0 deletions crates/ra_analysis/src/syntax_ptr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use ra_syntax::{
File, TextRange, SyntaxKind, SyntaxNode, SyntaxNodeRef,
ast::{self, AstNode},
};

use crate::FileId;
use crate::db::SyntaxDatabase;

/// SyntaxPtr is a cheap `Copy` id which identifies a particular syntax node,
/// without retainig syntax tree in memory. You need to explicitelly `resovle`
/// `SyntaxPtr` to get a `SyntaxNode`
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct SyntaxPtr {
file_id: FileId,
local: LocalSyntaxPtr,
}

impl SyntaxPtr {
pub(crate) fn new(file_id: FileId, node: SyntaxNodeRef) -> SyntaxPtr {
let local = LocalSyntaxPtr::new(node);
SyntaxPtr { file_id, local }
}

pub(crate) fn resolve(self, db: &impl SyntaxDatabase) -> SyntaxNode {
let syntax = db.file_syntax(self.file_id);
self.local.resolve(&syntax)
}
}


/// A pionter to a syntax node inside a file.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct LocalSyntaxPtr {
range: TextRange,
kind: SyntaxKind,
}

impl LocalSyntaxPtr {
fn new(node: SyntaxNodeRef) -> LocalSyntaxPtr {
LocalSyntaxPtr {
range: node.range(),
kind: node.kind(),
}
}

fn resolve(self, file: &File) -> SyntaxNode {
let mut curr = file.syntax();
loop {
if curr.range() == self.range && curr.kind() == self.kind {
return curr.owned();
}
curr = curr.children()
.find(|it| self.range.is_subrange(&it.range()))
.unwrap_or_else(|| panic!("can't resovle local ptr to SyntaxNode: {:?}", self))
}
}
}


#[test]
fn test_local_syntax_ptr() {
let file = File::parse("struct Foo { f: u32, }");
let field = file.syntax().descendants().find_map(ast::NamedFieldDef::cast).unwrap();
let ptr = LocalSyntaxPtr::new(field.syntax());
let field_syntax = ptr.resolve(&file);
assert_eq!(field.syntax(), field_syntax);
}
3 changes: 1 addition & 2 deletions crates/ra_editor/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use rustc_hash::{FxHashMap, FxHashSet};
use ra_syntax::{
algo::visit::{visitor, visitor_ctx, Visitor, VisitorCtx},
ast::{self, AstChildren, LoopBodyOwner, ModuleItemOwner},
text_utils::is_subrange,
AstNode, File,
SyntaxKind::*,
SyntaxNodeRef, TextUnit,
Expand Down Expand Up @@ -191,7 +190,7 @@ fn is_in_loop_body(name_ref: ast::NameRef) -> bool {
.visit::<ast::LoopExpr, _>(LoopBodyOwner::loop_body)
.accept(node);
if let Some(Some(body)) = loop_body {
if is_subrange(body.syntax().range(), name_ref.syntax().range()) {
if name_ref.syntax().range().is_subrange(&body.syntax().range()) {
return true;
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/ra_syntax/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ itertools = "0.7.8"
drop_bomb = "0.1.4"
parking_lot = "0.6.0"
rowan = "0.1.1"
text_unit = "0.1.5"

[dev-dependencies]
test_utils = { path = "../test_utils" }
Expand Down
4 changes: 2 additions & 2 deletions crates/ra_syntax/src/algo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod visit;
// pub mod walk;

use crate::{
text_utils::{contains_offset_nonstrict, is_subrange},
text_utils::{contains_offset_nonstrict},
SyntaxNodeRef, TextRange, TextUnit,
};

Expand Down Expand Up @@ -91,7 +91,7 @@ impl<'f> Iterator for LeafAtOffset<'f> {

pub fn find_covering_node(root: SyntaxNodeRef, range: TextRange) -> SyntaxNodeRef {
assert!(
is_subrange(root.range(), range),
range.is_subrange(&root.range()),
"node range: {:?}, target range: {:?}",
root.range(),
range,
Expand Down
4 changes: 0 additions & 4 deletions crates/ra_syntax/src/text_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ pub fn contains_offset_nonstrict(range: TextRange, offset: TextUnit) -> bool {
range.start() <= offset && offset <= range.end()
}

pub fn is_subrange(range: TextRange, subrange: TextRange) -> bool {
range.start() <= subrange.start() && subrange.end() <= range.end()
}

pub fn intersect(r1: TextRange, r2: TextRange) -> Option<TextRange> {
let start = r1.start().max(r2.start());
let end = r1.end().min(r2.end());
Expand Down