From d3c90ded2b9a4f75e101fa3abc60cd3aebc439c9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Aug 2018 22:00:13 +0300 Subject: Borrowed AST --- crates/libeditor/src/code_actions.rs | 15 +++++----- crates/libeditor/src/extend_selection.rs | 5 ++-- crates/libeditor/src/lib.rs | 26 ++++++++--------- crates/libeditor/src/symbols.rs | 50 ++++++++++++++++---------------- 4 files changed, 47 insertions(+), 49 deletions(-) (limited to 'crates/libeditor/src') diff --git a/crates/libeditor/src/code_actions.rs b/crates/libeditor/src/code_actions.rs index bb6eb0d61..80c396337 100644 --- a/crates/libeditor/src/code_actions.rs +++ b/crates/libeditor/src/code_actions.rs @@ -1,8 +1,8 @@ -use {TextUnit, File, EditBuilder, Edit}; +use {TextUnit, EditBuilder, Edit}; use libsyntax2::{ - ast::{self, AstNode, AttrsOwner}, + ast::{self, AstNode, AttrsOwner, ParsedFile}, SyntaxKind::COMMA, - SyntaxNodeRef, RefRoot, + SyntaxNodeRef, algo::{ Direction, siblings, find_leaf_at_offset, ancestors, @@ -19,9 +19,8 @@ pub enum CursorPosition { Offset(TextUnit), } -pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option ActionResult + 'a> { +pub fn flip_comma<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option ActionResult + 'a> { let syntax = file.syntax(); - let syntax = syntax.as_ref(); let comma = find_leaf_at_offset(syntax, offset).find(|leaf| leaf.kind() == COMMA)?; let left = non_trivia_sibling(comma, Direction::Backward)?; @@ -37,8 +36,8 @@ pub fn flip_comma<'a>(file: &'a File, offset: TextUnit) -> Option(file: &'a File, offset: TextUnit) -> Option ActionResult + 'a> { - let nominal = find_node::>(file.syntax_ref(), offset)?; +pub fn add_derive<'a>(file: &'a ParsedFile, offset: TextUnit) -> Option ActionResult + 'a> { + let nominal = find_node::(file.syntax(), offset)?; Some(move || { let derive_attr = nominal .attrs() @@ -70,7 +69,7 @@ fn non_trivia_sibling(node: SyntaxNodeRef, direction: Direction) -> Option>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option { +pub fn find_node<'a, N: AstNode<'a>>(syntax: SyntaxNodeRef<'a>, offset: TextUnit) -> Option { let leaves = find_leaf_at_offset(syntax, offset); let leaf = leaves.clone() .find(|leaf| !leaf.kind().is_trivia()) diff --git a/crates/libeditor/src/extend_selection.rs b/crates/libeditor/src/extend_selection.rs index ed7d9b3f7..cb6edb576 100644 --- a/crates/libeditor/src/extend_selection.rs +++ b/crates/libeditor/src/extend_selection.rs @@ -1,11 +1,10 @@ use libsyntax2::{ - ast, AstNode, - TextRange, SyntaxNodeRef, + ParsedFile, TextRange, SyntaxNodeRef, SyntaxKind::WHITESPACE, algo::{find_leaf_at_offset, find_covering_node, ancestors}, }; -pub fn extend_selection(file: &ast::File, range: TextRange) -> Option { +pub fn extend_selection(file: &ParsedFile, range: TextRange) -> Option { let syntax = file.syntax(); extend(syntax.as_ref(), range) } diff --git a/crates/libeditor/src/lib.rs b/crates/libeditor/src/lib.rs index 76cb4d028..d9d0369f6 100644 --- a/crates/libeditor/src/lib.rs +++ b/crates/libeditor/src/lib.rs @@ -15,7 +15,7 @@ use libsyntax2::{ algo::{walk, find_leaf_at_offset}, SyntaxKind::{self, *}, }; -pub use libsyntax2::{File, TextRange, TextUnit}; +pub use libsyntax2::{ParsedFile, TextRange, TextUnit}; pub use self::{ line_index::{LineIndex, LineCol}, extend_selection::extend_selection, @@ -51,18 +51,18 @@ pub enum RunnableKind { Bin, } -pub fn parse(text: &str) -> ast::File { - ast::File::parse(text) +pub fn parse(text: &str) -> ast::ParsedFile { + ast::ParsedFile::parse(text) } -pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option { +pub fn matching_brace(file: &ast::ParsedFile, offset: TextUnit) -> Option { const BRACES: &[SyntaxKind] = &[ L_CURLY, R_CURLY, L_BRACK, R_BRACK, L_PAREN, R_PAREN, L_ANGLE, R_ANGLE, ]; - let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax_ref(), offset) + let (brace_node, brace_idx) = find_leaf_at_offset(file.syntax(), offset) .filter_map(|node| { let idx = BRACES.iter().position(|&brace| brace == node.kind())?; Some((node, idx)) @@ -75,9 +75,9 @@ pub fn matching_brace(file: &ast::File, offset: TextUnit) -> Option { Some(matching_node.range().start()) } -pub fn highlight(file: &ast::File) -> Vec { +pub fn highlight(file: &ast::ParsedFile) -> Vec { let mut res = Vec::new(); - for node in walk::preorder(file.syntax_ref()) { + for node in walk::preorder(file.syntax()) { let tag = match node.kind() { ERROR => "error", COMMENT | DOC_COMMENT => "comment", @@ -98,10 +98,10 @@ pub fn highlight(file: &ast::File) -> Vec { res } -pub fn diagnostics(file: &ast::File) -> Vec { +pub fn diagnostics(file: &ast::ParsedFile) -> Vec { let mut res = Vec::new(); - for node in walk::preorder(file.syntax_ref()) { + for node in walk::preorder(file.syntax()) { if node.kind() == ERROR { res.push(Diagnostic { range: node.range(), @@ -116,12 +116,12 @@ pub fn diagnostics(file: &ast::File) -> Vec { res } -pub fn syntax_tree(file: &ast::File) -> String { - ::libsyntax2::utils::dump_tree(&file.syntax()) +pub fn syntax_tree(file: &ast::ParsedFile) -> String { + ::libsyntax2::utils::dump_tree(file.syntax()) } -pub fn runnables(file: &ast::File) -> Vec { - file +pub fn runnables(file: &ast::ParsedFile) -> Vec { + file.ast() .functions() .filter_map(|f| { let name = f.name()?.text(); diff --git a/crates/libeditor/src/symbols.rs b/crates/libeditor/src/symbols.rs index cf5bd2a41..d7bd111e6 100644 --- a/crates/libeditor/src/symbols.rs +++ b/crates/libeditor/src/symbols.rs @@ -1,6 +1,6 @@ use smol_str::SmolStr; use libsyntax2::{ - SyntaxKind, SyntaxNodeRef, AstNode, RefRoot, + SyntaxKind, SyntaxNodeRef, AstNode, ParsedFile, ast::{self, NameOwner}, algo::{ visit::{visitor, Visitor}, @@ -25,14 +25,14 @@ pub struct FileSymbol { pub kind: SyntaxKind, } -pub fn file_symbols(file: &ast::File) -> Vec { - preorder(file.syntax_ref()) +pub fn file_symbols(file: &ParsedFile) -> Vec { + preorder(file.syntax()) .filter_map(to_symbol) .collect() } fn to_symbol(node: SyntaxNodeRef) -> Option { - fn decl<'a, N: NameOwner>>(node: N) -> Option { + fn decl<'a, N: NameOwner<'a>>(node: N) -> Option { let name = node.name()?; Some(FileSymbol { name: name.text(), @@ -41,23 +41,23 @@ fn to_symbol(node: SyntaxNodeRef) -> Option { }) } visitor() - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) .accept(node)? } -pub fn file_structure(file: &ast::File) -> Vec { +pub fn file_structure(file: &ParsedFile) -> Vec { let mut res = Vec::new(); let mut stack = Vec::new(); - for event in walk(file.syntax_ref()) { + for event in walk(file.syntax()) { match event { WalkEvent::Enter(node) => { match structure_node(node) { @@ -80,7 +80,7 @@ pub fn file_structure(file: &ast::File) -> Vec { } fn structure_node(node: SyntaxNodeRef) -> Option { - fn decl<'a, N: NameOwner>>(node: N) -> Option { + fn decl<'a, N: NameOwner<'a>>(node: N) -> Option { let name = node.name()?; Some(StructureNode { parent: None, @@ -92,16 +92,16 @@ fn structure_node(node: SyntaxNodeRef) -> Option { } visitor() - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(decl::>) - .visit(|im: ast::ImplItem<_>| { + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(decl::) + .visit(|im: ast::ImplItem| { let target_type = im.target_type()?; let target_trait = im.target_trait(); let label = match target_trait { -- cgit v1.2.3