@@ -6,7 +6,6 @@ use pgt_console::fmt::Termcolor;
6
6
use pgt_console:: fmt:: { self , Formatter } ;
7
7
use pgt_diagnostics:: termcolor:: NoColor ;
8
8
use pgt_diagnostics:: { Diagnostic , DiagnosticTags , Location , PrintDescription , Severity , Visit } ;
9
- use pgt_text_edit:: { CompressedOp , DiffOp , TextEdit } ;
10
9
use pgt_text_size:: { TextRange , TextSize } ;
11
10
use std:: any:: Any ;
12
11
use std:: borrow:: Cow ;
@@ -18,74 +17,6 @@ use tower_lsp::lsp_types;
18
17
use tower_lsp:: lsp_types:: { self as lsp, CodeDescription , Url } ;
19
18
use tracing:: error;
20
19
21
- pub ( crate ) fn text_edit (
22
- line_index : & LineIndex ,
23
- diff : TextEdit ,
24
- position_encoding : PositionEncoding ,
25
- offset : Option < u32 > ,
26
- ) -> Result < Vec < lsp:: TextEdit > > {
27
- let mut result: Vec < lsp:: TextEdit > = Vec :: new ( ) ;
28
- let mut offset = if let Some ( offset) = offset {
29
- TextSize :: from ( offset)
30
- } else {
31
- TextSize :: from ( 0 )
32
- } ;
33
-
34
- for op in diff. iter ( ) {
35
- match op {
36
- CompressedOp :: DiffOp ( DiffOp :: Equal { range } ) => {
37
- offset += range. len ( ) ;
38
- }
39
- CompressedOp :: DiffOp ( DiffOp :: Insert { range } ) => {
40
- let start = to_lsp:: position ( line_index, offset, position_encoding) ?;
41
-
42
- // Merge with a previous delete operation if possible
43
- let last_edit = result. last_mut ( ) . filter ( |text_edit| {
44
- text_edit. range . end == start && text_edit. new_text . is_empty ( )
45
- } ) ;
46
-
47
- if let Some ( last_edit) = last_edit {
48
- last_edit. new_text = diff. get_text ( * range) . to_string ( ) ;
49
- } else {
50
- result. push ( lsp:: TextEdit {
51
- range : lsp:: Range :: new ( start, start) ,
52
- new_text : diff. get_text ( * range) . to_string ( ) ,
53
- } ) ;
54
- }
55
- }
56
- CompressedOp :: DiffOp ( DiffOp :: Delete { range } ) => {
57
- let start = to_lsp:: position ( line_index, offset, position_encoding) ?;
58
- offset += range. len ( ) ;
59
- let end = to_lsp:: position ( line_index, offset, position_encoding) ?;
60
-
61
- result. push ( lsp:: TextEdit {
62
- range : lsp:: Range :: new ( start, end) ,
63
- new_text : String :: new ( ) ,
64
- } ) ;
65
- }
66
-
67
- CompressedOp :: EqualLines { line_count } => {
68
- let mut line_col = line_index
69
- . line_col ( offset)
70
- . expect ( "diff length is overflowing the line count in the original file" ) ;
71
-
72
- line_col. line += line_count. get ( ) + 1 ;
73
- line_col. col = 0 ;
74
-
75
- // SAFETY: This should only happen if `line_index` wasn't built
76
- // from the same string as the old revision of `diff`
77
- let new_offset = line_index
78
- . offset ( line_col)
79
- . expect ( "diff length is overflowing the line count in the original file" ) ;
80
-
81
- offset = new_offset;
82
- }
83
- }
84
- }
85
-
86
- Ok ( result)
87
- }
88
-
89
20
/// Convert an [pgt_diagnostics::Diagnostic] to a [lsp::Diagnostic], using the span
90
21
/// of the diagnostic's primary label as the diagnostic range.
91
22
/// Requires a [LineIndex] to convert a byte offset range to the line/col range
@@ -209,6 +140,7 @@ impl Visit for RelatedInformationVisitor<'_> {
209
140
}
210
141
211
142
/// Convert a piece of markup into a String
143
+ #[ allow( unused) ]
212
144
fn print_markup ( markup : & MarkupBuf ) -> String {
213
145
let mut message = Termcolor ( NoColor :: new ( Vec :: new ( ) ) ) ;
214
146
fmt:: Display :: fmt ( markup, & mut Formatter :: new ( & mut message) )
@@ -304,11 +236,81 @@ pub(crate) fn apply_document_changes(
304
236
305
237
#[ cfg( test) ]
306
238
mod tests {
307
- use crate :: adapters:: PositionEncoding ;
308
239
use crate :: adapters:: line_index:: LineIndex ;
309
- use pgt_text_edit:: TextEdit ;
240
+ use crate :: adapters:: { PositionEncoding , to_lsp} ;
241
+ use anyhow:: Result ;
242
+ use pgt_text_edit:: { CompressedOp , DiffOp , TextEdit } ;
243
+ use pgt_text_size:: TextSize ;
310
244
use tower_lsp:: lsp_types as lsp;
311
245
246
+ fn text_edit (
247
+ line_index : & LineIndex ,
248
+ diff : TextEdit ,
249
+ position_encoding : PositionEncoding ,
250
+ offset : Option < u32 > ,
251
+ ) -> Result < Vec < lsp:: TextEdit > > {
252
+ let mut result: Vec < lsp:: TextEdit > = Vec :: new ( ) ;
253
+ let mut offset = if let Some ( offset) = offset {
254
+ TextSize :: from ( offset)
255
+ } else {
256
+ TextSize :: from ( 0 )
257
+ } ;
258
+
259
+ for op in diff. iter ( ) {
260
+ match op {
261
+ CompressedOp :: DiffOp ( DiffOp :: Equal { range } ) => {
262
+ offset += range. len ( ) ;
263
+ }
264
+ CompressedOp :: DiffOp ( DiffOp :: Insert { range } ) => {
265
+ let start = to_lsp:: position ( line_index, offset, position_encoding) ?;
266
+
267
+ // Merge with a previous delete operation if possible
268
+ let last_edit = result. last_mut ( ) . filter ( |text_edit| {
269
+ text_edit. range . end == start && text_edit. new_text . is_empty ( )
270
+ } ) ;
271
+
272
+ if let Some ( last_edit) = last_edit {
273
+ last_edit. new_text = diff. get_text ( * range) . to_string ( ) ;
274
+ } else {
275
+ result. push ( lsp:: TextEdit {
276
+ range : lsp:: Range :: new ( start, start) ,
277
+ new_text : diff. get_text ( * range) . to_string ( ) ,
278
+ } ) ;
279
+ }
280
+ }
281
+ CompressedOp :: DiffOp ( DiffOp :: Delete { range } ) => {
282
+ let start = to_lsp:: position ( line_index, offset, position_encoding) ?;
283
+ offset += range. len ( ) ;
284
+ let end = to_lsp:: position ( line_index, offset, position_encoding) ?;
285
+
286
+ result. push ( lsp:: TextEdit {
287
+ range : lsp:: Range :: new ( start, end) ,
288
+ new_text : String :: new ( ) ,
289
+ } ) ;
290
+ }
291
+
292
+ CompressedOp :: EqualLines { line_count } => {
293
+ let mut line_col = line_index
294
+ . line_col ( offset)
295
+ . expect ( "diff length is overflowing the line count in the original file" ) ;
296
+
297
+ line_col. line += line_count. get ( ) + 1 ;
298
+ line_col. col = 0 ;
299
+
300
+ // SAFETY: This should only happen if `line_index` wasn't built
301
+ // from the same string as the old revision of `diff`
302
+ let new_offset = line_index
303
+ . offset ( line_col)
304
+ . expect ( "diff length is overflowing the line count in the original file" ) ;
305
+
306
+ offset = new_offset;
307
+ }
308
+ }
309
+ }
310
+
311
+ Ok ( result)
312
+ }
313
+
312
314
#[ test]
313
315
fn test_diff_1 ( ) {
314
316
const OLD : & str = "line 1 old
@@ -330,7 +332,7 @@ line 7 new";
330
332
let line_index = LineIndex :: new ( OLD ) ;
331
333
let diff = TextEdit :: from_unicode_words ( OLD , NEW ) ;
332
334
333
- let text_edit = super :: text_edit ( & line_index, diff, PositionEncoding :: Utf8 , None ) . unwrap ( ) ;
335
+ let text_edit = text_edit ( & line_index, diff, PositionEncoding :: Utf8 , None ) . unwrap ( ) ;
334
336
335
337
assert_eq ! (
336
338
text_edit. as_slice( ) ,
@@ -373,7 +375,7 @@ line 7 new";
373
375
let line_index = LineIndex :: new ( OLD ) ;
374
376
let diff = TextEdit :: from_unicode_words ( OLD , NEW ) ;
375
377
376
- let text_edit = super :: text_edit ( & line_index, diff, PositionEncoding :: Utf8 , None ) . unwrap ( ) ;
378
+ let text_edit = text_edit ( & line_index, diff, PositionEncoding :: Utf8 , None ) . unwrap ( ) ;
377
379
378
380
assert_eq ! (
379
381
text_edit. as_slice( ) ,
0 commit comments