Skip to content
This repository has been archived by the owner on Aug 31, 2023. It is now read-only.

feat(rome_lsp): add a replace_range method to Document #4135

Merged
merged 2 commits into from
Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 123 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/rome_lsp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ futures = "0.3"
[dev-dependencies]
tower = { version = "0.4.12", features = ["timeout"] }
tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros"] }
proptest = "1.0.0"
15 changes: 12 additions & 3 deletions crates/rome_lsp/src/documents.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use anyhow::Result;
use rome_rowan::TextRange;

use crate::line_index::LineIndex;

/// Represents an open [`textDocument`]. Can be cheaply cloned.
Expand All @@ -6,16 +9,22 @@ use crate::line_index::LineIndex;
#[derive(Clone)]
pub(crate) struct Document {
pub(crate) version: i32,
pub(crate) content: String,
pub(crate) line_index: LineIndex,
}

impl Document {
pub(crate) fn new(version: i32, text: &str) -> Self {
pub(crate) fn new(version: i32, text: impl Into<String>) -> Self {
Self {
version,
content: text.into(),
line_index: LineIndex::new(text),
}
}

pub(crate) fn text(&self) -> &str {
self.line_index.text()
}

pub(crate) fn replace_range(&mut self, range: TextRange, replace_with: &str) -> Result<()> {
self.line_index.replace_range(range, replace_with)
}
}
16 changes: 7 additions & 9 deletions crates/rome_lsp/src/handlers/text_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,34 @@ pub(crate) async fn did_change(
let version = params.text_document.version;

let rome_path = session.file_path(&url)?;
let doc = session.document(&url)?;
let mut doc = session.document(&url)?;

let mut content = doc.content;
tracing::trace!("old document: {content:?}");
tracing::trace!("old document: {:?}", doc.text());

for change in params.content_changes {
match change.range {
Some(range) => {
let text_range = utils::text_range(&doc.line_index, range)?;
let range = Range::<usize>::from(text_range);
tracing::trace!("replace range {range:?} with {:?}", change.text);
content.replace_range(range, &change.text);
doc.replace_range(text_range, &change.text)?;
}
None => {
tracing::trace!("replace content {:?}", change.text);
content = change.text;
doc = Document::new(version, change.text);
}
}
}

tracing::trace!("new document: {content:?}");

let doc = Document::new(version, &content);
tracing::trace!("new document: {:?}", doc.text());

session.workspace.change_file(ChangeFileParams {
path: rome_path,
version,
content,
content: doc.text().into(),
})?;

doc.version = version;
session.insert_document(url.clone(), doc);

if let Err(err) = session.update_diagnostics(url).await {
Expand Down
Loading