4th Street Bar Hive-Bar

Hive-Bar Powered by Hive beta-fdb5b5b

Community post

Cool Go Programming Language Plugins for Sublime Text

You can install these plugins via Preferences > Browse Packages > User and create [toolname].py file for every tool. Then in Preferences > Key Bindings, add the keybinding provided or add keybinding of your choice.

1. gorename

The gorename command performs precise type-safe renaming of identifiers in Go source code.

Installation

go get golang.org/x/tools/cmd/gorename

Plugin

import sublime
import sublime_plugin
from subprocess import call
import threading

class Rename(threading.Thread):
    def __init__(self, offset, word):
        threading.Thread.__init__(self)
        self.offset = offset
        self.word = word

    def run(self):
        call(["/Users/anarchyrucks/go/bin/gorename", "-offset", self.offset, "-to", self.word])
        print(self.offset)
        self.view.run_command("gs_fmt")


class GoRenameCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        word = ""
        for region in self.view.sel():
            word = self.view.substr(self.view.word(region))
        self.view.window().show_input_panel("Go Rename:", word, self.do_rename, None, None)

    def do_rename(self, word):
        filepath = self.view.file_name()
        name = filepath.split('/')[-1]
        row, col = self.view.rowcol(self.view.sel()[0].begin())
        offset = self.view.text_point(row, col)
        offset_arg = filepath + ":#" + str(offset)
        ren = Rename(offset_arg, word)
        ren.start()

Key binding

{ "keys": ["super+shift+r"],"command": "go_rename" }

2. gokeyify

gokeyify turns unkeyed struct literals T{1, 2, 3} into keyed ones T{A: 1, B: 2, C: 3}.

Install

go get honnef.co/go/tools/cmd/keyify

Plugin

import sublime
import sublime_plugin
from subprocess import Popen, PIPE
import json

class GoKeyifyCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        filepath = self.view.file_name()
        name = filepath.split('/')[-1]
        row, col = self.view.rowcol(self.view.sel()[0].begin())
        offset = self.view.text_point(row, col)
        offset_arg = filepath + ":#" + str(offset)

        process = Popen(["/Users/anarchyrucks/go/bin/keyify", "-json", offset_arg], stdout=PIPE)
        output_bytes, err = process.communicate()
        if err != None:
            print("Couldn't get the output")
            return
        else:
            output = output_bytes.decode('utf-8')
            try:
                json_output = json.loads(output)
                start = json_output['start']
                end = json_output['end']
                replacement = json_output['replacement']
                select_region = sublime.Region(start, end)
                self.view.replace(edit, select_region, replacement)
                self.view.run_command("gs_fmt")
            except:
                print("Couldn't find/parse struct")

Key binding

{ "keys": ["super+shift+k"],"command": "go_keyify" }

3. goalternate

Switch between the _test.go file and the .go program file with a single command.

Plugin

import sublime
import sublime_plugin
import os
import time
import threading

class Eraser(threading.Thread):
    def __init__(self, view):
        threading.Thread.__init__(self)
        self.view = view
    def run(self):
        time.sleep(5)
        print("here")
        self.view.erase_status("1")

class GoAlternateCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        filepath = self.view.file_name()
        name = filepath.split('/')[-1]
        path = filepath.split(name)[0]
        if name.endswith("_test.go"):
            altpath = path + name.split("_test.go")[0] + ".go"
            if os.path.isfile(altpath):
                self.view.window().open_file(altpath)
                return
        else:
            altpath = path + name.split('.go')[0] + "_test.go"
            if os.path.isfile(altpath):
                self.view.window().open_file(altpath)
                return
        self.view.set_status("1", "Alternate file not found")
        er = Eraser(self.view)
        er.start()

Key binding

{
    "keys": ["alt+super+t"],
    "command": "go_alternate",
    "context": [
      {
        "key": "selector",
        "operator": "equal",
        "operand": "source.go"
      }
    ]
}

4. gomodifytags

Add json:"keyName" automatically for your struct field tags.
Convert

type Name struct {
	First string
	Last  string
}

to

type Name struct {
	First string `json:"first"`
	Last  string `json:"last"`
}

with a single command.

Install

go get github.com/fatih/gomodifytags

Plugin

import sublime
import sublime_plugin
from subprocess import Popen, PIPE
import json

def replace(self, edit, process):
    output_bytes, err = process.communicate()
    if err != None:
        print("Couldn't get the output")
        return
    else:
        output = output_bytes.decode('utf-8')
        try:
            json_output = json.loads(output)
            start_line = json_output['start']
            end_line = json_output['end']
            print(start_line, end_line)
            start = self.view.text_point(start_line - 1, 0)
            end = self.view.text_point(end_line + 1, 0)
            print(start, end)

            lines = json_output['lines']

            replacement = ""
            for line in lines:
                replacement += line + "\n"
            if replacement != "":
                select_region = sublime.Region(start, end)
                self.view.replace(edit, select_region, replacement)
                self.view.run_command("gs_fmt")
        except:
            print("Couldn't find/parse struct")

class GoAddTagsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        filepath = self.view.file_name()
        name = filepath.split('/')[-1]

        row, col = self.view.rowcol(self.view.sel()[0].begin())
        offset = self.view.text_point(row, col)

        process = Popen(["/Users/anarchyrucks/go/bin/gomodifytags", "-file", filepath, "-offset", str(offset), "-add-tags", "json", "-format", "json", "-transform", "camelcase"], stdout=PIPE)
        replace(self, edit, process)

class GoRemoveTagsCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        filepath = self.view.file_name()
        name = filepath.split('/')[-1]

        row, col = self.view.rowcol(self.view.sel()[0].begin())
        offset = self.view.text_point(row, col)

        process = Popen(["/Users/anarchyrucks/go/bin/gomodifytags", "-file", filepath, "-offset", str(offset), "-remove-tags", "json", "-format", "json", "-transform", "camelcase"], stdout=PIPE)
        replace(self, edit, process)

Key binding

{ "keys": ["super+alt+a"],"command": "go_add_tags" }
{ "keys": ["super+alt+r"],"command": "go_remove_tags" }

5 upvotes $0.00

Replies (6)

Review before signing

Posting as . Signing with . Keychain permission: Posting. Hive Keychain will ask you to approve this action next.


  
Technical details

Operation fingerprint: