Skip to content

Commit f6d66ec

Browse files
authored
Merge pull request #96 from Thom1729/reassign-syntaxes
Implement automatic syntax reassignment.
2 parents f85ea70 + 38c1da9 commit f6d66ec

File tree

5 files changed

+42
-3
lines changed

5 files changed

+42
-3
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ If true, JS Custom will automatically rebuild your syntaxes when you modify your
6666

6767
If true, when you run the `close_tag` command in a JavaScript file, this package's `jsx_close_tag` command will be invoked instead.
6868

69+
### `reassign_when_deleting`: string or `false`
70+
71+
When you remove a custom configuration, JS Custom will automatically find any views using that configuration and assign them to this default syntax so that Sublime won't show an error popup. You can set this setting to the path or scope of any syntax definition, or set it to `false` to disable the feature entirely.
72+
6973
## Syntax Options
7074

7175
These options, specified in your `defaults` or in a named custom configuration, determine what features your custom syntaxes will have. Omitted options will be treated as `null`.

plugin.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from .src.commands.build_syntaxes import BuildJsCustomSyntaxCommand, BuildJsCustomSyntaxesCommand
1313
from .src.commands.build_tests import BuildJsCustomTestsCommand
1414
from .src.commands.clear_user_data import ClearJsCustomUserDataCommand
15+
from .src.commands.reassign_syntaxes import ReassignSyntaxesCommand
1516
from .src.commands.jsx_close_tag import JsxCloseTagCommand
1617
from .src.listeners.jsx_close_tag import JsxCloseTagListener
1718

@@ -22,6 +23,7 @@
2223
'BuildJsCustomSyntaxCommand',
2324
'BuildJsCustomTestsCommand',
2425
'ClearJsCustomUserDataCommand',
26+
'ReassignSyntaxesCommand',
2527
'JsxCloseTagCommand',
2628
'JsxCloseTagListener',
2729
]

src/commands/build_syntaxes.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import sublime
12
import sublime_plugin
23

34
from threading import Thread
4-
from sublime_lib import OutputPanel
5+
from sublime_lib import OutputPanel, get_syntax_for_scope
56

67
from ..settings import get_settings
78
from ..paths import USER_DATA_PATH
@@ -19,7 +20,8 @@ def run(self, versions=None):
1920
output = OutputPanel.create(self.window, 'YAMLMacros')
2021
output.show()
2122

22-
configurations = get_configurations(get_settings())
23+
settings = get_settings()
24+
configurations = get_configurations(settings)
2325

2426
to_delete = {
2527
syntax_path.stem: syntax_path
@@ -43,6 +45,19 @@ def filter_by_versions(d):
4345
except FileExistsError:
4446
pass
4547

48+
if settings.get('reassign_when_deleting', False):
49+
replacement = settings['reassign_when_deleting']
50+
if replacement.startswith('scope:'):
51+
replacement = get_syntax_for_scope(replacement[6:])
52+
53+
paths_to_delete = [str(path) for path in to_delete.values()]
54+
55+
print(paths_to_delete, replacement)
56+
sublime.run_command('reassign_syntaxes', {
57+
'syntaxes': paths_to_delete,
58+
'replacement': replacement,
59+
})
60+
4661
def run():
4762
for name, syntax_path in to_delete.items():
4863
print('JS Custom: Deleting configuration {}…'.format(name))

src/commands/reassign_syntaxes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import sublime
2+
import sublime_plugin
3+
4+
__all__ = ['ReassignSyntaxesCommand']
5+
6+
7+
class ReassignSyntaxesCommand(sublime_plugin.ApplicationCommand):
8+
def run(self, syntaxes, replacement):
9+
for window in sublime.windows():
10+
for view in window.views():
11+
settings = view.settings()
12+
if settings.get('syntax') in syntaxes:
13+
print('replacing {} with {}'.format(
14+
settings.get('syntax'),
15+
replacement
16+
))
17+
settings.set('syntax', replacement)

sublime/JS Custom.sublime-settings

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,6 @@
3737
},
3838

3939
"auto_build": true,
40-
"jsx_close_tag": true
40+
"jsx_close_tag": true,
41+
"reassign_when_deleting": "scope:source.js",
4142
}

0 commit comments

Comments
 (0)