Skip to content

Commit ce0aebc

Browse files
committed
* implement goto definition with a dedicated margo.sh command goto.definition
* misc polish
1 parent 14b1fa0 commit ce0aebc

File tree

9 files changed

+96
-56
lines changed

9 files changed

+96
-56
lines changed

Default (Linux).sublime-keymap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
},
7878
{
7979
"keys": ["ctrl+.", "ctrl+g"],
80-
"command": "gs_doc",
81-
"args": {"mode": "goto"},
80+
"command": "gs9o_open",
81+
"args": {"run": ["goto.definition"], "focus_view": false, "show_view": false},
8282
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
8383
},
8484
{

Default (OSX).sublime-keymap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
},
7272
{
7373
"keys": ["super+.", "super+g"],
74-
"command": "gs_doc",
75-
"args": {"mode": "goto"},
74+
"command": "gs9o_open",
75+
"args": {"run": ["goto.definition"], "focus_view": false, "show_view": false},
7676
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
7777
},
7878
{

Default (Windows).sublime-keymap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@
7777
},
7878
{
7979
"keys": ["ctrl+.", "ctrl+g"],
80-
"command": "gs_doc",
81-
"args": {"mode": "goto"},
80+
"command": "gs9o_open",
81+
"args": {"run": ["goto.definition"], "focus_view": false, "show_view": false},
8282
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
8383
},
8484
{

gosubl/margo.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from .margo_agent import MargoAgent
44
from .margo_common import OutputLogger, TokenCounter
55
from .margo_render import render, render_src
6-
from .margo_state import State, actions, Config, _view_scope_lang
6+
from .margo_state import State, actions, client_actions, Config, _view_scope_lang, view_is_9o
77
from collections import namedtuple
88
import glob
99
import os
@@ -12,6 +12,7 @@
1212

1313
class MargoSingleton(object):
1414
def __init__(self):
15+
self._ready = False
1516
self.package_dir = os.path.dirname(os.path.abspath(__file__))
1617
self.out = OutputLogger('margo')
1718
self.agent_tokens = TokenCounter('agent', format='{}#{:03d}', start=6)
@@ -21,6 +22,12 @@ def __init__(self):
2122
self.state = State()
2223
self.status = []
2324
self.output_handler = None
25+
self._client_actions_handlers = {
26+
client_actions.Activate: self._handle_act_activate,
27+
client_actions.Restart: self._handle_act_restart,
28+
client_actions.Shutdown: self._handle_act_shutdown,
29+
client_actions.CmdOutput: self._handle_act_output,
30+
}
2431

2532
def render(self, rs=None):
2633
# ST has some locking issues due to its "thread-safe" API
@@ -47,6 +54,9 @@ def _render():
4754
sublime.set_timeout(_render)
4855

4956

57+
def _handle_act_activate(self, rs, act):
58+
gs.focus(act.name or act.path, row=act.row, col=act.col, focus_pat='')
59+
5060
def _handle_act_restart(self, rs, act):
5161
self.restart()
5262

@@ -59,12 +69,12 @@ def _handle_act_output(self, rs, act):
5969
h(rs, act)
6070

6171
def _handle_client_actions(self, rs):
62-
for a in rs.state.client_actions:
63-
try:
64-
f = getattr(self, '_handle_act_' + a.name)
65-
f(rs, a)
66-
except AttributeError:
67-
self.out.println('Unknown client-action: %s: %s' % (a.name, a))
72+
for act in rs.state.client_actions:
73+
f = self._client_actions_handlers.get(act.action_name)
74+
if f:
75+
f(rs, act)
76+
else:
77+
self.out.println('Unknown client-action: %s: %s' % (act.action_name, act))
6878

6979
def render_status(self, *a):
7080
self.status = list(a)
@@ -90,6 +100,9 @@ def stop(self):
90100
a.stop()
91101

92102
def enabled(self, view):
103+
if not self._ready:
104+
return False
105+
93106
if '*' in self.enabled_for_langs:
94107
return True
95108

@@ -107,7 +120,7 @@ def can_trigger_event(self, view, allow_9o=False):
107120
return False
108121

109122
vs = view.settings()
110-
if allow_9o and vs.get('9o'):
123+
if allow_9o and view_is_9o(view):
111124
return True
112125

113126
if vs.get('is_widget'):
@@ -117,7 +130,6 @@ def can_trigger_event(self, view, allow_9o=False):
117130

118131
def event(self, name, view, handler, args):
119132
allow_9o = name in (
120-
'query_completions',
121133
)
122134
if not self.can_trigger_event(view, allow_9o=allow_9o):
123135
return None
@@ -256,6 +268,7 @@ def ext_fn():
256268
mg = MargoSingleton()
257269

258270
def gs_init(_):
271+
mg._ready = True
259272
mg.start()
260273

261274
def gs_fini(_):

gosubl/margo_agent.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,6 @@ def _handle_send_ipc(self, rq):
163163
try:
164164
ipc_enc(rq.data(), self.proc.stdin)
165165
exc = None
166-
except ipc_silent_exceptions as e:
167-
exc = e
168166
except Exception as e:
169167
exc = e
170168
if not self.stopped.is_set():

gosubl/margo_render.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,27 @@ def __init__(self, *, key, scope, icon, flags):
4343
issue_key_pfx = '#mg.Issue.'
4444
issue_cfg_error = IssueCfg(
4545
key = issue_key_pfx + 'error',
46-
scope = 'keyword sublimelinter.mark.error region.redish',
46+
scope = 'region.redish',
4747
icon = 'Packages/GoSublime/images/issue.png',
4848
flags = sublime.DRAW_SQUIGGLY_UNDERLINE | sublime.DRAW_NO_OUTLINE | sublime.DRAW_NO_FILL,
4949
)
5050
issue_cfg_warning = IssueCfg(
5151
key = issue_key_pfx + 'warning',
52-
scope = 'entity sublimelinter.mark.warning region.orangish',
52+
scope = 'region.orangish',
53+
icon = issue_cfg_error.icon,
54+
flags = issue_cfg_error.flags,
55+
)
56+
issue_cfg_notice = IssueCfg(
57+
key = issue_key_pfx + 'notice',
58+
scope = 'region.greenish',
5359
icon = issue_cfg_error.icon,
5460
flags = issue_cfg_error.flags,
5561
)
5662
issue_cfg_default = issue_cfg_error
5763
issue_cfgs = {
5864
'error': issue_cfg_error,
5965
'warning': issue_cfg_warning,
66+
'notice': issue_cfg_notice,
6067
}
6168

6269
def _render_issues(view, issues):

gosubl/margo_state.py

Lines changed: 51 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@
2121
'RunCmd',
2222
)})
2323

24+
client_actions = NS(**{k: k for k in (
25+
'Activate',
26+
'Restart',
27+
'Shutdown',
28+
'CmdOutput',
29+
)})
30+
2431
class Config(object):
2532
def __init__(self, m):
2633
self.override_settings = m.get('OverrideSettings') or {}
@@ -48,33 +55,51 @@ def __init__(self, v={}):
4855

4956
self.client_actions = []
5057
for ca in (v.get('ClientActions') or []):
51-
if ca.get('Name') == 'output':
52-
self.client_actions.append(CmdOutput(v=ca))
53-
else:
54-
self.client_actions.append(ClientAction(v=ca))
58+
CA = client_action_creators.get(ca.get('Name') or '') or ClientAction
59+
self.client_actions.append(CA(v=ca))
5560

5661
def __repr__(self):
5762
return repr(self.__dict__)
5863

5964
class ClientAction(object):
6065
def __init__(self, v={}):
61-
self.name = v.get('Name') or ''
62-
self.data = v.get('Data') or {}
66+
self.action_name = v.get('Name') or ''
67+
self.action_data = v.get('Data') or {}
6368

6469
def __repr__(self):
6570
return repr(vars(self))
6671

67-
class CmdOutput(ClientAction):
68-
def __init__(self, v={}):
72+
class ClientAction_Output(ClientAction):
73+
def __init__(self, v):
6974
super().__init__(v=v)
70-
self.fd = self.data.get('Fd') or ''
71-
self.output = self.data.get('Output') or ''
72-
self.close = self.data.get('Close') or False
73-
self.fd = self.data.get('Fd') or ''
75+
ad = self.action_data
76+
77+
self.fd = ad.get('Fd') or ''
78+
self.output = ad.get('Output') or ''
79+
self.close = ad.get('Close') or False
80+
self.fd = ad.get('Fd') or ''
7481

7582
def __repr__(self):
7683
return repr(vars(self))
7784

85+
class ClientAction_Activate(ClientAction):
86+
def __init__(self, v):
87+
super().__init__(v=v)
88+
ad = self.action_data
89+
90+
self.path = ad.get('Path') or ''
91+
self.name = ad.get('Name') or ''
92+
self.row = ad.get('Row') or 0
93+
self.col = ad.get('Col') or 0
94+
95+
def __repr__(self):
96+
return repr(vars(self))
97+
98+
client_action_creators = {
99+
client_actions.CmdOutput: ClientAction_Output,
100+
client_actions.Activate: ClientAction_Activate,
101+
}
102+
78103
class Completion(object):
79104
def __init__(self, v):
80105
self.query = v.get('Query') or ''
@@ -198,38 +223,27 @@ def _editor_props(view):
198223
'Settings': sett,
199224
}
200225

226+
def view_is_9o(view):
227+
return view is not None and view.settings().get('9o')
228+
201229
def _view_props(view):
202-
view = gs.active_view(view=view)
230+
was_9o = view_is_9o(view)
231+
if was_9o:
232+
view = gs.active_view()
233+
else:
234+
view = gs.active_view(view=view)
235+
203236
if view is None:
204237
return {}
205238

206239
pos = gs.sel(view).begin()
207-
row, col = view.rowcol(pos)
208240
scope, lang, fn, props = _view_header(view, pos)
209-
wd = gs.basedir_or_cwd(fn)
210-
211-
if lang == '9o':
212-
if 'prompt.9o' in scope:
213-
r = view.extract_scope(pos)
214-
pos -= r.begin()
215-
s = view.substr(r)
216-
src = s.lstrip().lstrip('#').lstrip()
217-
pos -= len(s) - len(src)
218-
src = src.rstrip()
219-
else:
220-
pos = 0
221-
src = ''
222-
223-
wd = view.settings().get('9o.wd') or wd
224-
props['Path'] = '_.9o'
225-
else:
226-
src = _view_src(view, lang)
241+
wd = gs.getwd() or gs.basedir_or_cwd(fn)
242+
src = _view_src(view, lang)
227243

228244
props.update({
229245
'Wd': wd,
230246
'Pos': pos,
231-
'Row': row,
232-
'Col': col,
233247
'Dirty': view.is_dirty(),
234248
'Src': src,
235249
})
@@ -272,7 +286,6 @@ def _view_header(view, pos):
272286
return scope, lang, path, {
273287
'Path': path,
274288
'Name': view_name(view, ext=ext, lang=lang),
275-
'Ext': ext,
276289
'Hash': _view_hash(view),
277290
'Lang': lang,
278291
'Scope': scope,
@@ -296,6 +309,9 @@ def _view_scope_lang(view, pos):
296309
return ('', '')
297310

298311
scope = view.scope_name(pos).strip().lower()
312+
if view_is_9o(view):
313+
return (scope, 'cmd-promp')
314+
299315
l = _scope_lang_pat.findall(scope)
300316
lang = l[-1] if l else ''
301317
return (scope, lang)

gscommands.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ def run(self, edit, row, col=0):
8282
r = sublime.Region(pt, pt)
8383
self.view.sel().clear()
8484
self.view.sel().add(r)
85-
self.view.show(pt)
85+
self.view.show(pt, True)
86+
xpos, ypos = self.view.viewport_position()
87+
self.view.set_viewport_position((0, ypos), False)
8688
dmn = 'gs.focus.%s:%s:%s' % (gs.view_fn(self.view), row, col)
8789
flags = sublime.DRAW_EMPTY_AS_OVERWRITE
8890
show = lambda: self.view.add_regions(dmn, [r], 'comment', 'bookmark', flags)

gsev.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ def run(self, edit):
3030
view = self.view
3131
if gs.is_go_source_view(view):
3232
if not gstest.handle_action(view, 'left-click'):
33-
view.run_command('gs_doc', {"mode": "goto"})
33+
view.run_command('gs9o_open', {
34+
"run": ["goto.definition"],
35+
"focus_view": False,
36+
"show_view": False,
37+
})
3438
elif view.score_selector(gs.sel(view).begin(), "text.9o") > 0:
3539
view.window().run_command("gs9o_open_selection")
3640

0 commit comments

Comments
 (0)