diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-14 20:58:20 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-14 20:58:20 +0000 |
commit | e8e82ce032f8678929b015e6f70ac060bb2cf94c (patch) | |
tree | 9fb158e9f7115bb70cf2b8623b70710c55497ed4 /crates/ra_syntax/src/yellow/syntax_text.rs | |
parent | 784ff638e549a27503b719e5c2f0009b40d25364 (diff) | |
parent | 37ba237e6686d94783d1f025d23823ad7c0cb0c8 (diff) |
Merge #485
485: Add type inference for a bunch of primitives r=flodiebold a=marcusklaas
This PR adds inference for `&str`, `&[u8]`, `char`, `bool`, floats and integers. For floats and integers it uses type variables to infer the exact type, i.e. `u32`, from context when it's not annotated explicitly.
I'm not quite happy with the implementation yet, but I think it mostly works now.
Co-authored-by: Marcus Klaas de Vries <[email protected]>
Diffstat (limited to 'crates/ra_syntax/src/yellow/syntax_text.rs')
-rw-r--r-- | crates/ra_syntax/src/yellow/syntax_text.rs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/crates/ra_syntax/src/yellow/syntax_text.rs b/crates/ra_syntax/src/yellow/syntax_text.rs index 08dbe57a2..378cd1b2e 100644 --- a/crates/ra_syntax/src/yellow/syntax_text.rs +++ b/crates/ra_syntax/src/yellow/syntax_text.rs | |||
@@ -15,6 +15,7 @@ impl<'a> SyntaxText<'a> { | |||
15 | range: node.range(), | 15 | range: node.range(), |
16 | } | 16 | } |
17 | } | 17 | } |
18 | |||
18 | pub fn chunks(&self) -> impl Iterator<Item = &'a str> { | 19 | pub fn chunks(&self) -> impl Iterator<Item = &'a str> { |
19 | let range = self.range; | 20 | let range = self.range; |
20 | self.node.descendants().filter_map(move |node| { | 21 | self.node.descendants().filter_map(move |node| { |
@@ -24,15 +25,19 @@ impl<'a> SyntaxText<'a> { | |||
24 | Some(&text[range]) | 25 | Some(&text[range]) |
25 | }) | 26 | }) |
26 | } | 27 | } |
28 | |||
27 | pub fn push_to(&self, buf: &mut String) { | 29 | pub fn push_to(&self, buf: &mut String) { |
28 | self.chunks().for_each(|it| buf.push_str(it)); | 30 | self.chunks().for_each(|it| buf.push_str(it)); |
29 | } | 31 | } |
32 | |||
30 | pub fn to_string(&self) -> String { | 33 | pub fn to_string(&self) -> String { |
31 | self.chunks().collect() | 34 | self.chunks().collect() |
32 | } | 35 | } |
36 | |||
33 | pub fn contains(&self, c: char) -> bool { | 37 | pub fn contains(&self, c: char) -> bool { |
34 | self.chunks().any(|it| it.contains(c)) | 38 | self.chunks().any(|it| it.contains(c)) |
35 | } | 39 | } |
40 | |||
36 | pub fn find(&self, c: char) -> Option<TextUnit> { | 41 | pub fn find(&self, c: char) -> Option<TextUnit> { |
37 | let mut acc: TextUnit = 0.into(); | 42 | let mut acc: TextUnit = 0.into(); |
38 | for chunk in self.chunks() { | 43 | for chunk in self.chunks() { |
@@ -44,9 +49,11 @@ impl<'a> SyntaxText<'a> { | |||
44 | } | 49 | } |
45 | None | 50 | None |
46 | } | 51 | } |
52 | |||
47 | pub fn len(&self) -> TextUnit { | 53 | pub fn len(&self) -> TextUnit { |
48 | self.range.len() | 54 | self.range.len() |
49 | } | 55 | } |
56 | |||
50 | pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> { | 57 | pub fn slice(&self, range: impl SyntaxTextSlice) -> SyntaxText<'a> { |
51 | let range = range.restrict(self.range).unwrap_or_else(|| { | 58 | let range = range.restrict(self.range).unwrap_or_else(|| { |
52 | panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range) | 59 | panic!("invalid slice, range: {:?}, slice: {:?}", self.range, range) |
@@ -56,8 +63,10 @@ impl<'a> SyntaxText<'a> { | |||
56 | range, | 63 | range, |
57 | } | 64 | } |
58 | } | 65 | } |
59 | pub fn char_at(&self, offset: TextUnit) -> Option<char> { | 66 | |
67 | pub fn char_at(&self, offset: impl Into<TextUnit>) -> Option<char> { | ||
60 | let mut start: TextUnit = 0.into(); | 68 | let mut start: TextUnit = 0.into(); |
69 | let offset = offset.into(); | ||
61 | for chunk in self.chunks() { | 70 | for chunk in self.chunks() { |
62 | let end = start + TextUnit::of_str(chunk); | 71 | let end = start + TextUnit::of_str(chunk); |
63 | if start <= offset && offset < end { | 72 | if start <= offset && offset < end { |