mirror of
https://github.com/kovidgoyal/kitty.git
synced 2024-09-11 22:47:45 +03:00
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)
|
||||
|
||||
|
||||
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 (
|
||||
'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
|
||||
background white
|
||||
title_fg black
|
||||
|
@ -73,10 +73,12 @@ def initialize_highlighter(style='default'):
|
||||
formatter = DiffFormatter(style)
|
||||
|
||||
|
||||
def highlight_data(code, filename):
|
||||
base, ext = os.path.splitext(filename)
|
||||
if ext.lower() == '.pyj':
|
||||
filename = base + '.py'
|
||||
def highlight_data(code, filename, aliases=None):
|
||||
if aliases:
|
||||
base, ext = os.path.splitext(filename)
|
||||
alias = aliases.get(ext[1:])
|
||||
if alias is not None:
|
||||
filename = base + '.' + alias
|
||||
try:
|
||||
lexer = get_lexer_for_filename(filename, stripnl=False)
|
||||
except ClassNotFound:
|
||||
@ -106,17 +108,17 @@ def highlight_line(line):
|
||||
return ans
|
||||
|
||||
|
||||
def highlight_for_diff(path):
|
||||
def highlight_for_diff(path, aliases):
|
||||
ans = []
|
||||
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:
|
||||
for line in hd.splitlines():
|
||||
ans.append(highlight_line(line))
|
||||
return ans
|
||||
|
||||
|
||||
def highlight_collection(collection):
|
||||
def highlight_collection(collection, aliases=None):
|
||||
jobs = {}
|
||||
ans = {}
|
||||
with concurrent.futures.ProcessPoolExecutor(max_workers=os.cpu_count()) as executor:
|
||||
@ -126,7 +128,7 @@ def highlight_collection(collection):
|
||||
if p:
|
||||
is_binary = isinstance(data_for_path(p), bytes)
|
||||
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):
|
||||
path = jobs[future]
|
||||
try:
|
||||
@ -138,11 +140,12 @@ def highlight_collection(collection):
|
||||
|
||||
|
||||
def main():
|
||||
from .config import defaults
|
||||
# kitty +runpy "from kittens.diff.highlight import main; main()" file
|
||||
import sys
|
||||
initialize_highlighter()
|
||||
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:
|
||||
raise SystemExit('Unknown filetype: {}'.format(sys.argv[-1]))
|
||||
print(highlighted)
|
||||
|
@ -243,7 +243,7 @@ def on_job_done(self, job_id, job_result):
|
||||
if initialize_highlighter is not None and not self.highlighting_done:
|
||||
self.highlighting_done = True
|
||||
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':
|
||||
hdata = job_result['result']
|
||||
if isinstance(hdata, str):
|
||||
|
Loading…
Reference in New Issue
Block a user