From 8d34262956059aca7e6fded351a9299b3581a5cf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 16:52:28 +0200 Subject: Rename ra_toolchain -> toolchain --- crates/flycheck/Cargo.toml | 2 +- crates/flycheck/src/lib.rs | 2 +- crates/ra_proc_macro_srv/Cargo.toml | 2 +- crates/ra_proc_macro_srv/src/tests/utils.rs | 2 +- crates/ra_project_model/Cargo.toml | 2 +- crates/ra_project_model/src/cargo_workspace.rs | 4 +- crates/ra_project_model/src/lib.rs | 2 +- crates/ra_project_model/src/sysroot.rs | 4 +- crates/ra_toolchain/Cargo.toml | 12 ----- crates/ra_toolchain/src/lib.rs | 68 -------------------------- crates/rust-analyzer/Cargo.toml | 2 +- crates/rust-analyzer/src/handlers.rs | 2 +- crates/toolchain/Cargo.toml | 12 +++++ crates/toolchain/src/lib.rs | 66 +++++++++++++++++++++++++ 14 files changed, 90 insertions(+), 92 deletions(-) delete mode 100644 crates/ra_toolchain/Cargo.toml delete mode 100644 crates/ra_toolchain/src/lib.rs create mode 100644 crates/toolchain/Cargo.toml create mode 100644 crates/toolchain/src/lib.rs (limited to 'crates') diff --git a/crates/flycheck/Cargo.toml b/crates/flycheck/Cargo.toml index ff8a1e568..acc72bc59 100644 --- a/crates/flycheck/Cargo.toml +++ b/crates/flycheck/Cargo.toml @@ -14,4 +14,4 @@ log = "0.4.8" cargo_metadata = "0.11.1" serde_json = "1.0.48" jod-thread = "0.1.1" -ra_toolchain = { path = "../ra_toolchain" } +toolchain = { path = "../toolchain" } diff --git a/crates/flycheck/src/lib.rs b/crates/flycheck/src/lib.rs index 31e14246d..16078d104 100644 --- a/crates/flycheck/src/lib.rs +++ b/crates/flycheck/src/lib.rs @@ -193,7 +193,7 @@ impl FlycheckActor { extra_args, features, } => { - let mut cmd = Command::new(ra_toolchain::cargo()); + let mut cmd = Command::new(toolchain::cargo()); cmd.arg(command); cmd.args(&["--workspace", "--message-format=json", "--manifest-path"]) .arg(self.workspace_root.join("Cargo.toml")); diff --git a/crates/ra_proc_macro_srv/Cargo.toml b/crates/ra_proc_macro_srv/Cargo.toml index a690cc044..1c25e7229 100644 --- a/crates/ra_proc_macro_srv/Cargo.toml +++ b/crates/ra_proc_macro_srv/Cargo.toml @@ -23,4 +23,4 @@ cargo_metadata = "0.11.1" difference = "2.0.0" # used as proc macro test target serde_derive = "1.0.106" -ra_toolchain = { path = "../ra_toolchain" } +toolchain = { path = "../toolchain" } diff --git a/crates/ra_proc_macro_srv/src/tests/utils.rs b/crates/ra_proc_macro_srv/src/tests/utils.rs index dcb00671f..1b6a0b6fb 100644 --- a/crates/ra_proc_macro_srv/src/tests/utils.rs +++ b/crates/ra_proc_macro_srv/src/tests/utils.rs @@ -12,7 +12,7 @@ mod fixtures { // Use current project metadata to get the proc-macro dylib path pub fn dylib_path(crate_name: &str, version: &str) -> std::path::PathBuf { - let command = Command::new(ra_toolchain::cargo()) + let command = Command::new(toolchain::cargo()) .args(&["check", "--message-format", "json"]) .output() .unwrap() diff --git a/crates/ra_project_model/Cargo.toml b/crates/ra_project_model/Cargo.toml index 27b1f5d33..171fe8626 100644 --- a/crates/ra_project_model/Cargo.toml +++ b/crates/ra_project_model/Cargo.toml @@ -17,7 +17,7 @@ cargo_metadata = "0.11.1" arena = { path = "../arena" } ra_cfg = { path = "../ra_cfg" } ra_db = { path = "../ra_db" } -ra_toolchain = { path = "../ra_toolchain" } +toolchain = { path = "../toolchain" } ra_proc_macro = { path = "../ra_proc_macro" } paths = { path = "../paths" } stdx = { path = "../stdx" } diff --git a/crates/ra_project_model/src/cargo_workspace.rs b/crates/ra_project_model/src/cargo_workspace.rs index a526d743d..abf8dca96 100644 --- a/crates/ra_project_model/src/cargo_workspace.rs +++ b/crates/ra_project_model/src/cargo_workspace.rs @@ -140,7 +140,7 @@ impl CargoWorkspace { cargo_features: &CargoConfig, ) -> Result { let mut meta = MetadataCommand::new(); - meta.cargo_path(ra_toolchain::cargo()); + meta.cargo_path(toolchain::cargo()); meta.manifest_path(cargo_toml.to_path_buf()); if cargo_features.all_features { meta.features(CargoOpt::AllFeatures); @@ -288,7 +288,7 @@ pub fn load_extern_resources( cargo_toml: &Path, cargo_features: &CargoConfig, ) -> Result { - let mut cmd = Command::new(ra_toolchain::cargo()); + let mut cmd = Command::new(toolchain::cargo()); cmd.args(&["check", "--message-format=json", "--manifest-path"]).arg(cargo_toml); if cargo_features.all_features { cmd.arg("--all-features"); diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 300e75135..46f44910c 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -513,7 +513,7 @@ fn get_rustc_cfg_options(target: Option<&str>) -> Vec { } let rustc_cfgs = { - let mut cmd = Command::new(ra_toolchain::rustc()); + let mut cmd = Command::new(toolchain::rustc()); cmd.args(&["--print", "cfg", "-O"]); if let Some(target) = target { cmd.args(&["--target", target]); diff --git a/crates/ra_project_model/src/sysroot.rs b/crates/ra_project_model/src/sysroot.rs index 6ef001769..8239797b6 100644 --- a/crates/ra_project_model/src/sysroot.rs +++ b/crates/ra_project_model/src/sysroot.rs @@ -101,13 +101,13 @@ fn get_or_install_rust_src(cargo_toml: &AbsPath) -> Result { return Ok(path); } let current_dir = cargo_toml.parent().unwrap(); - let mut rustc = Command::new(ra_toolchain::rustc()); + let mut rustc = Command::new(toolchain::rustc()); rustc.current_dir(current_dir).args(&["--print", "sysroot"]); let stdout = utf8_stdout(rustc)?; let sysroot_path = AbsPath::assert(Path::new(stdout.trim())); let mut src = get_rust_src(sysroot_path); if src.is_none() { - let mut rustup = Command::new(ra_toolchain::rustup()); + let mut rustup = Command::new(toolchain::rustup()); rustup.current_dir(current_dir).args(&["component", "add", "rust-src"]); utf8_stdout(rustup)?; src = get_rust_src(sysroot_path); diff --git a/crates/ra_toolchain/Cargo.toml b/crates/ra_toolchain/Cargo.toml deleted file mode 100644 index 84b748c0a..000000000 --- a/crates/ra_toolchain/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -edition = "2018" -name = "ra_toolchain" -version = "0.1.0" -authors = ["rust-analyzer developers"] -license = "MIT OR Apache-2.0" - -[lib] -doctest = false - -[dependencies] -home = "0.5.3" diff --git a/crates/ra_toolchain/src/lib.rs b/crates/ra_toolchain/src/lib.rs deleted file mode 100644 index 9916e52c4..000000000 --- a/crates/ra_toolchain/src/lib.rs +++ /dev/null @@ -1,68 +0,0 @@ -//! This crate contains a single public function -//! [`get_path_for_executable`](fn.get_path_for_executable.html). -//! See docs there for more information. -use std::{env, iter, path::PathBuf}; - -pub fn cargo() -> PathBuf { - get_path_for_executable("cargo") -} - -pub fn rustc() -> PathBuf { - get_path_for_executable("rustc") -} - -pub fn rustup() -> PathBuf { - get_path_for_executable("rustup") -} - -pub fn rustfmt() -> PathBuf { - get_path_for_executable("rustfmt") -} - -/// Return a `PathBuf` to use for the given executable. -/// -/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that -/// gives a valid Cargo executable; or it may return a full path to a valid -/// Cargo. -fn get_path_for_executable(executable_name: &'static str) -> PathBuf { - // The current implementation checks three places for an executable to use: - // 1) Appropriate environment variable (erroring if this is set but not a usable executable) - // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc - // 2) `` - // example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH - // 3) `~/.cargo/bin/` - // example: for cargo, this tries ~/.cargo/bin/cargo - // It seems that this is a reasonable place to try for cargo, rustc, and rustup - let env_var = executable_name.to_ascii_uppercase(); - if let Some(path) = env::var_os(&env_var) { - return path.into(); - } - - if lookup_in_path(executable_name) { - return executable_name.into(); - } - - if let Some(mut path) = home::home_dir() { - path.push(".cargo"); - path.push("bin"); - path.push(executable_name); - if let Some(path) = probe(path) { - return path; - } - } - - executable_name.into() -} - -fn lookup_in_path(exec: &str) -> bool { - let paths = env::var_os("PATH").unwrap_or_default(); - env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some() -} - -fn probe(path: PathBuf) -> Option { - let with_extension = match env::consts::EXE_EXTENSION { - "" => None, - it => Some(path.with_extension(it)), - }; - iter::once(path).chain(with_extension).find(|it| it.is_file()) -} diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 9bc5cc631..3f70510fd 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -43,7 +43,7 @@ ra_text_edit = { path = "../ra_text_edit" } vfs = { path = "../vfs" } vfs-notify = { path = "../vfs-notify" } ra_cfg = { path = "../ra_cfg" } -ra_toolchain = { path = "../ra_toolchain" } +toolchain = { path = "../toolchain" } # This should only be used in CLI ra_db = { path = "../ra_db" } diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index d9b75eed4..07f4af3d3 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -692,7 +692,7 @@ pub(crate) fn handle_formatting( let mut rustfmt = match &snap.config.rustfmt { RustfmtConfig::Rustfmt { extra_args } => { - let mut cmd = process::Command::new(ra_toolchain::rustfmt()); + let mut cmd = process::Command::new(toolchain::rustfmt()); cmd.args(extra_args); if let Some(&crate_id) = crate_ids.first() { // Assume all crates are in the same edition diff --git a/crates/toolchain/Cargo.toml b/crates/toolchain/Cargo.toml new file mode 100644 index 000000000..4856668f8 --- /dev/null +++ b/crates/toolchain/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "toolchain" +version = "0.0.0" +license = "MIT OR Apache-2.0" +authors = ["rust-analyzer developers"] +edition = "2018" + +[lib] +doctest = false + +[dependencies] +home = "0.5.3" diff --git a/crates/toolchain/src/lib.rs b/crates/toolchain/src/lib.rs new file mode 100644 index 000000000..3b6886f5b --- /dev/null +++ b/crates/toolchain/src/lib.rs @@ -0,0 +1,66 @@ +//! Discovery of `cargo` & `rustc` executables. +use std::{env, iter, path::PathBuf}; + +pub fn cargo() -> PathBuf { + get_path_for_executable("cargo") +} + +pub fn rustc() -> PathBuf { + get_path_for_executable("rustc") +} + +pub fn rustup() -> PathBuf { + get_path_for_executable("rustup") +} + +pub fn rustfmt() -> PathBuf { + get_path_for_executable("rustfmt") +} + +/// Return a `PathBuf` to use for the given executable. +/// +/// E.g., `get_path_for_executable("cargo")` may return just `cargo` if that +/// gives a valid Cargo executable; or it may return a full path to a valid +/// Cargo. +fn get_path_for_executable(executable_name: &'static str) -> PathBuf { + // The current implementation checks three places for an executable to use: + // 1) Appropriate environment variable (erroring if this is set but not a usable executable) + // example: for cargo, this checks $CARGO environment variable; for rustc, $RUSTC; etc + // 2) `` + // example: for cargo, this tries just `cargo`, which will succeed if `cargo` is on the $PATH + // 3) `~/.cargo/bin/` + // example: for cargo, this tries ~/.cargo/bin/cargo + // It seems that this is a reasonable place to try for cargo, rustc, and rustup + let env_var = executable_name.to_ascii_uppercase(); + if let Some(path) = env::var_os(&env_var) { + return path.into(); + } + + if lookup_in_path(executable_name) { + return executable_name.into(); + } + + if let Some(mut path) = home::home_dir() { + path.push(".cargo"); + path.push("bin"); + path.push(executable_name); + if let Some(path) = probe(path) { + return path; + } + } + + executable_name.into() +} + +fn lookup_in_path(exec: &str) -> bool { + let paths = env::var_os("PATH").unwrap_or_default(); + env::split_paths(&paths).map(|path| path.join(exec)).find_map(probe).is_some() +} + +fn probe(path: PathBuf) -> Option { + let with_extension = match env::consts::EXE_EXTENSION { + "" => None, + it => Some(path.with_extension(it)), + }; + iter::once(path).chain(with_extension).find(|it| it.is_file()) +} -- cgit v1.2.3 From 7510048ec0a5d5e7136e3ea258954eb244d15baf Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 16:58:56 +0200 Subject: Cleanup TextEdit API --- crates/ra_assists/src/assist_context.rs | 4 ++-- crates/ra_ide/src/diagnostics.rs | 6 ++--- .../ra_ide/src/diagnostics/diagnostics_with_fix.rs | 4 ++-- crates/ra_ide/src/join_lines.rs | 4 ++-- crates/ra_ide/src/references/rename.rs | 2 +- crates/ra_ssr/src/replacing.rs | 2 +- crates/ra_text_edit/Cargo.toml | 7 +++--- crates/ra_text_edit/src/lib.rs | 27 +++++++++++++++------- 8 files changed, 33 insertions(+), 23 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs index afd3fd4b9..afba860d1 100644 --- a/crates/ra_assists/src/assist_context.rs +++ b/crates/ra_assists/src/assist_context.rs @@ -15,7 +15,7 @@ use ra_syntax::{ AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize, TokenAtOffset, }; -use ra_text_edit::TextEditBuilder; +use ra_text_edit::{TextEdit, TextEditBuilder}; use crate::{ assist_config::{AssistConfig, SnippetCap}, @@ -214,7 +214,7 @@ pub(crate) struct AssistBuilder { impl AssistBuilder { pub(crate) fn new(file_id: FileId) -> AssistBuilder { AssistBuilder { - edit: TextEditBuilder::default(), + edit: TextEdit::builder(), file_id, is_snippet: false, change: SourceChange::default(), diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index 07bf133bd..e006c7775 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -14,7 +14,7 @@ use ra_syntax::{ ast::{self, AstNode}, SyntaxNode, TextRange, T, }; -use ra_text_edit::{TextEdit, TextEditBuilder}; +use ra_text_edit::TextEdit; use crate::{Diagnostic, FileId, Fix, SourceFileEdit}; @@ -103,7 +103,7 @@ fn check_unnecessary_braces_in_use_statement( text_edit_for_remove_unnecessary_braces_with_self_in_use_statement(&single_use_tree) .unwrap_or_else(|| { let to_replace = single_use_tree.syntax().text().to_string(); - let mut edit_builder = TextEditBuilder::default(); + let mut edit_builder = TextEdit::builder(); edit_builder.delete(use_range); edit_builder.insert(use_range.start(), to_replace); edit_builder.finish() @@ -149,7 +149,7 @@ fn check_struct_shorthand_initialization( let field_expr = expr.syntax().text().to_string(); let field_name_is_tup_index = name_ref.as_tuple_field().is_some(); if field_name == field_expr && !field_name_is_tup_index { - let mut edit_builder = TextEditBuilder::default(); + let mut edit_builder = TextEdit::builder(); edit_builder.delete(record_field.syntax().text_range()); edit_builder.insert(record_field.syntax().text_range().start(), field_name); let edit = edit_builder.finish(); diff --git a/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs b/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs index f7c73773f..88e593e00 100644 --- a/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs +++ b/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs @@ -13,7 +13,7 @@ use ra_ide_db::{ RootDatabase, }; use ra_syntax::{algo, ast, AstNode}; -use ra_text_edit::{TextEdit, TextEditBuilder}; +use ra_text_edit::TextEdit; /// A [Diagnostic] that potentially has a fix available. /// @@ -70,7 +70,7 @@ impl DiagnosticWithFix for MissingFields { } let edit = { - let mut builder = TextEditBuilder::default(); + let mut builder = TextEdit::builder(); algo::diff(&old_field_list.syntax(), &new_field_list.syntax()) .into_text_edit(&mut builder); builder.finish() diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs index 6907c09e8..1c881386f 100644 --- a/crates/ra_ide/src/join_lines.rs +++ b/crates/ra_ide/src/join_lines.rs @@ -23,7 +23,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { let syntax = file.syntax(); let text = syntax.text().slice(range.start()..); let pos = match text.find_char('\n') { - None => return TextEditBuilder::default().finish(), + None => return TextEdit::builder().finish(), Some(pos) => pos, }; TextRange::at(range.start() + pos, TextSize::of('\n')) @@ -35,7 +35,7 @@ pub fn join_lines(file: &SourceFile, range: TextRange) -> TextEdit { NodeOrToken::Node(node) => node, NodeOrToken::Token(token) => token.parent(), }; - let mut edit = TextEditBuilder::default(); + let mut edit = TextEdit::builder(); for token in node.descendants_with_tokens().filter_map(|it| it.into_token()) { let range = match range.intersect(token.text_range()) { Some(range) => range, diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index c8d80fcf7..8c1ac3c56 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -281,7 +281,7 @@ mod tests { let ra_fixture_after = &trim_indent(ra_fixture_after); let (analysis, position) = analysis_and_position(ra_fixture_before); let source_change = analysis.rename(position, new_name).unwrap(); - let mut text_edit_builder = TextEditBuilder::default(); + let mut text_edit_builder = TextEdit::builder(); let mut file_id: Option = None; if let Some(change) = source_change { for edit in change.info.source_file_edits { diff --git a/crates/ra_ssr/src/replacing.rs b/crates/ra_ssr/src/replacing.rs index 0943244ff..36ced3842 100644 --- a/crates/ra_ssr/src/replacing.rs +++ b/crates/ra_ssr/src/replacing.rs @@ -24,7 +24,7 @@ fn matches_to_edit_at_offset( relative_start: TextSize, rules: &[ResolvedRule], ) -> TextEdit { - let mut edit_builder = ra_text_edit::TextEditBuilder::default(); + let mut edit_builder = TextEdit::builder(); for m in &matches.matches { edit_builder.replace( m.range.range.checked_sub(relative_start).unwrap(), diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml index dbb223350..427862a5c 100644 --- a/crates/ra_text_edit/Cargo.toml +++ b/crates/ra_text_edit/Cargo.toml @@ -1,10 +1,9 @@ [package] -edition = "2018" name = "ra_text_edit" -version = "0.1.0" -authors = ["rust-analyzer developers"] -publish = false +version = "0.0.0" license = "MIT OR Apache-2.0" +authors = ["rust-analyzer developers"] +edition = "2018" [lib] doctest = false diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs index d68791cf1..ab8cd7fd1 100644 --- a/crates/ra_text_edit/src/lib.rs +++ b/crates/ra_text_edit/src/lib.rs @@ -3,8 +3,6 @@ //! `rust-analyzer` never mutates text itself and only sends diffs to clients, //! so `TextEdit` is the ultimate representation of the work done by //! rust-analyzer. -use std::{slice, vec}; - pub use text_size::{TextRange, TextSize}; /// `InsertDelete` -- a single "atomic" change to text @@ -46,20 +44,24 @@ impl Indel { } impl TextEdit { + pub fn builder() -> TextEditBuilder { + TextEditBuilder::default() + } + pub fn insert(offset: TextSize, text: String) -> TextEdit { - let mut builder = TextEditBuilder::default(); + let mut builder = TextEdit::builder(); builder.insert(offset, text); builder.finish() } pub fn delete(range: TextRange) -> TextEdit { - let mut builder = TextEditBuilder::default(); + let mut builder = TextEdit::builder(); builder.delete(range); builder.finish() } pub fn replace(range: TextRange, replace_with: String) -> TextEdit { - let mut builder = TextEditBuilder::default(); + let mut builder = TextEdit::builder(); builder.replace(range, replace_with); builder.finish() } @@ -72,8 +74,8 @@ impl TextEdit { self.indels.is_empty() } - pub fn iter(&self) -> slice::Iter<'_, Indel> { - self.indels.iter() + pub fn iter(&self) -> std::slice::Iter<'_, Indel> { + self.into_iter() } pub fn apply(&self, text: &mut String) { @@ -139,13 +141,22 @@ impl TextEdit { impl IntoIterator for TextEdit { type Item = Indel; - type IntoIter = vec::IntoIter; + type IntoIter = std::vec::IntoIter; fn into_iter(self) -> Self::IntoIter { self.indels.into_iter() } } +impl<'a> IntoIterator for &'a TextEdit { + type Item = &'a Indel; + type IntoIter = std::slice::Iter<'a, Indel>; + + fn into_iter(self) -> Self::IntoIter { + self.indels.iter() + } +} + impl TextEditBuilder { pub fn replace(&mut self, range: TextRange, replace_with: String) { self.indels.push(Indel::replace(range, replace_with)) -- cgit v1.2.3 From 6dafc13f5f776980cd2560fb79d3d4790811c96d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 12 Aug 2020 17:03:06 +0200 Subject: Rename ra_text_edit -> text_edit --- crates/ra_assists/Cargo.toml | 2 +- crates/ra_assists/src/assist_context.rs | 2 +- crates/ra_assists/src/utils/insert_use.rs | 4 +- crates/ra_ide/Cargo.toml | 2 +- crates/ra_ide/src/completion/complete_postfix.rs | 2 +- .../ra_ide/src/completion/complete_trait_impl.rs | 2 +- crates/ra_ide/src/completion/completion_context.rs | 2 +- crates/ra_ide/src/completion/completion_item.rs | 2 +- crates/ra_ide/src/diagnostics.rs | 2 +- .../ra_ide/src/diagnostics/diagnostics_with_fix.rs | 2 +- crates/ra_ide/src/join_lines.rs | 2 +- crates/ra_ide/src/lib.rs | 2 +- crates/ra_ide/src/references/rename.rs | 4 +- crates/ra_ide/src/typing.rs | 2 +- crates/ra_ide/src/typing/on_enter.rs | 2 +- crates/ra_ide_db/Cargo.toml | 2 +- crates/ra_ide_db/src/source_change.rs | 2 +- crates/ra_ssr/Cargo.toml | 2 +- crates/ra_ssr/src/replacing.rs | 2 +- crates/ra_syntax/Cargo.toml | 2 +- crates/ra_syntax/fuzz/Cargo.toml | 2 +- crates/ra_syntax/src/algo.rs | 2 +- crates/ra_syntax/src/fuzz.rs | 2 +- crates/ra_syntax/src/lib.rs | 2 +- crates/ra_syntax/src/parsing/reparsing.rs | 2 +- crates/ra_text_edit/Cargo.toml | 12 -- crates/ra_text_edit/src/lib.rs | 186 --------------------- crates/rust-analyzer/Cargo.toml | 2 +- crates/text_edit/Cargo.toml | 12 ++ crates/text_edit/src/lib.rs | 186 +++++++++++++++++++++ 30 files changed, 226 insertions(+), 226 deletions(-) delete mode 100644 crates/ra_text_edit/Cargo.toml delete mode 100644 crates/ra_text_edit/src/lib.rs create mode 100644 crates/text_edit/Cargo.toml create mode 100644 crates/text_edit/src/lib.rs (limited to 'crates') diff --git a/crates/ra_assists/Cargo.toml b/crates/ra_assists/Cargo.toml index 6f5ace941..e4a5ee6c1 100644 --- a/crates/ra_assists/Cargo.toml +++ b/crates/ra_assists/Cargo.toml @@ -16,7 +16,7 @@ either = "1.5.3" stdx = { path = "../stdx" } ra_syntax = { path = "../ra_syntax" } -ra_text_edit = { path = "../ra_text_edit" } +text_edit = { path = "../text_edit" } ra_fmt = { path = "../ra_fmt" } profile = { path = "../profile" } ra_db = { path = "../ra_db" } diff --git a/crates/ra_assists/src/assist_context.rs b/crates/ra_assists/src/assist_context.rs index afba860d1..fcaa1aedc 100644 --- a/crates/ra_assists/src/assist_context.rs +++ b/crates/ra_assists/src/assist_context.rs @@ -15,7 +15,7 @@ use ra_syntax::{ AstNode, SourceFile, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize, TokenAtOffset, }; -use ra_text_edit::{TextEdit, TextEditBuilder}; +use text_edit::{TextEdit, TextEditBuilder}; use crate::{ assist_config::{AssistConfig, SnippetCap}, diff --git a/crates/ra_assists/src/utils/insert_use.rs b/crates/ra_assists/src/utils/insert_use.rs index 32780fceb..13dbe1919 100644 --- a/crates/ra_assists/src/utils/insert_use.rs +++ b/crates/ra_assists/src/utils/insert_use.rs @@ -2,6 +2,7 @@ // FIXME: rewrite according to the plan, outlined in // https://github.com/rust-analyzer/rust-analyzer/issues/3301#issuecomment-592931553 +use either::Either; use hir::{self, ModPath}; use ra_syntax::{ ast::{self, NameOwner, VisibilityOwner}, @@ -9,10 +10,9 @@ use ra_syntax::{ SyntaxKind::{PATH, PATH_SEGMENT}, SyntaxNode, T, }; -use ra_text_edit::TextEditBuilder; +use text_edit::TextEditBuilder; use crate::assist_context::AssistContext; -use either::Either; /// Determines the containing syntax node in which to insert a `use` statement affecting `position`. pub(crate) fn find_insert_use_container( diff --git a/crates/ra_ide/Cargo.toml b/crates/ra_ide/Cargo.toml index bbc9ba4e7..84c25f0b8 100644 --- a/crates/ra_ide/Cargo.toml +++ b/crates/ra_ide/Cargo.toml @@ -22,7 +22,7 @@ oorandom = "11.1.2" stdx = { path = "../stdx" } ra_syntax = { path = "../ra_syntax" } -ra_text_edit = { path = "../ra_text_edit" } +text_edit = { path = "../text_edit" } ra_db = { path = "../ra_db" } ra_ide_db = { path = "../ra_ide_db" } ra_cfg = { path = "../ra_cfg" } diff --git a/crates/ra_ide/src/completion/complete_postfix.rs b/crates/ra_ide/src/completion/complete_postfix.rs index 8735b9010..42087da8d 100644 --- a/crates/ra_ide/src/completion/complete_postfix.rs +++ b/crates/ra_ide/src/completion/complete_postfix.rs @@ -4,7 +4,7 @@ use ra_syntax::{ ast::{self, AstNode}, TextRange, TextSize, }; -use ra_text_edit::TextEdit; +use text_edit::TextEdit; use crate::{ completion::{ diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index d9a0ef167..b397baf10 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -37,7 +37,7 @@ use ra_syntax::{ ast::{self, edit, Impl}, AstNode, SyntaxKind, SyntaxNode, TextRange, T, }; -use ra_text_edit::TextEdit; +use text_edit::TextEdit; use crate::{ completion::{ diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 4aa761148..0cb57fb1b 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs @@ -9,7 +9,7 @@ use ra_syntax::{ SyntaxKind::*, SyntaxNode, SyntaxToken, TextRange, TextSize, }; -use ra_text_edit::Indel; +use text_edit::Indel; use super::patterns::{ has_bind_pat_parent, has_block_expr_parent, has_impl_as_prev_sibling, has_impl_parent, diff --git a/crates/ra_ide/src/completion/completion_item.rs b/crates/ra_ide/src/completion/completion_item.rs index 7bdda316c..1c0684f4e 100644 --- a/crates/ra_ide/src/completion/completion_item.rs +++ b/crates/ra_ide/src/completion/completion_item.rs @@ -4,7 +4,7 @@ use std::fmt; use hir::Documentation; use ra_syntax::TextRange; -use ra_text_edit::TextEdit; +use text_edit::TextEdit; use crate::completion::completion_config::SnippetCap; diff --git a/crates/ra_ide/src/diagnostics.rs b/crates/ra_ide/src/diagnostics.rs index e006c7775..54810d5bb 100644 --- a/crates/ra_ide/src/diagnostics.rs +++ b/crates/ra_ide/src/diagnostics.rs @@ -14,7 +14,7 @@ use ra_syntax::{ ast::{self, AstNode}, SyntaxNode, TextRange, T, }; -use ra_text_edit::TextEdit; +use text_edit::TextEdit; use crate::{Diagnostic, FileId, Fix, SourceFileEdit}; diff --git a/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs b/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs index 88e593e00..8fb25de6c 100644 --- a/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs +++ b/crates/ra_ide/src/diagnostics/diagnostics_with_fix.rs @@ -13,7 +13,7 @@ use ra_ide_db::{ RootDatabase, }; use ra_syntax::{algo, ast, AstNode}; -use ra_text_edit::TextEdit; +use text_edit::TextEdit; /// A [Diagnostic] that potentially has a fix available. /// diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs index 1c881386f..caf63933a 100644 --- a/crates/ra_ide/src/join_lines.rs +++ b/crates/ra_ide/src/join_lines.rs @@ -7,7 +7,7 @@ use ra_syntax::{ SyntaxKind::{self, WHITESPACE}, SyntaxNode, SyntaxToken, TextRange, TextSize, T, }; -use ra_text_edit::{TextEdit, TextEditBuilder}; +use text_edit::{TextEdit, TextEditBuilder}; // Feature: Join Lines // diff --git a/crates/ra_ide/src/lib.rs b/crates/ra_ide/src/lib.rs index bfcf5d750..09cb5faf6 100644 --- a/crates/ra_ide/src/lib.rs +++ b/crates/ra_ide/src/lib.rs @@ -96,7 +96,7 @@ pub use ra_ide_db::{ RootDatabase, }; pub use ra_ssr::SsrError; -pub use ra_text_edit::{Indel, TextEdit}; +pub use text_edit::{Indel, TextEdit}; pub type Cancelable = Result; diff --git a/crates/ra_ide/src/references/rename.rs b/crates/ra_ide/src/references/rename.rs index 8c1ac3c56..9c688fb06 100644 --- a/crates/ra_ide/src/references/rename.rs +++ b/crates/ra_ide/src/references/rename.rs @@ -11,9 +11,9 @@ use ra_syntax::{ ast::{self, NameOwner}, lex_single_valid_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, SyntaxToken, }; -use ra_text_edit::TextEdit; use std::convert::TryInto; use test_utils::mark; +use text_edit::TextEdit; use crate::{ references::find_all_refs, FilePosition, FileSystemEdit, RangeInfo, Reference, ReferenceKind, @@ -271,9 +271,9 @@ fn rename_reference( #[cfg(test)] mod tests { use expect::{expect, Expect}; - use ra_text_edit::TextEditBuilder; use stdx::trim_indent; use test_utils::{assert_eq_text, mark}; + use text_edit::TextEdit; use crate::{mock_analysis::analysis_and_position, FileId}; diff --git a/crates/ra_ide/src/typing.rs b/crates/ra_ide/src/typing.rs index d3ce744b4..952429cde 100644 --- a/crates/ra_ide/src/typing.rs +++ b/crates/ra_ide/src/typing.rs @@ -26,7 +26,7 @@ use ra_syntax::{ TextRange, TextSize, }; -use ra_text_edit::TextEdit; +use text_edit::TextEdit; use crate::SourceChange; diff --git a/crates/ra_ide/src/typing/on_enter.rs b/crates/ra_ide/src/typing/on_enter.rs index 143b1ae41..c0c5ce3bc 100644 --- a/crates/ra_ide/src/typing/on_enter.rs +++ b/crates/ra_ide/src/typing/on_enter.rs @@ -9,8 +9,8 @@ use ra_syntax::{ SyntaxKind::*, SyntaxToken, TextRange, TextSize, TokenAtOffset, }; -use ra_text_edit::TextEdit; use test_utils::mark; +use text_edit::TextEdit; // Feature: On Enter // diff --git a/crates/ra_ide_db/Cargo.toml b/crates/ra_ide_db/Cargo.toml index 92b8ef82a..5446a5961 100644 --- a/crates/ra_ide_db/Cargo.toml +++ b/crates/ra_ide_db/Cargo.toml @@ -22,7 +22,7 @@ either = "1.5.3" stdx = { path = "../stdx" } ra_syntax = { path = "../ra_syntax" } -ra_text_edit = { path = "../ra_text_edit" } +text_edit = { path = "../text_edit" } ra_db = { path = "../ra_db" } profile = { path = "../profile" } test_utils = { path = "../test_utils" } diff --git a/crates/ra_ide_db/src/source_change.rs b/crates/ra_ide_db/src/source_change.rs index abb83f421..ae21132dd 100644 --- a/crates/ra_ide_db/src/source_change.rs +++ b/crates/ra_ide_db/src/source_change.rs @@ -4,7 +4,7 @@ //! It can be viewed as a dual for `AnalysisChange`. use ra_db::FileId; -use ra_text_edit::TextEdit; +use text_edit::TextEdit; #[derive(Default, Debug, Clone)] pub struct SourceChange { diff --git a/crates/ra_ssr/Cargo.toml b/crates/ra_ssr/Cargo.toml index 84e4b171e..d0f2ae733 100644 --- a/crates/ra_ssr/Cargo.toml +++ b/crates/ra_ssr/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/rust-analyzer/rust-analyzer" doctest = false [dependencies] -ra_text_edit = { path = "../ra_text_edit" } +text_edit = { path = "../text_edit" } ra_syntax = { path = "../ra_syntax" } ra_db = { path = "../ra_db" } ra_ide_db = { path = "../ra_ide_db" } diff --git a/crates/ra_ssr/src/replacing.rs b/crates/ra_ssr/src/replacing.rs index 36ced3842..74f9e7db6 100644 --- a/crates/ra_ssr/src/replacing.rs +++ b/crates/ra_ssr/src/replacing.rs @@ -4,8 +4,8 @@ use crate::matching::Var; use crate::{resolving::ResolvedRule, Match, SsrMatches}; use ra_syntax::ast::{self, AstToken}; use ra_syntax::{SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, TextRange, TextSize}; -use ra_text_edit::TextEdit; use rustc_hash::{FxHashMap, FxHashSet}; +use text_edit::TextEdit; /// Returns a text edit that will replace each match in `matches` with its corresponding replacement /// template. Placeholders in the template will have been substituted with whatever they matched to diff --git a/crates/ra_syntax/Cargo.toml b/crates/ra_syntax/Cargo.toml index fc4d7aa04..f2789e6a3 100644 --- a/crates/ra_syntax/Cargo.toml +++ b/crates/ra_syntax/Cargo.toml @@ -20,7 +20,7 @@ once_cell = "1.3.1" stdx = { path = "../stdx" } -ra_text_edit = { path = "../ra_text_edit" } +text_edit = { path = "../text_edit" } ra_parser = { path = "../ra_parser" } # This crate transitively depends on `smol_str` via `rowan`. diff --git a/crates/ra_syntax/fuzz/Cargo.toml b/crates/ra_syntax/fuzz/Cargo.toml index 613ad2857..4cec3c4cd 100644 --- a/crates/ra_syntax/fuzz/Cargo.toml +++ b/crates/ra_syntax/fuzz/Cargo.toml @@ -11,7 +11,7 @@ cargo-fuzz = true [dependencies] ra_syntax = { path = ".." } -ra_text_edit = { path = "../../ra_text_edit" } +text_edit = { path = "../../text_edit" } libfuzzer-sys = { git = "https://github.com/rust-fuzz/libfuzzer-sys.git" } # Prevent this from interfering with workspaces diff --git a/crates/ra_syntax/src/algo.rs b/crates/ra_syntax/src/algo.rs index 26b3c813a..6254b38ba 100644 --- a/crates/ra_syntax/src/algo.rs +++ b/crates/ra_syntax/src/algo.rs @@ -6,8 +6,8 @@ use std::{ }; use itertools::Itertools; -use ra_text_edit::TextEditBuilder; use rustc_hash::FxHashMap; +use text_edit::TextEditBuilder; use crate::{ AstNode, Direction, NodeOrToken, SyntaxElement, SyntaxKind, SyntaxNode, SyntaxNodePtr, diff --git a/crates/ra_syntax/src/fuzz.rs b/crates/ra_syntax/src/fuzz.rs index 39f9b12ab..fbb97aa27 100644 --- a/crates/ra_syntax/src/fuzz.rs +++ b/crates/ra_syntax/src/fuzz.rs @@ -5,7 +5,7 @@ use std::{ str::{self, FromStr}, }; -use ra_text_edit::Indel; +use text_edit::Indel; use crate::{validation, AstNode, SourceFile, TextRange}; diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs index 8a4d45386..465607f55 100644 --- a/crates/ra_syntax/src/lib.rs +++ b/crates/ra_syntax/src/lib.rs @@ -39,8 +39,8 @@ pub mod fuzz; use std::{marker::PhantomData, sync::Arc}; -use ra_text_edit::Indel; use stdx::format_to; +use text_edit::Indel; pub use crate::{ algo::InsertPosition, diff --git a/crates/ra_syntax/src/parsing/reparsing.rs b/crates/ra_syntax/src/parsing/reparsing.rs index ed5a42ea3..6644ffca4 100644 --- a/crates/ra_syntax/src/parsing/reparsing.rs +++ b/crates/ra_syntax/src/parsing/reparsing.rs @@ -7,7 +7,7 @@ //! and try to parse only this block. use ra_parser::Reparser; -use ra_text_edit::Indel; +use text_edit::Indel; use crate::{ algo, diff --git a/crates/ra_text_edit/Cargo.toml b/crates/ra_text_edit/Cargo.toml deleted file mode 100644 index 427862a5c..000000000 --- a/crates/ra_text_edit/Cargo.toml +++ /dev/null @@ -1,12 +0,0 @@ -[package] -name = "ra_text_edit" -version = "0.0.0" -license = "MIT OR Apache-2.0" -authors = ["rust-analyzer developers"] -edition = "2018" - -[lib] -doctest = false - -[dependencies] -text-size = "1.0.0" diff --git a/crates/ra_text_edit/src/lib.rs b/crates/ra_text_edit/src/lib.rs deleted file mode 100644 index ab8cd7fd1..000000000 --- a/crates/ra_text_edit/src/lib.rs +++ /dev/null @@ -1,186 +0,0 @@ -//! Representation of a `TextEdit`. -//! -//! `rust-analyzer` never mutates text itself and only sends diffs to clients, -//! so `TextEdit` is the ultimate representation of the work done by -//! rust-analyzer. -pub use text_size::{TextRange, TextSize}; - -/// `InsertDelete` -- a single "atomic" change to text -/// -/// Must not overlap with other `InDel`s -#[derive(Debug, Clone)] -pub struct Indel { - pub insert: String, - /// Refers to offsets in the original text - pub delete: TextRange, -} - -#[derive(Default, Debug, Clone)] -pub struct TextEdit { - indels: Vec, -} - -#[derive(Debug, Default, Clone)] -pub struct TextEditBuilder { - indels: Vec, -} - -impl Indel { - pub fn insert(offset: TextSize, text: String) -> Indel { - Indel::replace(TextRange::empty(offset), text) - } - pub fn delete(range: TextRange) -> Indel { - Indel::replace(range, String::new()) - } - pub fn replace(range: TextRange, replace_with: String) -> Indel { - Indel { delete: range, insert: replace_with } - } - - pub fn apply(&self, text: &mut String) { - let start: usize = self.delete.start().into(); - let end: usize = self.delete.end().into(); - text.replace_range(start..end, &self.insert); - } -} - -impl TextEdit { - pub fn builder() -> TextEditBuilder { - TextEditBuilder::default() - } - - pub fn insert(offset: TextSize, text: String) -> TextEdit { - let mut builder = TextEdit::builder(); - builder.insert(offset, text); - builder.finish() - } - - pub fn delete(range: TextRange) -> TextEdit { - let mut builder = TextEdit::builder(); - builder.delete(range); - builder.finish() - } - - pub fn replace(range: TextRange, replace_with: String) -> TextEdit { - let mut builder = TextEdit::builder(); - builder.replace(range, replace_with); - builder.finish() - } - - pub fn len(&self) -> usize { - self.indels.len() - } - - pub fn is_empty(&self) -> bool { - self.indels.is_empty() - } - - pub fn iter(&self) -> std::slice::Iter<'_, Indel> { - self.into_iter() - } - - pub fn apply(&self, text: &mut String) { - match self.len() { - 0 => return, - 1 => { - self.indels[0].apply(text); - return; - } - _ => (), - } - - let mut total_len = TextSize::of(&*text); - for indel in self.indels.iter() { - total_len += TextSize::of(&indel.insert); - total_len -= indel.delete.end() - indel.delete.start(); - } - let mut buf = String::with_capacity(total_len.into()); - let mut prev = 0; - for indel in self.indels.iter() { - let start: usize = indel.delete.start().into(); - let end: usize = indel.delete.end().into(); - if start > prev { - buf.push_str(&text[prev..start]); - } - buf.push_str(&indel.insert); - prev = end; - } - buf.push_str(&text[prev..text.len()]); - assert_eq!(TextSize::of(&buf), total_len); - - // FIXME: figure out a way to mutate the text in-place or reuse the - // memory in some other way - *text = buf - } - - pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> { - // FIXME: can be done without allocating intermediate vector - let mut all = self.iter().chain(other.iter()).collect::>(); - if !check_disjoint(&mut all) { - return Err(other); - } - self.indels.extend(other.indels); - assert!(check_disjoint(&mut self.indels)); - Ok(()) - } - - pub fn apply_to_offset(&self, offset: TextSize) -> Option { - let mut res = offset; - for indel in self.indels.iter() { - if indel.delete.start() >= offset { - break; - } - if offset < indel.delete.end() { - return None; - } - res += TextSize::of(&indel.insert); - res -= indel.delete.len(); - } - Some(res) - } -} - -impl IntoIterator for TextEdit { - type Item = Indel; - type IntoIter = std::vec::IntoIter; - - fn into_iter(self) -> Self::IntoIter { - self.indels.into_iter() - } -} - -impl<'a> IntoIterator for &'a TextEdit { - type Item = &'a Indel; - type IntoIter = std::slice::Iter<'a, Indel>; - - fn into_iter(self) -> Self::IntoIter { - self.indels.iter() - } -} - -impl TextEditBuilder { - pub fn replace(&mut self, range: TextRange, replace_with: String) { - self.indels.push(Indel::replace(range, replace_with)) - } - pub fn delete(&mut self, range: TextRange) { - self.indels.push(Indel::delete(range)) - } - pub fn insert(&mut self, offset: TextSize, text: String) { - self.indels.push(Indel::insert(offset, text)) - } - pub fn finish(self) -> TextEdit { - let mut indels = self.indels; - assert!(check_disjoint(&mut indels)); - TextEdit { indels } - } - pub fn invalidates_offset(&self, offset: TextSize) -> bool { - self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset)) - } -} - -fn check_disjoint(indels: &mut [impl std::borrow::Borrow]) -> bool { - indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end())); - indels - .iter() - .zip(indels.iter().skip(1)) - .all(|(l, r)| l.borrow().delete.end() <= r.borrow().delete.start()) -} diff --git a/crates/rust-analyzer/Cargo.toml b/crates/rust-analyzer/Cargo.toml index 3f70510fd..ef244da59 100644 --- a/crates/rust-analyzer/Cargo.toml +++ b/crates/rust-analyzer/Cargo.toml @@ -39,7 +39,7 @@ ra_ide = { path = "../ra_ide" } profile = { path = "../profile" } ra_project_model = { path = "../ra_project_model" } ra_syntax = { path = "../ra_syntax" } -ra_text_edit = { path = "../ra_text_edit" } +text_edit = { path = "../text_edit" } vfs = { path = "../vfs" } vfs-notify = { path = "../vfs-notify" } ra_cfg = { path = "../ra_cfg" } diff --git a/crates/text_edit/Cargo.toml b/crates/text_edit/Cargo.toml new file mode 100644 index 000000000..a69b1ef2b --- /dev/null +++ b/crates/text_edit/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "text_edit" +version = "0.0.0" +license = "MIT OR Apache-2.0" +authors = ["rust-analyzer developers"] +edition = "2018" + +[lib] +doctest = false + +[dependencies] +text-size = "1.0.0" diff --git a/crates/text_edit/src/lib.rs b/crates/text_edit/src/lib.rs new file mode 100644 index 000000000..ab8cd7fd1 --- /dev/null +++ b/crates/text_edit/src/lib.rs @@ -0,0 +1,186 @@ +//! Representation of a `TextEdit`. +//! +//! `rust-analyzer` never mutates text itself and only sends diffs to clients, +//! so `TextEdit` is the ultimate representation of the work done by +//! rust-analyzer. +pub use text_size::{TextRange, TextSize}; + +/// `InsertDelete` -- a single "atomic" change to text +/// +/// Must not overlap with other `InDel`s +#[derive(Debug, Clone)] +pub struct Indel { + pub insert: String, + /// Refers to offsets in the original text + pub delete: TextRange, +} + +#[derive(Default, Debug, Clone)] +pub struct TextEdit { + indels: Vec, +} + +#[derive(Debug, Default, Clone)] +pub struct TextEditBuilder { + indels: Vec, +} + +impl Indel { + pub fn insert(offset: TextSize, text: String) -> Indel { + Indel::replace(TextRange::empty(offset), text) + } + pub fn delete(range: TextRange) -> Indel { + Indel::replace(range, String::new()) + } + pub fn replace(range: TextRange, replace_with: String) -> Indel { + Indel { delete: range, insert: replace_with } + } + + pub fn apply(&self, text: &mut String) { + let start: usize = self.delete.start().into(); + let end: usize = self.delete.end().into(); + text.replace_range(start..end, &self.insert); + } +} + +impl TextEdit { + pub fn builder() -> TextEditBuilder { + TextEditBuilder::default() + } + + pub fn insert(offset: TextSize, text: String) -> TextEdit { + let mut builder = TextEdit::builder(); + builder.insert(offset, text); + builder.finish() + } + + pub fn delete(range: TextRange) -> TextEdit { + let mut builder = TextEdit::builder(); + builder.delete(range); + builder.finish() + } + + pub fn replace(range: TextRange, replace_with: String) -> TextEdit { + let mut builder = TextEdit::builder(); + builder.replace(range, replace_with); + builder.finish() + } + + pub fn len(&self) -> usize { + self.indels.len() + } + + pub fn is_empty(&self) -> bool { + self.indels.is_empty() + } + + pub fn iter(&self) -> std::slice::Iter<'_, Indel> { + self.into_iter() + } + + pub fn apply(&self, text: &mut String) { + match self.len() { + 0 => return, + 1 => { + self.indels[0].apply(text); + return; + } + _ => (), + } + + let mut total_len = TextSize::of(&*text); + for indel in self.indels.iter() { + total_len += TextSize::of(&indel.insert); + total_len -= indel.delete.end() - indel.delete.start(); + } + let mut buf = String::with_capacity(total_len.into()); + let mut prev = 0; + for indel in self.indels.iter() { + let start: usize = indel.delete.start().into(); + let end: usize = indel.delete.end().into(); + if start > prev { + buf.push_str(&text[prev..start]); + } + buf.push_str(&indel.insert); + prev = end; + } + buf.push_str(&text[prev..text.len()]); + assert_eq!(TextSize::of(&buf), total_len); + + // FIXME: figure out a way to mutate the text in-place or reuse the + // memory in some other way + *text = buf + } + + pub fn union(&mut self, other: TextEdit) -> Result<(), TextEdit> { + // FIXME: can be done without allocating intermediate vector + let mut all = self.iter().chain(other.iter()).collect::>(); + if !check_disjoint(&mut all) { + return Err(other); + } + self.indels.extend(other.indels); + assert!(check_disjoint(&mut self.indels)); + Ok(()) + } + + pub fn apply_to_offset(&self, offset: TextSize) -> Option { + let mut res = offset; + for indel in self.indels.iter() { + if indel.delete.start() >= offset { + break; + } + if offset < indel.delete.end() { + return None; + } + res += TextSize::of(&indel.insert); + res -= indel.delete.len(); + } + Some(res) + } +} + +impl IntoIterator for TextEdit { + type Item = Indel; + type IntoIter = std::vec::IntoIter; + + fn into_iter(self) -> Self::IntoIter { + self.indels.into_iter() + } +} + +impl<'a> IntoIterator for &'a TextEdit { + type Item = &'a Indel; + type IntoIter = std::slice::Iter<'a, Indel>; + + fn into_iter(self) -> Self::IntoIter { + self.indels.iter() + } +} + +impl TextEditBuilder { + pub fn replace(&mut self, range: TextRange, replace_with: String) { + self.indels.push(Indel::replace(range, replace_with)) + } + pub fn delete(&mut self, range: TextRange) { + self.indels.push(Indel::delete(range)) + } + pub fn insert(&mut self, offset: TextSize, text: String) { + self.indels.push(Indel::insert(offset, text)) + } + pub fn finish(self) -> TextEdit { + let mut indels = self.indels; + assert!(check_disjoint(&mut indels)); + TextEdit { indels } + } + pub fn invalidates_offset(&self, offset: TextSize) -> bool { + self.indels.iter().any(|indel| indel.delete.contains_inclusive(offset)) + } +} + +fn check_disjoint(indels: &mut [impl std::borrow::Borrow]) -> bool { + indels.sort_by_key(|indel| (indel.borrow().delete.start(), indel.borrow().delete.end())); + indels + .iter() + .zip(indels.iter().skip(1)) + .all(|(l, r)| l.borrow().delete.end() <= r.borrow().delete.start()) +} -- cgit v1.2.3