Module: Rvim::Syntax
- Defined in:
- lib/rvim/syntax.rb
Constant Summary collapse
- COLORS =
{ red: "\e[31m", green: "\e[32m", yellow: "\e[33m", blue: "\e[34m", magenta: "\e[35m", cyan: "\e[36m", white: "\e[37m", default: "\e[39m", }.freeze
- RESET =
"\e[39m"
Class Method Summary collapse
-
.coalesce(segments) ⇒ Object
Drop overlapping segments, keeping the earliest-starting one.
- .detect_language(filepath) ⇒ Object
-
.highlight(line, lang) ⇒ Object
Returns Array of [byte_start, byte_end, color_symbol].
- .register(lang, tokens) ⇒ Object
- .tokens_for(lang) ⇒ Object
Class Method Details
.coalesce(segments) ⇒ Object
Drop overlapping segments, keeping the earliest-starting one. Tokens registered first dominate later ones at the same starting byte because Ruby’s stable sort preserves insertion order on tie.
52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rvim/syntax.rb', line 52 def self.coalesce(segments) sorted = segments.sort_by { |s, _e, _c| s } kept = [] last_end = -1 sorted.each do |s, e, c| next if s <= last_end kept << [s, e, c] last_end = e end kept end |
.detect_language(filepath) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rvim/syntax.rb', line 65 def self.detect_language(filepath) return nil unless filepath case File.extname(filepath) when '.rb', '.gemspec', '.rake' then :ruby when '.md', '.markdown' then :markdown when '.json' then :json when '.sh', '.bash', '.zsh', '.ksh' then :shell when '.py', '.pyw' then :python when '.js', '.mjs', '.cjs', '.jsx', '.ts', '.tsx' then :javascript when '.yml', '.yaml' then :yaml end end |
.highlight(line, lang) ⇒ Object
Returns Array of [byte_start, byte_end, color_symbol]. byte_end is inclusive (matches our existing highlight conventions).
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rvim/syntax.rb', line 29 def self.highlight(line, lang) table = @tokens[lang] return [] unless table segments = [] table.each do |tok| offset = 0 pattern = tok[:pattern] while (m = pattern.match(line, offset)) b = m.pre_match.bytesize e = b + m[0].bytesize break if e == b # zero-width safety segments << [b, e - 1, tok[:color]] offset = m.end(0) end end coalesce(segments) end |
.register(lang, tokens) ⇒ Object
19 20 21 |
# File 'lib/rvim/syntax.rb', line 19 def self.register(lang, tokens) @tokens[lang] = tokens end |
.tokens_for(lang) ⇒ Object
23 24 25 |
# File 'lib/rvim/syntax.rb', line 23 def self.tokens_for(lang) @tokens[lang] end |