Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add syntax config for single line string quotes; JSON syntax #46

Merged
merged 2 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ Syntax highlighting can be configured using INI files which follow this format:
name=Rust
extensions=rs
highlight_numbers=true
highlight_strings=true
singleline_string_quotes=", '
singleline_comment_start=//
multiline_comment_delims=/*, */
; In Rust, the multi-line string delimiter is the same as the single-line string delimiter
Expand Down
23 changes: 10 additions & 13 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,18 @@ impl Row {
let c = line[i];

// At this point, hl_state is Normal or String

if syntax.hightlight_sl_strings {
if let HLState::String(quote) = hl_state {
self.hl.push(HLType::String);
if c == quote {
hl_state = HLState::Normal;
} else if c == b'\\' && i != line.len() - 1 {
self.hl.push(HLType::String);
}
continue;
} else if c == b'"' || c == b'\'' {
hl_state = HLState::String(c);
if let HLState::String(quote) = hl_state {
self.hl.push(HLType::String);
if c == quote {
hl_state = HLState::Normal;
} else if c == b'\\' && i != line.len() - 1 {
self.hl.push(HLType::String);
continue;
}
continue;
} else if syntax.sl_string_quotes.contains(&(c as char)) {
hl_state = HLState::String(c);
self.hl.push(HLType::String);
continue;
}

let prev_sep = (i == 0) || is_sep(line[i - 1]);
Expand Down
28 changes: 25 additions & 3 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ pub struct Conf {
pub name: String,
/// Whether to highlight numbers.
pub highlight_numbers: bool,
/// Whether to highlight single-line strings.
pub hightlight_sl_strings: bool,
/// Quotes for single-line strings.
pub sl_string_quotes: Vec<char>,
/// The tokens that starts a single-line comment, e.g. "//".
pub sl_comment_start: Vec<String>,
/// The tokens that start and end a multi-line comment, e.g. ("/*", "*/").
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Conf {
"name" => sc.name = pv(val)?,
"extensions" => extensions.extend(val.split(',').map(|u| String::from(u.trim()))),
"highlight_numbers" => sc.highlight_numbers = pv(val)?,
"highlight_strings" => sc.hightlight_sl_strings = pv(val)?,
"singleline_string_quotes" => sc.sl_string_quotes = pvs(val)?,
"singleline_comment_start" => sc.sl_comment_start = pvs(val)?,
"multiline_comment_delims" =>
sc.ml_comment_delims = match &val.split(',').collect::<Vec<_>>()[..] {
Expand All @@ -91,3 +91,25 @@ impl Conf {
Ok((sc, extensions))
}
}

#[cfg(test)]
mod tests {
use std::fs;

use super::*;
use std::collections::HashSet;

#[test]
fn syntax_d_files() {
let mut file_count = 0;
let mut syntax_names = HashSet::new();
for path in fs::read_dir("./syntax.d").unwrap() {
let (conf, extensions) = Conf::from_file(&path.unwrap().path()).unwrap();
assert!(!extensions.is_empty());
syntax_names.insert(conf.name);
file_count += 1;
}
assert!(file_count > 0);
assert_eq!(file_count, syntax_names.len());
}
}
2 changes: 1 addition & 1 deletion syntax.d/bash.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name=Bash
extensions=bash, sh
singleline_comment_start=#
highlight_strings=true
singleline_string_quotes=", '
# https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html
keywords_1=:, ., break, cd, continue, eval, exec, exit, export, getopts, hash, pwd, readonly, return, shift, test, [, times, trap, umask, unset
# https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html
Expand Down
5 changes: 5 additions & 0 deletions syntax.d/json.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name=json
extensions=json
highlight_numbers=true
singleline_string_quotes="
keywords_1=true,false,null
2 changes: 1 addition & 1 deletion syntax.d/python.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name=Python
extensions=py, pyi
highlight_numbers=true
highlight_strings=true
singleline_string_quotes=", '
singleline_comment_start=#
multiline_string_delim="""
; https://github.com/python/cpython/blob/3.8/Lib/keyword.py
Expand Down
2 changes: 1 addition & 1 deletion syntax.d/rust.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name=Rust
extensions=rs
highlight_numbers=true
highlight_strings=true
singleline_string_quotes=", '
singleline_comment_start=//
multiline_comment_delims=/*, */
; In Rust, the multi-line string delimiter is the same as the single-line string delimiter
Expand Down
2 changes: 1 addition & 1 deletion syntax.d/toml.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name=TOML
extensions=toml
highlight_numbers=true
highlight_strings=true
singleline_string_quotes=", '
singleline_comment_start=#
multiline_string_delim="""
keywords_1=true, false