From 6d5d82e412dea19ea48eecc6f7d5a4aa223a9599 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 20 Jul 2019 12:48:24 +0300 Subject: [PATCH 1/2] move debug_dump to fmt::Debug --- crates/ra_cli/src/main.rs | 2 +- crates/ra_hir/src/expr.rs | 2 +- .../src/display/navigation_target.rs | 2 +- crates/ra_ide_api/src/syntax_tree.rs | 6 +- crates/ra_mbe/src/tests.rs | 8 +-- crates/ra_syntax/src/fuzz.rs | 6 +- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/parsing/reparsing.rs | 4 +- crates/ra_syntax/src/syntax_node.rs | 56 ++++++++----------- crates/ra_syntax/src/validation.rs | 4 +- 10 files changed, 42 insertions(+), 50 deletions(-) diff --git a/crates/ra_cli/src/main.rs b/crates/ra_cli/src/main.rs index 375e2f508fa..de8191ca316 100644 --- a/crates/ra_cli/src/main.rs +++ b/crates/ra_cli/src/main.rs @@ -55,7 +55,7 @@ fn main() -> Result<()> { let _p = profile("parsing"); let file = file()?; if !matches.is_present("no-dump") { - println!("{}", file.syntax().debug_dump()); + println!("{:#?}", file.syntax()); } std::mem::forget(file); } diff --git a/crates/ra_hir/src/expr.rs b/crates/ra_hir/src/expr.rs index 66cb9633bbb..6eed67f50c1 100644 --- a/crates/ra_hir/src/expr.rs +++ b/crates/ra_hir/src/expr.rs @@ -843,7 +843,7 @@ where let file_id = call_id.as_file(MacroFileKind::Expr); if let Some(node) = self.db.parse_or_expand(file_id) { if let Some(expr) = ast::Expr::cast(node) { - log::debug!("macro expansion {}", expr.syntax().debug_dump()); + log::debug!("macro expansion {:#?}", expr.syntax()); let old_file_id = std::mem::replace(&mut self.current_file_id, file_id); let id = self.collect_expr(expr); diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index 8aff5f2cdf1..c212ebf280a 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -233,7 +233,7 @@ impl NavigationTarget { pub(crate) fn from_macro_def(db: &RootDatabase, macro_call: hir::MacroDef) -> NavigationTarget { let src = macro_call.source(db); - log::debug!("nav target {}", src.ast.syntax().debug_dump()); + log::debug!("nav target {:#?}", src.ast.syntax()); NavigationTarget::from_named( src.file_id.original_file(db), &src.ast, diff --git a/crates/ra_ide_api/src/syntax_tree.rs b/crates/ra_ide_api/src/syntax_tree.rs index 3d7373d02bc..ae1849aaa63 100644 --- a/crates/ra_ide_api/src/syntax_tree.rs +++ b/crates/ra_ide_api/src/syntax_tree.rs @@ -25,9 +25,9 @@ pub(crate) fn syntax_tree( } }; - node.debug_dump() + format!("{:#?}", node) } else { - parse.tree().syntax().debug_dump() + format!("{:#?}", parse.tree().syntax()) } } @@ -85,7 +85,7 @@ fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option { impl Parse { pub fn debug_dump(&self) -> String { - let mut buf = self.tree().syntax().debug_dump(); + let mut buf = format!("{:#?}", self.tree().syntax()); for err in self.errors.iter() { writeln!(buf, "error {:?}: {}", err.location(), err.kind()).unwrap(); } diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index b4ad9e0199a..c7183299b58 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -188,8 +188,8 @@ mod tests { }; assert_eq_text!( - &fully_reparsed.tree().syntax().debug_dump(), - &incrementally_reparsed.tree().syntax().debug_dump(), + &format!("{:#?}", fully_reparsed.tree().syntax()), + &format!("{:#?}", incrementally_reparsed.tree().syntax()), ); } diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 51bae04de77..3b20e96e267 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -6,11 +6,7 @@ //! The *real* implementation is in the (language-agnostic) `rowan` crate, this //! modules just wraps its API. -use std::{ - fmt::{self, Write}, - iter::successors, - ops::RangeInclusive, -}; +use std::{fmt, iter::successors, ops::RangeInclusive}; use ra_parser::ParseError; use rowan::GreenNodeBuilder; @@ -36,8 +32,29 @@ pub enum InsertPosition { pub struct SyntaxNode(pub(crate) rowan::cursor::SyntaxNode); impl fmt::Debug for SyntaxNode { - fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{:?}@{:?}", self.kind(), self.range()) + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + if f.alternate() { + let mut level = 0; + for event in self.preorder_with_tokens() { + match event { + WalkEvent::Enter(element) => { + for _ in 0..level { + write!(f, " ")?; + } + match element { + SyntaxElement::Node(node) => writeln!(f, "{:?}", node)?, + SyntaxElement::Token(token) => writeln!(f, "{:?}", token)?, + } + level += 1; + } + WalkEvent::Leave(_) => level -= 1, + } + } + assert_eq!(level, 0); + Ok(()) + } else { + write!(f, "{:?}@{:?}", self.kind(), self.range()) + } } } @@ -173,31 +190,6 @@ impl SyntaxNode { }) } - pub fn debug_dump(&self) -> String { - let mut level = 0; - let mut buf = String::new(); - - for event in self.preorder_with_tokens() { - match event { - WalkEvent::Enter(element) => { - for _ in 0..level { - buf.push_str(" "); - } - match element { - SyntaxElement::Node(node) => writeln!(buf, "{:?}", node).unwrap(), - SyntaxElement::Token(token) => writeln!(buf, "{:?}", token).unwrap(), - } - level += 1; - } - WalkEvent::Leave(_) => level -= 1, - } - } - - assert_eq!(level, 0); - - buf - } - pub(crate) fn replace_with(&self, replacement: GreenNode) -> GreenNode { self.0.replace_with(replacement) } diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 7140d10c333..943f88de9a3 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -89,9 +89,9 @@ pub(crate) fn validate_block_structure(root: &SyntaxNode) { assert_eq!( node.parent(), pair.parent(), - "\nunpaired curleys:\n{}\n{}\n", + "\nunpaired curleys:\n{}\n{:#?}\n", root.text(), - root.debug_dump(), + root, ); assert!( node.next_sibling().is_none() && pair.prev_sibling().is_none(), From f3bdbec1b68fa0e20f0b7b6c6ef64e1507970b0d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 20 Jul 2019 12:58:27 +0300 Subject: [PATCH 2/2] rename range -> text_range --- crates/ra_assists/src/add_derive.rs | 6 +-- crates/ra_assists/src/add_explicit_type.rs | 4 +- crates/ra_assists/src/add_impl.rs | 4 +- .../src/add_missing_impl_members.rs | 4 +- crates/ra_assists/src/assist_ctx.rs | 2 +- crates/ra_assists/src/ast_editor.rs | 5 +- crates/ra_assists/src/auto_import.rs | 18 +++---- crates/ra_assists/src/change_visibility.rs | 23 ++++----- crates/ra_assists/src/fill_match_arms.rs | 4 +- crates/ra_assists/src/flip_binexpr.rs | 6 +-- crates/ra_assists/src/flip_comma.rs | 6 +-- .../ra_assists/src/inline_local_variable.rs | 7 ++- crates/ra_assists/src/introduce_variable.rs | 12 ++--- crates/ra_assists/src/move_guard.rs | 20 ++++---- crates/ra_assists/src/remove_dbg.rs | 4 +- .../src/replace_if_let_with_match.rs | 4 +- crates/ra_assists/src/split_import.rs | 8 ++-- crates/ra_cli/src/analysis_stats.rs | 2 +- crates/ra_hir/src/expr/scope.rs | 2 +- crates/ra_hir/src/source_binder.rs | 2 +- crates/ra_hir/src/ty/tests.rs | 2 +- crates/ra_ide_api/src/call_info.rs | 4 +- .../src/completion/complete_keyword.rs | 2 +- .../src/completion/complete_path.rs | 2 +- .../src/completion/complete_postfix.rs | 2 +- .../src/completion/completion_context.rs | 32 ++++++++----- crates/ra_ide_api/src/diagnostics.rs | 12 ++--- .../src/display/navigation_target.rs | 4 +- crates/ra_ide_api/src/display/structure.rs | 8 ++-- crates/ra_ide_api/src/extend_selection.rs | 48 ++++++++++--------- crates/ra_ide_api/src/folding_ranges.rs | 9 ++-- crates/ra_ide_api/src/goto_definition.rs | 4 +- crates/ra_ide_api/src/goto_type_definition.rs | 2 +- crates/ra_ide_api/src/hover.rs | 10 ++-- crates/ra_ide_api/src/impls.rs | 4 +- crates/ra_ide_api/src/join_lines.rs | 22 ++++----- crates/ra_ide_api/src/matching_brace.rs | 2 +- crates/ra_ide_api/src/references.rs | 2 +- crates/ra_ide_api/src/runnables.rs | 4 +- crates/ra_ide_api/src/symbol_index.rs | 2 +- crates/ra_ide_api/src/syntax_highlighting.rs | 14 +++--- crates/ra_ide_api/src/syntax_tree.rs | 2 +- crates/ra_ide_api/src/typing.rs | 8 ++-- crates/ra_mbe/src/syntax_bridge.rs | 6 +-- crates/ra_syntax/src/algo.rs | 2 +- crates/ra_syntax/src/fuzz.rs | 2 +- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/parsing/reparsing.rs | 12 +++-- crates/ra_syntax/src/ptr.rs | 6 +-- crates/ra_syntax/src/syntax_node.rs | 14 +++--- crates/ra_syntax/src/syntax_text.rs | 8 ++-- crates/ra_syntax/src/validation.rs | 8 ++-- crates/ra_syntax/src/validation/block.rs | 5 +- crates/ra_syntax/src/validation/field_expr.rs | 2 +- 54 files changed, 219 insertions(+), 192 deletions(-) diff --git a/crates/ra_assists/src/add_derive.rs b/crates/ra_assists/src/add_derive.rs index f19196f53d6..9c88644df48 100644 --- a/crates/ra_assists/src/add_derive.rs +++ b/crates/ra_assists/src/add_derive.rs @@ -22,9 +22,9 @@ pub(crate) fn add_derive(mut ctx: AssistCtx) -> Option edit.insert(node_start, "#[derive()]\n"); node_start + TextUnit::of_str("#[derive(") } - Some(tt) => tt.syntax().range().end() - TextUnit::of_char(')'), + Some(tt) => tt.syntax().text_range().end() - TextUnit::of_char(')'), }; - edit.target(nominal.syntax().range()); + edit.target(nominal.syntax().text_range()); edit.set_cursor(offset) }); @@ -37,7 +37,7 @@ fn derive_insertion_offset(nominal: &ast::NominalDef) -> Option { .syntax() .children_with_tokens() .find(|it| it.kind() != COMMENT && it.kind() != WHITESPACE)?; - Some(non_ws_child.range().start()) + Some(non_ws_child.text_range().start()) } #[cfg(test)] diff --git a/crates/ra_assists/src/add_explicit_type.rs b/crates/ra_assists/src/add_explicit_type.rs index a69cfc8e3a3..88970929ffc 100644 --- a/crates/ra_assists/src/add_explicit_type.rs +++ b/crates/ra_assists/src/add_explicit_type.rs @@ -16,10 +16,10 @@ pub(crate) fn add_explicit_type(mut ctx: AssistCtx) -> Option< PatKind::BindPat(bind_pat) => bind_pat, _ => return None, }; - let pat_range = pat.syntax().range(); + let pat_range = pat.syntax().text_range(); // The binding must have a name let name = pat.name()?; - let name_range = name.syntax().range(); + let name_range = name.syntax().text_range(); // Assist not applicable if the type has already been specified if stmt.syntax().children_with_tokens().any(|child| child.kind() == T![:]) { return None; diff --git a/crates/ra_assists/src/add_impl.rs b/crates/ra_assists/src/add_impl.rs index cebc1953967..9a0cfb4e7fd 100644 --- a/crates/ra_assists/src/add_impl.rs +++ b/crates/ra_assists/src/add_impl.rs @@ -11,9 +11,9 @@ pub(crate) fn add_impl(mut ctx: AssistCtx) -> Option { let nominal = ctx.node_at_offset::()?; let name = nominal.name()?; ctx.add_action(AssistId("add_impl"), "add impl", |edit| { - edit.target(nominal.syntax().range()); + edit.target(nominal.syntax().text_range()); let type_params = nominal.type_param_list(); - let start_offset = nominal.syntax().range().end(); + let start_offset = nominal.syntax().text_range().end(); let mut buf = String::new(); buf.push_str("\n\nimpl"); if let Some(type_params) = &type_params { diff --git a/crates/ra_assists/src/add_missing_impl_members.rs b/crates/ra_assists/src/add_missing_impl_members.rs index b992a4dc802..995e44d5eed 100644 --- a/crates/ra_assists/src/add_missing_impl_members.rs +++ b/crates/ra_assists/src/add_missing_impl_members.rs @@ -43,7 +43,7 @@ fn add_missing_impl_members_inner( let trait_def = { let file_id = ctx.frange.file_id; - let position = FilePosition { file_id, offset: impl_node.syntax().range().start() }; + let position = FilePosition { file_id, offset: impl_node.syntax().text_range().start() }; let analyzer = hir::SourceAnalyzer::new(ctx.db, position.file_id, impl_node.syntax(), None); resolve_target_trait_def(ctx.db, &analyzer, &impl_node)? @@ -87,7 +87,7 @@ fn add_missing_impl_members_inner( ast_editor.append_items(items); let first_new_item = ast_editor.ast().impl_items().nth(n_existing_items).unwrap(); - let cursor_position = first_new_item.syntax().range().start(); + let cursor_position = first_new_item.syntax().text_range().start(); ast_editor.into_text_edit(edit.text_edit_builder()); edit.set_cursor(cursor_position); diff --git a/crates/ra_assists/src/assist_ctx.rs b/crates/ra_assists/src/assist_ctx.rs index e52085f85df..4d5a76de673 100644 --- a/crates/ra_assists/src/assist_ctx.rs +++ b/crates/ra_assists/src/assist_ctx.rs @@ -141,7 +141,7 @@ impl AssistBuilder { if let Some(indent) = leading_indent(node) { replace_with = reindent(&replace_with, &indent) } - self.replace(node.range(), replace_with) + self.replace(node.text_range(), replace_with) } pub(crate) fn set_edit_builder(&mut self, edit: TextEditBuilder) { diff --git a/crates/ra_assists/src/ast_editor.rs b/crates/ra_assists/src/ast_editor.rs index a35334d7e29..36235eb137f 100644 --- a/crates/ra_assists/src/ast_editor.rs +++ b/crates/ra_assists/src/ast_editor.rs @@ -22,7 +22,10 @@ impl AstEditor { // FIXME: compute a more fine-grained diff here. // If *you* know a nice algorithm to compute diff between two syntax // tree, tell me about it! - builder.replace(self.original_ast.syntax().range(), self.ast().syntax().text().to_string()); + builder.replace( + self.original_ast.syntax().text_range(), + self.ast().syntax().text().to_string(), + ); } pub fn ast(&self) -> &N { diff --git a/crates/ra_assists/src/auto_import.rs b/crates/ra_assists/src/auto_import.rs index 0eb4bdb6225..43e75eee187 100644 --- a/crates/ra_assists/src/auto_import.rs +++ b/crates/ra_assists/src/auto_import.rs @@ -356,7 +356,7 @@ fn best_action_for_target( // todo: we should include even whitespace blocks as anchor candidates let anchor = container .children() - .find(|n| n.range().start() < anchor.range().start()) + .find(|n| n.text_range().start() < anchor.text_range().start()) .or_else(|| Some(anchor)); ImportAction::add_new_use(anchor, false) @@ -418,7 +418,7 @@ fn make_assist_add_new_use( buf.push_str(&spaces); } } - let position = if after { anchor.range().end() } else { anchor.range().start() }; + let position = if after { anchor.text_range().end() } else { anchor.text_range().start() }; edit.insert(position, buf); } } @@ -434,10 +434,10 @@ fn make_assist_add_in_tree_list( let mut buf = String::new(); let comma = last.syntax().siblings(Direction::Next).find(|n| n.kind() == T![,]); let offset = if let Some(comma) = comma { - comma.range().end() + comma.text_range().end() } else { buf.push_str(","); - last.syntax().range().end() + last.syntax().text_range().end() }; if add_self { buf.push_str(" self") @@ -462,11 +462,11 @@ fn make_assist_add_nested_import( if let Some(use_tree) = use_tree { let (start, add_colon_colon) = if let Some(first_segment_to_split) = first_segment_to_split { - (first_segment_to_split.syntax().range().start(), false) + (first_segment_to_split.syntax().text_range().start(), false) } else { - (use_tree.syntax().range().end(), true) + (use_tree.syntax().text_range().end(), true) }; - let end = use_tree.syntax().range().end(); + let end = use_tree.syntax().text_range().end(); let mut buf = String::new(); if add_colon_colon { @@ -497,8 +497,8 @@ fn apply_auto_import( // Here we are assuming the assist will provide a correct use statement // so we can delete the path qualifier edit.delete(TextRange::from_to( - path.syntax().range().start(), - last.syntax().range().start(), + path.syntax().text_range().start(), + last.syntax().text_range().start(), )); } } diff --git a/crates/ra_assists/src/change_visibility.rs b/crates/ra_assists/src/change_visibility.rs index ab10d2aa44c..d28cdd07bf6 100644 --- a/crates/ra_assists/src/change_visibility.rs +++ b/crates/ra_assists/src/change_visibility.rs @@ -35,14 +35,15 @@ fn add_vis(mut ctx: AssistCtx) -> Option { if parent.children().any(|child| child.kind() == VISIBILITY) { return None; } - (vis_offset(&parent), keyword.range()) + (vis_offset(&parent), keyword.text_range()) } else { let ident = ctx.token_at_offset().find(|leaf| leaf.kind() == IDENT)?; let field = ident.parent().ancestors().find_map(ast::NamedFieldDef::cast)?; - if field.name()?.syntax().range() != ident.range() && field.visibility().is_some() { + if field.name()?.syntax().text_range() != ident.text_range() && field.visibility().is_some() + { return None; } - (vis_offset(field.syntax()), ident.range()) + (vis_offset(field.syntax()), ident.text_range()) }; ctx.add_action(AssistId("change_visibility"), "make pub(crate)", |edit| { @@ -61,25 +62,25 @@ fn vis_offset(node: &SyntaxNode) -> TextUnit { _ => false, }) .next() - .map(|it| it.range().start()) - .unwrap_or_else(|| node.range().start()) + .map(|it| it.text_range().start()) + .unwrap_or_else(|| node.text_range().start()) } fn change_vis(mut ctx: AssistCtx, vis: ast::Visibility) -> Option { if vis.syntax().text() == "pub" { ctx.add_action(AssistId("change_visibility"), "change to pub(crate)", |edit| { - edit.target(vis.syntax().range()); - edit.replace(vis.syntax().range(), "pub(crate)"); - edit.set_cursor(vis.syntax().range().start()) + edit.target(vis.syntax().text_range()); + edit.replace(vis.syntax().text_range(), "pub(crate)"); + edit.set_cursor(vis.syntax().text_range().start()) }); return ctx.build(); } if vis.syntax().text() == "pub(crate)" { ctx.add_action(AssistId("change_visibility"), "change to pub", |edit| { - edit.target(vis.syntax().range()); - edit.replace(vis.syntax().range(), "pub"); - edit.set_cursor(vis.syntax().range().start()); + edit.target(vis.syntax().text_range()); + edit.replace(vis.syntax().text_range(), "pub"); + edit.set_cursor(vis.syntax().text_range().start()); }); return ctx.build(); diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs index b96806ac665..93942989266 100644 --- a/crates/ra_assists/src/fill_match_arms.rs +++ b/crates/ra_assists/src/fill_match_arms.rs @@ -84,8 +84,8 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx) -> Option (),\n"); } buf.push_str("}"); - edit.target(match_expr.syntax().range()); - edit.set_cursor(expr.syntax().range().start()); + edit.target(match_expr.syntax().text_range()); + edit.set_cursor(expr.syntax().text_range().start()); edit.replace_node_and_indent(match_expr.syntax(), buf); }); diff --git a/crates/ra_assists/src/flip_binexpr.rs b/crates/ra_assists/src/flip_binexpr.rs index 2e591ad3bd4..b55b36a8edb 100644 --- a/crates/ra_assists/src/flip_binexpr.rs +++ b/crates/ra_assists/src/flip_binexpr.rs @@ -8,7 +8,7 @@ pub(crate) fn flip_binexpr(mut ctx: AssistCtx) -> Option()?; let lhs = expr.lhs()?.syntax().clone(); let rhs = expr.rhs()?.syntax().clone(); - let op_range = expr.op_token()?.range(); + let op_range = expr.op_token()?.text_range(); // The assist should be applied only if the cursor is on the operator let cursor_in_range = ctx.frange.range.is_subrange(&op_range); if !cursor_in_range { @@ -25,8 +25,8 @@ pub(crate) fn flip_binexpr(mut ctx: AssistCtx) -> Option) -> Option let prev = non_trivia_sibling(comma.clone().into(), Direction::Prev)?; let next = non_trivia_sibling(comma.clone().into(), Direction::Next)?; ctx.add_action(AssistId("flip_comma"), "flip comma", |edit| { - edit.target(comma.range()); - edit.replace(prev.range(), next.to_string()); - edit.replace(next.range(), prev.to_string()); + edit.target(comma.text_range()); + edit.replace(prev.text_range(), next.to_string()); + edit.replace(next.text_range(), prev.to_string()); }); ctx.build() diff --git a/crates/ra_assists/src/inline_local_variable.rs b/crates/ra_assists/src/inline_local_variable.rs index 3c17089deb9..82c4d54b0c6 100644 --- a/crates/ra_assists/src/inline_local_variable.rs +++ b/crates/ra_assists/src/inline_local_variable.rs @@ -22,9 +22,12 @@ pub(crate) fn inline_local_varialbe(mut ctx: AssistCtx) -> Opt .next_sibling_or_token() .and_then(|it| ast::Whitespace::cast(it.as_token()?.clone())) { - TextRange::from_to(let_stmt.syntax().range().start(), whitespace.syntax().range().end()) + TextRange::from_to( + let_stmt.syntax().text_range().start(), + whitespace.syntax().text_range().end(), + ) } else { - let_stmt.syntax().range() + let_stmt.syntax().text_range() }; let analyzer = hir::SourceAnalyzer::new(ctx.db, ctx.frange.file_id, bind_pat.syntax(), None); let refs = analyzer.find_all_refs(&bind_pat); diff --git a/crates/ra_assists/src/introduce_variable.rs b/crates/ra_assists/src/introduce_variable.rs index ce28132c9af..df6c5898992 100644 --- a/crates/ra_assists/src/introduce_variable.rs +++ b/crates/ra_assists/src/introduce_variable.rs @@ -48,7 +48,7 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option if !full_stmt.unwrap().has_semi() { buf.push_str(";"); } - edit.replace(expr.syntax().range(), buf); + edit.replace(expr.syntax().text_range(), buf); } else { buf.push_str(";"); @@ -66,14 +66,14 @@ pub(crate) fn introduce_variable(mut ctx: AssistCtx) -> Option buf.push_str(text); } - edit.target(expr.syntax().range()); - edit.replace(expr.syntax().range(), "var_name".to_string()); - edit.insert(anchor_stmt.range().start(), buf); + edit.target(expr.syntax().text_range()); + edit.replace(expr.syntax().text_range(), "var_name".to_string()); + edit.insert(anchor_stmt.text_range().start(), buf); if wrap_in_block { - edit.insert(anchor_stmt.range().end(), " }"); + edit.insert(anchor_stmt.text_range().end(), " }"); } } - edit.set_cursor(anchor_stmt.range().start() + cursor_offset); + edit.set_cursor(anchor_stmt.text_range().start() + cursor_offset); }); ctx.build() diff --git a/crates/ra_assists/src/move_guard.rs b/crates/ra_assists/src/move_guard.rs index 313c9ad1878..0f3cdbe5335 100644 --- a/crates/ra_assists/src/move_guard.rs +++ b/crates/ra_assists/src/move_guard.rs @@ -17,11 +17,11 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op let buf = format!("if {} {{ {} }}", guard_conditions.syntax().text(), arm_expr.syntax().text()); ctx.add_action(AssistId("move_guard_to_arm_body"), "move guard to arm body", |edit| { - edit.target(guard.syntax().range()); + edit.target(guard.syntax().text_range()); let offseting_amount = match &space_before_guard { Some(SyntaxElement::Token(tok)) => { if let Some(_) = ast::Whitespace::cast(tok.clone()) { - let ele = space_before_guard.unwrap().range(); + let ele = space_before_guard.unwrap().text_range(); edit.delete(ele); ele.len() } else { @@ -31,9 +31,11 @@ pub(crate) fn move_guard_to_arm_body(mut ctx: AssistCtx) -> Op _ => TextUnit::from(0), }; - edit.delete(guard.syntax().range()); + edit.delete(guard.syntax().text_range()); edit.replace_node_and_indent(arm_expr.syntax(), buf); - edit.set_cursor(arm_expr.syntax().range().start() + TextUnit::from(3) - offseting_amount); + edit.set_cursor( + arm_expr.syntax().text_range().start() + TextUnit::from(3) - offseting_amount, + ); }); ctx.build() } @@ -62,18 +64,18 @@ pub(crate) fn move_arm_cond_to_match_guard(mut ctx: AssistCtx) AssistId("move_arm_cond_to_match_guard"), "move condition to match guard", |edit| { - edit.target(if_expr.syntax().range()); + edit.target(if_expr.syntax().text_range()); let then_only_expr = then_block.statements().next().is_none(); match &then_block.expr() { Some(then_expr) if then_only_expr => { - edit.replace(if_expr.syntax().range(), then_expr.syntax().text()) + edit.replace(if_expr.syntax().text_range(), then_expr.syntax().text()) } - _ => edit.replace(if_expr.syntax().range(), then_block.syntax().text()), + _ => edit.replace(if_expr.syntax().text_range(), then_block.syntax().text()), } - edit.insert(last_match_pat.syntax().range().end(), buf); - edit.set_cursor(last_match_pat.syntax().range().end() + TextUnit::from(1)); + edit.insert(last_match_pat.syntax().text_range().end(), buf); + edit.set_cursor(last_match_pat.syntax().text_range().end() + TextUnit::from(1)); }, ); ctx.build() diff --git a/crates/ra_assists/src/remove_dbg.rs b/crates/ra_assists/src/remove_dbg.rs index 5657ee4b874..870133fda07 100644 --- a/crates/ra_assists/src/remove_dbg.rs +++ b/crates/ra_assists/src/remove_dbg.rs @@ -12,7 +12,7 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx) -> Option return None; } - let macro_range = macro_call.syntax().range(); + let macro_range = macro_call.syntax().text_range(); // If the cursor is inside the macro call, we'll try to maintain the cursor // position by subtracting the length of dbg!( from the start of the file @@ -43,7 +43,7 @@ pub(crate) fn remove_dbg(mut ctx: AssistCtx) -> Option }; ctx.add_action(AssistId("remove_dbg"), "remove dbg!()", |edit| { - edit.target(macro_call.syntax().range()); + edit.target(macro_call.syntax().text_range()); edit.replace(macro_range, macro_content); edit.set_cursor(cursor_pos); }); diff --git a/crates/ra_assists/src/replace_if_let_with_match.rs b/crates/ra_assists/src/replace_if_let_with_match.rs index 5de6aa2664a..c0bf6d23512 100644 --- a/crates/ra_assists/src/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/replace_if_let_with_match.rs @@ -17,9 +17,9 @@ pub(crate) fn replace_if_let_with_match(mut ctx: AssistCtx) -> ctx.add_action(AssistId("replace_if_let_with_match"), "replace with match", |edit| { let match_expr = build_match_expr(expr, pat, then_block, else_block); - edit.target(if_expr.syntax().range()); + edit.target(if_expr.syntax().text_range()); edit.replace_node_and_indent(if_expr.syntax(), match_expr); - edit.set_cursor(if_expr.syntax().range().start()) + edit.set_cursor(if_expr.syntax().text_range().start()) }); ctx.build() diff --git a/crates/ra_assists/src/split_import.rs b/crates/ra_assists/src/split_import.rs index a8feb67c8c9..2c1edddb90d 100644 --- a/crates/ra_assists/src/split_import.rs +++ b/crates/ra_assists/src/split_import.rs @@ -15,14 +15,14 @@ pub(crate) fn split_import(mut ctx: AssistCtx) -> Option tree.syntax().range().end(), - None => top_path.syntax().range().end(), + Some(tree) => tree.syntax().text_range().end(), + None => top_path.syntax().text_range().end(), }; ctx.add_action(AssistId("split_import"), "split import", |edit| { - edit.target(colon_colon.range()); + edit.target(colon_colon.text_range()); edit.insert(l_curly, "{"); edit.insert(r_curly, "}"); edit.set_cursor(l_curly + TextUnit::of_str("{")); diff --git a/crates/ra_cli/src/analysis_stats.rs b/crates/ra_cli/src/analysis_stats.rs index c2a2b66d54f..403aab35260 100644 --- a/crates/ra_cli/src/analysis_stats.rs +++ b/crates/ra_cli/src/analysis_stats.rs @@ -70,7 +70,7 @@ pub fn run(verbose: bool, memory_usage: bool, path: &Path, only: Option<&str>) - let src = f.source(db); let original_file = src.file_id.original_file(db); let path = db.file_relative_path(original_file); - let syntax_range = src.ast.syntax().range(); + let syntax_range = src.ast.syntax().text_range(); write!(msg, " ({:?} {})", path, syntax_range).unwrap(); } bar.set_message(&msg); diff --git a/crates/ra_hir/src/expr/scope.rs b/crates/ra_hir/src/expr/scope.rs index 6589b782c49..79e1857f936 100644 --- a/crates/ra_hir/src/expr/scope.rs +++ b/crates/ra_hir/src/expr/scope.rs @@ -296,7 +296,7 @@ mod tests { let local_name_entry = analyzer.resolve_local_name(&name_ref).unwrap(); let local_name = local_name_entry.ptr().either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()); - assert_eq!(local_name.range(), expected_name.syntax().range()); + assert_eq!(local_name.range(), expected_name.syntax().text_range()); } #[test] diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index df67d2c3938..4c173a4f73b 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -361,7 +361,7 @@ impl SourceAnalyzer { }) .map(|name_ref| ReferenceDescriptor { name: name_ref.text().to_string(), - range: name_ref.syntax().range(), + range: name_ref.syntax().text_range(), }) .collect() } diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 265740e5420..7b2bdeb3f62 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -3127,7 +3127,7 @@ fn infer(content: &str) -> String { for (syntax_ptr, ty) in &types { let node = syntax_ptr.to_node(source_file.syntax()); let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) { - (self_param.self_kw_token().range(), "self".to_string()) + (self_param.self_kw_token().text_range(), "self".to_string()) } else { (syntax_ptr.range(), node.text().to_string().replace("\n", " ")) }; diff --git a/crates/ra_ide_api/src/call_info.rs b/crates/ra_ide_api/src/call_info.rs index 270499612bb..212448d413b 100644 --- a/crates/ra_ide_api/src/call_info.rs +++ b/crates/ra_ide_api/src/call_info.rs @@ -47,7 +47,7 @@ pub(crate) fn call_info(db: &RootDatabase, position: FilePosition) -> Option Option bool { .visit::(|it| it.loop_body()) .accept(&node); if let Some(Some(body)) = loop_body { - if leaf.range().is_subrange(&body.syntax().range()) { + if leaf.text_range().is_subrange(&body.syntax().text_range()) { return true; } } diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 29586cd1f13..5fd41eeb35c 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -26,7 +26,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { if Some(module) == ctx.module { if let Some(import) = res.import { if let Either::A(use_tree) = module.import_source(ctx.db, import) { - if use_tree.syntax().range().contains_inclusive(ctx.offset) { + if use_tree.syntax().text_range().contains_inclusive(ctx.offset) { // for `use self::foo<|>`, don't suggest `foo` as a completion tested_by!(dont_complete_current_use); continue; diff --git a/crates/ra_ide_api/src/completion/complete_postfix.rs b/crates/ra_ide_api/src/completion/complete_postfix.rs index c75b1c159c3..6d834f6cec4 100644 --- a/crates/ra_ide_api/src/completion/complete_postfix.rs +++ b/crates/ra_ide_api/src/completion/complete_postfix.rs @@ -12,7 +12,7 @@ use ra_text_edit::TextEditBuilder; fn postfix_snippet(ctx: &CompletionContext, label: &str, detail: &str, snippet: &str) -> Builder { let edit = { let receiver_range = - ctx.dot_receiver.as_ref().expect("no receiver available").syntax().range(); + ctx.dot_receiver.as_ref().expect("no receiver available").syntax().text_range(); let delete_range = TextRange::from_to(receiver_range.start(), ctx.source_range().end()); let mut builder = TextEditBuilder::default(); builder.replace(delete_range, snippet.to_string()); diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index b803271abaf..2f78d5409c6 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -78,7 +78,7 @@ impl<'a> CompletionContext<'a> { pub(crate) fn source_range(&self) -> TextRange { match self.token.kind() { // workaroud when completion is triggered by trigger characters. - IDENT => self.token.range(), + IDENT => self.token.text_range(), _ => TextRange::offset_len(self.offset, 0.into()), } } @@ -123,13 +123,17 @@ impl<'a> CompletionContext<'a> { } fn classify_name_ref(&mut self, original_file: SourceFile, name_ref: ast::NameRef) { - let name_range = name_ref.syntax().range(); + let name_range = name_ref.syntax().text_range(); if name_ref.syntax().parent().and_then(ast::NamedField::cast).is_some() { self.struct_lit_syntax = find_node_at_offset(original_file.syntax(), self.offset); } - let top_node = - name_ref.syntax().ancestors().take_while(|it| it.range() == name_range).last().unwrap(); + let top_node = name_ref + .syntax() + .ancestors() + .take_while(|it| it.text_range() == name_range) + .last() + .unwrap(); match top_node.parent().map(|it| it.kind()) { Some(SOURCE_FILE) | Some(ITEM_LIST) => { @@ -180,23 +184,27 @@ impl<'a> CompletionContext<'a> { .ancestors() .find_map(|node| { if let Some(stmt) = ast::ExprStmt::cast(node.clone()) { - return Some(stmt.syntax().range() == name_ref.syntax().range()); + return Some( + stmt.syntax().text_range() == name_ref.syntax().text_range(), + ); } if let Some(block) = ast::Block::cast(node) { return Some( - block.expr().map(|e| e.syntax().range()) - == Some(name_ref.syntax().range()), + block.expr().map(|e| e.syntax().text_range()) + == Some(name_ref.syntax().text_range()), ); } None }) .unwrap_or(false); - if let Some(off) = name_ref.syntax().range().start().checked_sub(2.into()) { + if let Some(off) = name_ref.syntax().text_range().start().checked_sub(2.into()) { if let Some(if_expr) = find_node_at_offset::(original_file.syntax(), off) { - if if_expr.syntax().range().end() < name_ref.syntax().range().start() { + if if_expr.syntax().text_range().end() + < name_ref.syntax().text_range().start() + { self.after_if = true; } } @@ -208,14 +216,14 @@ impl<'a> CompletionContext<'a> { // ident, so it should have the same range in the non-modified file self.dot_receiver = field_expr .expr() - .map(|e| e.syntax().range()) + .map(|e| e.syntax().text_range()) .and_then(|r| find_node_with_range(original_file.syntax(), r)); } if let Some(method_call_expr) = ast::MethodCallExpr::cast(parent) { // As above self.dot_receiver = method_call_expr .expr() - .map(|e| e.syntax().range()) + .map(|e| e.syntax().text_range()) .and_then(|r| find_node_with_range(original_file.syntax(), r)); self.is_call = true; } @@ -229,6 +237,6 @@ fn find_node_with_range(syntax: &SyntaxNode, range: TextRange) -> Op fn is_node(node: &SyntaxNode) -> bool { match node.ancestors().filter_map(N::cast).next() { None => false, - Some(n) => n.syntax().range() == node.range(), + Some(n) => n.syntax().text_range() == node.text_range(), } } diff --git a/crates/ra_ide_api/src/diagnostics.rs b/crates/ra_ide_api/src/diagnostics.rs index affbad6cd05..028dc3d4fab 100644 --- a/crates/ra_ide_api/src/diagnostics.rs +++ b/crates/ra_ide_api/src/diagnostics.rs @@ -96,7 +96,7 @@ fn check_unnecessary_braces_in_use_statement( ) -> Option<()> { let use_tree_list = ast::UseTreeList::cast(node.clone())?; if let Some((single_use_tree,)) = use_tree_list.use_trees().collect_tuple() { - let range = use_tree_list.syntax().range(); + let range = use_tree_list.syntax().text_range(); let edit = text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(&single_use_tree) .unwrap_or_else(|| { @@ -126,8 +126,8 @@ fn text_edit_for_remove_unnecessary_braces_with_self_in_use_statement( ) -> Option { let use_tree_list_node = single_use_tree.syntax().parent()?; if single_use_tree.path()?.segment()?.syntax().first_child_or_token()?.kind() == T![self] { - let start = use_tree_list_node.prev_sibling_or_token()?.range().start(); - let end = use_tree_list_node.range().end(); + let start = use_tree_list_node.prev_sibling_or_token()?.text_range().start(); + let end = use_tree_list_node.text_range().end(); let range = TextRange::from_to(start, end); let mut edit_builder = TextEditBuilder::default(); edit_builder.delete(range); @@ -149,12 +149,12 @@ fn check_struct_shorthand_initialization( let field_expr = expr.syntax().text().to_string(); if field_name == field_expr { let mut edit_builder = TextEditBuilder::default(); - edit_builder.delete(named_field.syntax().range()); - edit_builder.insert(named_field.syntax().range().start(), field_name); + edit_builder.delete(named_field.syntax().text_range()); + edit_builder.insert(named_field.syntax().text_range().start(), field_name); let edit = edit_builder.finish(); acc.push(Diagnostic { - range: named_field.syntax().range(), + range: named_field.syntax().text_range(), message: "Shorthand struct initialization".to_string(), severity: Severity::WeakWarning, fix: Some(SourceChange::source_file_edit( diff --git a/crates/ra_ide_api/src/display/navigation_target.rs b/crates/ra_ide_api/src/display/navigation_target.rs index c212ebf280a..84fabdb9e17 100644 --- a/crates/ra_ide_api/src/display/navigation_target.rs +++ b/crates/ra_ide_api/src/display/navigation_target.rs @@ -275,7 +275,7 @@ impl NavigationTarget { ) -> NavigationTarget { //FIXME: use `_` instead of empty string let name = node.name().map(|it| it.text().clone()).unwrap_or_default(); - let focus_range = node.name().map(|it| it.syntax().range()); + let focus_range = node.name().map(|it| it.syntax().text_range()); NavigationTarget::from_syntax(file_id, name, focus_range, node.syntax(), docs, description) } @@ -291,7 +291,7 @@ impl NavigationTarget { file_id, name, kind: node.kind(), - full_range: node.range(), + full_range: node.text_range(), focus_range, // ptr: Some(LocalSyntaxPtr::new(node)), container_name: None, diff --git a/crates/ra_ide_api/src/display/structure.rs b/crates/ra_ide_api/src/display/structure.rs index 0b1a8b6e6f0..2ba10b2ef34 100644 --- a/crates/ra_ide_api/src/display/structure.rs +++ b/crates/ra_ide_api/src/display/structure.rs @@ -73,8 +73,8 @@ fn structure_node(node: &SyntaxNode) -> Option { Some(StructureNode { parent: None, label: name.text().to_string(), - navigation_range: name.syntax().range(), - node_range: node.syntax().range(), + navigation_range: name.syntax().text_range(), + node_range: node.syntax().text_range(), kind: node.syntax().kind(), detail, deprecated: node.attrs().filter_map(|x| x.as_named()).any(|x| x == "deprecated"), @@ -140,8 +140,8 @@ fn structure_node(node: &SyntaxNode) -> Option { let node = StructureNode { parent: None, label, - navigation_range: target_type.syntax().range(), - node_range: im.syntax().range(), + navigation_range: target_type.syntax().text_range(), + node_range: im.syntax().text_range(), kind: im.syntax().kind(), detail: None, deprecated: false, diff --git a/crates/ra_ide_api/src/extend_selection.rs b/crates/ra_ide_api/src/extend_selection.rs index 292f61f4acc..140820df6c8 100644 --- a/crates/ra_ide_api/src/extend_selection.rs +++ b/crates/ra_ide_api/src/extend_selection.rs @@ -42,19 +42,20 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option return None, TokenAtOffset::Single(l) => { if string_kinds.contains(&l.kind()) { - extend_single_word_in_comment_or_string(&l, offset).unwrap_or_else(|| l.range()) + extend_single_word_in_comment_or_string(&l, offset) + .unwrap_or_else(|| l.text_range()) } else { - l.range() + l.text_range() } } - TokenAtOffset::Between(l, r) => pick_best(l, r).range(), + TokenAtOffset::Between(l, r) => pick_best(l, r).text_range(), }; return Some(leaf_range); }; let node = match find_covering_element(root, range) { SyntaxElement::Token(token) => { - if token.range() != range { - return Some(token.range()); + if token.text_range() != range { + return Some(token.text_range()); } if let Some(comment) = ast::Comment::cast(token.clone()) { if let Some(range) = extend_comments(comment) { @@ -65,12 +66,12 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option node, }; - if node.range() != range { - return Some(node.range()); + if node.text_range() != range { + return Some(node.text_range()); } // Using shallowest node with same range allows us to traverse siblings. - let node = node.ancestors().take_while(|n| n.range() == node.range()).last().unwrap(); + let node = node.ancestors().take_while(|n| n.text_range() == node.text_range()).last().unwrap(); if node.parent().map(|n| list_kinds.contains(&n.kind())) == Some(true) { if let Some(range) = extend_list_item(&node) { @@ -78,7 +79,7 @@ fn try_extend_selection(root: &SyntaxNode, range: TextRange) -> Option Option { let text: &str = leaf.text(); - let cursor_position: u32 = (offset - leaf.range().start()).into(); + let cursor_position: u32 = (offset - leaf.text_range().start()).into(); let (before, after) = text.split_at(cursor_position as usize); @@ -104,31 +105,31 @@ fn extend_single_word_in_comment_or_string( if range.is_empty() { None } else { - Some(range + leaf.range().start()) + Some(range + leaf.text_range().start()) } } fn extend_ws(root: &SyntaxNode, ws: SyntaxToken, offset: TextUnit) -> TextRange { let ws_text = ws.text(); - let suffix = TextRange::from_to(offset, ws.range().end()) - ws.range().start(); - let prefix = TextRange::from_to(ws.range().start(), offset) - ws.range().start(); + let suffix = TextRange::from_to(offset, ws.text_range().end()) - ws.text_range().start(); + let prefix = TextRange::from_to(ws.text_range().start(), offset) - ws.text_range().start(); let ws_suffix = &ws_text.as_str()[suffix]; let ws_prefix = &ws_text.as_str()[prefix]; if ws_text.contains('\n') && !ws_suffix.contains('\n') { if let Some(node) = ws.next_sibling_or_token() { let start = match ws_prefix.rfind('\n') { - Some(idx) => ws.range().start() + TextUnit::from((idx + 1) as u32), - None => node.range().start(), + Some(idx) => ws.text_range().start() + TextUnit::from((idx + 1) as u32), + None => node.text_range().start(), }; - let end = if root.text().char_at(node.range().end()) == Some('\n') { - node.range().end() + TextUnit::of_char('\n') + let end = if root.text().char_at(node.text_range().end()) == Some('\n') { + node.text_range().end() + TextUnit::of_char('\n') } else { - node.range().end() + node.text_range().end() }; return TextRange::from_to(start, end); } } - ws.range() + ws.text_range() } fn pick_best<'a>(l: SyntaxToken, r: SyntaxToken) -> SyntaxToken { @@ -161,7 +162,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option { } if let Some(comma_node) = nearby_comma(node, Direction::Prev) { - return Some(TextRange::from_to(comma_node.range().start(), node.range().end())); + return Some(TextRange::from_to(comma_node.text_range().start(), node.text_range().end())); } if let Some(comma_node) = nearby_comma(node, Direction::Next) { // Include any following whitespace when comma if after list item. @@ -171,7 +172,7 @@ fn extend_list_item(node: &SyntaxNode) -> Option { .filter(|node| is_single_line_ws(node)) .unwrap_or(comma_node); - return Some(TextRange::from_to(node.range().start(), final_node.range().end())); + return Some(TextRange::from_to(node.text_range().start(), final_node.text_range().end())); } None @@ -181,7 +182,10 @@ fn extend_comments(comment: ast::Comment) -> Option { let prev = adj_comments(&comment, Direction::Prev); let next = adj_comments(&comment, Direction::Next); if prev != next { - Some(TextRange::from_to(prev.syntax().range().start(), next.syntax().range().end())) + Some(TextRange::from_to( + prev.syntax().text_range().start(), + next.syntax().text_range().end(), + )) } else { None } diff --git a/crates/ra_ide_api/src/folding_ranges.rs b/crates/ra_ide_api/src/folding_ranges.rs index c2b981aedc1..9699000dbb1 100644 --- a/crates/ra_ide_api/src/folding_ranges.rs +++ b/crates/ra_ide_api/src/folding_ranges.rs @@ -35,7 +35,7 @@ pub(crate) fn folding_ranges(file: &SourceFile) -> Vec { SyntaxElement::Token(token) => token.text().contains('\n'), }; if is_multiline { - res.push(Fold { range: element.range(), kind }); + res.push(Fold { range: element.text_range(), kind }); continue; } } @@ -132,7 +132,7 @@ fn contiguous_range_for_group_unless( } if first != &last { - Some(TextRange::from_to(first.range().start(), last.range().end())) + Some(TextRange::from_to(first.text_range().start(), last.text_range().end())) } else { // The group consists of only one element, therefore it cannot be folded None @@ -178,7 +178,10 @@ fn contiguous_range_for_comment( } if first != last { - Some(TextRange::from_to(first.syntax().range().start(), last.syntax().range().end())) + Some(TextRange::from_to( + first.syntax().text_range().start(), + last.syntax().text_range().end(), + )) } else { // The group consists of only one element, therefore it cannot be folded None diff --git a/crates/ra_ide_api/src/goto_definition.rs b/crates/ra_ide_api/src/goto_definition.rs index 82b5e3b5e17..ddd55a9c158 100644 --- a/crates/ra_ide_api/src/goto_definition.rs +++ b/crates/ra_ide_api/src/goto_definition.rs @@ -23,11 +23,11 @@ pub(crate) fn goto_definition( let syntax = parse.tree().syntax().clone(); if let Some(name_ref) = find_node_at_offset::(&syntax, position.offset) { let navs = reference_definition(db, position.file_id, &name_ref).to_vec(); - return Some(RangeInfo::new(name_ref.syntax().range(), navs.to_vec())); + return Some(RangeInfo::new(name_ref.syntax().text_range(), navs.to_vec())); } if let Some(name) = find_node_at_offset::(&syntax, position.offset) { let navs = name_definition(db, position.file_id, &name)?; - return Some(RangeInfo::new(name.syntax().range(), navs)); + return Some(RangeInfo::new(name.syntax().text_range(), navs)); } None } diff --git a/crates/ra_ide_api/src/goto_type_definition.rs b/crates/ra_ide_api/src/goto_type_definition.rs index fc4b6e1af59..007259d9ea2 100644 --- a/crates/ra_ide_api/src/goto_type_definition.rs +++ b/crates/ra_ide_api/src/goto_type_definition.rs @@ -32,7 +32,7 @@ pub(crate) fn goto_type_definition( let adt_def = analyzer.autoderef(db, ty).find_map(|ty| ty.as_adt().map(|adt| adt.0))?; let nav = NavigationTarget::from_adt_def(db, adt_def); - Some(RangeInfo::new(node.range(), vec![nav])) + Some(RangeInfo::new(node.text_range(), vec![nav])) } #[cfg(test)] diff --git a/crates/ra_ide_api/src/hover.rs b/crates/ra_ide_api/src/hover.rs index e503bf6a96a..2a5ac782152 100644 --- a/crates/ra_ide_api/src/hover.rs +++ b/crates/ra_ide_api/src/hover.rs @@ -172,7 +172,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option(file.syntax(), position.offset) { if let Some(parent) = name.syntax().parent() { @@ -210,7 +210,7 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option Option Option { // if we picked identifier, expand to pattern/expression let node = leaf_node .ancestors() - .take_while(|it| it.range() == leaf_node.range()) + .take_while(|it| it.text_range() == leaf_node.text_range()) .find(|it| ast::Expr::cast(it.clone()).is_some() || ast::Pat::cast(it.clone()).is_some())?; let analyzer = hir::SourceAnalyzer::new(db, frange.file_id, &node, None); let ty = if let Some(ty) = ast::Expr::cast(node.clone()).and_then(|e| analyzer.type_of(db, &e)) diff --git a/crates/ra_ide_api/src/impls.rs b/crates/ra_ide_api/src/impls.rs index 15999d4331a..f5b6ffd6162 100644 --- a/crates/ra_ide_api/src/impls.rs +++ b/crates/ra_ide_api/src/impls.rs @@ -15,12 +15,12 @@ pub(crate) fn goto_implementation( if let Some(nominal_def) = find_node_at_offset::(&syntax, position.offset) { return Some(RangeInfo::new( - nominal_def.syntax().range(), + nominal_def.syntax().text_range(), impls_for_def(db, &nominal_def, module)?, )); } else if let Some(trait_def) = find_node_at_offset::(&syntax, position.offset) { return Some(RangeInfo::new( - trait_def.syntax().range(), + trait_def.syntax().text_range(), impls_for_trait(db, &trait_def, module)?, )); } diff --git a/crates/ra_ide_api/src/join_lines.rs b/crates/ra_ide_api/src/join_lines.rs index 9b81ad9e4b2..fa998ebe1e9 100644 --- a/crates/ra_ide_api/src/join_lines.rs +++ b/crates/ra_ide_api/src/join_lines.rs @@ -28,14 +28,14 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { }; let mut edit = TextEditBuilder::default(); for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) { - let range = match range.intersection(&token.range()) { + let range = match range.intersection(&token.text_range()) { Some(range) => range, None => continue, - } - token.range().start(); + } - token.text_range().start(); let text = token.text(); for (pos, _) in text[range].bytes().enumerate().filter(|&(_, b)| b == b'\n') { let pos: TextUnit = (pos as u32).into(); - let off = token.range().start() + range.start() + pos; + let off = token.text_range().start() + range.start() + pos; if !edit.invalidates_offset(off) { remove_newline(&mut edit, &token, off); } @@ -49,7 +49,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU if token.kind() != WHITESPACE || token.text().bytes().filter(|&b| b == b'\n').count() != 1 { // The node is either the first or the last in the file let suff = &token.text()[TextRange::from_to( - offset - token.range().start() + TextUnit::of_char('\n'), + offset - token.text_range().start() + TextUnit::of_char('\n'), TextUnit::of_str(token.text()), )]; let spaces = suff.bytes().take_while(|&b| b == b' ').count(); @@ -86,7 +86,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU let next = token.next_sibling_or_token().unwrap(); if is_trailing_comma(prev.kind(), next.kind()) { // Removes: trailing comma, newline (incl. surrounding whitespace) - edit.delete(TextRange::from_to(prev.range().start(), token.range().end())); + edit.delete(TextRange::from_to(prev.text_range().start(), token.text_range().end())); } else if prev.kind() == T![,] && next.kind() == T!['}'] { // Removes: comma, newline (incl. surrounding whitespace) let space = if let Some(left) = prev.prev_sibling_or_token() { @@ -95,7 +95,7 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU " " }; edit.replace( - TextRange::from_to(prev.range().start(), token.range().end()), + TextRange::from_to(prev.text_range().start(), token.text_range().end()), space.to_string(), ); } else if let (Some(_), Some(next)) = ( @@ -104,12 +104,12 @@ fn remove_newline(edit: &mut TextEditBuilder, token: &SyntaxToken, offset: TextU ) { // Removes: newline (incl. surrounding whitespace), start of the next comment edit.delete(TextRange::from_to( - token.range().start(), - next.syntax().range().start() + TextUnit::of_str(next.prefix()), + token.text_range().start(), + next.syntax().text_range().start() + TextUnit::of_str(next.prefix()), )); } else { // Remove newline but add a computed amount of whitespace characters - edit.replace(token.range(), compute_ws(prev.kind(), next.kind()).to_string()); + edit.replace(token.text_range(), compute_ws(prev.kind(), next.kind()).to_string()); } } @@ -125,7 +125,7 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Op let block_expr = ast::BlockExpr::cast(block.syntax().parent()?)?; let expr = extract_trivial_expression(&block)?; - let block_range = block_expr.syntax().range(); + let block_range = block_expr.syntax().text_range(); let mut buf = expr.syntax().text().to_string(); // Match block needs to have a comma after the block @@ -143,7 +143,7 @@ fn join_single_expr_block(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Op fn join_single_use_tree(edit: &mut TextEditBuilder, token: &SyntaxToken) -> Option<()> { let use_tree_list = ast::UseTreeList::cast(token.parent())?; let (tree,) = use_tree_list.use_trees().collect_tuple()?; - edit.replace(use_tree_list.syntax().range(), tree.syntax().text().to_string()); + edit.replace(use_tree_list.syntax().text_range(), tree.syntax().text().to_string()); Some(()) } diff --git a/crates/ra_ide_api/src/matching_brace.rs b/crates/ra_ide_api/src/matching_brace.rs index 102327fd739..1e2fac8485a 100644 --- a/crates/ra_ide_api/src/matching_brace.rs +++ b/crates/ra_ide_api/src/matching_brace.rs @@ -12,7 +12,7 @@ pub fn matching_brace(file: &SourceFile, offset: TextUnit) -> Option { let parent = brace_node.parent(); let matching_kind = BRACES[brace_idx ^ 1]; let matching_node = parent.children_with_tokens().find(|node| node.kind() == matching_kind)?; - Some(matching_node.range().start()) + Some(matching_node.text_range().start()) } #[cfg(test)] diff --git a/crates/ra_ide_api/src/references.rs b/crates/ra_ide_api/src/references.rs index 1c4cd49dcce..5c74d3e363e 100644 --- a/crates/ra_ide_api/src/references.rs +++ b/crates/ra_ide_api/src/references.rs @@ -166,7 +166,7 @@ fn rename_mod( file_id: position.file_id, edit: { let mut builder = ra_text_edit::TextEditBuilder::default(); - builder.replace(ast_name.syntax().range(), new_name.into()); + builder.replace(ast_name.syntax().text_range(), new_name.into()); builder.finish() }, }; diff --git a/crates/ra_ide_api/src/runnables.rs b/crates/ra_ide_api/src/runnables.rs index 2009584341f..07412a9efd9 100644 --- a/crates/ra_ide_api/src/runnables.rs +++ b/crates/ra_ide_api/src/runnables.rs @@ -47,7 +47,7 @@ fn runnable_fn(fn_def: ast::FnDef) -> Option { } else { return None; }; - Some(Runnable { range: fn_def.syntax().range(), kind }) + Some(Runnable { range: fn_def.syntax().text_range(), kind }) } fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Option { @@ -62,7 +62,7 @@ fn runnable_mod(db: &RootDatabase, file_id: FileId, module: ast::Module) -> Opti if !has_test_function { return None; } - let range = module.syntax().range(); + let range = module.syntax().text_range(); let module = hir::source_binder::module_from_child_node(db, file_id, module.syntax())?; let path = module.path_to_root(db).into_iter().rev().filter_map(|it| it.name(db)).join("::"); diff --git a/crates/ra_ide_api/src/symbol_index.rs b/crates/ra_ide_api/src/symbol_index.rs index e784b5f69a1..d4afddab4a8 100644 --- a/crates/ra_ide_api/src/symbol_index.rs +++ b/crates/ra_ide_api/src/symbol_index.rs @@ -271,7 +271,7 @@ fn source_file_to_file_symbols(source_file: &SourceFile, file_id: FileId) -> Vec fn to_symbol(node: &SyntaxNode) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> { fn decl(node: N) -> Option<(SmolStr, SyntaxNodePtr, TextRange)> { let name = node.name()?; - let name_range = name.syntax().range(); + let name_range = name.syntax().text_range(); let name = name.text().clone(); let ptr = SyntaxNodePtr::new(node.syntax()); diff --git a/crates/ra_ide_api/src/syntax_highlighting.rs b/crates/ra_ide_api/src/syntax_highlighting.rs index 477827fa710..878a94f061f 100644 --- a/crates/ra_ide_api/src/syntax_highlighting.rs +++ b/crates/ra_ide_api/src/syntax_highlighting.rs @@ -189,11 +189,11 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec range_end = sibling.range().end(), + T![!] | IDENT => range_end = sibling.text_range().end(), _ => (), } } @@ -209,7 +209,7 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec"); let tokens = parse.tree().syntax().descendants_with_tokens().filter_map(|it| it.into_token()); for token in tokens { - could_intersect.retain(|it| token.range().start() <= it.range.end()); + could_intersect.retain(|it| token.text_range().start() <= it.range.end()); while let Some(r) = ranges.get(frontier) { - if r.range.start() <= token.range().end() { + if r.range.start() <= token.text_range().end() { could_intersect.push(r); frontier += 1; } else { @@ -251,7 +251,7 @@ pub(crate) fn highlight_as_html(db: &RootDatabase, file_id: FileId, rainbow: boo let text = html_escape(&token.text()); let ranges = could_intersect .iter() - .filter(|it| token.range().is_subrange(&it.range)) + .filter(|it| token.text_range().is_subrange(&it.range)) .collect::>(); if ranges.is_empty() { buf.push_str(&text); diff --git a/crates/ra_ide_api/src/syntax_tree.rs b/crates/ra_ide_api/src/syntax_tree.rs index ae1849aaa63..76c50f6d6ca 100644 --- a/crates/ra_ide_api/src/syntax_tree.rs +++ b/crates/ra_ide_api/src/syntax_tree.rs @@ -45,7 +45,7 @@ fn syntax_tree_for_string(token: &SyntaxToken, text_range: TextRange) -> Option< fn syntax_tree_for_token(node: &SyntaxToken, text_range: TextRange) -> Option { // Range of the full node - let node_range = node.range(); + let node_range = node.text_range(); let text = node.text().to_string(); // We start at some point inside the node diff --git a/crates/ra_ide_api/src/typing.rs b/crates/ra_ide_api/src/typing.rs index ad0ababcc0a..1747a529ecb 100644 --- a/crates/ra_ide_api/src/typing.rs +++ b/crates/ra_ide_api/src/typing.rs @@ -24,7 +24,7 @@ pub(crate) fn on_enter(db: &RootDatabase, position: FilePosition) -> Option Option Option { - let ws = match find_token_at_offset(file.syntax(), token.range().start()) { + let ws = match find_token_at_offset(file.syntax(), token.text_range().start()) { TokenAtOffset::Between(l, r) => { assert!(r == *token); l @@ -71,7 +71,7 @@ pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option { return None; } if let Some(expr) = let_stmt.initializer() { - let expr_range = expr.syntax().range(); + let expr_range = expr.syntax().text_range(); if expr_range.contains(eq_offset) && eq_offset != expr_range.start() { return None; } @@ -81,7 +81,7 @@ pub fn on_eq_typed(file: &SourceFile, eq_offset: TextUnit) -> Option { } else { return None; } - let offset = let_stmt.syntax().range().end(); + let offset = let_stmt.syntax().text_range().end(); let mut edit = TextEditBuilder::default(); edit.insert(offset, ";".to_string()); Some(edit.finish()) diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 64ed6a517da..7ff0fc47205 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -19,7 +19,7 @@ pub struct TokenMap { pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap)> { let mut token_map = TokenMap::default(); let node = ast.syntax(); - let tt = convert_tt(&mut token_map, node.range().start(), node)?; + let tt = convert_tt(&mut token_map, node.text_range().start(), node)?; Some((tt, token_map)) } @@ -27,7 +27,7 @@ pub fn ast_to_token_tree(ast: &ast::TokenTree) -> Option<(tt::Subtree, TokenMap) /// will consume). pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, TokenMap)> { let mut token_map = TokenMap::default(); - let tt = convert_tt(&mut token_map, node.range().start(), node)?; + let tt = convert_tt(&mut token_map, node.text_range().start(), node)?; Some((tt, token_map)) } @@ -229,7 +229,7 @@ fn convert_tt( || token.kind() == IDENT || token.kind() == LIFETIME { - let relative_range = token.range() - global_offset; + let relative_range = token.text_range() - global_offset; let id = token_map.alloc(relative_range); let text = token.text().clone(); tt::Leaf::from(tt::Ident { text, id }).into() diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index e2de5e0e39b..f47e11e66db 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -25,7 +25,7 @@ pub fn ancestors_at_offset( ) -> impl Iterator { find_token_at_offset(node, offset) .map(|token| token.parent().ancestors()) - .kmerge_by(|node1, node2| node1.range().len() < node2.range().len()) + .kmerge_by(|node1, node2| node1.text_range().len() < node2.text_range().len()) } /// Finds a node of specific Ast type at offset. Note that this is slightly diff --git a/crates/ra_syntax/src/fuzz.rs b/crates/ra_syntax/src/fuzz.rs index a9146c4f89f..698a624ecce 100644 --- a/crates/ra_syntax/src/fuzz.rs +++ b/crates/ra_syntax/src/fuzz.rs @@ -51,7 +51,7 @@ impl CheckReparse { for (a, b) in new_parse.tree().syntax().descendants().zip(full_reparse.tree().syntax().descendants()) { - if (a.kind(), a.range()) != (b.kind(), b.range()) { + if (a.kind(), a.text_range()) != (b.kind(), b.text_range()) { eprint!("original:\n{:#?}", parse.tree().syntax()); eprint!("reparsed:\n{:#?}", new_parse.tree().syntax()); eprint!("full reparse:\n{:#?}", full_reparse.tree().syntax()); diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 605350ac497..8af04c13623 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -234,7 +234,7 @@ fn api_walkthrough() { assert_eq!(expr_syntax.kind(), SyntaxKind::BIN_EXPR); // And text range: - assert_eq!(expr_syntax.range(), TextRange::from_to(32.into(), 37.into())); + assert_eq!(expr_syntax.text_range(), TextRange::from_to(32.into(), 37.into())); // You can get node's text as a `SyntaxText` object, which will traverse the // tree collecting token's text: diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index c7183299b58..2f388bdfe6e 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -46,7 +46,8 @@ fn reparse_token<'node>( WHITESPACE | COMMENT | IDENT | STRING | RAW_STRING => { if token.kind() == WHITESPACE || token.kind() == COMMENT { // removing a new line may extends previous token - if token.text().to_string()[edit.delete - token.range().start()].contains('\n') { + if token.text().to_string()[edit.delete - token.text_range().start()].contains('\n') + { return None; } } @@ -62,7 +63,7 @@ fn reparse_token<'node>( return None; } - if let Some(next_char) = root.text().char_at(token.range().end()) { + if let Some(next_char) = root.text().char_at(token.text_range().end()) { let tokens_with_next_char = tokenize(&format!("{}{}", text, next_char)); if tokens_with_next_char.len() == 1 { return None; @@ -70,7 +71,7 @@ fn reparse_token<'node>( } let new_token = GreenToken::new(rowan::SyntaxKind(token.kind().into()), text.into()); - Some((token.replace_with(new_token), token.range())) + Some((token.replace_with(new_token), token.text_range())) } _ => None, } @@ -90,11 +91,12 @@ fn reparse_block<'node>( let mut tree_sink = TextTreeSink::new(&text, &tokens); reparser.parse(&mut token_source, &mut tree_sink); let (green, new_errors) = tree_sink.finish(); - Some((node.replace_with(green), new_errors, node.range())) + Some((node.replace_with(green), new_errors, node.text_range())) } fn get_text_after_edit(element: SyntaxElement, edit: &AtomTextEdit) -> String { - let edit = AtomTextEdit::replace(edit.delete - element.range().start(), edit.insert.clone()); + let edit = + AtomTextEdit::replace(edit.delete - element.text_range().start(), edit.insert.clone()); let text = match element { SyntaxElement::Token(token) => token.text().to_string(), SyntaxElement::Node(node) => node.text().to_string(), diff --git a/crates/ra_syntax/src/ptr.rs b/crates/ra_syntax/src/ptr.rs index d1b30a2c927..8665c8976a5 100644 --- a/crates/ra_syntax/src/ptr.rs +++ b/crates/ra_syntax/src/ptr.rs @@ -12,15 +12,15 @@ pub struct SyntaxNodePtr { impl SyntaxNodePtr { pub fn new(node: &SyntaxNode) -> SyntaxNodePtr { - SyntaxNodePtr { range: node.range(), kind: node.kind() } + SyntaxNodePtr { range: node.text_range(), kind: node.kind() } } pub fn to_node(self, root: &SyntaxNode) -> SyntaxNode { assert!(root.parent().is_none()); successors(Some(root.clone()), |node| { - node.children().find(|it| self.range.is_subrange(&it.range())) + node.children().find(|it| self.range.is_subrange(&it.text_range())) }) - .find(|it| it.range() == self.range && it.kind() == self.kind) + .find(|it| it.text_range() == self.range && it.kind() == self.kind) .unwrap_or_else(|| panic!("can't resolve local ptr to SyntaxNode: {:?}", self)) } diff --git a/crates/ra_syntax/src/syntax_node.rs b/crates/ra_syntax/src/syntax_node.rs index 3b20e96e267..c42045d77ac 100644 --- a/crates/ra_syntax/src/syntax_node.rs +++ b/crates/ra_syntax/src/syntax_node.rs @@ -53,7 +53,7 @@ impl fmt::Debug for SyntaxNode { assert_eq!(level, 0); Ok(()) } else { - write!(f, "{:?}@{:?}", self.kind(), self.range()) + write!(f, "{:?}@{:?}", self.kind(), self.text_range()) } } } @@ -80,7 +80,7 @@ impl SyntaxNode { self.0.kind().0.into() } - pub fn range(&self) -> TextRange { + pub fn text_range(&self) -> TextRange { self.0.text_range() } @@ -291,7 +291,7 @@ pub struct SyntaxToken(pub(crate) rowan::cursor::SyntaxToken); impl fmt::Debug for SyntaxToken { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { - write!(fmt, "{:?}@{:?}", self.kind(), self.range())?; + write!(fmt, "{:?}@{:?}", self.kind(), self.text_range())?; if self.text().len() < 25 { return write!(fmt, " {:?}", self.text()); } @@ -321,7 +321,7 @@ impl SyntaxToken { self.0.text() } - pub fn range(&self) -> TextRange { + pub fn text_range(&self) -> TextRange { self.0.text_range() } @@ -453,10 +453,10 @@ impl SyntaxElement { .ancestors() } - pub fn range(&self) -> TextRange { + pub fn text_range(&self) -> TextRange { match self { - SyntaxElement::Node(it) => it.range(), - SyntaxElement::Token(it) => it.range(), + SyntaxElement::Node(it) => it.text_range(), + SyntaxElement::Token(it) => it.text_range(), } } diff --git a/crates/ra_syntax/src/syntax_text.rs b/crates/ra_syntax/src/syntax_text.rs index 2ad98809b48..f8ddff48e9a 100644 --- a/crates/ra_syntax/src/syntax_text.rs +++ b/crates/ra_syntax/src/syntax_text.rs @@ -13,7 +13,7 @@ pub struct SyntaxText { impl SyntaxText { pub(crate) fn new(node: SyntaxNode) -> SyntaxText { - let range = node.range(); + let range = node.text_range(); SyntaxText { node, range } } @@ -24,14 +24,14 @@ impl SyntaxText { self.node.descendants_with_tokens().try_fold(init, move |acc, element| { let res = match element { SyntaxElement::Token(token) => { - let range = match self.range.intersection(&token.range()) { + let range = match self.range.intersection(&token.text_range()) { None => return Ok(acc), Some(it) => it, }; - let slice = if range == token.range() { + let slice = if range == token.text_range() { token.text() } else { - let range = range - token.range().start(); + let range = range - token.text_range().start(); &token.text()[range] }; f(acc, slice)? diff --git a/crates/ra_syntax/src/validation.rs b/crates/ra_syntax/src/validation.rs index 943f88de9a3..19bdafef227 100644 --- a/crates/ra_syntax/src/validation.rs +++ b/crates/ra_syntax/src/validation.rs @@ -33,7 +33,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec) { if let Some(end) = text.rfind('\'') { if let Some(without_quotes) = text.get(2..end) { if let Err((off, err)) = unescape::unescape_byte(without_quotes) { - let off = token.range().start() + TextUnit::from_usize(off + 2); + let off = token.text_range().start() + TextUnit::from_usize(off + 2); acc.push(SyntaxError::new(err.into(), off)) } } @@ -43,7 +43,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec) { if let Some(end) = text.rfind('\'') { if let Some(without_quotes) = text.get(1..end) { if let Err((off, err)) = unescape::unescape_char(without_quotes) { - let off = token.range().start() + TextUnit::from_usize(off + 1); + let off = token.text_range().start() + TextUnit::from_usize(off + 1); acc.push(SyntaxError::new(err.into(), off)) } } @@ -55,7 +55,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec) { unescape::unescape_byte_str(without_quotes, &mut |range, char| { if let Err(err) = char { let off = range.start; - let off = token.range().start() + TextUnit::from_usize(off + 2); + let off = token.text_range().start() + TextUnit::from_usize(off + 2); acc.push(SyntaxError::new(err.into(), off)) } }) @@ -68,7 +68,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec) { unescape::unescape_str(without_quotes, &mut |range, char| { if let Err(err) = char { let off = range.start; - let off = token.range().start() + TextUnit::from_usize(off + 1); + let off = token.text_range().start() + TextUnit::from_usize(off + 1); acc.push(SyntaxError::new(err.into(), off)) } }) diff --git a/crates/ra_syntax/src/validation/block.rs b/crates/ra_syntax/src/validation/block.rs index f5573bd8f00..c5588658d88 100644 --- a/crates/ra_syntax/src/validation/block.rs +++ b/crates/ra_syntax/src/validation/block.rs @@ -16,6 +16,7 @@ pub(crate) fn validate_block_node(node: ast::Block, errors: &mut Vec {} } } - errors - .extend(node.attrs().map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().range()))) + errors.extend( + node.attrs().map(|attr| SyntaxError::new(InvalidBlockAttr, attr.syntax().text_range())), + ) } diff --git a/crates/ra_syntax/src/validation/field_expr.rs b/crates/ra_syntax/src/validation/field_expr.rs index 0e18bd9ca01..004f199fd53 100644 --- a/crates/ra_syntax/src/validation/field_expr.rs +++ b/crates/ra_syntax/src/validation/field_expr.rs @@ -7,7 +7,7 @@ use crate::{ pub(crate) fn validate_field_expr_node(node: ast::FieldExpr, errors: &mut Vec) { if let Some(FieldKind::Index(idx)) = node.field_access() { if idx.text().chars().any(|c| c < '0' || c > '9') { - errors.push(SyntaxError::new(InvalidTupleIndexFormat, idx.range())); + errors.push(SyntaxError::new(InvalidTupleIndexFormat, idx.text_range())); } } }