Skip to content

fix: splitter crash #464

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 18, 2025
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
14 changes: 6 additions & 8 deletions crates/pgt_lsp/src/handlers/text_document.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use crate::{
diagnostics::LspError, documents::Document, session::Session, utils::apply_document_changes,
};
use crate::{documents::Document, session::Session, utils::apply_document_changes};
use anyhow::Result;
use pgt_workspace::workspace::{
ChangeFileParams, CloseFileParams, GetFileContentParams, OpenFileParams,
};
use tower_lsp::lsp_types;
use tracing::error;
use tracing::{error, field};

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

// Handler for `textDocument/didChange` LSP notification
#[tracing::instrument(level = "debug", skip(session), err)]
/// Handler for `textDocument/didChange` LSP notification
#[tracing::instrument(level = "debug", skip_all, fields(url = field::display(&params.text_document.uri), version = params.text_document.version), err)]
pub(crate) async fn did_change(
session: &Session,
params: lsp_types::DidChangeTextDocumentParams,
) -> Result<(), LspError> {
) -> Result<()> {
let url = params.text_document.uri;
let version = params.text_document.version;

Expand All @@ -56,7 +54,7 @@ pub(crate) async fn did_change(
let text = apply_document_changes(
session.position_encoding(),
old_text,
&params.content_changes,
params.content_changes,
);

tracing::trace!("new document: {:?}", text);
Expand Down
14 changes: 7 additions & 7 deletions crates/pgt_lsp/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::adapters::from_lsp::text_range;
use crate::adapters::line_index::LineIndex;
use crate::adapters::{PositionEncoding, from_lsp, to_lsp};
use crate::adapters::{PositionEncoding, to_lsp};
use anyhow::{Context, Result, ensure};
use pgt_console::MarkupBuf;
use pgt_console::fmt::Termcolor;
Expand All @@ -10,8 +11,8 @@ use pgt_text_size::{TextRange, TextSize};
use std::any::Any;
use std::borrow::Cow;
use std::fmt::{Debug, Display};
use std::io;
use std::ops::{Add, Range};
use std::{io, mem};
use tower_lsp::jsonrpc::Error as LspError;
use tower_lsp::lsp_types;
use tower_lsp::lsp_types::{self as lsp, CodeDescription, Url};
Expand Down Expand Up @@ -183,7 +184,7 @@ pub(crate) fn panic_to_lsp_error(err: Box<dyn Any + Send>) -> LspError {
pub(crate) fn apply_document_changes(
position_encoding: PositionEncoding,
current_content: String,
content_changes: &[lsp_types::TextDocumentContentChangeEvent],
mut content_changes: Vec<lsp_types::TextDocumentContentChangeEvent>,
) -> String {
// Skip to the last full document change, as it invalidates all previous changes anyways.
let mut start = content_changes
Expand All @@ -192,12 +193,12 @@ pub(crate) fn apply_document_changes(
.position(|change| change.range.is_none())
.map_or(0, |idx| content_changes.len() - idx - 1);

let mut text: String = match content_changes.get(start) {
let mut text: String = match content_changes.get_mut(start) {
// peek at the first content change as an optimization
Some(lsp_types::TextDocumentContentChangeEvent {
range: None, text, ..
}) => {
let text = text.clone();
let text = mem::take(text);
start += 1;

// The only change is a full document update
Expand Down Expand Up @@ -225,12 +226,11 @@ pub(crate) fn apply_document_changes(
line_index = LineIndex::new(&text);
}
index_valid = range.start.line;
if let Ok(range) = from_lsp::text_range(&line_index, range, position_encoding) {
if let Ok(range) = text_range(&line_index, range, position_encoding) {
text.replace_range(Range::<usize>::from(range), &change.text);
}
}
}

text
}

Expand Down
Loading