1use crate::ra_ap_syntax::{
2 ast::{Name, NameRef, Path, PathSegment},
3 AstNode,
4};
5
6use ra_ap_text_edit::{TextEdit, TextEditBuilder, TextRange, TextSize};
7
8#[derive(Default, Debug, Clone)]
9pub struct Upgrader {
10 edit: TextEditBuilder,
11}
12
13pub trait ToTextRange {
14 fn to(self) -> TextRange;
15}
16
17impl ToTextRange for TextRange {
18 fn to(self) -> TextRange {
19 self
20 }
21}
22
23impl ToTextRange for Option<Path> {
24 fn to(self) -> TextRange {
25 self.unwrap().segment().to()
26 }
27}
28
29impl ToTextRange for Option<PathSegment> {
30 fn to(self) -> TextRange {
31 self.unwrap().name_ref().to()
32 }
33}
34
35impl ToTextRange for Option<NameRef> {
36 fn to(self) -> TextRange {
37 self.unwrap().syntax().text_range()
38 }
39}
40
41impl ToTextRange for Option<Name> {
42 fn to(self) -> TextRange {
43 self.unwrap().syntax().text_range()
44 }
45}
46
47impl Upgrader {
48 pub fn replace<T, S>(&mut self, range: T, replace_with: S)
49 where
50 T: ToTextRange,
51 S: Into<String>,
52 {
53 self.edit.replace(range.to(), replace_with.into())
54 }
55
56 pub fn delete<T>(&mut self, range: T)
57 where
58 T: ToTextRange,
59 {
60 self.edit.delete(range.to())
61 }
62
63 pub fn insert<S>(&mut self, offset: TextSize, text: S)
64 where
65 S: Into<String>,
66 {
67 self.edit.insert(offset, text.into())
68 }
69
70 pub fn add_dep(&mut self) {}
71
72 pub fn add_feature(&mut self) {}
73
74 pub(crate) fn finish(&mut self) -> TextEdit {
75 let edit = self.edit.clone().finish();
76 self.edit = TextEditBuilder::default();
77 edit
78 }
79}