From a6911a63114140c833969d9d564dffabdb5fd00b Mon Sep 17 00:00:00 2001 From: Sergey Chikuyonok Date: Mon, 10 Aug 2020 23:00:22 +0300 Subject: [PATCH] Allow enable/disable abbreviation capturing and preview MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- Emmet.sublime-settings | 12 ++++++++++-- README.md | 18 ++++++++++++++++-- lib/abbreviation.py | 20 +++++++++++++++----- main.py | 5 +++-- 4 files changed, 44 insertions(+), 11 deletions(-) diff --git a/Emmet.sublime-settings b/Emmet.sublime-settings index f7d4fca..9f7ec5c 100644 --- a/Emmet.sublime-settings +++ b/Emmet.sublime-settings @@ -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 diff --git a/README.md b/README.md index 8a7f6fe..1a8091b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/abbreviation.py b/lib/abbreviation.py index 94ea168..6aef427 100644 --- a/lib/abbreviation.py +++ b/lib/abbreviation.py @@ -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): @@ -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() @@ -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) - diff --git a/main.py b/main.py index 2889c27..1befae7 100644 --- a/main.py +++ b/main.py @@ -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: