"itertools",
"profile",
"ra_db",
- "ra_fmt",
"ra_hir",
"ra_ide_db",
"rustc-hash",
"vfs",
]
-[[package]]
-name = "ra_fmt"
-version = "0.1.0"
-dependencies = [
- "itertools",
- "syntax",
-]
-
[[package]]
name = "ra_hir"
version = "0.1.0"
"profile",
"ra_assists",
"ra_db",
- "ra_fmt",
"ra_hir",
"ra_ide_db",
"ra_ssr",
syntax = { path = "../syntax" }
text_edit = { path = "../text_edit" }
-ra_fmt = { path = "../ra_fmt" }
profile = { path = "../profile" }
ra_db = { path = "../ra_db" }
ra_ide_db = { path = "../ra_ide_db" }
use hir::{EnumVariant, Module, ModuleDef, Name};
use ra_db::FileId;
-use ra_fmt::leading_indent;
use ra_ide_db::{defs::Definition, search::Reference, RootDatabase};
use rustc_hash::FxHashSet;
use syntax::{
algo::find_node_at_offset,
- ast::{self, ArgListOwner, AstNode, NameOwner, VisibilityOwner},
+ ast::{self, edit::IndentLevel, ArgListOwner, AstNode, NameOwner, VisibilityOwner},
SourceFile, TextRange, TextSize,
};
Some(())
}
-// FIXME: this should use strongly-typed `make`, rather than string manipulation1
+// FIXME: this should use strongly-typed `make`, rather than string manipulation.
fn extract_struct_def(
builder: &mut AssistBuilder,
enum_: &ast::Enum,
} else {
"".to_string()
};
- let indent = if let Some(indent) = leading_indent(enum_.syntax()) {
- indent.to_string()
- } else {
- "".to_string()
- };
+ let indent = IndentLevel::from_node(enum_.syntax());
let struct_def = format!(
r#"{}struct {}{};
// FIXME: rewrite according to the plan, outlined in
// https://github.com/rust-analyzer/rust-analyzer/issues/3301#issuecomment-592931553
+use std::iter::successors;
+
use either::Either;
use hir::{self, ModPath};
use syntax::{
ast::{self, NameOwner, VisibilityOwner},
- AstNode, Direction, SmolStr,
+ AstNode, AstToken, Direction, SmolStr,
SyntaxKind::{PATH, PATH_SEGMENT},
- SyntaxNode, T,
+ SyntaxNode, SyntaxToken, T,
};
use text_edit::TextEditBuilder;
edit: &mut TextEditBuilder,
) {
if let Some(anchor) = anchor {
- let indent = ra_fmt::leading_indent(anchor);
+ let indent = leading_indent(anchor);
let mut buf = String::new();
if after {
buf.push_str("\n");
edit.insert(end, "}".to_string());
}
}
+
+/// If the node is on the beginning of the line, calculate indent.
+fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
+ for token in prev_tokens(node.first_token()?) {
+ if let Some(ws) = ast::Whitespace::cast(token.clone()) {
+ let ws_text = ws.text();
+ if let Some(pos) = ws_text.rfind('\n') {
+ return Some(ws_text[pos + 1..].into());
+ }
+ }
+ if token.text().contains('\n') {
+ break;
+ }
+ }
+ return None;
+ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
+ successors(token.prev_token(), |token| token.prev_token())
+ }
+}
+++ /dev/null
-[package]
-edition = "2018"
-name = "ra_fmt"
-version = "0.1.0"
-authors = ["rust-analyzer developers"]
-publish = false
-license = "MIT OR Apache-2.0"
-
-[lib]
-doctest = false
-
-[dependencies]
-itertools = "0.9.0"
-
-syntax = { path = "../syntax" }
+++ /dev/null
-//! This crate provides some utilities for indenting rust code.
-
-use std::iter::successors;
-
-use syntax::{
- ast::{self, AstToken},
- SmolStr, SyntaxNode, SyntaxToken,
-};
-
-/// If the node is on the beginning of the line, calculate indent.
-pub fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
- for token in prev_tokens(node.first_token()?) {
- if let Some(ws) = ast::Whitespace::cast(token.clone()) {
- let ws_text = ws.text();
- if let Some(pos) = ws_text.rfind('\n') {
- return Some(ws_text[pos + 1..].into());
- }
- }
- if token.text().contains('\n') {
- break;
- }
- }
- None
-}
-
-fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
- successors(token.prev_token(), |token| token.prev_token())
-}
ra_db = { path = "../ra_db" }
ra_ide_db = { path = "../ra_ide_db" }
cfg = { path = "../cfg" }
-ra_fmt = { path = "../ra_fmt" }
profile = { path = "../profile" }
test_utils = { path = "../test_utils" }
ra_assists = { path = "../ra_assists" }
mod on_enter;
use ra_db::{FilePosition, SourceDatabase};
-use ra_fmt::leading_indent;
use ra_ide_db::{source_change::SourceFileEdit, RootDatabase};
use syntax::{
algo::find_node_at_offset,
- ast::{self, AstToken},
+ ast::{self, edit::IndentLevel, AstToken},
AstNode, SourceFile,
SyntaxKind::{FIELD_EXPR, METHOD_CALL_EXPR},
TextRange, TextSize,
if !matches!(parent.kind(), FIELD_EXPR | METHOD_CALL_EXPR) {
return None;
}
- let prev_indent = leading_indent(&parent)?;
+ let prev_indent = IndentLevel::from_node(&parent);
let target_indent = format!(" {}", prev_indent);
let target_indent_len = TextSize::of(&target_indent);
if current_indent_len == target_indent_len {