dotfiles/vim/lua/helpers.lua

27 lines
598 B
Lua
Raw Normal View History

-- Return the project's go module path from go.mod, or an empty string if the file cannot be read.
function Get_project_go_module_path()
local root_path = vim.fn.getcwd()
local mod_path = root_path .. "/go.mod"
if vim.fn.filereadable(mod_path) == 0 then
return ""
end
local file = io.open(mod_path, "r")
if file == nil then
return ""
end
io.input(file)
-- The first line of go.mod, something like:
-- module github.com/username/repo
local line = io.read()
io.close(file)
local match = string.match(line, "^module (.+)")
if match == nil then
return ""
end
return match
end