Skip to content

Commit fda8ddc

Browse files
committed
Introduce Location and make SyntaxError fields private
1 parent 3b42dda commit fda8ddc

File tree

10 files changed

+82
-52
lines changed

10 files changed

+82
-52
lines changed

crates/ra_editor/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use ra_syntax::{
3131
algo::find_leaf_at_offset,
3232
ast::{self, AstNode, NameOwner},
3333
File,
34+
Location,
3435
SyntaxKind::{self, *},
3536
SyntaxNodeRef, TextRange, TextUnit,
3637
};
@@ -100,11 +101,18 @@ pub fn highlight(file: &File) -> Vec<HighlightedRange> {
100101
}
101102

102103
pub fn diagnostics(file: &File) -> Vec<Diagnostic> {
104+
fn location_to_range(location: Location) -> TextRange {
105+
match location {
106+
Location::Offset(offset) => TextRange::offset_len(offset, 1.into()),
107+
Location::Range(range) => range,
108+
}
109+
}
110+
103111
file.errors()
104112
.into_iter()
105113
.map(|err| Diagnostic {
106-
range: err.range,
107-
msg: format!("Syntax Error: {}", err.kind),
114+
range: location_to_range(err.location()),
115+
msg: format!("Syntax Error: {}", err),
108116
})
109117
.collect()
110118
}

crates/ra_syntax/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub use crate::{
5454
rowan::{SmolStr, TextRange, TextUnit},
5555
syntax_kinds::SyntaxKind,
5656
yellow::{
57-
Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent,
57+
Direction, OwnedRoot, RefRoot, SyntaxError, SyntaxNode, SyntaxNodeRef, TreeRoot, WalkEvent, Location,
5858
},
5959
};
6060

crates/ra_syntax/src/parser_impl/event.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
TextRange, TextUnit,
1616
yellow::syntax_error::{
1717
ParseError,
18+
SyntaxError,
1819
SyntaxErrorKind,
1920
},
2021
};
@@ -162,8 +163,7 @@ impl<'a, S: Sink> EventProcessor<'a, S> {
162163
self.leaf(kind, len, n_raw_tokens);
163164
}
164165
Event::Error { msg } => self.sink.error(
165-
SyntaxErrorKind::ParseError(msg),
166-
TextRange::offset_len(self.text_pos, 1.into()),
166+
SyntaxError::new(SyntaxErrorKind::ParseError(msg), self.text_pos),
167167
),
168168
}
169169
}

crates/ra_syntax/src/parser_impl/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ use crate::{
1010
event::{Event, EventProcessor},
1111
input::{InputPosition, ParserInput},
1212
},
13-
SmolStr, TextRange,
13+
SmolStr,
1414
yellow::syntax_error::{
1515
ParseError,
16-
SyntaxErrorKind,
16+
SyntaxError,
1717
},
1818
};
1919

@@ -25,7 +25,7 @@ pub(crate) trait Sink {
2525
fn leaf(&mut self, kind: SyntaxKind, text: SmolStr);
2626
fn start_internal(&mut self, kind: SyntaxKind);
2727
fn finish_internal(&mut self);
28-
fn error(&mut self, kind: SyntaxErrorKind, offset: TextRange);
28+
fn error(&mut self, error: SyntaxError);
2929
fn finish(self) -> Self::Tree;
3030
}
3131

crates/ra_syntax/src/reparsing.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -165,20 +165,14 @@ fn merge_errors(
165165
) -> Vec<SyntaxError> {
166166
let mut res = Vec::new();
167167
for e in old_errors {
168-
if e.range.start() <= old_node.range().start() {
168+
if e.offset() <= old_node.range().start() {
169169
res.push(e)
170-
} else if e.range.start() >= old_node.range().end() {
171-
res.push(SyntaxError {
172-
kind: e.kind,
173-
range: e.range + TextUnit::of_str(&edit.insert) - edit.delete.len(),
174-
})
170+
} else if e.offset() >= old_node.range().end() {
171+
res.push(e.add_offset(TextUnit::of_str(&edit.insert) - edit.delete.len()));
175172
}
176173
}
177174
for e in new_errors {
178-
res.push(SyntaxError {
179-
kind: e.kind,
180-
range: e.range + old_node.range().start(),
181-
})
175+
res.push(e.add_offset(old_node.range().start()));
182176
}
183177
res
184178
}

crates/ra_syntax/src/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt::Write;
44
/// Parse a file and create a string representation of the resulting parse tree.
55
pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
66
let mut errors: Vec<_> = syntax.root_data().to_vec();
7-
errors.sort_by_key(|e| e.range.start());
7+
errors.sort_by_key(|e| e.offset());
88
let mut err_pos = 0;
99
let mut level = 0;
1010
let mut buf = String::new();
@@ -23,9 +23,9 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
2323
writeln!(buf, "{:?}", node).unwrap();
2424
if node.first_child().is_none() {
2525
let off = node.range().end();
26-
while err_pos < errors.len() && errors[err_pos].range.start() <= off {
26+
while err_pos < errors.len() && errors[err_pos].offset() <= off {
2727
indent!();
28-
writeln!(buf, "err: `{}`", errors[err_pos].kind).unwrap();
28+
writeln!(buf, "err: `{}`", errors[err_pos]).unwrap();
2929
err_pos += 1;
3030
}
3131
}
@@ -37,7 +37,7 @@ pub fn dump_tree(syntax: SyntaxNodeRef) -> String {
3737

3838
assert_eq!(level, 0);
3939
for err in errors[err_pos..].iter() {
40-
writeln!(buf, "err: `{}`", err.kind).unwrap();
40+
writeln!(buf, "err: `{}`", err).unwrap();
4141
}
4242

4343
buf

crates/ra_syntax/src/validation.rs

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,11 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) {
3333
AsciiEscape => {
3434
if text.len() == 1 {
3535
// Escape sequence consists only of leading `\`
36-
errors.push(SyntaxError {
37-
kind: EmptyAsciiEscape,
38-
range: range,
39-
});
36+
errors.push(SyntaxError::new(EmptyAsciiEscape, range));
4037
} else {
4138
let escape_code = text.chars().skip(1).next().unwrap();
4239
if !is_ascii_escape(escape_code) {
43-
errors.push(SyntaxError {
44-
kind: InvalidAsciiEscape,
45-
range: range,
46-
});
40+
errors.push(SyntaxError::new(InvalidAsciiEscape, range));
4741
}
4842
}
4943
}
@@ -64,24 +58,15 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) {
6458
}
6559

6660
if !components.has_closing_quote {
67-
errors.push(SyntaxError {
68-
kind: UnclosedChar,
69-
range: node.syntax().range(),
70-
});
61+
errors.push(SyntaxError::new(UnclosedChar, node.syntax().range()));
7162
}
7263

7364
if len == 0 {
74-
errors.push(SyntaxError {
75-
kind: EmptyChar,
76-
range: node.syntax().range(),
77-
});
65+
errors.push(SyntaxError::new(EmptyChar, node.syntax().range()));
7866
}
7967

8068
if len > 1 {
81-
errors.push(SyntaxError {
82-
kind: LongChar,
83-
range: node.syntax().range(),
84-
});
69+
errors.push(SyntaxError::new(LongChar, node.syntax().range()));
8570
}
8671
}
8772

crates/ra_syntax/src/yellow/builder.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::{
22
parser_impl::Sink,
3-
yellow::{GreenNode, RaTypes, SyntaxError, SyntaxErrorKind},
4-
SmolStr, SyntaxKind, TextRange,
3+
yellow::{GreenNode, RaTypes, SyntaxError},
4+
SmolStr, SyntaxKind,
55
};
66
use rowan::GreenNodeBuilder;
77

@@ -34,8 +34,7 @@ impl Sink for GreenBuilder {
3434
self.inner.finish_internal();
3535
}
3636

37-
fn error(&mut self, kind: SyntaxErrorKind, range: TextRange) {
38-
let error = SyntaxError { kind, range };
37+
fn error(&mut self, error: SyntaxError) {
3938
self.errors.push(error)
4039
}
4140

crates/ra_syntax/src/yellow/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::{
1111
};
1212

1313
pub(crate) use self::builder::GreenBuilder;
14-
pub use self::syntax_error::{SyntaxError, SyntaxErrorKind};
14+
pub use self::syntax_error::{SyntaxError, SyntaxErrorKind, Location};
1515
pub use rowan::{TreeRoot, WalkEvent};
1616

1717
#[derive(Debug, Clone, Copy)]

crates/ra_syntax/src/yellow/syntax_error.rs

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,60 @@
11
use std::fmt;
22

3-
use crate::TextRange;
3+
use crate::{TextRange, TextUnit};
44

55
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
66
pub struct SyntaxError {
7-
pub kind: SyntaxErrorKind,
8-
pub range: TextRange,
7+
kind: SyntaxErrorKind,
8+
location: Location,
9+
}
10+
11+
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12+
pub enum Location {
13+
Offset(TextUnit),
14+
Range(TextRange),
15+
}
16+
17+
impl Into<Location> for TextUnit {
18+
fn into(self) -> Location {
19+
Location::Offset(self)
20+
}
21+
}
22+
23+
impl Into<Location> for TextRange {
24+
fn into(self) -> Location {
25+
Location::Range(self)
26+
}
927
}
1028

1129
impl SyntaxError {
12-
pub fn new(kind: SyntaxErrorKind, range: TextRange) -> SyntaxError {
13-
SyntaxError { kind, range }
30+
pub fn new<L: Into<Location>>(kind: SyntaxErrorKind, loc: L) -> SyntaxError {
31+
SyntaxError { kind, location: loc.into() }
32+
}
33+
34+
pub fn location(&self) -> Location {
35+
self.location.clone()
36+
}
37+
38+
pub fn offset(&self) -> TextUnit {
39+
match self.location {
40+
Location::Offset(offset) => offset,
41+
Location::Range(range) => range.start(),
42+
}
43+
}
44+
45+
pub fn add_offset(mut self, plus_offset: TextUnit) -> SyntaxError {
46+
self.location = match self.location {
47+
Location::Range(range) => Location::Range(range + plus_offset),
48+
Location::Offset(offset) => Location::Offset(offset + plus_offset)
49+
};
50+
51+
self
52+
}
53+
}
54+
55+
impl fmt::Display for SyntaxError {
56+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
57+
self.kind.fmt(f)
1458
}
1559
}
1660

0 commit comments

Comments
 (0)