Skip to content

Commit

Permalink
Allow enable/disable abbreviation capturing and preview
Browse files Browse the repository at this point in the history
Both `auto_mark` and `abbreviation_preview` options supports either true/false values to enable/disable and “markup” / “stylesheet” to enable it for specific syntaxes only
  • Loading branch information
sergeche committed Aug 10, 2020
1 parent 4757ba3 commit a6911a6
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
12 changes: 10 additions & 2 deletions Emmet.sublime-settings
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
{
// Automatically marks (captures) Emmet abbreviation when typing text: captured abbreviation
// is highlighted with underline. Use Tab key inside captured abbreviation to exapnd it.
// Works in limited syntaxes only, see `abbreviation_scopes` option
// Works in limited syntaxes only, see `abbreviation_scopes` option
// Possible values:
// – true: enable capturing for both markup and stylesheet abbreviations
// – false: completely disable capturing
// – "markup" or "stylesheet": enable capturing for either markup or stylesheet abbreviations
"auto_mark": true,

// Preview captured abbreviations, works only if `auto_mark` is enabled
// Preview captured abbreviations, works only if `auto_mark` is enabled.
// Possible values:
// – 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,

// Scope for marked abbreviation region highlighting
Expand Down
18 changes: 16 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,27 @@ In CSS, Sublime Text uses slightly different autocomplete behavior by default: i

![Emmet abbreviation example](./images/emmet4.gif)

If you don’t like inline preview for CSS, you can disable it for CSS only:

* Go to _Preferences > Package Settings > Emmet > Settings_ menu item.
* Set `abbreviation_preview` option to `"markup"`, e.g. `"abbreviation_preview": "markup"`. This will enable previews for markup syntaxes (HTML, XML, JSX etc.) only.
* You can also disable previews completely by setting `abbreviation_preview` value to `false`.


In Emmet 2, CSS abbreviations are enhanced with dynamic color snippets: you can type, for example, `#f.5` to quickly get `rgba(255, 255, 255, 0.5)`.

### Disable abbreviation capturing

To disable automatic abbreviation capturing, go to _Preferences > Package Settings > Emmet > Settings_ menu item and set `auto_mark` option to `false`. With abbreviation capturing disabled, you have several options to expand abbreviations manually:
To fine tune automatic abbreviation capturing, go to _Preferences > Package Settings > Emmet > Settings_ menu item and update `auto_mark` option:

* Set value to `false` to completely disable abbreviation capturing.
* Set value to either `"markup"` or `"stylesheet"` to enable capturing for markup (HTML, XML, JSX etc) or stylesheet (CSS, SCSS, LESS etc.) syntaxes only.

For example, if you want abbreviation capturing for HTML and disable it for CSS, set `"auto_mark": "markup"`. You can also apply the same values for `abbreviation_preview` option to enable/disable interactive previews completely or for specific syntaxes only.

With abbreviation capturing disabled, you have several options to expand abbreviations manually:

* You can type abbreviation (or pur caret behind existing abbreviation) and run `Emmet: Expand Abbreviation` action from command palette. It is recommended to set [keyboard shortcut](https://www.sublimetext.com/docs/3/key_bindings.html) for this action:
* You can type abbreviation (or put caret behind existing abbreviation) and run `Emmet: Expand Abbreviation` action from command palette. It is recommended to set [keyboard shortcut](https://www.sublimetext.com/docs/3/key_bindings.html) for this action:

```json
// Put this code snippet into your .sublime-keymap file
Expand Down
20 changes: 15 additions & 5 deletions lib/abbreviation.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,21 @@ def is_simple_markup_abbreviation(abbr: MarkupAbbreviation) -> bool:

def allow_tracking(editor: sublime.View, pos: int) -> bool:
"Check if abbreviation tracking is allowed in editor at given location"
if is_enabled(editor):
if is_enabled(editor, pos):
syntax_name = syntax.from_pos(editor, pos)
return syntax.is_supported(syntax_name) or syntax.is_jsx(syntax_name)

return False


def is_enabled(view: sublime.View) -> bool:
def is_enabled(view: sublime.View, pos: int) -> bool:
"Check if Emmet abbreviation tracking is enabled"
return get_settings('auto_mark', False)
auto_mark = get_settings('auto_mark', False)
if isinstance(auto_mark, bool):
return auto_mark

syntax_info = syntax.info(view, pos)
return syntax_info['type'] == auto_mark


def mark(editor: sublime.View, tracker: AbbreviationTracker):
Expand All @@ -498,9 +503,15 @@ def unmark(editor: sublime.View):
hide_preview(editor)


def is_preview_enabled(tracker: AbbreviationTracker) -> bool:
"Check if preview is enabled for given tracker"
preview = get_settings('abbreviation_preview', True)
return preview is True or preview == tracker.config.type


def show_preview(editor: sublime.View, tracker: AbbreviationTracker):
"Displays expanded preview of abbreviation in current tracker in given view"
if not get_settings('abbreviation_preview', True):
if not is_preview_enabled(tracker):
return

key = editor.id()
Expand Down Expand Up @@ -641,4 +652,3 @@ def expand_tracker(editor: sublime.View, edit: sublime.Edit, tracker: Abbreviati
if isinstance(tracker, AbbreviationTrackerValid):
snippet = expand(tracker.abbreviation, tracker.config)
replace_with_snippet(editor, edit, tracker.region, snippet)

5 changes: 3 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,11 @@ def on_activated(self, editor: sublime.View):

@main_view
def on_selection_modified(self, editor: sublime.View):
if not abbreviation.is_enabled(editor):
pos = get_caret(editor)

if not abbreviation.is_enabled(editor, pos):
return

pos = get_caret(editor)
trk = abbreviation.handle_selection_change(editor, pos)

if trk:
Expand Down

0 comments on commit a6911a6

Please sign in to comment.