diff options
author | Aleksey Kladov <[email protected]> | 2020-07-30 21:19:58 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-07-30 21:23:12 +0100 |
commit | be49547b446cba240f8f2a9592284e77d4a6896f (patch) | |
tree | de01434050b1fb9437dd519ca0d65229d52156a3 | |
parent | 239dd506f68db0cbe4417b6e5c7f737d8ff8a159 (diff) |
Use split_once polyfill
-rw-r--r-- | crates/ra_project_model/src/cfg_flag.rs | 4 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cli/analysis_bench.rs | 21 | ||||
-rw-r--r-- | crates/rust-analyzer/src/handlers.rs | 4 | ||||
-rw-r--r-- | crates/stdx/src/lib.rs | 15 | ||||
-rw-r--r-- | crates/test_utils/src/fixture.rs | 8 |
5 files changed, 30 insertions, 22 deletions
diff --git a/crates/ra_project_model/src/cfg_flag.rs b/crates/ra_project_model/src/cfg_flag.rs index 1bc5d4832..bd50056c6 100644 --- a/crates/ra_project_model/src/cfg_flag.rs +++ b/crates/ra_project_model/src/cfg_flag.rs | |||
@@ -4,7 +4,7 @@ | |||
4 | use std::str::FromStr; | 4 | use std::str::FromStr; |
5 | 5 | ||
6 | use ra_cfg::CfgOptions; | 6 | use ra_cfg::CfgOptions; |
7 | use stdx::split_delim; | 7 | use stdx::split_once; |
8 | 8 | ||
9 | #[derive(Clone, Eq, PartialEq, Debug)] | 9 | #[derive(Clone, Eq, PartialEq, Debug)] |
10 | pub enum CfgFlag { | 10 | pub enum CfgFlag { |
@@ -15,7 +15,7 @@ pub enum CfgFlag { | |||
15 | impl FromStr for CfgFlag { | 15 | impl FromStr for CfgFlag { |
16 | type Err = String; | 16 | type Err = String; |
17 | fn from_str(s: &str) -> Result<Self, Self::Err> { | 17 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
18 | let res = match split_delim(s, '=') { | 18 | let res = match split_once(s, '=') { |
19 | Some((key, value)) => { | 19 | Some((key, value)) => { |
20 | if !(value.starts_with('"') && value.ends_with('"')) { | 20 | if !(value.starts_with('"') && value.ends_with('"')) { |
21 | return Err(format!("Invalid cfg ({:?}), value should be in quotes", s)); | 21 | return Err(format!("Invalid cfg ({:?}), value should be in quotes", s)); |
diff --git a/crates/rust-analyzer/src/cli/analysis_bench.rs b/crates/rust-analyzer/src/cli/analysis_bench.rs index 076184ad6..13a106638 100644 --- a/crates/rust-analyzer/src/cli/analysis_bench.rs +++ b/crates/rust-analyzer/src/cli/analysis_bench.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | 2 | ||
3 | use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant}; | 3 | use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant}; |
4 | 4 | ||
5 | use anyhow::{format_err, Result}; | 5 | use anyhow::{bail, format_err, Result}; |
6 | use ra_db::{ | 6 | use ra_db::{ |
7 | salsa::{Database, Durability}, | 7 | salsa::{Database, Durability}, |
8 | FileId, | 8 | FileId, |
@@ -30,19 +30,18 @@ pub struct Position { | |||
30 | impl FromStr for Position { | 30 | impl FromStr for Position { |
31 | type Err = anyhow::Error; | 31 | type Err = anyhow::Error; |
32 | fn from_str(s: &str) -> Result<Self> { | 32 | fn from_str(s: &str) -> Result<Self> { |
33 | let (path_line, column) = rsplit_at_char(s, ':')?; | 33 | let mut split = s.rsplitn(3, ':'); |
34 | let (path, line) = rsplit_at_char(path_line, ':')?; | 34 | match (split.next(), split.next(), split.next()) { |
35 | let path = env::current_dir().unwrap().join(path); | 35 | (Some(column), Some(line), Some(path)) => { |
36 | let path = AbsPathBuf::assert(path); | 36 | let path = env::current_dir().unwrap().join(path); |
37 | Ok(Position { path, line: line.parse()?, column: column.parse()? }) | 37 | let path = AbsPathBuf::assert(path); |
38 | Ok(Position { path, line: line.parse()?, column: column.parse()? }) | ||
39 | } | ||
40 | _ => bail!("position should be in file:line:column format: {:?}", s), | ||
41 | } | ||
38 | } | 42 | } |
39 | } | 43 | } |
40 | 44 | ||
41 | fn rsplit_at_char(s: &str, c: char) -> Result<(&str, &str)> { | ||
42 | let idx = s.rfind(c).ok_or_else(|| format_err!("no `{}` in {}", c, s))?; | ||
43 | Ok((&s[..idx], &s[idx + 1..])) | ||
44 | } | ||
45 | |||
46 | pub fn analysis_bench( | 45 | pub fn analysis_bench( |
47 | verbosity: Verbosity, | 46 | verbosity: Verbosity, |
48 | path: &Path, | 47 | path: &Path, |
diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index 62ed6880b..e73b3a211 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs | |||
@@ -26,7 +26,7 @@ use ra_project_model::TargetKind; | |||
26 | use ra_syntax::{algo, ast, AstNode, SyntaxKind, TextRange, TextSize}; | 26 | use ra_syntax::{algo, ast, AstNode, SyntaxKind, TextRange, TextSize}; |
27 | use serde::{Deserialize, Serialize}; | 27 | use serde::{Deserialize, Serialize}; |
28 | use serde_json::to_value; | 28 | use serde_json::to_value; |
29 | use stdx::{format_to, split_delim}; | 29 | use stdx::{format_to, split_once}; |
30 | 30 | ||
31 | use crate::{ | 31 | use crate::{ |
32 | cargo_target_spec::CargoTargetSpec, | 32 | cargo_target_spec::CargoTargetSpec, |
@@ -865,7 +865,7 @@ pub(crate) fn handle_resolve_code_action( | |||
865 | .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect()); | 865 | .map(|it| it.into_iter().filter_map(from_proto::assist_kind).collect()); |
866 | 866 | ||
867 | let assists = snap.analysis.resolved_assists(&snap.config.assist, frange)?; | 867 | let assists = snap.analysis.resolved_assists(&snap.config.assist, frange)?; |
868 | let (id_string, index) = split_delim(¶ms.id, ':').unwrap(); | 868 | let (id_string, index) = split_once(¶ms.id, ':').unwrap(); |
869 | let index = index.parse::<usize>().unwrap(); | 869 | let index = index.parse::<usize>().unwrap(); |
870 | let assist = &assists[index]; | 870 | let assist = &assists[index]; |
871 | assert!(assist.assist.id.0 == id_string); | 871 | assert!(assist.assist.id.0 == id_string); |
diff --git a/crates/stdx/src/lib.rs b/crates/stdx/src/lib.rs index ea0e6b949..b65875c96 100644 --- a/crates/stdx/src/lib.rs +++ b/crates/stdx/src/lib.rs | |||
@@ -109,9 +109,18 @@ pub fn replace(buf: &mut String, from: char, to: &str) { | |||
109 | *buf = buf.replace(from, to) | 109 | *buf = buf.replace(from, to) |
110 | } | 110 | } |
111 | 111 | ||
112 | pub fn split_delim(haystack: &str, delim: char) -> Option<(&str, &str)> { | 112 | // https://github.com/rust-lang/rust/issues/74773 |
113 | let idx = haystack.find(delim)?; | 113 | pub fn split_once(haystack: &str, delim: char) -> Option<(&str, &str)> { |
114 | Some((&haystack[..idx], &haystack[idx + delim.len_utf8()..])) | 114 | let mut split = haystack.splitn(2, delim); |
115 | let prefix = split.next()?; | ||
116 | let suffix = split.next()?; | ||
117 | Some((prefix, suffix)) | ||
118 | } | ||
119 | pub fn rsplit_once(haystack: &str, delim: char) -> Option<(&str, &str)> { | ||
120 | let mut split = haystack.rsplitn(2, delim); | ||
121 | let suffix = split.next()?; | ||
122 | let prefix = split.next()?; | ||
123 | Some((prefix, suffix)) | ||
115 | } | 124 | } |
116 | 125 | ||
117 | pub fn trim_indent(mut text: &str) -> String { | 126 | pub fn trim_indent(mut text: &str) -> String { |
diff --git a/crates/test_utils/src/fixture.rs b/crates/test_utils/src/fixture.rs index ed764046b..e40b61a94 100644 --- a/crates/test_utils/src/fixture.rs +++ b/crates/test_utils/src/fixture.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | //! rust-analyzer database from a single string. | 2 | //! rust-analyzer database from a single string. |
3 | 3 | ||
4 | use rustc_hash::FxHashMap; | 4 | use rustc_hash::FxHashMap; |
5 | use stdx::{lines_with_ends, split_delim, trim_indent}; | 5 | use stdx::{lines_with_ends, split_once, trim_indent}; |
6 | 6 | ||
7 | #[derive(Debug, Eq, PartialEq)] | 7 | #[derive(Debug, Eq, PartialEq)] |
8 | pub struct Fixture { | 8 | pub struct Fixture { |
@@ -71,14 +71,14 @@ impl Fixture { | |||
71 | let mut cfg_key_values = Vec::new(); | 71 | let mut cfg_key_values = Vec::new(); |
72 | let mut env = FxHashMap::default(); | 72 | let mut env = FxHashMap::default(); |
73 | for component in components[1..].iter() { | 73 | for component in components[1..].iter() { |
74 | let (key, value) = split_delim(component, ':').unwrap(); | 74 | let (key, value) = split_once(component, ':').unwrap(); |
75 | match key { | 75 | match key { |
76 | "crate" => krate = Some(value.to_string()), | 76 | "crate" => krate = Some(value.to_string()), |
77 | "deps" => deps = value.split(',').map(|it| it.to_string()).collect(), | 77 | "deps" => deps = value.split(',').map(|it| it.to_string()).collect(), |
78 | "edition" => edition = Some(value.to_string()), | 78 | "edition" => edition = Some(value.to_string()), |
79 | "cfg" => { | 79 | "cfg" => { |
80 | for entry in value.split(',') { | 80 | for entry in value.split(',') { |
81 | match split_delim(entry, '=') { | 81 | match split_once(entry, '=') { |
82 | Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())), | 82 | Some((k, v)) => cfg_key_values.push((k.to_string(), v.to_string())), |
83 | None => cfg_atoms.push(entry.to_string()), | 83 | None => cfg_atoms.push(entry.to_string()), |
84 | } | 84 | } |
@@ -86,7 +86,7 @@ impl Fixture { | |||
86 | } | 86 | } |
87 | "env" => { | 87 | "env" => { |
88 | for key in value.split(',') { | 88 | for key in value.split(',') { |
89 | if let Some((k, v)) = split_delim(key, '=') { | 89 | if let Some((k, v)) = split_once(key, '=') { |
90 | env.insert(k.into(), v.into()); | 90 | env.insert(k.into(), v.into()); |
91 | } | 91 | } |
92 | } | 92 | } |