1- use crate :: { documents:: Document , session:: Session , utils:: apply_document_changes} ;
1+ use crate :: {
2+ diagnostics:: LspError , documents:: Document , session:: Session , utils:: apply_document_changes,
3+ } ;
24use anyhow:: Result ;
35use pg_lsp_converters:: from_proto:: text_range;
46use pg_workspace:: workspace:: {
@@ -47,13 +49,15 @@ pub(crate) async fn did_open(
4749pub ( crate ) async fn did_change (
4850 session : & Session ,
4951 params : lsp_types:: DidChangeTextDocumentParams ,
50- ) -> Result < ( ) > {
52+ ) -> Result < ( ) , LspError > {
53+ tracing:: info!( "did_change: {:#?}" , params) ;
54+
5155 let url = params. text_document . uri ;
5256 let version = params. text_document . version ;
5357
5458 let pglsp_path = session. file_path ( & url) ?;
5559
56- // we need to keep the documents here too for the line index
60+ let old_doc = session . document ( & url ) ? ;
5761 let old_text = session. workspace . get_file_content ( GetFileContentParams {
5862 path : pglsp_path. clone ( ) ,
5963 } ) ?;
@@ -71,23 +75,21 @@ pub(crate) async fn did_change(
7175 & params. content_changes [ start..] ,
7276 ) ;
7377
74- let new_doc = Document :: new ( version, & text) ;
75-
7678 session. workspace . change_file ( ChangeFileParams {
7779 path : pglsp_path,
7880 version,
7981 changes : params. content_changes [ start..]
8082 . iter ( )
8183 . map ( |c| ChangeParams {
8284 range : c. range . and_then ( |r| {
83- text_range ( & new_doc . line_index , r, session. position_encoding ( ) ) . ok ( )
85+ text_range ( & old_doc . line_index , r, session. position_encoding ( ) ) . ok ( )
8486 } ) ,
8587 text : c. text . clone ( ) ,
8688 } )
8789 . collect ( ) ,
8890 } ) ?;
8991
90- session. insert_document ( url. clone ( ) , new_doc ) ;
92+ session. insert_document ( url. clone ( ) , Document :: new ( version , & text ) ) ;
9193
9294 if let Err ( err) = session. update_diagnostics ( url) . await {
9395 error ! ( "Failed to update diagnostics: {}" , err) ;
0 commit comments