diff options
Diffstat (limited to 'crates/ra_ide_db/src')
-rw-r--r-- | crates/ra_ide_db/src/defs.rs | 22 | ||||
-rw-r--r-- | crates/ra_ide_db/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_db/src/line_index_utils.rs | 302 | ||||
-rw-r--r-- | crates/ra_ide_db/src/marks.rs | 12 | ||||
-rw-r--r-- | crates/ra_ide_db/src/search.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide_db/src/source_change.rs | 100 |
6 files changed, 28 insertions, 412 deletions
diff --git a/crates/ra_ide_db/src/defs.rs b/crates/ra_ide_db/src/defs.rs index f990e3bb9..8b06cbfc5 100644 --- a/crates/ra_ide_db/src/defs.rs +++ b/crates/ra_ide_db/src/defs.rs | |||
@@ -6,7 +6,7 @@ | |||
6 | // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). | 6 | // FIXME: this badly needs rename/rewrite (matklad, 2020-02-06). |
7 | 7 | ||
8 | use hir::{ | 8 | use hir::{ |
9 | Adt, Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, | 9 | Field, HasVisibility, ImplDef, Local, MacroDef, Module, ModuleDef, Name, PathResolution, |
10 | Semantics, TypeParam, Visibility, | 10 | Semantics, TypeParam, Visibility, |
11 | }; | 11 | }; |
12 | use ra_prof::profile; | 12 | use ra_prof::profile; |
@@ -14,7 +14,6 @@ use ra_syntax::{ | |||
14 | ast::{self, AstNode}, | 14 | ast::{self, AstNode}, |
15 | match_ast, | 15 | match_ast, |
16 | }; | 16 | }; |
17 | use test_utils::tested_by; | ||
18 | 17 | ||
19 | use crate::RootDatabase; | 18 | use crate::RootDatabase; |
20 | 19 | ||
@@ -42,18 +41,10 @@ impl Definition { | |||
42 | } | 41 | } |
43 | 42 | ||
44 | pub fn visibility(&self, db: &RootDatabase) -> Option<Visibility> { | 43 | pub fn visibility(&self, db: &RootDatabase) -> Option<Visibility> { |
45 | let module = self.module(db); | ||
46 | |||
47 | match self { | 44 | match self { |
48 | Definition::Macro(_) => None, | 45 | Definition::Macro(_) => None, |
49 | Definition::Field(sf) => Some(sf.visibility(db)), | 46 | Definition::Field(sf) => Some(sf.visibility(db)), |
50 | Definition::ModuleDef(def) => match def { | 47 | Definition::ModuleDef(def) => def.definition_visibility(db), |
51 | ModuleDef::EnumVariant(id) => { | ||
52 | let parent = id.parent_enum(db); | ||
53 | module?.visibility_of(db, &ModuleDef::Adt(Adt::Enum(parent))) | ||
54 | } | ||
55 | _ => module?.visibility_of(db, def), | ||
56 | }, | ||
57 | Definition::SelfType(_) => None, | 48 | Definition::SelfType(_) => None, |
58 | Definition::Local(_) => None, | 49 | Definition::Local(_) => None, |
59 | Definition::TypeParam(_) => None, | 50 | Definition::TypeParam(_) => None, |
@@ -126,7 +117,6 @@ fn classify_name_inner(sema: &Semantics<RootDatabase>, name: &ast::Name) -> Opti | |||
126 | match_ast! { | 117 | match_ast! { |
127 | match parent { | 118 | match parent { |
128 | ast::Alias(it) => { | 119 | ast::Alias(it) => { |
129 | tested_by!(goto_def_for_use_alias; force); | ||
130 | let use_tree = it.syntax().parent().and_then(ast::UseTree::cast)?; | 120 | let use_tree = it.syntax().parent().and_then(ast::UseTree::cast)?; |
131 | let path = use_tree.path()?; | 121 | let path = use_tree.path()?; |
132 | let path_segment = path.segment()?; | 122 | let path_segment = path.segment()?; |
@@ -211,6 +201,8 @@ impl NameRefClass { | |||
211 | } | 201 | } |
212 | } | 202 | } |
213 | 203 | ||
204 | // Note: we don't have unit-tests for this rather important function. | ||
205 | // It is primarily exercised via goto definition tests in `ra_ide`. | ||
214 | pub fn classify_name_ref( | 206 | pub fn classify_name_ref( |
215 | sema: &Semantics<RootDatabase>, | 207 | sema: &Semantics<RootDatabase>, |
216 | name_ref: &ast::NameRef, | 208 | name_ref: &ast::NameRef, |
@@ -220,22 +212,18 @@ pub fn classify_name_ref( | |||
220 | let parent = name_ref.syntax().parent()?; | 212 | let parent = name_ref.syntax().parent()?; |
221 | 213 | ||
222 | if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { | 214 | if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) { |
223 | tested_by!(goto_def_for_methods; force); | ||
224 | if let Some(func) = sema.resolve_method_call(&method_call) { | 215 | if let Some(func) = sema.resolve_method_call(&method_call) { |
225 | return Some(NameRefClass::Definition(Definition::ModuleDef(func.into()))); | 216 | return Some(NameRefClass::Definition(Definition::ModuleDef(func.into()))); |
226 | } | 217 | } |
227 | } | 218 | } |
228 | 219 | ||
229 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { | 220 | if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) { |
230 | tested_by!(goto_def_for_fields; force); | ||
231 | if let Some(field) = sema.resolve_field(&field_expr) { | 221 | if let Some(field) = sema.resolve_field(&field_expr) { |
232 | return Some(NameRefClass::Definition(Definition::Field(field))); | 222 | return Some(NameRefClass::Definition(Definition::Field(field))); |
233 | } | 223 | } |
234 | } | 224 | } |
235 | 225 | ||
236 | if let Some(record_field) = ast::RecordField::for_field_name(name_ref) { | 226 | if let Some(record_field) = ast::RecordField::for_field_name(name_ref) { |
237 | tested_by!(goto_def_for_record_fields; force); | ||
238 | tested_by!(goto_def_for_field_init_shorthand; force); | ||
239 | if let Some((field, local)) = sema.resolve_record_field(&record_field) { | 227 | if let Some((field, local)) = sema.resolve_record_field(&record_field) { |
240 | let field = Definition::Field(field); | 228 | let field = Definition::Field(field); |
241 | let res = match local { | 229 | let res = match local { |
@@ -247,7 +235,6 @@ pub fn classify_name_ref( | |||
247 | } | 235 | } |
248 | 236 | ||
249 | if let Some(record_field_pat) = ast::RecordFieldPat::cast(parent.clone()) { | 237 | if let Some(record_field_pat) = ast::RecordFieldPat::cast(parent.clone()) { |
250 | tested_by!(goto_def_for_record_field_pats; force); | ||
251 | if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) { | 238 | if let Some(field) = sema.resolve_record_field_pat(&record_field_pat) { |
252 | let field = Definition::Field(field); | 239 | let field = Definition::Field(field); |
253 | return Some(NameRefClass::Definition(field)); | 240 | return Some(NameRefClass::Definition(field)); |
@@ -255,7 +242,6 @@ pub fn classify_name_ref( | |||
255 | } | 242 | } |
256 | 243 | ||
257 | if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { | 244 | if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) { |
258 | tested_by!(goto_def_for_macros; force); | ||
259 | if let Some(macro_def) = sema.resolve_macro_call(¯o_call) { | 245 | if let Some(macro_def) = sema.resolve_macro_call(¯o_call) { |
260 | return Some(NameRefClass::Definition(Definition::Macro(macro_def))); | 246 | return Some(NameRefClass::Definition(Definition::Macro(macro_def))); |
261 | } | 247 | } |
diff --git a/crates/ra_ide_db/src/lib.rs b/crates/ra_ide_db/src/lib.rs index 52fcd7b6f..1b74e6558 100644 --- a/crates/ra_ide_db/src/lib.rs +++ b/crates/ra_ide_db/src/lib.rs | |||
@@ -2,9 +2,7 @@ | |||
2 | //! | 2 | //! |
3 | //! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search. | 3 | //! It is mainly a `HirDatabase` for semantic analysis, plus a `SymbolsDatabase`, for fuzzy search. |
4 | 4 | ||
5 | pub mod marks; | ||
6 | pub mod line_index; | 5 | pub mod line_index; |
7 | pub mod line_index_utils; | ||
8 | pub mod symbol_index; | 6 | pub mod symbol_index; |
9 | pub mod change; | 7 | pub mod change; |
10 | pub mod defs; | 8 | pub mod defs; |
diff --git a/crates/ra_ide_db/src/line_index_utils.rs b/crates/ra_ide_db/src/line_index_utils.rs deleted file mode 100644 index 7fa6fc448..000000000 --- a/crates/ra_ide_db/src/line_index_utils.rs +++ /dev/null | |||
@@ -1,302 +0,0 @@ | |||
1 | //! Code actions can specify desirable final position of the cursor. | ||
2 | //! | ||
3 | //! The position is specified as a `TextSize` in the final file. We need to send | ||
4 | //! it in `(Line, Column)` coordinate though. However, we only have a LineIndex | ||
5 | //! for a file pre-edit! | ||
6 | //! | ||
7 | //! Code in this module applies this "to (Line, Column) after edit" | ||
8 | //! transformation. | ||
9 | |||
10 | use std::convert::TryInto; | ||
11 | |||
12 | use ra_syntax::{TextRange, TextSize}; | ||
13 | use ra_text_edit::{Indel, TextEdit}; | ||
14 | |||
15 | use crate::line_index::{LineCol, LineIndex, Utf16Char}; | ||
16 | |||
17 | pub fn translate_offset_with_edit( | ||
18 | line_index: &LineIndex, | ||
19 | offset: TextSize, | ||
20 | text_edit: &TextEdit, | ||
21 | ) -> LineCol { | ||
22 | let mut state = Edits::from_text_edit(&text_edit); | ||
23 | |||
24 | let mut res = RunningLineCol::new(); | ||
25 | |||
26 | macro_rules! test_step { | ||
27 | ($x:ident) => { | ||
28 | match &$x { | ||
29 | Step::Newline(n) => { | ||
30 | if offset < *n { | ||
31 | return res.to_line_col(offset); | ||
32 | } else { | ||
33 | res.add_line(*n); | ||
34 | } | ||
35 | } | ||
36 | Step::Utf16Char(x) => { | ||
37 | if offset < x.end() { | ||
38 | // if the offset is inside a multibyte char it's invalid | ||
39 | // clamp it to the start of the char | ||
40 | let clamp = offset.min(x.start()); | ||
41 | return res.to_line_col(clamp); | ||
42 | } else { | ||
43 | res.adjust_col(*x); | ||
44 | } | ||
45 | } | ||
46 | } | ||
47 | }; | ||
48 | } | ||
49 | |||
50 | for orig_step in LineIndexStepIter::from(line_index) { | ||
51 | loop { | ||
52 | let translated_step = state.translate_step(&orig_step); | ||
53 | match state.next_steps(&translated_step) { | ||
54 | NextSteps::Use => { | ||
55 | test_step!(translated_step); | ||
56 | break; | ||
57 | } | ||
58 | NextSteps::ReplaceMany(ns) => { | ||
59 | for n in ns { | ||
60 | test_step!(n); | ||
61 | } | ||
62 | break; | ||
63 | } | ||
64 | NextSteps::AddMany(ns) => { | ||
65 | for n in ns { | ||
66 | test_step!(n); | ||
67 | } | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | |||
73 | loop { | ||
74 | match state.next_inserted_steps() { | ||
75 | None => break, | ||
76 | Some(ns) => { | ||
77 | for n in ns { | ||
78 | test_step!(n); | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | res.to_line_col(offset) | ||
85 | } | ||
86 | |||
87 | #[derive(Debug, Clone)] | ||
88 | enum Step { | ||
89 | Newline(TextSize), | ||
90 | Utf16Char(TextRange), | ||
91 | } | ||
92 | |||
93 | #[derive(Debug)] | ||
94 | struct LineIndexStepIter<'a> { | ||
95 | line_index: &'a LineIndex, | ||
96 | next_newline_idx: usize, | ||
97 | utf16_chars: Option<(TextSize, std::slice::Iter<'a, Utf16Char>)>, | ||
98 | } | ||
99 | |||
100 | impl LineIndexStepIter<'_> { | ||
101 | fn from(line_index: &LineIndex) -> LineIndexStepIter { | ||
102 | let mut x = LineIndexStepIter { line_index, next_newline_idx: 0, utf16_chars: None }; | ||
103 | // skip first newline since it's not real | ||
104 | x.next(); | ||
105 | x | ||
106 | } | ||
107 | } | ||
108 | |||
109 | impl Iterator for LineIndexStepIter<'_> { | ||
110 | type Item = Step; | ||
111 | fn next(&mut self) -> Option<Step> { | ||
112 | self.utf16_chars | ||
113 | .as_mut() | ||
114 | .and_then(|(newline, x)| { | ||
115 | let x = x.next()?; | ||
116 | Some(Step::Utf16Char(TextRange::new(*newline + x.start, *newline + x.end))) | ||
117 | }) | ||
118 | .or_else(|| { | ||
119 | let next_newline = *self.line_index.newlines.get(self.next_newline_idx)?; | ||
120 | self.utf16_chars = self | ||
121 | .line_index | ||
122 | .utf16_lines | ||
123 | .get(&(self.next_newline_idx as u32)) | ||
124 | .map(|x| (next_newline, x.iter())); | ||
125 | self.next_newline_idx += 1; | ||
126 | Some(Step::Newline(next_newline)) | ||
127 | }) | ||
128 | } | ||
129 | } | ||
130 | |||
131 | #[derive(Debug)] | ||
132 | struct OffsetStepIter<'a> { | ||
133 | text: &'a str, | ||
134 | offset: TextSize, | ||
135 | } | ||
136 | |||
137 | impl Iterator for OffsetStepIter<'_> { | ||
138 | type Item = Step; | ||
139 | fn next(&mut self) -> Option<Step> { | ||
140 | let (next, next_offset) = self | ||
141 | .text | ||
142 | .char_indices() | ||
143 | .filter_map(|(i, c)| { | ||
144 | let i: TextSize = i.try_into().unwrap(); | ||
145 | let char_len = TextSize::of(c); | ||
146 | if c == '\n' { | ||
147 | let next_offset = self.offset + i + char_len; | ||
148 | let next = Step::Newline(next_offset); | ||
149 | Some((next, next_offset)) | ||
150 | } else { | ||
151 | if !c.is_ascii() { | ||
152 | let start = self.offset + i; | ||
153 | let end = start + char_len; | ||
154 | let next = Step::Utf16Char(TextRange::new(start, end)); | ||
155 | let next_offset = end; | ||
156 | Some((next, next_offset)) | ||
157 | } else { | ||
158 | None | ||
159 | } | ||
160 | } | ||
161 | }) | ||
162 | .next()?; | ||
163 | let next_idx: usize = (next_offset - self.offset).into(); | ||
164 | self.text = &self.text[next_idx..]; | ||
165 | self.offset = next_offset; | ||
166 | Some(next) | ||
167 | } | ||
168 | } | ||
169 | |||
170 | #[derive(Debug)] | ||
171 | enum NextSteps<'a> { | ||
172 | Use, | ||
173 | ReplaceMany(OffsetStepIter<'a>), | ||
174 | AddMany(OffsetStepIter<'a>), | ||
175 | } | ||
176 | |||
177 | #[derive(Debug)] | ||
178 | struct TranslatedEdit<'a> { | ||
179 | delete: TextRange, | ||
180 | insert: &'a str, | ||
181 | diff: i64, | ||
182 | } | ||
183 | |||
184 | struct Edits<'a> { | ||
185 | edits: &'a [Indel], | ||
186 | current: Option<TranslatedEdit<'a>>, | ||
187 | acc_diff: i64, | ||
188 | } | ||
189 | |||
190 | impl<'a> Edits<'a> { | ||
191 | fn from_text_edit(text_edit: &'a TextEdit) -> Edits<'a> { | ||
192 | let mut x = Edits { edits: text_edit.as_indels(), current: None, acc_diff: 0 }; | ||
193 | x.advance_edit(); | ||
194 | x | ||
195 | } | ||
196 | fn advance_edit(&mut self) { | ||
197 | self.acc_diff += self.current.as_ref().map_or(0, |x| x.diff); | ||
198 | match self.edits.split_first() { | ||
199 | Some((next, rest)) => { | ||
200 | let delete = self.translate_range(next.delete); | ||
201 | let diff = next.insert.len() as i64 - usize::from(next.delete.len()) as i64; | ||
202 | self.current = Some(TranslatedEdit { delete, insert: &next.insert, diff }); | ||
203 | self.edits = rest; | ||
204 | } | ||
205 | None => { | ||
206 | self.current = None; | ||
207 | } | ||
208 | } | ||
209 | } | ||
210 | |||
211 | fn next_inserted_steps(&mut self) -> Option<OffsetStepIter<'a>> { | ||
212 | let cur = self.current.as_ref()?; | ||
213 | let res = Some(OffsetStepIter { offset: cur.delete.start(), text: &cur.insert }); | ||
214 | self.advance_edit(); | ||
215 | res | ||
216 | } | ||
217 | |||
218 | fn next_steps(&mut self, step: &Step) -> NextSteps { | ||
219 | let step_pos = match *step { | ||
220 | Step::Newline(n) => n, | ||
221 | Step::Utf16Char(r) => r.end(), | ||
222 | }; | ||
223 | match &mut self.current { | ||
224 | Some(edit) => { | ||
225 | if step_pos <= edit.delete.start() { | ||
226 | NextSteps::Use | ||
227 | } else if step_pos <= edit.delete.end() { | ||
228 | let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert }; | ||
229 | // empty slice to avoid returning steps again | ||
230 | edit.insert = &edit.insert[edit.insert.len()..]; | ||
231 | NextSteps::ReplaceMany(iter) | ||
232 | } else { | ||
233 | let iter = OffsetStepIter { offset: edit.delete.start(), text: &edit.insert }; | ||
234 | // empty slice to avoid returning steps again | ||
235 | edit.insert = &edit.insert[edit.insert.len()..]; | ||
236 | self.advance_edit(); | ||
237 | NextSteps::AddMany(iter) | ||
238 | } | ||
239 | } | ||
240 | None => NextSteps::Use, | ||
241 | } | ||
242 | } | ||
243 | |||
244 | fn translate_range(&self, range: TextRange) -> TextRange { | ||
245 | if self.acc_diff == 0 { | ||
246 | range | ||
247 | } else { | ||
248 | let start = self.translate(range.start()); | ||
249 | let end = self.translate(range.end()); | ||
250 | TextRange::new(start, end) | ||
251 | } | ||
252 | } | ||
253 | |||
254 | fn translate(&self, x: TextSize) -> TextSize { | ||
255 | if self.acc_diff == 0 { | ||
256 | x | ||
257 | } else { | ||
258 | TextSize::from((usize::from(x) as i64 + self.acc_diff) as u32) | ||
259 | } | ||
260 | } | ||
261 | |||
262 | fn translate_step(&self, x: &Step) -> Step { | ||
263 | if self.acc_diff == 0 { | ||
264 | x.clone() | ||
265 | } else { | ||
266 | match *x { | ||
267 | Step::Newline(n) => Step::Newline(self.translate(n)), | ||
268 | Step::Utf16Char(r) => Step::Utf16Char(self.translate_range(r)), | ||
269 | } | ||
270 | } | ||
271 | } | ||
272 | } | ||
273 | |||
274 | #[derive(Debug)] | ||
275 | struct RunningLineCol { | ||
276 | line: u32, | ||
277 | last_newline: TextSize, | ||
278 | col_adjust: TextSize, | ||
279 | } | ||
280 | |||
281 | impl RunningLineCol { | ||
282 | fn new() -> RunningLineCol { | ||
283 | RunningLineCol { line: 0, last_newline: TextSize::from(0), col_adjust: TextSize::from(0) } | ||
284 | } | ||
285 | |||
286 | fn to_line_col(&self, offset: TextSize) -> LineCol { | ||
287 | LineCol { | ||
288 | line: self.line, | ||
289 | col_utf16: ((offset - self.last_newline) - self.col_adjust).into(), | ||
290 | } | ||
291 | } | ||
292 | |||
293 | fn add_line(&mut self, newline: TextSize) { | ||
294 | self.line += 1; | ||
295 | self.last_newline = newline; | ||
296 | self.col_adjust = TextSize::from(0); | ||
297 | } | ||
298 | |||
299 | fn adjust_col(&mut self, range: TextRange) { | ||
300 | self.col_adjust += range.len() - TextSize::from(1); | ||
301 | } | ||
302 | } | ||
diff --git a/crates/ra_ide_db/src/marks.rs b/crates/ra_ide_db/src/marks.rs deleted file mode 100644 index 386fe605c..000000000 --- a/crates/ra_ide_db/src/marks.rs +++ /dev/null | |||
@@ -1,12 +0,0 @@ | |||
1 | //! See test_utils/src/marks.rs | ||
2 | |||
3 | test_utils::marks![ | ||
4 | goto_def_for_macros | ||
5 | goto_def_for_use_alias | ||
6 | goto_def_for_methods | ||
7 | goto_def_for_fields | ||
8 | goto_def_for_record_fields | ||
9 | goto_def_for_field_init_shorthand | ||
10 | goto_def_for_record_field_pats | ||
11 | search_filters_by_range | ||
12 | ]; | ||
diff --git a/crates/ra_ide_db/src/search.rs b/crates/ra_ide_db/src/search.rs index b464959fc..589f44771 100644 --- a/crates/ra_ide_db/src/search.rs +++ b/crates/ra_ide_db/src/search.rs | |||
@@ -12,7 +12,6 @@ use ra_db::{FileId, FileRange, SourceDatabaseExt}; | |||
12 | use ra_prof::profile; | 12 | use ra_prof::profile; |
13 | use ra_syntax::{ast, match_ast, AstNode, TextRange, TextSize}; | 13 | use ra_syntax::{ast, match_ast, AstNode, TextRange, TextSize}; |
14 | use rustc_hash::FxHashMap; | 14 | use rustc_hash::FxHashMap; |
15 | use test_utils::tested_by; | ||
16 | 15 | ||
17 | use crate::{ | 16 | use crate::{ |
18 | defs::{classify_name_ref, Definition, NameRefClass}, | 17 | defs::{classify_name_ref, Definition, NameRefClass}, |
@@ -209,7 +208,6 @@ impl Definition { | |||
209 | for (idx, _) in text.match_indices(pat) { | 208 | for (idx, _) in text.match_indices(pat) { |
210 | let offset: TextSize = idx.try_into().unwrap(); | 209 | let offset: TextSize = idx.try_into().unwrap(); |
211 | if !search_range.contains_inclusive(offset) { | 210 | if !search_range.contains_inclusive(offset) { |
212 | tested_by!(search_filters_by_range; force); | ||
213 | continue; | 211 | continue; |
214 | } | 212 | } |
215 | 213 | ||
diff --git a/crates/ra_ide_db/src/source_change.rs b/crates/ra_ide_db/src/source_change.rs index af81a91a4..e713f4b7e 100644 --- a/crates/ra_ide_db/src/source_change.rs +++ b/crates/ra_ide_db/src/source_change.rs | |||
@@ -3,90 +3,35 @@ | |||
3 | //! | 3 | //! |
4 | //! It can be viewed as a dual for `AnalysisChange`. | 4 | //! It can be viewed as a dual for `AnalysisChange`. |
5 | 5 | ||
6 | use ra_db::{FileId, FilePosition, RelativePathBuf, SourceRootId}; | 6 | use ra_db::{FileId, RelativePathBuf, SourceRootId}; |
7 | use ra_text_edit::{TextEdit, TextSize}; | 7 | use ra_text_edit::TextEdit; |
8 | 8 | ||
9 | #[derive(Debug, Clone)] | 9 | #[derive(Debug, Clone)] |
10 | pub struct SourceChange { | 10 | pub struct SourceChange { |
11 | /// For display in the undo log in the editor | ||
12 | pub label: String, | ||
13 | pub source_file_edits: Vec<SourceFileEdit>, | 11 | pub source_file_edits: Vec<SourceFileEdit>, |
14 | pub file_system_edits: Vec<FileSystemEdit>, | 12 | pub file_system_edits: Vec<FileSystemEdit>, |
15 | pub cursor_position: Option<FilePosition>, | 13 | pub is_snippet: bool, |
16 | } | 14 | } |
17 | 15 | ||
18 | impl SourceChange { | 16 | impl SourceChange { |
19 | /// Creates a new SourceChange with the given label | 17 | /// Creates a new SourceChange with the given label |
20 | /// from the edits. | 18 | /// from the edits. |
21 | pub fn from_edits<L: Into<String>>( | 19 | pub fn from_edits( |
22 | label: L, | ||
23 | source_file_edits: Vec<SourceFileEdit>, | 20 | source_file_edits: Vec<SourceFileEdit>, |
24 | file_system_edits: Vec<FileSystemEdit>, | 21 | file_system_edits: Vec<FileSystemEdit>, |
25 | ) -> Self { | 22 | ) -> Self { |
26 | SourceChange { | 23 | SourceChange { source_file_edits, file_system_edits, is_snippet: false } |
27 | label: label.into(), | ||
28 | source_file_edits, | ||
29 | file_system_edits, | ||
30 | cursor_position: None, | ||
31 | } | ||
32 | } | 24 | } |
33 | 25 | ||
34 | /// Creates a new SourceChange with the given label, | 26 | /// Creates a new SourceChange with the given label, |
35 | /// containing only the given `SourceFileEdits`. | 27 | /// containing only the given `SourceFileEdits`. |
36 | pub fn source_file_edits<L: Into<String>>(label: L, edits: Vec<SourceFileEdit>) -> Self { | 28 | pub fn source_file_edits(edits: Vec<SourceFileEdit>) -> Self { |
37 | let label = label.into(); | 29 | SourceChange { source_file_edits: edits, file_system_edits: vec![], is_snippet: false } |
38 | assert!(label.starts_with(char::is_uppercase)); | ||
39 | SourceChange { | ||
40 | label: label, | ||
41 | source_file_edits: edits, | ||
42 | file_system_edits: vec![], | ||
43 | cursor_position: None, | ||
44 | } | ||
45 | } | ||
46 | |||
47 | /// Creates a new SourceChange with the given label, | ||
48 | /// containing only the given `FileSystemEdits`. | ||
49 | pub(crate) fn file_system_edits<L: Into<String>>(label: L, edits: Vec<FileSystemEdit>) -> Self { | ||
50 | SourceChange { | ||
51 | label: label.into(), | ||
52 | source_file_edits: vec![], | ||
53 | file_system_edits: edits, | ||
54 | cursor_position: None, | ||
55 | } | ||
56 | } | ||
57 | |||
58 | /// Creates a new SourceChange with the given label, | ||
59 | /// containing only a single `SourceFileEdit`. | ||
60 | pub fn source_file_edit<L: Into<String>>(label: L, edit: SourceFileEdit) -> Self { | ||
61 | SourceChange::source_file_edits(label, vec![edit]) | ||
62 | } | ||
63 | |||
64 | /// Creates a new SourceChange with the given label | ||
65 | /// from the given `FileId` and `TextEdit` | ||
66 | pub fn source_file_edit_from<L: Into<String>>( | ||
67 | label: L, | ||
68 | file_id: FileId, | ||
69 | edit: TextEdit, | ||
70 | ) -> Self { | ||
71 | SourceChange::source_file_edit(label, SourceFileEdit { file_id, edit }) | ||
72 | } | 30 | } |
73 | |||
74 | /// Creates a new SourceChange with the given label | 31 | /// Creates a new SourceChange with the given label |
75 | /// from the given `FileId` and `TextEdit` | 32 | /// from the given `FileId` and `TextEdit` |
76 | pub fn file_system_edit<L: Into<String>>(label: L, edit: FileSystemEdit) -> Self { | 33 | pub fn source_file_edit_from(file_id: FileId, edit: TextEdit) -> Self { |
77 | SourceChange::file_system_edits(label, vec![edit]) | 34 | SourceFileEdit { file_id, edit }.into() |
78 | } | ||
79 | |||
80 | /// Sets the cursor position to the given `FilePosition` | ||
81 | pub fn with_cursor(mut self, cursor_position: FilePosition) -> Self { | ||
82 | self.cursor_position = Some(cursor_position); | ||
83 | self | ||
84 | } | ||
85 | |||
86 | /// Sets the cursor position to the given `FilePosition` | ||
87 | pub fn with_cursor_opt(mut self, cursor_position: Option<FilePosition>) -> Self { | ||
88 | self.cursor_position = cursor_position; | ||
89 | self | ||
90 | } | 35 | } |
91 | } | 36 | } |
92 | 37 | ||
@@ -96,25 +41,28 @@ pub struct SourceFileEdit { | |||
96 | pub edit: TextEdit, | 41 | pub edit: TextEdit, |
97 | } | 42 | } |
98 | 43 | ||
44 | impl From<SourceFileEdit> for SourceChange { | ||
45 | fn from(edit: SourceFileEdit) -> SourceChange { | ||
46 | SourceChange { | ||
47 | source_file_edits: vec![edit], | ||
48 | file_system_edits: Vec::new(), | ||
49 | is_snippet: false, | ||
50 | } | ||
51 | } | ||
52 | } | ||
53 | |||
99 | #[derive(Debug, Clone)] | 54 | #[derive(Debug, Clone)] |
100 | pub enum FileSystemEdit { | 55 | pub enum FileSystemEdit { |
101 | CreateFile { source_root: SourceRootId, path: RelativePathBuf }, | 56 | CreateFile { source_root: SourceRootId, path: RelativePathBuf }, |
102 | MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf }, | 57 | MoveFile { src: FileId, dst_source_root: SourceRootId, dst_path: RelativePathBuf }, |
103 | } | 58 | } |
104 | 59 | ||
105 | pub struct SingleFileChange { | 60 | impl From<FileSystemEdit> for SourceChange { |
106 | pub label: String, | 61 | fn from(edit: FileSystemEdit) -> SourceChange { |
107 | pub edit: TextEdit, | ||
108 | pub cursor_position: Option<TextSize>, | ||
109 | } | ||
110 | |||
111 | impl SingleFileChange { | ||
112 | pub fn into_source_change(self, file_id: FileId) -> SourceChange { | ||
113 | SourceChange { | 62 | SourceChange { |
114 | label: self.label, | 63 | source_file_edits: Vec::new(), |
115 | source_file_edits: vec![SourceFileEdit { file_id, edit: self.edit }], | 64 | file_system_edits: vec![edit], |
116 | file_system_edits: Vec::new(), | 65 | is_snippet: false, |
117 | cursor_position: self.cursor_position.map(|offset| FilePosition { file_id, offset }), | ||
118 | } | 66 | } |
119 | } | 67 | } |
120 | } | 68 | } |