Extend empty diagnostic ranges at the ends of lines

This commit is contained in:
Max Brunsfeld 2021-11-03 13:49:39 -07:00
parent 9384823e47
commit baf636a4a4
2 changed files with 78 additions and 15 deletions

View file

@ -751,6 +751,10 @@ impl Buffer {
if range.start == range.end { if range.start == range.end {
range.end.column += 1; range.end.column += 1;
range.end = content.clip_point_utf16(range.end, Bias::Right); range.end = content.clip_point_utf16(range.end, Bias::Right);
if range.start == range.end && range.end.column > 0 {
range.start.column -= 1;
range.start = content.clip_point_utf16(range.start, Bias::Left);
}
} }
Some(( Some((
range, range,

View file

@ -633,6 +633,66 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
] ]
); );
}); });
}
#[gpui::test]
async fn test_empty_diagnostic_ranges(mut cx: gpui::TestAppContext) {
cx.add_model(|cx| {
let text = concat!(
"let one = ;\n", //
"let two = \n",
"let three = 3;\n",
);
let mut buffer = Buffer::new(0, text, cx);
buffer.set_language(Some(Arc::new(rust_lang())), None, cx);
buffer
.update_diagnostics(
None,
vec![
lsp::Diagnostic {
range: lsp::Range::new(
lsp::Position::new(0, 10),
lsp::Position::new(0, 10),
),
severity: Some(lsp::DiagnosticSeverity::ERROR),
message: "syntax error 1".to_string(),
..Default::default()
},
lsp::Diagnostic {
range: lsp::Range::new(
lsp::Position::new(1, 10),
lsp::Position::new(1, 10),
),
severity: Some(lsp::DiagnosticSeverity::ERROR),
message: "syntax error 2".to_string(),
..Default::default()
},
],
cx,
)
.unwrap();
// An empty range is extended forward to include the following character.
// At the end of a line, an empty range is extended backward to include
// the preceding character.
let chunks = chunks_with_diagnostics(&buffer, 0..buffer.len());
assert_eq!(
chunks
.iter()
.map(|(s, d)| (s.as_str(), *d))
.collect::<Vec<_>>(),
&[
("let one = ", None),
(";", Some(lsp::DiagnosticSeverity::ERROR)),
("\nlet two =", None),
(" ", Some(lsp::DiagnosticSeverity::ERROR)),
("\nlet three = 3;\n", None)
]
);
buffer
});
}
fn chunks_with_diagnostics<T: ToOffset>( fn chunks_with_diagnostics<T: ToOffset>(
buffer: &Buffer, buffer: &Buffer,
@ -651,7 +711,6 @@ async fn test_diagnostics(mut cx: gpui::TestAppContext) {
} }
chunks chunks
} }
}
#[test] #[test]
fn test_contiguous_ranges() { fn test_contiguous_ranges() {