1
1
//! Utilities for LSP-related boilerplate code.
2
- use std:: { error:: Error , ops:: Range } ;
2
+ use std:: { borrow :: Cow , error:: Error , ops:: Range } ;
3
3
4
4
use lsp_server:: Notification ;
5
5
use ra_db:: Canceled ;
@@ -84,8 +84,8 @@ impl GlobalState {
84
84
pub ( crate ) fn apply_document_changes (
85
85
old_text : & mut String ,
86
86
content_changes : Vec < lsp_types:: TextDocumentContentChangeEvent > ,
87
+ mut line_index : Cow < ' _ , LineIndex > ,
87
88
) {
88
- let mut line_index = LineIndex :: new ( old_text) ;
89
89
// The changes we got must be applied sequentially, but can cross lines so we
90
90
// have to keep our line index updated.
91
91
// Some clients (e.g. Code) sort the ranges in reverse. As an optimization, we
@@ -110,7 +110,7 @@ pub(crate) fn apply_document_changes(
110
110
match change. range {
111
111
Some ( range) => {
112
112
if !index_valid. covers ( range. end . line ) {
113
- line_index = LineIndex :: new ( & old_text) ;
113
+ line_index = Cow :: Owned ( LineIndex :: new ( old_text) ) ;
114
114
}
115
115
index_valid = IndexValid :: UpToLineExclusive ( range. start . line ) ;
116
116
let range = from_proto:: text_range ( & line_index, range) ;
@@ -145,10 +145,15 @@ mod tests {
145
145
} ;
146
146
}
147
147
148
+ fn run ( text : & mut String , changes : Vec < TextDocumentContentChangeEvent > ) {
149
+ let line_index = Cow :: Owned ( LineIndex :: new ( & text) ) ;
150
+ super :: apply_document_changes ( text, changes, line_index) ;
151
+ }
152
+
148
153
let mut text = String :: new ( ) ;
149
- apply_document_changes ( & mut text, vec ! [ ] ) ;
154
+ run ( & mut text, vec ! [ ] ) ;
150
155
assert_eq ! ( text, "" ) ;
151
- apply_document_changes (
156
+ run (
152
157
& mut text,
153
158
vec ! [ TextDocumentContentChangeEvent {
154
159
range: None ,
@@ -157,39 +162,36 @@ mod tests {
157
162
} ] ,
158
163
) ;
159
164
assert_eq ! ( text, "the" ) ;
160
- apply_document_changes ( & mut text, c ! [ 0 , 3 ; 0 , 3 => " quick" ] ) ;
165
+ run ( & mut text, c ! [ 0 , 3 ; 0 , 3 => " quick" ] ) ;
161
166
assert_eq ! ( text, "the quick" ) ;
162
- apply_document_changes ( & mut text, c ! [ 0 , 0 ; 0 , 4 => "" , 0 , 5 ; 0 , 5 => " foxes" ] ) ;
167
+ run ( & mut text, c ! [ 0 , 0 ; 0 , 4 => "" , 0 , 5 ; 0 , 5 => " foxes" ] ) ;
163
168
assert_eq ! ( text, "quick foxes" ) ;
164
- apply_document_changes ( & mut text, c ! [ 0 , 11 ; 0 , 11 => "\n dream" ] ) ;
169
+ run ( & mut text, c ! [ 0 , 11 ; 0 , 11 => "\n dream" ] ) ;
165
170
assert_eq ! ( text, "quick foxes\n dream" ) ;
166
- apply_document_changes ( & mut text, c ! [ 1 , 0 ; 1 , 0 => "have " ] ) ;
171
+ run ( & mut text, c ! [ 1 , 0 ; 1 , 0 => "have " ] ) ;
167
172
assert_eq ! ( text, "quick foxes\n have dream" ) ;
168
- apply_document_changes (
169
- & mut text,
170
- c ! [ 0 , 0 ; 0 , 0 => "the " , 1 , 4 ; 1 , 4 => " quiet" , 1 , 16 ; 1 , 16 => "s\n " ] ,
171
- ) ;
173
+ run ( & mut text, c ! [ 0 , 0 ; 0 , 0 => "the " , 1 , 4 ; 1 , 4 => " quiet" , 1 , 16 ; 1 , 16 => "s\n " ] ) ;
172
174
assert_eq ! ( text, "the quick foxes\n have quiet dreams\n " ) ;
173
- apply_document_changes ( & mut text, c ! [ 0 , 15 ; 0 , 15 => "\n " , 2 , 17 ; 2 , 17 => "\n " ] ) ;
175
+ run ( & mut text, c ! [ 0 , 15 ; 0 , 15 => "\n " , 2 , 17 ; 2 , 17 => "\n " ] ) ;
174
176
assert_eq ! ( text, "the quick foxes\n \n have quiet dreams\n \n " ) ;
175
- apply_document_changes (
177
+ run (
176
178
& mut text,
177
179
c ! [ 1 , 0 ; 1 , 0 => "DREAM" , 2 , 0 ; 2 , 0 => "they " , 3 , 0 ; 3 , 0 => "DON'T THEY?" ] ,
178
180
) ;
179
181
assert_eq ! ( text, "the quick foxes\n DREAM\n they have quiet dreams\n DON'T THEY?\n " ) ;
180
- apply_document_changes ( & mut text, c ! [ 0 , 10 ; 1 , 5 => "" , 2 , 0 ; 2 , 12 => "" ] ) ;
182
+ run ( & mut text, c ! [ 0 , 10 ; 1 , 5 => "" , 2 , 0 ; 2 , 12 => "" ] ) ;
181
183
assert_eq ! ( text, "the quick \n they have quiet dreams\n " ) ;
182
184
183
185
text = String :: from ( "❤️" ) ;
184
- apply_document_changes ( & mut text, c ! [ 0 , 0 ; 0 , 0 => "a" ] ) ;
186
+ run ( & mut text, c ! [ 0 , 0 ; 0 , 0 => "a" ] ) ;
185
187
assert_eq ! ( text, "a❤️" ) ;
186
188
187
189
text = String :: from ( "a\n b" ) ;
188
- apply_document_changes ( & mut text, c ! [ 0 , 1 ; 1 , 0 => "\n țc" , 0 , 1 ; 1 , 1 => "d" ] ) ;
190
+ run ( & mut text, c ! [ 0 , 1 ; 1 , 0 => "\n țc" , 0 , 1 ; 1 , 1 => "d" ] ) ;
189
191
assert_eq ! ( text, "adcb" ) ;
190
192
191
193
text = String :: from ( "a\n b" ) ;
192
- apply_document_changes ( & mut text, c ! [ 0 , 1 ; 1 , 0 => "ț\n c" , 0 , 2 ; 0 , 2 => "c" ] ) ;
194
+ run ( & mut text, c ! [ 0 , 1 ; 1 , 0 => "ț\n c" , 0 , 2 ; 0 , 2 => "c" ] ) ;
193
195
assert_eq ! ( text, "ațc\n cb" ) ;
194
196
}
195
197
}
0 commit comments