From 1c94b9b701ca031da3544dd04f4fc64bfd7f284c Mon Sep 17 00:00:00 2001 From: mechatroner Date: Mon, 13 Jul 2020 00:21:48 -0400 Subject: [PATCH] use pregenerated syntax files when possible --- auto_syntax.py | 7 +++++++ main.py | 25 +++++++++++++++++++------ other/make_sublime_syntax.py | 11 ++--------- 3 files changed, 28 insertions(+), 15 deletions(-) diff --git a/auto_syntax.py b/auto_syntax.py index 80fb28a..25e583a 100755 --- a/auto_syntax.py +++ b/auto_syntax.py @@ -1,4 +1,5 @@ import binascii +import re legacy_syntax_names = { @@ -165,3 +166,9 @@ def make_sublime_syntax(delim, policy): else: return make_sublime_syntax_simple(delim) + +def get_pregenerated_delims(): + delims = [chr(i) for i in range(32, 127)] + delims.append('\t') + delims = [delim for delim in delims if re.match('^[a-zA-Z0-9]$', delim) is None] + return delims diff --git a/main.py b/main.py index 52c4016..4110118 100644 --- a/main.py +++ b/main.py @@ -52,19 +52,25 @@ def get_table_names_path(): def ensure_syntax_file(delim, policy): + pregenerated_delims = auto_syntax.get_pregenerated_delims() name = auto_syntax.get_syntax_file_basename(delim, policy) + if policy == 'simple' and delim in pregenerated_delims: + return (name, True, False) + if policy == 'quoted' and delim in [';', ',']: + return (name, True, False) + syntax_path = os.path.join(sublime.packages_path(), 'User', name) syntax_text = auto_syntax.make_sublime_syntax(delim, policy).encode('utf-8') try: with open(syntax_path, 'rb') as f: old_syntax_text = f.read() if old_syntax_text == syntax_text: - return (name, False) + return (name, False, False) except Exception: pass with open(syntax_path, 'wb') as dst: dst.write(syntax_text) - return (name, True) + return (name, False, True) def get_field_by_line_position(fields, delim_size, query_pos): @@ -374,9 +380,13 @@ def do_enable_rainbow(view, delim, policy, store_settings): view.settings().set('pre_rainbow_syntax', pre_rainbow_syntax) view.settings().set('rainbow_mode', True) # We use this as F5 key condition - syntax_file_basename, created = ensure_syntax_file(delim, policy) - dbg_log(logging_enabled, 'Syntax file basename: "{}", Created: {}'.format(syntax_file_basename, created)) - syntax_settings_path = os.path.join(sublime.packages_path(), 'User', get_syntax_settings_file_basename(syntax_file_basename)) + syntax_file_basename, pregenerated, created = ensure_syntax_file(delim, policy) + dbg_log(logging_enabled, 'Syntax file basename: "{}", Pregenerated: {}, Created: {}'.format(syntax_file_basename, pregenerated, created)) + + if pregenerated: + syntax_settings_path = os.path.join(sublime.packages_path(), 'rainbow_csv', 'pregenerated_grammars', get_syntax_settings_file_basename(syntax_file_basename)) + else: + syntax_settings_path = os.path.join(sublime.packages_path(), 'User', get_syntax_settings_file_basename(syntax_file_basename)) use_custom_rainbow_colors = get_setting(view, 'use_custom_rainbow_colors', False) @@ -389,7 +399,10 @@ def do_enable_rainbow(view, delim, policy, store_settings): #else: # FIXME enable after fixing #27 # remove_sublime_settings(syntax_settings_path) - rainbow_syntax_file = 'Packages/User/{}'.format(syntax_file_basename) + if pregenerated: + rainbow_syntax_file = 'Packages/rainbow_csv/pregenerated_grammars/{}'.format(syntax_file_basename) + else: + rainbow_syntax_file = 'Packages/User/{}'.format(syntax_file_basename) dbg_log(logging_enabled, 'Current syntax file: "{}", New syntax file: "{}"'.format(pre_rainbow_syntax, rainbow_syntax_file)) if pre_rainbow_syntax == rainbow_syntax_file: return diff --git a/other/make_sublime_syntax.py b/other/make_sublime_syntax.py index 9d0f19d..a494fa9 100755 --- a/other/make_sublime_syntax.py +++ b/other/make_sublime_syntax.py @@ -12,13 +12,6 @@ import auto_syntax -def get_prod_delims(): - delims = [chr(i) for i in range(32, 127)] - delims.append('\t') - delims = [delim for delim in delims if re.match('^[a-zA-Z0-9]$', delim) is None] - return delims - - def name_normalize(delim): if delim == '<': return 'less-than' @@ -75,7 +68,7 @@ def main(): if args.make_grammars_old: dst_dir = args.make_grammars_old - delims = get_prod_delims() + delims = auto_syntax.get_pregenerated_delims() standard_delims = '\t|,;' for delim in delims: if standard_delims.find(delim) != -1: @@ -85,7 +78,7 @@ def main(): if args.make_grammars_prod: dst_dir = args.make_grammars_prod - delims = get_prod_delims() + delims = auto_syntax.get_pregenerated_delims() standard_delims = ',;' for delim in delims: if standard_delims.find(delim) != -1: