Skip to content

Commit c0567ec

Browse files
authored
fix: splitter crash (#464)
fixes a crash in the splitter when the last char is a closing parentheses.
1 parent 36a3c52 commit c0567ec

File tree

5 files changed

+511
-16
lines changed

5 files changed

+511
-16
lines changed

crates/pgt_lsp/src/handlers/text_document.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
use crate::{
2-
diagnostics::LspError, documents::Document, session::Session, utils::apply_document_changes,
3-
};
1+
use crate::{documents::Document, session::Session, utils::apply_document_changes};
42
use anyhow::Result;
53
use pgt_workspace::workspace::{
64
ChangeFileParams, CloseFileParams, GetFileContentParams, OpenFileParams,
75
};
86
use tower_lsp::lsp_types;
9-
use tracing::error;
7+
use tracing::{error, field};
108

119
/// Handler for `textDocument/didOpen` LSP notification
1210
#[tracing::instrument(level = "debug", skip(session), err)]
@@ -36,12 +34,12 @@ pub(crate) async fn did_open(
3634
Ok(())
3735
}
3836

39-
// Handler for `textDocument/didChange` LSP notification
40-
#[tracing::instrument(level = "debug", skip(session), err)]
37+
/// Handler for `textDocument/didChange` LSP notification
38+
#[tracing::instrument(level = "debug", skip_all, fields(url = field::display(&params.text_document.uri), version = params.text_document.version), err)]
4139
pub(crate) async fn did_change(
4240
session: &Session,
4341
params: lsp_types::DidChangeTextDocumentParams,
44-
) -> Result<(), LspError> {
42+
) -> Result<()> {
4543
let url = params.text_document.uri;
4644
let version = params.text_document.version;
4745

@@ -56,7 +54,7 @@ pub(crate) async fn did_change(
5654
let text = apply_document_changes(
5755
session.position_encoding(),
5856
old_text,
59-
&params.content_changes,
57+
params.content_changes,
6058
);
6159

6260
tracing::trace!("new document: {:?}", text);

crates/pgt_lsp/src/utils.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
use crate::adapters::from_lsp::text_range;
12
use crate::adapters::line_index::LineIndex;
2-
use crate::adapters::{PositionEncoding, from_lsp, to_lsp};
3+
use crate::adapters::{PositionEncoding, to_lsp};
34
use anyhow::{Context, Result, ensure};
45
use pgt_console::MarkupBuf;
56
use pgt_console::fmt::Termcolor;
@@ -10,8 +11,8 @@ use pgt_text_size::{TextRange, TextSize};
1011
use std::any::Any;
1112
use std::borrow::Cow;
1213
use std::fmt::{Debug, Display};
13-
use std::io;
1414
use std::ops::{Add, Range};
15+
use std::{io, mem};
1516
use tower_lsp::jsonrpc::Error as LspError;
1617
use tower_lsp::lsp_types;
1718
use tower_lsp::lsp_types::{self as lsp, CodeDescription, Url};
@@ -183,7 +184,7 @@ pub(crate) fn panic_to_lsp_error(err: Box<dyn Any + Send>) -> LspError {
183184
pub(crate) fn apply_document_changes(
184185
position_encoding: PositionEncoding,
185186
current_content: String,
186-
content_changes: &[lsp_types::TextDocumentContentChangeEvent],
187+
mut content_changes: Vec<lsp_types::TextDocumentContentChangeEvent>,
187188
) -> String {
188189
// Skip to the last full document change, as it invalidates all previous changes anyways.
189190
let mut start = content_changes
@@ -192,12 +193,12 @@ pub(crate) fn apply_document_changes(
192193
.position(|change| change.range.is_none())
193194
.map_or(0, |idx| content_changes.len() - idx - 1);
194195

195-
let mut text: String = match content_changes.get(start) {
196+
let mut text: String = match content_changes.get_mut(start) {
196197
// peek at the first content change as an optimization
197198
Some(lsp_types::TextDocumentContentChangeEvent {
198199
range: None, text, ..
199200
}) => {
200-
let text = text.clone();
201+
let text = mem::take(text);
201202
start += 1;
202203

203204
// The only change is a full document update
@@ -225,12 +226,11 @@ pub(crate) fn apply_document_changes(
225226
line_index = LineIndex::new(&text);
226227
}
227228
index_valid = range.start.line;
228-
if let Ok(range) = from_lsp::text_range(&line_index, range, position_encoding) {
229+
if let Ok(range) = text_range(&line_index, range, position_encoding) {
229230
text.replace_range(Range::<usize>::from(range), &change.text);
230231
}
231232
}
232233
}
233-
234234
text
235235
}
236236

0 commit comments

Comments
 (0)