DiffViewer: lines color by their file's syntax; prose reads plain (Ely R2.12)
This commit is contained in:
6 files changed
+138
-38
No files matched your search
+16
-2
@@ -44,6 +44,11 @@ const CONSTANTS: &[&str] = &[
|
||||
|
||||
/// Splits source into spans; spaces fall between them.
|
||||
pub(crate) fn lex(source: &str) -> Vec<(Range<usize>, Kind)> {
|
||||
lex_with(source, "//")
|
||||
}
|
||||
|
||||
/// Splits source whose line comments start with `comment`.
|
||||
pub(crate) fn lex_with(source: &str, comment: &str) -> Vec<(Range<usize>, Kind)> {
|
||||
let mut out = Vec::new();
|
||||
let mut chars = source.char_indices().peekable();
|
||||
while let Some((start, ch)) = chars.next() {
|
||||
@@ -61,7 +66,7 @@ pub(crate) fn lex(source: &str) -> Vec<(Range<usize>, Kind)> {
|
||||
}
|
||||
Kind::Text
|
||||
}
|
||||
'/' if chars.peek().is_some_and(|(_, next)| *next == '/') => {
|
||||
_ if source[start..].starts_with(comment) => {
|
||||
while chars.next_if(|(_, next)| *next != '\n').is_some() {}
|
||||
Kind::Comment
|
||||
}
|
||||
@@ -96,8 +101,17 @@ fn painted(spans: impl Iterator<Item = (Range<usize>, Hsla)>) -> Vec<(Range<usiz
|
||||
|
||||
/// Colors for one line of code in most languages, from the syntax palette.
|
||||
pub fn code_highlights(source: &str, cx: &App) -> Vec<(Range<usize>, Highlight)> {
|
||||
code_highlights_with(source, "//", cx)
|
||||
}
|
||||
|
||||
/// Colors for code whose line comments start with `comment`.
|
||||
pub(crate) fn code_highlights_with(
|
||||
source: &str,
|
||||
comment: &str,
|
||||
cx: &App,
|
||||
) -> Vec<(Range<usize>, Highlight)> {
|
||||
let syntax = &cx.theme().colors.syntax;
|
||||
let spans = lex(source);
|
||||
let spans = lex_with(source, comment);
|
||||
let next_is = |ix: usize, mark: &str| {
|
||||
spans
|
||||
.get(ix + 1)
|
||||
|
||||
+1
-1
@@ -55,7 +55,7 @@ pub use check::{CheckState, Checkbox, CheckboxGroup};
|
||||
pub use checked::{EmailInput, UrlInput, is_email, is_url};
|
||||
pub use chips::ChoiceChips;
|
||||
pub use code::{CodeInput, JsonInput, code_highlights, json_highlights};
|
||||
pub(crate) use code::{Kind, lex};
|
||||
pub(crate) use code::{Kind, code_highlights_with, lex};
|
||||
pub use color::{ColorPalette, ColorPicker, ColorSwatch, EyeDropper, GradientEditor, GradientStop};
|
||||
pub(crate) use color::{hex, parse_hex};
|
||||
pub use combobox::Combobox;
|
||||
|
||||
Reference in new issue
Block a user