Skip to content

Commit 68e6197

Browse files
committed
Merge pull request #209 from zhengbli/fixFormatOnKey
Move the trigger of ``format_on_key`` command to ``on_modified``
2 parents 2c19761 + 85f511f commit 68e6197

File tree

6 files changed

+69
-70
lines changed

6 files changed

+69
-70
lines changed

Default.sublime-keymap

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -135,57 +135,6 @@
135135
{ "key": "selector", "operator": "equal", "operand": "source.ts" },
136136
{ "key": "is_popup_visible"}
137137
]
138-
}, // In case when auto match is enabled, only format if not within {}
139-
{
140-
"keys": [ "}" ],
141-
"command": "typescript_format_on_key",
142-
"args": { "key": "}" },
143-
"context": [
144-
{ "key": "selector", "operator": "equal", "operand": "source.ts" },
145-
{ "key": "setting.typescript_auto_format", "operator": "equal", "operand": true },
146-
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
147-
{ "key": "num_selections", "operator": "equal", "operand": 1},
148-
{ "key": "selection_empty", "operator": "equal", "operand": true },
149-
150-
{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "\\{$" },
151-
{ "key": "following_text", "operator": "not_regex_contains", "operand": "^\\}" }
152-
]
153-
},
154-
// In case when auto match is disabled, format the block regardless
155-
{
156-
"keys": [ "}" ],
157-
"command": "typescript_format_on_key",
158-
"args": { "key": "}" },
159-
"context": [
160-
{ "key": "num_selections", "operator": "equal", "operand": 1},
161-
{ "key": "setting.typescript_auto_format", "operator": "equal", "operand": true },
162-
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
163-
{ "key": "selector", "operator": "equal", "operand": "source.ts" },
164-
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": false }
165-
]
166-
},
167-
{
168-
"keys": [ ";" ],
169-
"command": "typescript_format_on_key",
170-
"args": { "key": ";" },
171-
"context": [
172-
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
173-
{ "key": "setting.typescript_auto_format", "operator": "equal", "operand": true },
174-
{ "key": "selector", "operator": "equal", "operand": "source.ts" }
175-
]
176-
},
177-
{
178-
"keys": [ "enter" ],
179-
"command": "typescript_format_on_key",
180-
"args": { "key": "\n" },
181-
"context": [
182-
{ "key": "setting.typescript_auto_indent", "operator": "equal", "operand": true },
183-
{ "key": "auto_complete_visible", "operator": "equal", "operand": false },
184-
{ "key": "selector", "operator": "not_equal", "operand": "meta.scope.between-tag-pair" },
185-
{ "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
186-
{ "key": "num_selections", "operator": "equal", "operand": 1},
187-
{ "key": "selector", "operator": "equal", "operand": "source.ts" }
188-
]
189138
},
190139
{
191140
"keys": [ "enter" ],

typescript/commands/format.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ def run(self, text, key="", insert_key=True):
1414
if 0 == len(key):
1515
return
1616
check_update_view(self.view)
17-
loc = self.view.sel()[0].begin()
18-
19-
if insert_key:
20-
active_view().run_command('hide_auto_complete')
21-
insert_text(self.view, text, loc, key)
2217

2318
format_response = cli.service.format_on_key(self.view.file_name(), get_location_from_view(self.view), key)
2419
if format_response["success"]:

typescript/libs/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from .popup_manager import get_popup_manager
66
from .logger import log
77
from . import logger
8-
98
__all__ = [
109
'cli',
1110
'EditorClient',

typescript/libs/editor_client.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ def __init__(self):
3434
self.tab_size = 4
3535
self.indent_size = 4
3636
self.translate_tab_to_spaces = False
37+
self.ts_auto_format_enabled = True
38+
self.ts_auto_indent_enabled = True
39+
self.auto_match_enabled = True
3740

3841
def initialize(self):
3942
"""
@@ -55,7 +58,14 @@ def initialize(self):
5558
self.service = ServiceProxy(self.node_client)
5659

5760
# load formatting settings and set callbacks for setting changes
58-
for setting_name in ['tab_size', 'indent_size', 'translate_tabs_to_spaces']:
61+
for setting_name in [
62+
'tab_size',
63+
'indent_size',
64+
'translate_tabs_to_spaces',
65+
'typescript_auto_format',
66+
'typescript_auto_indent',
67+
'auto_match_enabled'
68+
]:
5969
settings.add_on_change(setting_name, self.load_format_settings)
6070
self.load_format_settings()
6171

@@ -66,6 +76,9 @@ def load_format_settings(self):
6676
self.tab_size = settings.get('tab_size', 4)
6777
self.indent_size = settings.get('indent_size', 4)
6878
self.translate_tab_to_spaces = settings.get('translate_tabs_to_spaces', False)
79+
self.ts_auto_format_enabled = settings.get("typescript_auto_format")
80+
self.ts_auto_indent_enabled = settings.get("typescript_auto_indent")
81+
self.auto_match_enabled = settings.get("auto_match_enabled")
6982
self.set_features()
7083

7184
def set_features(self):

typescript/libs/global_vars.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,34 @@
44
import sublime
55
from os.path import dirname
66

7+
# determine if the host is sublime text 2
8+
IS_ST2 = int(sublime.version()) < 3000
79

810
# Get the directory path to this file;
9-
# Note: MODULE_DIR, PLUGIN_DIR and PLUGIN_NAME only works correctly when:
10-
# 1. Using sublime 3
11-
# 2. Using sublime 2, and the plugin folder is not a symbol link
12-
# On sublime 2 with the plugin folder being a symbol link, the PLUGIN_FOLDER will points
13-
# to the linked real path instead, and the PLUGIN_NAME will be wrong too.
14-
MODULE_DIR = dirname(dirname(os.path.abspath(__file__)))
15-
PLUGIN_DIR = dirname(MODULE_DIR)
11+
# Note: there are several ways this plugin can be installed
12+
# 1. from the package control -> the absolute path of __file__ is real and contains plugin name
13+
# 2. git clone directly to the sublime packages folder -> the absolute path of __file__ is real and contains plugin name
14+
# 3. git clone to somewhere else, and link to the sublime packages folder -> the absolute path is real in Sublime 3,
15+
# but is somewhere else in Sublime 2;and therefore in Sublime 2 there is no direct way to obtain the plugin name
16+
if not IS_ST2:
17+
PLUGIN_DIR = dirname(dirname(dirname(os.path.abspath(__file__))))
18+
else:
19+
_sublime_packages_dir = sublime.packages_path()
20+
_cur_file_abspath = os.path.abspath(__file__)
21+
if _sublime_packages_dir not in _cur_file_abspath:
22+
# The plugin is installed as a link
23+
for p in os.listdir(_sublime_packages_dir):
24+
link_path = _sublime_packages_dir + os.sep + p
25+
if os.path.realpath(link_path) in _cur_file_abspath:
26+
PLUGIN_DIR = link_path
27+
break
28+
else:
29+
PLUGIN_DIR = dirname(dirname(dirname(os.path.abspath(__file__))))
1630
PLUGIN_NAME = os.path.basename(PLUGIN_DIR)
1731

1832
# only Sublime Text 3 build after 3072 support tooltip
1933
TOOLTIP_SUPPORT = int(sublime.version()) >= 3072
2034

21-
# determine if the host is sublime text 2
22-
IS_ST2 = int(sublime.version()) < 3000
23-
2435
# detect if quick info is available for symbol
2536
SUBLIME_WORD_MASK = 515
2637

typescript/listeners/format.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from .event_hub import EventHub
2-
2+
from ..libs.view_helpers import *
3+
from ..libs.logger import log
4+
from ..libs import cli
35

46
class FormatEventListener:
57
def on_post_text_command_with_info(self, view, command_name, args, info):
@@ -11,5 +13,35 @@ def on_post_text_command_with_info(self, view, command_name, args, info):
1113
"typescript_paste_and_format"]:
1214
print("handled changes for " + command_name)
1315

16+
def on_modified_with_info(self, view, info):
17+
log.debug("Format on key")
18+
19+
if (
20+
is_typescript(view) and
21+
cli.ts_auto_format_enabled and
22+
info.prev_sel and
23+
len(info.prev_sel) == 1 and
24+
info.prev_sel[0].empty()
25+
):
26+
last_command, args, repeat_times = view.command_history(0)
27+
log.debug("last_command:{0}, args:{1}".format(last_command, args))
28+
if last_command == "insert":
29+
pos = info.prev_sel[0].begin()
30+
if ";" in args["characters"]:
31+
view.run_command("typescript_format_on_key", {"key": ";"})
32+
if "}" in args["characters"]:
33+
if cli.auto_match_enabled:
34+
prev_char = view.substr(pos - 1)
35+
post_char = view.substr(pos + 1)
36+
log.debug("prev_char: {0}, post_char: {1}".format(prev_char, post_char))
37+
if prev_char != "{" and post_char != "}":
38+
view.run_command("typescript_format_on_key", {"key": "}"})
39+
else:
40+
view.run_command("typescript_format_on_key", {"key": "}"})
41+
if "\n" in args["characters"]:
42+
if cli.ts_auto_indent_enabled and view.score_selector(pos, "meta.scope.between-tag-pair") > 0:
43+
view.run_command("typescript_format_on_key", {"key": "\n"})
44+
1445
listener = FormatEventListener()
15-
EventHub.subscribe("on_post_text_command_with_info", listener.on_post_text_command_with_info)
46+
EventHub.subscribe("on_post_text_command_with_info", listener.on_post_text_command_with_info)
47+
EventHub.subscribe("on_modified_with_info", listener.on_modified_with_info)

0 commit comments

Comments
 (0)