aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/syntax_text.rs
blob: 652cb7a1e0845d59008bb5098404497c1ece1b5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use std::{
    fmt,
    ops::{self, Bound},
};

use crate::{SmolStr, SyntaxElement, SyntaxNode, TextRange, TextUnit};

#[derive(Clone)]
pub struct SyntaxText {
    node: SyntaxNode,
    range: TextRange,
}

impl SyntaxText {
    pub(crate) fn new(node: SyntaxNode) -> SyntaxText {
        let range = node.text_range();
        SyntaxText { node, range }
    }

    pub fn try_fold_chunks<T, F, E>(&self, init: T, mut f: F) -> Result<T, E>
    where
        F: FnMut(T, &str) -> Result<T, E>,
    {
        self.node.descendants_with_tokens().try_fold(init, move |acc, element| {
            let res = match element {
                SyntaxElement::Token(token) => {
                    let range = match self.range.intersection(&token.text_range()) {
                        None => return Ok(acc),
                        Some(it) => it,
                    };
                    let slice = if range == token.text_range() {
                        token.text()
                    } else {
                        let range = range - token.text_range().start();
                        &token.text()[range]
                    };
                    f(acc, slice)?
                }
                SyntaxElement::Node(_) => acc,
            };
            Ok(res)
        })
    }

    pub fn try_for_each_chunk<F: FnMut(&str) -> Result<(), E>, E>(
        &self,
        mut f: F,
    ) -> Result<(), E> {
        self.try_fold_chunks((), move |(), chunk| f(chunk))
    }

    pub fn for_each_chunk<F: FnMut(&str)>(&self, mut f: F) {
        enum Void {}
        match self.try_for_each_chunk(|chunk| Ok::<(), Void>(f(chunk))) {
            Ok(()) => (),
            Err(void) => match void {},
        }
    }

    pub fn to_smol_string(&self) -> SmolStr {
        self.to_string().into()
    }

    pub fn contains_char(&self, c: char) -> bool {
        self.try_for_each_chunk(|chunk| if chunk.contains(c) { Err(()) } else { Ok(()) }).is_err()
    }

    pub fn find_char(&self, c: char) -> Option<TextUnit> {
        let mut acc: TextUnit = 0.into();
        let res = self.try_for_each_chunk(|chunk| {
            if let Some(pos) = chunk.find(c) {
                let pos: TextUnit = (pos as u32).into();
                return Err(acc + pos);
            }
            acc += TextUnit::of_str(chunk);
            Ok(())
        });
        found(res)
    }

    pub fn len(&self) -> TextUnit {
        self.range.len()
    }

    pub fn is_empty(&self) -> bool {
        self.range.is_empty()
    }

    pub fn slice(&self, range: impl ops::RangeBounds<TextUnit>) -> SyntaxText {
        let start = match range.start_bound() {
            Bound::Included(&b) => b,
            Bound::Excluded(_) => panic!("utf-aware slicing can't work this way"),
            Bound::Unbounded => 0.into(),
        };
        let end = match range.end_bound() {
            Bound::Included(_) => panic!("utf-aware slicing can't work this way"),
            Bound::Excluded(&b) => b,
            Bound::Unbounded => self.len(),
        };
        assert!(start <= end);
        let len = end - start;
        let start = self.range.start() + start;
        let end = start + len;
        assert!(
            start <= end,
            "invalid slice, range: {:?}, slice: {:?}",
            self.range,
            (range.start_bound(), range.end_bound()),
        );
        let range = TextRange::from_to(start, end);
        assert!(
            range.is_subrange(&self.range),
            "invalid slice, range: {:?}, slice: {:?}",
            self.range,
            range,
        );
        SyntaxText { node: self.node.clone(), range }
    }

    pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> {
        let offset = offset.into();
        let mut start: TextUnit = 0.into();
        let res = self.try_for_each_chunk(|chunk| {
            let end = start + TextUnit::of_str(chunk);
            if start <= offset && offset < end {
                let off: usize = u32::from(offset - start) as usize;
                return Err(chunk[off..].chars().next().unwrap());
            }
            start = end;
            Ok(())
        });
        found(res)
    }
}

fn found<T>(res: Result<(), T>) -> Option<T> {
    match res {
        Ok(()) => None,
        Err(it) => Some(it),
    }
}

impl fmt::Debug for SyntaxText {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Debug::fmt(&self.to_string(), f)
    }
}

impl fmt::Display for SyntaxText {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        self.try_for_each_chunk(|chunk| fmt::Display::fmt(chunk, f))
    }
}

impl From<SyntaxText> for String {
    fn from(text: SyntaxText) -> String {
        text.to_string()
    }
}

impl PartialEq<str> for SyntaxText {
    fn eq(&self, mut rhs: &str) -> bool {
        self.try_for_each_chunk(|chunk| {
            if !rhs.starts_with(chunk) {
                return Err(());
            }
            rhs = &rhs[chunk.len()..];
            Ok(())
        })
        .is_ok()
    }
}

impl PartialEq<&'_ str> for SyntaxText {
    fn eq(&self, rhs: &&str) -> bool {
        self == *rhs
    }
}