From 1fcf687657c7029e7e36dc2cc17bdec7a391d63f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 13 Feb 2021 01:28:48 +0300 Subject: Fix bitrotted module name --- crates/rust-analyzer/src/config.rs | 2 +- crates/rust-analyzer/src/from_proto.rs | 2 +- crates/rust-analyzer/src/global_state.rs | 2 +- crates/rust-analyzer/src/handlers.rs | 3 +- crates/rust-analyzer/src/lib.rs | 2 +- crates/rust-analyzer/src/line_endings.rs | 65 ------------------------------ crates/rust-analyzer/src/line_index.rs | 68 ++++++++++++++++++++++++++++++++ crates/rust-analyzer/src/lsp_utils.rs | 2 +- crates/rust-analyzer/src/to_proto.rs | 2 +- 9 files changed, 75 insertions(+), 73 deletions(-) delete mode 100644 crates/rust-analyzer/src/line_endings.rs create mode 100644 crates/rust-analyzer/src/line_index.rs (limited to 'crates') diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 425ef145c..9c873c097 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -25,7 +25,7 @@ use vfs::AbsPathBuf; use crate::{ caps::completion_item_edit_resolve, diagnostics::DiagnosticsMapConfig, - line_endings::OffsetEncoding, lsp_ext::supports_utf8, + line_index::OffsetEncoding, lsp_ext::supports_utf8, }; config_data! { diff --git a/crates/rust-analyzer/src/from_proto.rs b/crates/rust-analyzer/src/from_proto.rs index 4f3ae8cc3..5b02b2598 100644 --- a/crates/rust-analyzer/src/from_proto.rs +++ b/crates/rust-analyzer/src/from_proto.rs @@ -9,7 +9,7 @@ use vfs::AbsPathBuf; use crate::{ from_json, global_state::GlobalStateSnapshot, - line_endings::{LineIndex, OffsetEncoding}, + line_index::{LineIndex, OffsetEncoding}, lsp_ext, Result, }; diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 85c87645c..52c249713 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -22,7 +22,7 @@ use crate::{ diagnostics::{CheckFixes, DiagnosticCollection}, document::DocumentData, from_proto, - line_endings::{LineEndings, LineIndex}, + line_index::{LineEndings, LineIndex}, main_loop::Task, op_queue::OpQueue, reload::SourceRootConfig, diff --git a/crates/rust-analyzer/src/handlers.rs b/crates/rust-analyzer/src/handlers.rs index d8b00e9c5..4f6f250d6 100644 --- a/crates/rust-analyzer/src/handlers.rs +++ b/crates/rust-analyzer/src/handlers.rs @@ -36,8 +36,7 @@ use crate::{ diff::diff, from_proto, global_state::{GlobalState, GlobalStateSnapshot}, - line_endings::LineEndings, - line_endings::LineIndex, + line_index::{LineEndings, LineIndex}, lsp_ext::{self, InlayHint, InlayHintsParams}, lsp_utils::all_edits_are_disjoint, to_proto, LspError, Result, diff --git a/crates/rust-analyzer/src/lib.rs b/crates/rust-analyzer/src/lib.rs index 2207b9a87..8b874239c 100644 --- a/crates/rust-analyzer/src/lib.rs +++ b/crates/rust-analyzer/src/lib.rs @@ -29,7 +29,7 @@ mod from_proto; mod semantic_tokens; mod markdown; mod diagnostics; -mod line_endings; +mod line_index; mod request_metrics; mod lsp_utils; mod thread_pool; diff --git a/crates/rust-analyzer/src/line_endings.rs b/crates/rust-analyzer/src/line_endings.rs deleted file mode 100644 index dd8901152..000000000 --- a/crates/rust-analyzer/src/line_endings.rs +++ /dev/null @@ -1,65 +0,0 @@ -//! We maintain invariant that all internal strings use `\n` as line separator. -//! This module does line ending conversion and detection (so that we can -//! convert back to `\r\n` on the way out). - -use std::sync::Arc; - -pub enum OffsetEncoding { - Utf8, - Utf16, -} - -pub(crate) struct LineIndex { - pub(crate) index: Arc, - pub(crate) endings: LineEndings, - pub(crate) encoding: OffsetEncoding, -} - -#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub(crate) enum LineEndings { - Unix, - Dos, -} - -impl LineEndings { - /// Replaces `\r\n` with `\n` in-place in `src`. - pub(crate) fn normalize(src: String) -> (String, LineEndings) { - if !src.as_bytes().contains(&b'\r') { - return (src, LineEndings::Unix); - } - - // We replace `\r\n` with `\n` in-place, which doesn't break utf-8 encoding. - // While we *can* call `as_mut_vec` and do surgery on the live string - // directly, let's rather steal the contents of `src`. This makes the code - // safe even if a panic occurs. - - let mut buf = src.into_bytes(); - let mut gap_len = 0; - let mut tail = buf.as_mut_slice(); - loop { - let idx = match find_crlf(&tail[gap_len..]) { - None => tail.len(), - Some(idx) => idx + gap_len, - }; - tail.copy_within(gap_len..idx, 0); - tail = &mut tail[idx - gap_len..]; - if tail.len() == gap_len { - break; - } - gap_len += 1; - } - - // Account for removed `\r`. - // After `set_len`, `buf` is guaranteed to contain utf-8 again. - let new_len = buf.len() - gap_len; - let src = unsafe { - buf.set_len(new_len); - String::from_utf8_unchecked(buf) - }; - return (src, LineEndings::Dos); - - fn find_crlf(src: &[u8]) -> Option { - src.windows(2).position(|it| it == b"\r\n") - } - } -} diff --git a/crates/rust-analyzer/src/line_index.rs b/crates/rust-analyzer/src/line_index.rs new file mode 100644 index 000000000..c116414da --- /dev/null +++ b/crates/rust-analyzer/src/line_index.rs @@ -0,0 +1,68 @@ +//! Enhances `ide::LineIndex` with additional info required to convert offsets +//! into lsp positions. +//! +//! We maintain invariant that all internal strings use `\n` as line separator. +//! This module does line ending conversion and detection (so that we can +//! convert back to `\r\n` on the way out). + +use std::sync::Arc; + +pub enum OffsetEncoding { + Utf8, + Utf16, +} + +pub(crate) struct LineIndex { + pub(crate) index: Arc, + pub(crate) endings: LineEndings, + pub(crate) encoding: OffsetEncoding, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub(crate) enum LineEndings { + Unix, + Dos, +} + +impl LineEndings { + /// Replaces `\r\n` with `\n` in-place in `src`. + pub(crate) fn normalize(src: String) -> (String, LineEndings) { + if !src.as_bytes().contains(&b'\r') { + return (src, LineEndings::Unix); + } + + // We replace `\r\n` with `\n` in-place, which doesn't break utf-8 encoding. + // While we *can* call `as_mut_vec` and do surgery on the live string + // directly, let's rather steal the contents of `src`. This makes the code + // safe even if a panic occurs. + + let mut buf = src.into_bytes(); + let mut gap_len = 0; + let mut tail = buf.as_mut_slice(); + loop { + let idx = match find_crlf(&tail[gap_len..]) { + None => tail.len(), + Some(idx) => idx + gap_len, + }; + tail.copy_within(gap_len..idx, 0); + tail = &mut tail[idx - gap_len..]; + if tail.len() == gap_len { + break; + } + gap_len += 1; + } + + // Account for removed `\r`. + // After `set_len`, `buf` is guaranteed to contain utf-8 again. + let new_len = buf.len() - gap_len; + let src = unsafe { + buf.set_len(new_len); + String::from_utf8_unchecked(buf) + }; + return (src, LineEndings::Dos); + + fn find_crlf(src: &[u8]) -> Option { + src.windows(2).position(|it| it == b"\r\n") + } + } +} diff --git a/crates/rust-analyzer/src/lsp_utils.rs b/crates/rust-analyzer/src/lsp_utils.rs index 6b8e347b9..84f78b5b8 100644 --- a/crates/rust-analyzer/src/lsp_utils.rs +++ b/crates/rust-analyzer/src/lsp_utils.rs @@ -7,7 +7,7 @@ use lsp_server::Notification; use crate::{ from_proto, global_state::GlobalState, - line_endings::{LineEndings, LineIndex, OffsetEncoding}, + line_index::{LineEndings, LineIndex, OffsetEncoding}, }; pub(crate) fn is_canceled(e: &(dyn Error + 'static)) -> bool { diff --git a/crates/rust-analyzer/src/to_proto.rs b/crates/rust-analyzer/src/to_proto.rs index 368d916e7..6aff65575 100644 --- a/crates/rust-analyzer/src/to_proto.rs +++ b/crates/rust-analyzer/src/to_proto.rs @@ -17,7 +17,7 @@ use serde_json::to_value; use crate::{ cargo_target_spec::CargoTargetSpec, global_state::GlobalStateSnapshot, - line_endings::{LineEndings, LineIndex, OffsetEncoding}, + line_index::{LineEndings, LineIndex, OffsetEncoding}, lsp_ext, semantic_tokens, Result, }; -- cgit v1.2.3