From b61313e3b0528a28bd95246bf1b8f13d5d0d040b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrick=20F=C3=B6rster?= Date: Sat, 29 Apr 2023 09:23:23 +0200 Subject: [PATCH] Fix fwd search regression when formatting on save See #880. --- CHANGELOG.md | 1 + crates/texlab/src/server.rs | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 53154ac8..e478763a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fix spurious completion results when completing environments ([#883](https://github.com/latex-lsp/texlab/issues/883)) +- Fix regression when guessing cursor position after formatting ([#880](https://github.com/latex-lsp/texlab/issues/880)) ## [5.5.0] - 2023-04-16 diff --git a/crates/texlab/src/server.rs b/crates/texlab/src/server.rs index b7782a2e..6b118655 100644 --- a/crates/texlab/src/server.rs +++ b/crates/texlab/src/server.rs @@ -14,6 +14,7 @@ use base_db::{Config, Owner, Workspace}; use commands::{BuildCommand, CleanCommand, CleanTarget, ForwardSearch}; use crossbeam_channel::{Receiver, Sender}; use distro::{Distro, Language}; +use itertools::{FoldWhile, Itertools}; use lsp_server::{Connection, ErrorCode, Message, RequestId}; use lsp_types::{notification::*, request::*, *}; use parking_lot::{Mutex, RwLock}; @@ -387,13 +388,27 @@ impl Server { } None => { let language = document.language; + let line_col = document.line_index.line_col(document.cursor); + + let (_, new_cursor) = change + .text + .lines() + .fold_while((0, 0), |(number, index), line| { + if number == line_col.line { + FoldWhile::Done((number, index)) + } else { + itertools::FoldWhile::Continue((number + 1, index + line.len())) + } + }) + .into_inner(); + drop(document); workspace.open( uri.clone(), change.text, language, Owner::Client, - TextSize::default(), + TextSize::from(new_cursor as u32), ); } };