Skip to content

Commit

Permalink
Fix custom color scheme
Browse files Browse the repository at this point in the history
- The color scheme path could not be found due to a syntax error
  (case sensitivity).
- When pregenerated grammars are used, the wrong settings path was
  chosen.
  - And auto detection/reloading package settings wasn't working when
    Rainbow CSV syntax was auto-detected by file extension, because then
    it was no longer "plain text".
- Use json5 to parse Syntax-specific settings and set the color scheme.
  - Fixes a bug where modifying the Syntax-specific settings would
    conflict with how the color scheme was enabled and disabled:
  - A pre-existing settings file would prevent the color scheme from
    being used.
  - Disabling the custom color scheme would remove all settings,
    including user preferences like disabling `word_wrap`.
  - Unfortunately, comments are not preserved, so this still leads to
    some data loss. See: dpranke/pyjson5#28

Closes #39
  • Loading branch information
parasyte committed Aug 28, 2024
1 parent 66f9f8e commit b11ea9e
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import rainbow_csv.sublime_rbql as sublime_rbql
import rainbow_csv.rbql.csv_utils as csv_utils
import rainbow_csv.auto_syntax as auto_syntax
import rainbow_csv.json5.json5 as json5


table_index_path_cached = None
table_names_path_cached = None


SETTINGS_FILE = 'RainbowCSV.sublime-settings'
COLOR_SCHEME_FILE = 'RainbowCSV.sublime-color-scheme'
custom_settings = None # Gets auto updated on every SETTINGS_FILE write


Expand Down Expand Up @@ -103,7 +105,7 @@ def generate_tab_statusline(tabstop_val, delim_size, template_fields, max_output


def get_user_color_scheme_path():
return os.path.join(subLime.packages_path(), 'User', 'RainbowCSV.sublime-color-scheme')
return os.path.join(sublime.packages_path(), 'User', COLOR_SCHEME_FILE)


def get_colorscheme_before():
Expand Down Expand Up @@ -307,6 +309,16 @@ def is_plain_text(view):
return True
if syntax.find('Plain Text (CSV).sublime-syntax') != -1: # Provided by "A File Icon" package
return True


def is_rainbow_csv(view):
syntax = view.settings().get('syntax')
if syntax.find('CSV (Rainbow).sublime-syntax') != 1:
return True
if syntax.find('TSV (Rainbow).sublime-syntax') != 1:
return True
if syntax.find('Rainbow_CSV_hex_') != 1:
return True
return False


Expand Down Expand Up @@ -343,14 +355,27 @@ def get_syntax_settings_file_basename(syntax_file_basename):


def make_sublime_settings(syntax_settings_path):
if not os.path.exists(syntax_settings_path):
try:
with open(syntax_settings_path, 'r') as f:
settings = json5.load(f)
except Exception:
settings = {}

if settings.get('color_scheme') != COLOR_SCHEME_FILE:
settings['color_scheme'] = COLOR_SCHEME_FILE
with open(syntax_settings_path, 'w') as f:
f.write('{\n "color_scheme": "RainbowCSV.sublime-color-scheme"\n}')
f.write(f'{json5.dumps(settings, quote_keys=True, indent=4)}\n')


def remove_sublime_settings(syntax_settings_path):
try:
os.remove(syntax_settings_path)
with open(syntax_settings_path, 'r') as f:
settings = json5.load(f)

if settings.get('color_scheme') == COLOR_SCHEME_FILE:
del settings['color_scheme']
with open(syntax_settings_path, 'w') as f:
f.write(f'{json5.dumps(settings, quote_keys=True, indent=4)}\n')
except Exception:
pass

Expand Down Expand Up @@ -399,11 +424,7 @@ def do_enable_rainbow(view, delim, policy, store_settings):
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))

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)

if use_custom_rainbow_colors:
Expand Down Expand Up @@ -866,7 +887,7 @@ def run_rainbow_autodetect(view):
if delim is not None:
do_enable_rainbow(view, delim, policy, store_settings=False)
return
if not is_plain_text(view):
if not is_plain_text(view) and not is_rainbow_csv(view):
return
enable_autodetection = get_setting(view, 'enable_rainbow_csv_autodetect', True)
min_lines_to_check = 5
Expand Down

0 comments on commit b11ea9e

Please sign in to comment.