Skip to content

Commit

Permalink
Added option to disable JSX prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeche committed Oct 25, 2020
1 parent 3954cfe commit 8c4ae7e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 18 deletions.
11 changes: 9 additions & 2 deletions Emmet.sublime-settings
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,17 @@
// – true: enable preview for both markup and stylesheet abbreviations
// – false: completely disable preview
// – "markup" or "stylesheet": enable previews for either markup or stylesheet abbreviations
"abbreviation_preview": true,
"abbreviation_preview": true,

// If enabled, all abbreviations in JSX must be prefixed with `<` character.
// It allows you to explicitly specify that you are typing abbreviation and
// want to expand it with `Tab` key.
// Disabling this option will likely break native ST `Tab` key handler like
// inserting completions and indenting code
"jsx_prefix": true,

// Scope for marked abbreviation region highlighting
"marker_scope": "region.accent",
"marker_scope": "comment",

// Editor scope to Emmet syntax mapping
"syntax_scopes": {
Expand Down
14 changes: 9 additions & 5 deletions lib/abbreviation.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ..emmet import Abbreviation as MarkupAbbreviation, markup_abbreviation, stylesheet_abbreviation
from ..emmet.config import Config
from ..emmet.stylesheet import CSSAbbreviationScope
from .emmet_sublime import JSX_PREFIX, expand, extract_abbreviation
from .emmet_sublime import get_jsx_prefix, expand, extract_abbreviation
from .utils import pairs, pairs_end, known_tags, replace_with_snippet
from .context import get_activation_context
from .config import get_preview_config, get_settings, get_user_css
Expand Down Expand Up @@ -113,15 +113,19 @@ def typing_abbreviation(editor: sublime.View, pos: int) -> AbbreviationTracker:
line = editor.line(pos)
prefix = editor.substr(sublime.Region(max(line.begin(), pos - 2), pos))
syntax_name = syntax.from_pos(editor, pos)
jsx_prefix = get_jsx_prefix()
start = -1
end = pos
offset = 0

if syntax.is_jsx(syntax_name):
if syntax.is_jsx(syntax_name) and jsx_prefix:
# In JSX, abbreviations should be prefixed
if len(prefix) == 2 and prefix[0] == JSX_PREFIX and re_jsx_abbr_start.match(prefix[1]):
if len(prefix) == 2 and prefix[0] == jsx_prefix and re_jsx_abbr_start.match(prefix[1]):
start = pos - 2
offset = len(JSX_PREFIX)
offset = len(jsx_prefix)
else:
# Abbreviation is not prefixed, abort
return None
elif re_word_bound.match(prefix):
start = pos - 1

Expand Down Expand Up @@ -257,7 +261,7 @@ def create_tracker(editor: sublime.View, region: sublime.Region, params: dict) -
parsed_abbr = stylesheet_abbreviation(abbreviation, config)
else:
parsed_abbr = markup_abbreviation(abbreviation, config)
jsx = config and syntax.is_jsx(config.syntax)
jsx = config and syntax.is_jsx(config.syntax) and bool(get_jsx_prefix())
tracker_params['simple'] = not jsx and is_simple_markup_abbreviation(parsed_abbr)

preview_config = get_preview_config(config)
Expand Down
19 changes: 8 additions & 11 deletions lib/emmet_sublime.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@
from .config import get_settings, get_config
from .utils import to_region

JSX_PREFIX = '<'


def escape_text(text: str, **kwargs):
"Escapes all `$` in plain text for snippet output"
return re.sub(r'\$', '\\$', text)
Expand Down Expand Up @@ -138,6 +135,7 @@ def extract_abbreviation(view: sublime.View, loc: int, config: Config = None):
begin = region.begin()
abbr_pos = pt - begin
look_ahead = config.type != 'stylesheet'
prefix = get_jsx_prefix() if syntax.is_jsx(config.syntax) else None

if config is None:
config = get_config(view, pt)
Expand All @@ -147,22 +145,16 @@ def extract_abbreviation(view: sublime.View, loc: int, config: Config = None):
# No look-ahead for stylesheets: they do not support brackets syntax
# and enabled look-ahead produces false matches
'lookAhead': look_ahead,
'prefix': JSX_PREFIX if syntax.is_jsx(config.syntax) else None
'prefix': prefix
})

if not abbr_data and syntax.is_jsx(config.syntax):
# Try JSX without prefix
abbr_data = extract(text, abbr_pos, {
'type': config.type,
'lookAhead': config.type != 'stylesheet',
})

if not abbr_data and look_ahead:
# Try without lookAhead option: useful for abbreviations inside
# string literals
abbr_data = extract(text, abbr_pos, {
'type': config.type,
'lookAhead': False,
'prefix': prefix
})

if abbr_data:
Expand All @@ -172,3 +164,8 @@ def extract_abbreviation(view: sublime.View, loc: int, config: Config = None):
return abbr_data

return None


def get_jsx_prefix() -> str:
"Returns prefix for capturing JSX abbreviations"
return '<' if get_settings('jsx_prefix') else ''

0 comments on commit 8c4ae7e

Please sign in to comment.