Skip to content

Implement custom suffix to replace .js. #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/syntax/JS Custom.sublime-syntax.yaml-macros
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
%YAML 1.2
%TAG ! tag:yaml-macros:yamlmacros.lib.extend,yamlmacros.lib.include,JSCustom.src.syntax.macros:
---
!apply
!do_suffix
- !apply
- !include_resource Packages/JSCustom/src/syntax/JavaScript.yaml
- !get_extensions Packages/JSCustom/extensions
42 changes: 41 additions & 1 deletion src/syntax/macros.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from sublime_lib import ResourcePath


__all__ = ['get_extensions']
__all__ = ['get_extensions', 'do_suffix']


def parse_documents(text):
Expand Down Expand Up @@ -66,3 +66,43 @@ def get_extensions(path):
ret.append(result)

return ret

import re

JS_EXPR = re.compile(r'\.js(?:$|(?=[.\s]))')

def do_suffix(definition):
arguments = (yield).context

suffix = arguments.get('suffix', None)
if suffix:
def replace_suffix(string):
return JS_EXPR.sub('.' + suffix, string)

def patch_context(context):
for rule in context:
for key in ('scope', 'meta_scope', 'meta_content_scope'):
if key in rule:
rule[key] = replace_suffix(rule[key])

if 'captures' in rule:
for index, capture_scope in rule['captures'].items():
rule['captures'][index] = replace_suffix(capture_scope)

for key in ('push', 'set'):
if key in rule:
if (
isinstance(rule[key], list) and
len(rule[key])
):
if isinstance(rule[key][0], dict):
patch_context(rule[key])
else:
for child in rule[key]:
if isinstance(child, list):
patch_context(child)

for name, context in definition['contexts'].items():
patch_context(context)

return definition