Skip to content

Commit

Permalink
Add $CONTROL_PAR_CUSTOM_ID to syntax coloring (#420)
Browse files Browse the repository at this point in the history
Improve automatic KSP syntax recognition (on load, save, clone, modify or activate view)
  • Loading branch information
mkruselj authored Dec 20, 2023
1 parent 6936845 commit 8b47e2c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 18 deletions.
2 changes: 1 addition & 1 deletion KSP.sublime-syntax
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ contexts:
ACTIVE_INDEX|ALLOW_AUTOMATION|AUTOMATION_(ID|NAME)|
(BAR|BG|OFF|ON|OVERLOAD|PEAK|SLICEMARKERS|WAVE(_CURSOR|_END)?|WAVETABLE(_END)?|ZERO_LINE)_COLOR|
(BASE|FILE)PATH|(BG|WAVE(_END)?|WAVETABLE(_END)?)_ALPHA|
COLUMN_WIDTH|CURSOR_PICTURE|
COLUMN_WIDTH|CURSOR_PICTURE|CUSTOM_ID|
DEFAULT_VALUE|DISABLE_TEXT_SHIFTING|DND_(ACCEPT_(ARRAY|AUDIO|MIDI)|BEHAVIOUR)|
ENABLE_DND|
FILE_TYPE|FONT_TYPE(_OFF_HOVER|_OFF_PRESSED|_ON|_ON_HOVER|_ON_PRESSED)?|
Expand Down
1 change: 1 addition & 0 deletions compiler/ksp_builtins_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@
$NI_CB_TYPE_RELEASE
$NI_CB_TYPE_RPN
$NI_CB_TYPE_UI_CONTROL
$NI_CB_TYPE_UI_CONTROLS
$NI_CB_TYPE_UI_UPDATE
$NI_CHORAL_MODE_DIMENSION
$NI_CHORAL_MODE_ENSEMBLE
Expand Down
63 changes: 46 additions & 17 deletions ksp_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,14 +723,17 @@ def run(self):
webbrowser.open('https://github.com/nojanath/SublimeKSP/wiki')


class KspFixLineEndings(sublime_plugin.EventListener):
class KspFixLineEndingsAndSetSyntax(sublime_plugin.EventListener):
def is_probably_ksp_file(self, view):
ext = os.path.splitext(view.file_name())[1].lower()
fn = view.file_name()
ext = ''

if fn:
ext = os.path.splitext(fn)[1].lower()

if ext == '.ksp' or ext == '.b3s' or ext == '.nbsc':
return True

elif ext == '.txt':
elif ext == '.txt' or ext == '':
code = view.substr(sublime.Region(0, view.size()))
score = sum(sc for (pat, sc) in [(r'^on init\b', 1),
(r'^on note\b', 1),
Expand All @@ -755,21 +758,47 @@ def is_probably_ksp_file(self, view):
def set_ksp_syntax(self, view):
view.set_syntax_file("KSP.sublime-syntax")

def on_load(self, view):
if self.is_probably_ksp_file(view):
with io.open(view.file_name(), 'r', encoding = 'latin-1') as file:
s = file.read()
def test_and_set_syntax_to_ksp(self, view):
is_ksp_syntax = False

if view.settings().get('syntax') == "KSP.sublime-syntax":
is_ksp_syntax = True

if self.is_probably_ksp_file(view) and not is_ksp_syntax:
fn = view.file_name()

if fn:
with io.open(view.file_name(), 'r', encoding = 'latin-1') as file:
s = file.read()

mixed_line_endings = re.search(r'\r(?!\n)', s) and '\r\n' in s

if mixed_line_endings:
s, changes = re.subn(r'\r+\n', '\n', s) # normalize line endings

if changes:
# strip trailing whitespace too while we're at it
s = '\n'.join(x.rstrip() for x in s.split('\n'))

view.run_command('replace_text_with', {'new_text': s})
sublime.set_timeout(lambda: utils.log_message('EOL characters automatically fixed! Please save to keep the changes.'), 100)

self.set_ksp_syntax(view)

def on_load_async(self, view):
self.test_and_set_syntax_to_ksp(view)

mixed_line_endings = re.search(r'\r(?!\n)', s) and '\r\n' in s
def on_reload_async(self, view):
self.test_and_set_syntax_to_ksp(view)

if mixed_line_endings:
s, changes = re.subn(r'\r+\n', '\n', s) # normalize line endings
def on_post_save_async(self, view):
self.test_and_set_syntax_to_ksp(view)

if changes:
# strip trailing whitespace too while we're at it
s = '\n'.join(x.rstrip() for x in s.split('\n'))
def on_clone_async(self, view):
self.test_and_set_syntax_to_ksp(view)

view.run_command('replace_text_with', {'new_text': s})
sublime.set_timeout(lambda: utils.log_message('EOL characters automatically fixed! Please save to keep the changes.'), 100)
def on_modified_async(self, view):
self.test_and_set_syntax_to_ksp(view)

self.set_ksp_syntax(view)
def on_activated(self, view):
self.test_and_set_syntax_to_ksp(view)

0 comments on commit 8b47e2c

Please sign in to comment.