Skip to content

Commit

Permalink
use pregenerated syntax files when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
mechatroner committed Jul 13, 2020
1 parent 81fd8f6 commit 1c94b9b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 15 deletions.
7 changes: 7 additions & 0 deletions auto_syntax.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import binascii
import re


legacy_syntax_names = {
Expand Down Expand Up @@ -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
25 changes: 19 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand Down
11 changes: 2 additions & 9 deletions other/make_sublime_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand Down

0 comments on commit 1c94b9b

Please sign in to comment.