Skip to content

Commit

Permalink
Introduce a per-regex whitelist option
Browse files Browse the repository at this point in the history
  • Loading branch information
Ekopalypse committed Aug 16, 2023
1 parent 11c64e0 commit fa93042
Show file tree
Hide file tree
Showing 3 changed files with 277 additions and 259 deletions.
198 changes: 103 additions & 95 deletions config/config_reader.v
Original file line number Diff line number Diff line change
@@ -1,95 +1,103 @@
module config
import os

pub struct RegexSetting {
pub mut:
regex string
color int
}

pub struct Lexer {
pub mut:
name string
regexes []RegexSetting
excluded_styles []int
}

pub struct Config {
pub mut:
all map[string]Lexer
}

pub fn read(config_file string) {
content := os.read_file(config_file) or { return }
lines := content.split_into_lines()
mut lexer := Lexer{}
mut setting := RegexSetting{}
p.lexers_to_enhance = Config{}

for line in lines {
mut line_ := line.trim(' ')
if line_.starts_with(';') || line.len < 3 { // the minimum length is something like 5=A
continue
}
else if line_.starts_with('[') {
if lexer.name != '' {
p.lexers_to_enhance.all[lexer.name] = lexer
lexer = Lexer{} // new lexer, resets everything
}
lexer.name = line_.trim("[]").trim(' ').to_lower()
setting = RegexSetting{}
}
else if line_.starts_with('indicator_id') {
indicator_id := line_.split('=')
if indicator_id.len == 2 {
indicator_id_ := indicator_id[1].trim(' ')
p.indicator_id = indicator_id_.int()
}
}
else if line_.starts_with('offset') {
offset := line_.split('=')
if offset.len == 2 {
p.offset = offset[1].trim(' ').int()
}
}
else if line_.starts_with('regex_error_style_id') {
regex_error_style_id := line_.split('=')
if regex_error_style_id.len == 2 {
p.regex_error_style_id = regex_error_style_id[1].trim(' ').int()
}
}
else if line_.starts_with('regex_error_color') {
regex_error_color := line_.split('=')
if regex_error_color.len == 2 {
p.regex_error_color = regex_error_color[1].trim(' ').int()
}
}
else {
if line_.starts_with('excluded_styles') {
excludes := line_.split('=')
if excludes.len == 2 {
ids := excludes[1].split(',')
for id in ids {
trimmed_id := id.trim(' ')
lexer.excluded_styles << trimmed_id.int()
}
}
} else {
// the line starts with a color
split_pos := line_.index('=') or { continue }
if split_pos > 0 {
color__ := line_[0..split_pos].trim(' ')
setting.color = color__.replace('#', '0x').int()
regex := line_[split_pos..].trim_left('=').trim(' ')
if regex.len > 0 {
setting.regex = regex
lexer.regexes << setting
}
}
}
}
}
if lexer.name != '' {
p.lexers_to_enhance.all[lexer.name] = lexer
}
}
module config
import os

pub struct RegexSetting {
pub mut:
regex string
color int
whitelist_styles []int
}

pub struct Lexer {
pub mut:
name string
regexes []RegexSetting
excluded_styles []int
}

pub struct Config {
pub mut:
all map[string]Lexer
}

pub fn read(config_file string) {
content := os.read_file(config_file) or { return }
lines := content.split_into_lines()
mut lexer := Lexer{}
mut setting := RegexSetting{}
p.lexers_to_enhance = Config{}

for line in lines {
mut line_ := line.trim(' ')
if line_.starts_with(';') || line.len < 3 { // the minimum length is something like 5=A
continue
}
else if line_.starts_with('[') {
if lexer.name != '' {
p.lexers_to_enhance.all[lexer.name] = lexer
lexer = Lexer{} // new lexer, resets everything
}
lexer.name = line_.trim("[]").trim(' ').to_lower()
setting = RegexSetting{}
}
else if line_.starts_with('indicator_id') {
indicator_id := line_.split('=')
if indicator_id.len == 2 {
indicator_id_ := indicator_id[1].trim(' ')
p.indicator_id = indicator_id_.int()
}
}
else if line_.starts_with('offset') {
offset := line_.split('=')
if offset.len == 2 {
p.offset = offset[1].trim(' ').int()
}
}
else if line_.starts_with('regex_error_style_id') {
regex_error_style_id := line_.split('=')
if regex_error_style_id.len == 2 {
p.regex_error_style_id = regex_error_style_id[1].trim(' ').int()
}
}
else if line_.starts_with('regex_error_color') {
regex_error_color := line_.split('=')
if regex_error_color.len == 2 {
p.regex_error_color = regex_error_color[1].trim(' ').int()
}
}
else {
if line_.starts_with('excluded_styles') {
excludes := line_.split('=')
if excludes.len == 2 {
ids := excludes[1].split(',')
for id in ids {
trimmed_id := id.trim(' ')
lexer.excluded_styles << trimmed_id.int()
}
}
} else {
// the line starts with a color
split_pos := line_.index('=') or { continue }
if split_pos > 0 {
color__ := line_[0..split_pos].trim(' ')
whitelist_str := color__.find_between('[', ']')
if whitelist_str.len > 0 {
setting.whitelist_styles = whitelist_str.split(",").map(int(it.trim_space().parse_int(10, 32) or { -1 }))
} else {
setting.whitelist_styles.clear()
}

setting.color = color__.replace('#', '0x').int()
regex := line_[split_pos..].trim_left('=').trim(' ')
if regex.len > 0 {
setting.regex = regex
lexer.regexes << setting
}
}
}
}
}
if lexer.name != '' {
p.lexers_to_enhance.all[lexer.name] = lexer
}
}
8 changes: 6 additions & 2 deletions enhance_any_lexer.v
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,12 @@ pub fn create_for_current_language() {
if ! lexer_already_defined {
tmpl := '
[${current_language}]
; color each word, 0x66ad1 is the color used, see the description above for more information on the color coding.
0x66ad1 = \\w+
; Each 3-digit number is styled with the color 0xff0050.
; Matches styled by IDs 3 and 6 are recolored.
0xff0050[3, 6] = \\d{3}
; Each word "test" is styled with the color 0x00bb00
; but only if the matches are not already styled by one of the IDs in excluded_styles.
0x00bb00 = test
; check in the respective styler xml if the following IDs are valid
excluded_styles = 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20,21,22,23
'
Expand Down
Loading

0 comments on commit fa93042

Please sign in to comment.