diff options
Diffstat (limited to 'config/essentials/vis/vis-go.lua')
| -rw-r--r-- | config/essentials/vis/vis-go.lua | 102 | 
1 files changed, 102 insertions, 0 deletions
diff --git a/config/essentials/vis/vis-go.lua b/config/essentials/vis/vis-go.lua new file mode 100644 index 0000000..b0b383f --- /dev/null +++ b/config/essentials/vis/vis-go.lua @@ -0,0 +1,102 @@ +local function jump_to(path, line, col) +	if path then +		vis:command(string.format("e %s", path)) +	end +	vis.win.selection:to(line, col) +end + +local Gostack = { s = {}, i = 1 } + +function Gostack:push(v) +	self.s[self.i] = v +	self.i = self.i + 1 +end + +function Gostack:pop() +	if self.i == 1 then +		return nil +	end +	self.i = self.i - 1 +	return self.s[self.i] +end + +local function godef() +	local win = vis.win +	if win.syntax ~= "go" then +		return 0 +	end + +	local file = win.file +	local pos = win.selection.pos +	local cmd = string.format("godef -i -o %d", pos) +	local status, out, err = vis:pipe(file, { start = 0, finish = file.size }, cmd) +	if status ~= 0 or not out then +		if err then +			vis:info(err) +		end +		return status +	end + +	Gostack:push({ path = file.path, line = win.selection.line, col = win.selection.col }) + +	local path, line, col = string.match(out, "([^:]+):([^:]+):([^:]+)") +	if not path then +		-- same file +		line, col = string.match(out, "([^:]+):([^:]+)") +	end +	jump_to(path, line, col) +end + +local function godef_back() +	if vis.win.syntax ~= "go" then +		return 0 +	end + +	local pos = Gostack:pop() +	if pos then +		jump_to(pos.path, pos.line, pos.col) +	end +end + +vis:map(vis.modes.NORMAL, "gd", godef, "Jump to Go symbol/definition") +vis:map(vis.modes.NORMAL, "gD", godef_back, "Jump back to previous Go symbol/definition") + +local function gorename(argv, force, win, selection) +	if win.syntax ~= "go" then +		return true +	end + +	local name = argv[1] +	if not name then +		vis:info("empty new name provided") +		return false +	end + +	local forceFlag = "" +	if force then +		forceFlag = "-force" +	end + +	local pos = selection.pos +	local f = +		io.popen(string.format("gorename -offset %s:#%d -to %s %s 2>&1", win.file.path, pos, name, forceFlag), "r") +	local out = f:read("*all") +	local success, _, _ = f:close() +	if not success then +		vis:message(out) +		return false +	end + +	-- refresh current file +	vis:command("e") +	win.selection.pos = pos + +	vis:info(out) +	return true +end + +vis:command_register( +	"gorename", +	gorename, +	"Perform precise type-safe renaming of identifiers in Go source code: :gorename newName" +)  | 
