aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-30 21:19:58 +0100
committerAleksey Kladov <[email protected]>2020-07-30 21:23:12 +0100
commitbe49547b446cba240f8f2a9592284e77d4a6896f (patch)
treede01434050b1fb9437dd519ca0d65229d52156a3 /crates
parent239dd506f68db0cbe4417b6e5c7f737d8ff8a159 (diff)
Use split_once polyfill
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_project_model/src/cfg_flag.rs4
-rw-r--r--crates/rust-analyzer/src/cli/analysis_bench.rs21
-rw-r--r--crates/rust-analyzer/src/handlers.rs4
-rw-r--r--crates/stdx/src/lib.rs15
-rw-r--r--crates/test_utils/src/fixture.rs8
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 @@
4use std::str::FromStr; 4use std::str::FromStr;
5 5
6use ra_cfg::CfgOptions; 6use ra_cfg::CfgOptions;
7use stdx::split_delim; 7use stdx::split_once;
8 8
9#[derive(Clone, Eq, PartialEq, Debug)] 9#[derive(Clone, Eq, PartialEq, Debug)]
10pub enum CfgFlag { 10pub enum CfgFlag {
@@ -15,7 +15,7 @@ pub enum CfgFlag {
15impl FromStr for CfgFlag { 15impl 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
3use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant}; 3use std::{env, path::Path, str::FromStr, sync::Arc, time::Instant};
4 4
5use anyhow::{format_err, Result}; 5use anyhow::{bail, format_err, Result};
6use ra_db::{ 6use ra_db::{
7 salsa::{Database, Durability}, 7 salsa::{Database, Durability},
8 FileId, 8 FileId,
@@ -30,19 +30,18 @@ pub struct Position {
30impl FromStr for Position { 30impl 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
41fn 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
46pub fn analysis_bench( 45pub 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;
26use ra_syntax::{algo, ast, AstNode, SyntaxKind, TextRange, TextSize}; 26use ra_syntax::{algo, ast, AstNode, SyntaxKind, TextRange, TextSize};
27use serde::{Deserialize, Serialize}; 27use serde::{Deserialize, Serialize};
28use serde_json::to_value; 28use serde_json::to_value;
29use stdx::{format_to, split_delim}; 29use stdx::{format_to, split_once};
30 30
31use crate::{ 31use 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(&params.id, ':').unwrap(); 868 let (id_string, index) = split_once(&params.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
112pub 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)?; 113pub 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}
119pub 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
117pub fn trim_indent(mut text: &str) -> String { 126pub 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
4use rustc_hash::FxHashMap; 4use rustc_hash::FxHashMap;
5use stdx::{lines_with_ends, split_delim, trim_indent}; 5use stdx::{lines_with_ends, split_once, trim_indent};
6 6
7#[derive(Debug, Eq, PartialEq)] 7#[derive(Debug, Eq, PartialEq)]
8pub struct Fixture { 8pub 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 }