1
+ use crate :: adapters:: from_lsp:: text_range;
1
2
use crate :: adapters:: line_index:: LineIndex ;
2
- use crate :: adapters:: { PositionEncoding , from_lsp , to_lsp} ;
3
+ use crate :: adapters:: { PositionEncoding , to_lsp} ;
3
4
use anyhow:: { Context , Result , ensure} ;
4
5
use pgt_console:: MarkupBuf ;
5
6
use pgt_console:: fmt:: Termcolor ;
@@ -10,8 +11,8 @@ use pgt_text_size::{TextRange, TextSize};
10
11
use std:: any:: Any ;
11
12
use std:: borrow:: Cow ;
12
13
use std:: fmt:: { Debug , Display } ;
13
- use std:: io;
14
14
use std:: ops:: { Add , Range } ;
15
+ use std:: { io, mem} ;
15
16
use tower_lsp:: jsonrpc:: Error as LspError ;
16
17
use tower_lsp:: lsp_types;
17
18
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 {
183
184
pub ( crate ) fn apply_document_changes (
184
185
position_encoding : PositionEncoding ,
185
186
current_content : String ,
186
- content_changes : & [ lsp_types:: TextDocumentContentChangeEvent ] ,
187
+ mut content_changes : Vec < lsp_types:: TextDocumentContentChangeEvent > ,
187
188
) -> String {
188
189
// Skip to the last full document change, as it invalidates all previous changes anyways.
189
190
let mut start = content_changes
@@ -192,12 +193,12 @@ pub(crate) fn apply_document_changes(
192
193
. position ( |change| change. range . is_none ( ) )
193
194
. map_or ( 0 , |idx| content_changes. len ( ) - idx - 1 ) ;
194
195
195
- let mut text: String = match content_changes. get ( start) {
196
+ let mut text: String = match content_changes. get_mut ( start) {
196
197
// peek at the first content change as an optimization
197
198
Some ( lsp_types:: TextDocumentContentChangeEvent {
198
199
range : None , text, ..
199
200
} ) => {
200
- let text = text . clone ( ) ;
201
+ let text = mem :: take ( text ) ;
201
202
start += 1 ;
202
203
203
204
// The only change is a full document update
@@ -225,12 +226,11 @@ pub(crate) fn apply_document_changes(
225
226
line_index = LineIndex :: new ( & text) ;
226
227
}
227
228
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) {
229
230
text. replace_range ( Range :: < usize > :: from ( range) , & change. text ) ;
230
231
}
231
232
}
232
233
}
233
-
234
234
text
235
235
}
236
236
0 commit comments