Skip to content

Commit f06b2bc

Browse files
committed
Use split1 when formatting function signature params
1 parent 1211a46 commit f06b2bc

File tree

5 files changed

+12
-11
lines changed

5 files changed

+12
-11
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_ide/src/display/function_signature.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use std::{
1010
use hir::{Docs, Documentation, HasSource, HirDisplay};
1111
use ra_ide_db::RootDatabase;
1212
use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
13-
use stdx::SepBy;
13+
use stdx::{split1, SepBy};
1414

1515
use crate::display::{generic_parameters, where_predicates};
1616

@@ -210,10 +210,8 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
210210
// macro-generated functions are missing whitespace
211211
fn fmt_param(param: ast::Param) -> String {
212212
let text = param.syntax().text().to_string();
213-
match text.find(':') {
214-
Some(pos) if 1 + pos < text.len() => {
215-
format!("{} {}", &text[0..1 + pos].trim(), &text[1 + pos..].trim())
216-
}
213+
match split1(&text, ':') {
214+
Some((left, right)) => format!("{}: {}", left.trim(), right.trim()),
217215
_ => text,
218216
}
219217
}

crates/stdx/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ pub fn replace(buf: &mut String, from: char, to: &str) {
124124
// FIXME: do this in place.
125125
*buf = buf.replace(from, to)
126126
}
127+
128+
pub fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
129+
let idx = haystack.find(delim)?;
130+
Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
131+
}

crates/test_utils/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ serde_json = "1.0.48"
1414
relative-path = "1.0.0"
1515
rustc-hash = "1.1.0"
1616

17-
ra_cfg = { path = "../ra_cfg" }
17+
ra_cfg = { path = "../ra_cfg" }
18+
stdx = { path = "../stdx" }

crates/test_utils/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use std::{
1515
};
1616

1717
pub use ra_cfg::CfgOptions;
18+
use stdx::split1;
1819

1920
pub use relative_path::{RelativePath, RelativePathBuf};
2021
pub use rustc_hash::FxHashMap;
@@ -332,11 +333,6 @@ fn parse_meta(meta: &str) -> FixtureMeta {
332333
FixtureMeta::File(FileMeta { path, crate_name: krate, deps, edition, cfg, env })
333334
}
334335

335-
fn split1(haystack: &str, delim: char) -> Option<(&str, &str)> {
336-
let idx = haystack.find(delim)?;
337-
Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..]))
338-
}
339-
340336
/// Adjusts the indentation of the first line to the minimum indentation of the rest of the lines.
341337
/// This allows fixtures to start off in a different indentation, e.g. to align the first line with
342338
/// the other lines visually:

0 commit comments

Comments
 (0)