Use split1 when formatting function signature params

This commit is contained in:
Aaron Loucks 2020-06-03 07:26:15 -04:00
parent 1211a46826
commit f06b2bcd91
5 changed files with 12 additions and 11 deletions

1
Cargo.lock generated
View file

@ -1639,6 +1639,7 @@ dependencies = [
"relative-path", "relative-path",
"rustc-hash", "rustc-hash",
"serde_json", "serde_json",
"stdx",
"text-size", "text-size",
] ]

View file

@ -10,7 +10,7 @@ use std::{
use hir::{Docs, Documentation, HasSource, HirDisplay}; use hir::{Docs, Documentation, HasSource, HirDisplay};
use ra_ide_db::RootDatabase; use ra_ide_db::RootDatabase;
use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
use stdx::SepBy; use stdx::{split1, SepBy};
use crate::display::{generic_parameters, where_predicates}; use crate::display::{generic_parameters, where_predicates};
@ -210,10 +210,8 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
// macro-generated functions are missing whitespace // macro-generated functions are missing whitespace
fn fmt_param(param: ast::Param) -> String { fn fmt_param(param: ast::Param) -> String {
let text = param.syntax().text().to_string(); let text = param.syntax().text().to_string();
match text.find(':') { match split1(&text, ':') {
Some(pos) if 1 + pos < text.len() => { Some((left, right)) => format!("{}: {}", left.trim(), right.trim()),
format!("{} {}", &text[0..1 + pos].trim(), &text[1 + pos..].trim())
}
_ => text, _ => text,
} }
} }

View file

@ -124,3 +124,8 @@ pub fn replace(buf: &mut String, from: char, to: &str) {
// FIXME: do this in place. // FIXME: do this in place.
*buf = buf.replace(from, to) *buf = buf.replace(from, to)
} }
pub fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
let idx = haystack.find(delim)?;
Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
}

View file

@ -14,4 +14,5 @@ serde_json = "1.0.48"
relative-path = "1.0.0" relative-path = "1.0.0"
rustc-hash = "1.1.0" rustc-hash = "1.1.0"
ra_cfg = { path = "../ra_cfg" } ra_cfg = { path = "../ra_cfg" }
stdx = { path = "../stdx" }

View file

@ -15,6 +15,7 @@ use std::{
}; };
pub use ra_cfg::CfgOptions; pub use ra_cfg::CfgOptions;
use stdx::split1;
pub use relative_path::{RelativePath, RelativePathBuf}; pub use relative_path::{RelativePath, RelativePathBuf};
pub use rustc_hash::FxHashMap; pub use rustc_hash::FxHashMap;
@ -332,11 +333,6 @@ fn parse_meta(meta: &str) -> FixtureMeta {
FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env }) FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env })
} }
fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
let idx = haystack.find(delim)?;
Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
}
/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines. /// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.
/// This allows fixtures to start off in a different indentation, e.g. to align the first line with /// This allows fixtures to start off in a different indentation, e.g. to align the first line with
/// the other lines visually: /// the other lines visually: