Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switching Margo's Go To Definition to use the types package #571

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions Default (Linux).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
"args": {"mode": "hint"},
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
},
{
"keys": ["ctrl+.", "ctrl+u"],
"command": "gs_doc",
"args": {"mode": "usage"},
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
},
{
"keys": ["ctrl+.", "ctrl+."],
"command": "show_overlay",
Expand Down
6 changes: 6 additions & 0 deletions Default (OSX).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@
"args": {"mode": "hint"},
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
},
{
"keys": ["super+.", "super+u"],
"command": "gs_doc",
"args": {"mode": "usage"},
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
},
{
"keys": ["super+.", "super+."],
"command": "show_overlay",
Expand Down
6 changes: 6 additions & 0 deletions Default (Windows).sublime-keymap
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@
"args": {"mode": "hint"},
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
},
{
"keys": ["ctrl+.", "ctrl+u"],
"command": "gs_doc",
"args": {"mode": "usage"},
"context": [{ "key": "selector", "operator": "equal", "operand": "source.go" }]
},
{
"keys": ["ctrl+.", "ctrl+."],
"command": "show_overlay",
Expand Down
9 changes: 9 additions & 0 deletions GoSublime.sublime-commands
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,19 @@
"command": "gs_doc",
"args": {"mode": "hint"}
},
{
"caption": "GoSublime: Show Usages",
"command": "gs_doc",
"args": {"mode": "usage"}
},
{
"caption": "GoSublime: Fmt the current file (without saving it)",
"command": "gs_fmt"
},
{
"caption": "GoSublime: Rename the current selection (gorename)",
"command": "gs_gorename"
},
{
"caption": "GoSublime: New Go File",
"command": "gs_new_go_file"
Expand Down
8 changes: 6 additions & 2 deletions gosubl/mg9.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,13 +390,17 @@ def imports(fn, src, toggle):
'tabWidth': gs.setting('fmt_tab_width'),
})

def doc(fn, src, offset, f):
def doc(fn, src, offset, f, mode='doc'):
tid = gs.begin(DOMAIN, 'Fetching doc info')
def cb(res, err):
gs.end(tid)
f(res, err)

acall('doc', {
#default to doc
if mode not in ['usage', 'doc']:
mode = 'doc'

acall(mode, {
'fn': fn or '',
'src': src or '',
'offset': offset or 0,
Expand Down
59 changes: 59 additions & 0 deletions gscommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from gosubl import gspatch
from gosubl import mg9
import datetime
import subprocess
import os
import sublime
import sublime_plugin
Expand Down Expand Up @@ -201,3 +202,61 @@ def run(self, edit, pos, content, added_path=''):
gs.set_attr(k, added_path)
else:
gs.del_attr(k)

class GsGorenameCommand(sublime_plugin.TextCommand):
def is_enabled(self):
fn = self.view.file_name()
if fn:
scope_ok = fn.lower().endswith('.go')
else:
scope_ok = gs.is_go_source_view(self.view)

return scope_ok

def run(self, edit):
view = self.view

# if view.is_dirty():
# sublime.error_message("{0}: GoRename failure: Unsaved file".format(DOMAIN))
# return

region = view.sel()[0]

# If the current selection is empty, try to expand
# it to the word under the cursor
if region.empty():
region = view.word(region)

if region.empty():
sublime.message_dialog('Select an identifier you would like to rename and try again.')
return

current_selection = view.substr(region)
filename = view.file_name()

def on_done(new_name):
if new_name == current_selection:
return

gs.println(DOMAIN, 'Requested New Name: {0}'.format(new_name))

offset = '{0}:#{1}'.format(filename, region.begin())
command = ['gorename', '-offset', offset, '-to', new_name]

gs.println(DOMAIN, 'CMD: {0}'.format(' '.join(command)))

out = ""
try:
p = gs.popen(command, stderr=subprocess.STDOUT)
out = p.communicate()[0]
if p.returncode != 0:
raise OSError("GoRename failed")

except Exception as e:
msg = gs.tbck.format_exc()
if out:
msg = '{0}\n{1}'.format(msg, gs.ustr(out))
gs.show_output('GsGorename', msg, replace=False, merge_domain=False)

view.window().show_input_panel("New name:", current_selection, on_done, None, None)

37 changes: 31 additions & 6 deletions gsdoc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def show_output(self, s):

def run(self, _, mode=''):
view = self.view
if (not gs.is_go_source_view(view)) or (mode not in ['goto', 'hint']):
if (not gs.is_go_source_view(view)) or (mode not in ['goto', 'hint', 'usage']):
return

pt = gs.sel(view).begin()
Expand All @@ -36,19 +36,44 @@ def f(docs, err):
if err:
self.show_output('// Error: %s' % err)
elif docs:
if mode == "goto":
if mode == "goto" or mode == "usage":
fn = ''
flags = 0
if len(docs) > 0:
d = docs[0]

#method to open doc
def open(d):
fn = d.get('fn', '')
row = d.get('row', 0)
col = d.get('col', 0)
if fn:
gs.println('opening %s:%s:%s' % (fn, row, col))
gs.focus(fn, row, col)
return
self.show_output("%s: cannot find definition" % DOMAIN)
self.show_output("%s: cannot find definition" % DOMAIN)

if len(docs) > 1:
def callback(idx):
open(docs[idx])

def highlight(idx):
d = docs[idx]
fn = d.get('fn', '')
row = d.get('row', 0) + 1
col = d.get('col', 0) + 1
sublime.active_window().open_file('%s:%s:%s' % (fn, row or 0, col or 0), sublime.TRANSIENT | sublime.ENCODED_POSITION)

#list of usages
lines = []
for d in docs:
lines.append(d.get('fn', '') + ':' + str(d.get('row', 0) + 1) + ':' + str(d.get('col', 0) + 1))

sublime.active_window().show_quick_panel(lines, callback, on_highlight=highlight)
return

elif len(docs) == 1:
open(docs[0])
return

elif mode == "hint":
s = []
for d in docs:
Expand All @@ -67,7 +92,7 @@ def f(docs, err):
doc = '\n\n\n'.join(s).strip()
self.show_output(doc or "// %s: no docs found" % DOMAIN)

mg9.doc(view.file_name(), src, pt, f)
mg9.doc(view.file_name(), src, pt, f, mode)

class GsBrowseDeclarationsCommand(sublime_plugin.WindowCommand):
def run(self, dir=''):
Expand Down
Loading