all repos

gopher.nvim @ ab68a58

Minimalistic plugin for Go development

gopher.nvim/lua/gopher/config.lua(view raw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
---@toc_entry Configuration
---@tag gopher.nvim-config
---@text config it is the place where you can configure the plugin.
--- also this is optional is you're ok with default settings.
--- You can look at default options |gopher.nvim-config-defaults|

---@type gopher.Config
---@private
local config = {}

---@tag gopher.nvim-config.ConfigGoTagTransform
---@text Possible values for |gopher.Config|.gotag.transform:
---
---@private
---@alias gopher.ConfigGoTagTransform
---| "snakecase"  "GopherUser" -> "gopher_user"
---| "camelcase"  "GopherUser" -> "gopherUser"
---| "lispcase"   "GopherUser" -> "gopher-user"
---| "pascalcase" "GopherUser" -> "GopherUser"
---| "titlecase"  "GopherUser" -> "Gopher User"
---| "keep"       keeps the original field name

--minidoc_replace_start {

---@tag gopher.nvim-config-defaults
---@eval return MiniDoc.afterlines_to_code(MiniDoc.current.eval_section)
---
---@class gopher.Config
local default_config = {
  --minidoc_replace_end

  -- log level, you might consider using DEBUG or TRACE for debugging the plugin
  ---@type number
  log_level = vim.log.levels.INFO,

  -- timeout for running internal commands
  ---@type number
  timeout = 2000,

  --- whether to setup plugin commands or not
  ---@type boolean
  setup_commands = true,

  -- user specified paths to binaries
  ---@class gopher.ConfigCommand
  commands = {
    go = "go",
    gomodifytags = "gomodifytags",
    gotests = "gotests",
    impl = "impl",
    iferr = "iferr",
  },
  ---@class gopher.ConfigGotests
  gotests = {
    -- gotests doesn't have template named "default" so this plugin uses "default" to set the default template
    template = "default",
    -- path to a directory containing custom test code templates
    ---@type string|nil
    template_dir = nil,
    -- switch table tests from using slice to map (with test name for the key)
    named = false,
  },
  ---@class gopher.ConfigGoTag
  gotag = {
    ---@type gopher.ConfigGoTagTransform
    transform = "snakecase",

    -- default tags to add to struct fields
    default_tag = "json",
  },
  iferr = {
    -- choose a custom error message
    ---@type string|nil
    message = nil,
  },
}
--minidoc_afterlines_end

---@type gopher.Config
---@private
local _config = default_config

-- I am kinda secret so don't tell anyone about me even dont use me
--
-- if you don't believe me that i am secret see
-- the line below it says @private
---@private
_config.___plugin_name = "gopher.nvim" ---@diagnostic disable-line: inject-field

---@param user_config? gopher.Config
---@private
function config.setup(user_config)
  vim.validate { user_config = { user_config, "table", true } }

  _config = vim.tbl_deep_extend("force", vim.deepcopy(default_config), user_config or {})

  vim.validate {
    log_level = { _config.log_level, "number" },
    timeout = { _config.timeout, "number" },
    setup_commands = { _config.setup_commands, "boolean" },
    ["commands"] = { _config.commands, "table" },
    ["commands.go"] = { _config.commands.go, "string" },
    ["commands.gomodifytags"] = { _config.commands.gomodifytags, "string" },
    ["commands.gotests"] = { _config.commands.gotests, "string" },
    ["commands.impl"] = { _config.commands.impl, "string" },
    ["commands.iferr"] = { _config.commands.iferr, "string" },
    ["gotests"] = { _config.gotests, "table" },
    ["gotests.template"] = { _config.gotests.template, "string" },
    ["gotests.template_dir"] = { _config.gotests.template, "string", true },
    ["gotests.named"] = { _config.gotests.named, "boolean" },
    ["gotag"] = { _config.gotag, "table" },
    ["gotag.transform"] = { _config.gotag.transform, "string" },
    ["gotag.default_tag"] = { _config.gotag.default_tag, "string" },
    ["iferr"] = { _config.iferr, "table" },
    ["iferr.message"] = { _config.iferr.message, "string", true },
  }

  if _config.setup_commands then
    require("gopher.commands").register()
  end
end

---@return boolean
---@private
function config.should_setup_commands()
  return config.setup_commands
end

setmetatable(config, {
  __index = function(_, key)
    return _config[key]
  end,
})

---@return gopher.Config
---@private
return config