Skip to content
Merged
Changes from 1 commit
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
16 changes: 9 additions & 7 deletions crates/pg_lsp/src/handlers/text_document.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::{documents::Document, session::Session, utils::apply_document_changes};
use crate::{
diagnostics::LspError, documents::Document, session::Session, utils::apply_document_changes,
};
use anyhow::Result;
use pg_lsp_converters::from_proto::text_range;
use pg_workspace::workspace::{
Expand Down Expand Up @@ -47,13 +49,15 @@ pub(crate) async fn did_open(
pub(crate) async fn did_change(
session: &Session,
params: lsp_types::DidChangeTextDocumentParams,
) -> Result<()> {
) -> Result<(), LspError> {
tracing::info!("did_change: {:#?}", params);

let url = params.text_document.uri;
let version = params.text_document.version;

let pglsp_path = session.file_path(&url)?;

// we need to keep the documents here too for the line index
let old_doc = session.document(&url)?;
let old_text = session.workspace.get_file_content(GetFileContentParams {
path: pglsp_path.clone(),
})?;
Expand All @@ -71,23 +75,21 @@ pub(crate) async fn did_change(
&params.content_changes[start..],
);

let new_doc = Document::new(version, &text);

session.workspace.change_file(ChangeFileParams {
path: pglsp_path,
version,
changes: params.content_changes[start..]
.iter()
.map(|c| ChangeParams {
range: c.range.and_then(|r| {
text_range(&new_doc.line_index, r, session.position_encoding()).ok()
text_range(&old_doc.line_index, r, session.position_encoding()).ok()
}),
text: c.text.clone(),
})
.collect(),
})?;

session.insert_document(url.clone(), new_doc);
session.insert_document(url.clone(), Document::new(version, &text));

if let Err(err) = session.update_diagnostics(url).await {
error!("Failed to update diagnostics: {}", err);
Expand Down
Loading