diff: Add an option to control syntax aliases
This commit is contained in:
parent
61e1b74434
commit
9ff7eae1af
@ -38,7 +38,18 @@ def set_formats(opts):
|
|||||||
formats['added_highlight'] = '48' + color_as_sgr(opts.highlight_added_bg)
|
formats['added_highlight'] = '48' + color_as_sgr(opts.highlight_added_bg)
|
||||||
|
|
||||||
|
|
||||||
type_map = {}
|
def syntax_aliases(raw):
|
||||||
|
ans = {}
|
||||||
|
for x in raw.split():
|
||||||
|
a, b = x.partition(':')[::2]
|
||||||
|
if a and b:
|
||||||
|
ans[a.lower()] = b
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
|
type_map = {
|
||||||
|
'syntax_aliases': syntax_aliases,
|
||||||
|
}
|
||||||
|
|
||||||
for name in (
|
for name in (
|
||||||
'foreground background title_fg title_bg margin_bg margin_fg removed_bg removed_margin_bg added_bg added_margin_bg filler_bg hunk_bg hunk_margin_bg'
|
'foreground background title_fg title_bg margin_bg margin_fg removed_bg removed_margin_bg added_bg added_margin_bg filler_bg hunk_bg hunk_margin_bg'
|
||||||
|
|||||||
@ -1,3 +1,11 @@
|
|||||||
|
# vim:fileencoding=utf-8:ft=conf
|
||||||
|
|
||||||
|
# File extension aliases for syntax highlight
|
||||||
|
# For example, to syntax highlight file.xyz as
|
||||||
|
# file.abc use a setting of xyz:abc
|
||||||
|
syntax_aliases pyj:py recipe:py
|
||||||
|
|
||||||
|
# Colors
|
||||||
foreground black
|
foreground black
|
||||||
background white
|
background white
|
||||||
title_fg black
|
title_fg black
|
||||||
|
|||||||
@ -73,10 +73,12 @@ def initialize_highlighter(style='default'):
|
|||||||
formatter = DiffFormatter(style)
|
formatter = DiffFormatter(style)
|
||||||
|
|
||||||
|
|
||||||
def highlight_data(code, filename):
|
def highlight_data(code, filename, aliases=None):
|
||||||
base, ext = os.path.splitext(filename)
|
if aliases:
|
||||||
if ext.lower() == '.pyj':
|
base, ext = os.path.splitext(filename)
|
||||||
filename = base + '.py'
|
alias = aliases.get(ext[1:])
|
||||||
|
if alias is not None:
|
||||||
|
filename = base + '.' + alias
|
||||||
try:
|
try:
|
||||||
lexer = get_lexer_for_filename(filename, stripnl=False)
|
lexer = get_lexer_for_filename(filename, stripnl=False)
|
||||||
except ClassNotFound:
|
except ClassNotFound:
|
||||||
@ -106,17 +108,17 @@ def highlight_line(line):
|
|||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def highlight_for_diff(path):
|
def highlight_for_diff(path, aliases):
|
||||||
ans = []
|
ans = []
|
||||||
lines = lines_for_path(path)
|
lines = lines_for_path(path)
|
||||||
hd = highlight_data('\n'.join(lines), path)
|
hd = highlight_data('\n'.join(lines), path, aliases)
|
||||||
if hd is not None:
|
if hd is not None:
|
||||||
for line in hd.splitlines():
|
for line in hd.splitlines():
|
||||||
ans.append(highlight_line(line))
|
ans.append(highlight_line(line))
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def highlight_collection(collection):
|
def highlight_collection(collection, aliases=None):
|
||||||
jobs = {}
|
jobs = {}
|
||||||
ans = {}
|
ans = {}
|
||||||
with concurrent.futures.ProcessPoolExecutor(max_workers=os.cpu_count()) as executor:
|
with concurrent.futures.ProcessPoolExecutor(max_workers=os.cpu_count()) as executor:
|
||||||
@ -126,7 +128,7 @@ def highlight_collection(collection):
|
|||||||
if p:
|
if p:
|
||||||
is_binary = isinstance(data_for_path(p), bytes)
|
is_binary = isinstance(data_for_path(p), bytes)
|
||||||
if not is_binary:
|
if not is_binary:
|
||||||
jobs[executor.submit(highlight_for_diff, p)] = p
|
jobs[executor.submit(highlight_for_diff, p, aliases)] = p
|
||||||
for future in concurrent.futures.as_completed(jobs):
|
for future in concurrent.futures.as_completed(jobs):
|
||||||
path = jobs[future]
|
path = jobs[future]
|
||||||
try:
|
try:
|
||||||
@ -138,11 +140,12 @@ def highlight_collection(collection):
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
from .config import defaults
|
||||||
# kitty +runpy "from kittens.diff.highlight import main; main()" file
|
# kitty +runpy "from kittens.diff.highlight import main; main()" file
|
||||||
import sys
|
import sys
|
||||||
initialize_highlighter()
|
initialize_highlighter()
|
||||||
with open(sys.argv[-1]) as f:
|
with open(sys.argv[-1]) as f:
|
||||||
highlighted = highlight_data(f.read(), f.name)
|
highlighted = highlight_data(f.read(), f.name, defaults.syntax_aliases)
|
||||||
if highlighted is None:
|
if highlighted is None:
|
||||||
raise SystemExit('Unknown filetype: {}'.format(sys.argv[-1]))
|
raise SystemExit('Unknown filetype: {}'.format(sys.argv[-1]))
|
||||||
print(highlighted)
|
print(highlighted)
|
||||||
|
|||||||
@ -243,7 +243,7 @@ class DiffHandler(Handler):
|
|||||||
if initialize_highlighter is not None and not self.highlighting_done:
|
if initialize_highlighter is not None and not self.highlighting_done:
|
||||||
self.highlighting_done = True
|
self.highlighting_done = True
|
||||||
initialize_highlighter()
|
initialize_highlighter()
|
||||||
self.start_job('highlight', highlight_collection, self.collection)
|
self.start_job('highlight', highlight_collection, self.collection, self.opts.syntax_aliases)
|
||||||
elif job_id == 'highlight':
|
elif job_id == 'highlight':
|
||||||
hdata = job_result['result']
|
hdata = job_result['result']
|
||||||
if isinstance(hdata, str):
|
if isinstance(hdata, str):
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user