aboutsummaryrefslogtreecommitdiff
path: root/lua/lexers/diff.lua
blob: 4c87dc280cb945bd48b07c4f80bf6f42d5cdf6f7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
-- Copyright 2006-2022 Mitchell. See LICENSE.
-- Diff LPeg lexer.

local lexer = require('lexer')
local token, word_match = lexer.token, lexer.word_match
local P, S = lpeg.P, lpeg.S

local lex = lexer.new('diff', {lex_by_line = true})

-- Text, separators, and file headers.
lex:add_rule('index', token(lexer.COMMENT, 'Index: ' * lexer.any^0 * -1))
lex:add_rule('separator', token(lexer.COMMENT, ('---' + P('*')^4 + P('=')^1) * lexer.space^0 * -1))
lex:add_rule('header', token('header', (P('*** ') + '--- ' + '+++ ') * lexer.any^1))
lex:add_style('header', lexer.styles.comment)

-- Location.
lex:add_rule('location', token(lexer.NUMBER, ('@@' + lexer.dec_num + '****') * lexer.any^1))

-- Additions, deletions, and changes.
lex:add_rule('addition', token('addition', S('>+') * lexer.any^0))
lex:add_style('addition', {fore = lexer.colors.green})
lex:add_rule('deletion', token('deletion', S('<-') * lexer.any^0))
lex:add_style('deletion', {fore = lexer.colors.red})
lex:add_rule('change', token('change', '!' * lexer.any^0))
lex:add_style('change', {fore = lexer.colors.yellow})

lex:add_rule('any_line', token(lexer.DEFAULT, lexer.any^1))

return lex