Skip to content
This repository was archived by the owner on Jul 6, 2024. It is now read-only.

Commit 6c65a2f

Browse files
authored
Merge pull request #13 from LintaoAmons/dev
Refactor utils and enhance error msg
2 parents 33e225b + 1fbdd27 commit 6c65a2f

File tree

12 files changed

+453
-491
lines changed

12 files changed

+453
-491
lines changed

lua/easy-commands/_types.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ local M = {}
88
---@field allow_visual_mode? boolean
99
---@field errorInfo? string
1010

11-
---@type EasyCommand.Command
12-
M.Command = {}
13-
1411
---@param t table
1512
---@return boolean
1613
function M.isCommand(t)

lua/easy-commands/config.lua

Lines changed: 118 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -1,160 +1,167 @@
11
local default_config = {
2-
disabledCommands = {},
3-
["RunCurrentLineAndOutputWithPrePostFix"] = {
4-
prefix = "```bash",
5-
postfix = "```",
6-
},
7-
---@type EasyCommand.Command[]
8-
myCommands = { {
9-
name = "EasyCommand",
10-
callback = 'lua vim.print("easy command user command")',
11-
} },
12-
aliases = {
13-
{
14-
from = "GitListCommits",
15-
to = "GitLog",
16-
},
17-
},
18-
-- TODO: commandSpecificConfig
19-
commandSpecificConfig = {
20-
["CommandName"] = {},
21-
},
2+
disabledCommands = {},
3+
["RunCurrentLineAndOutputWithPrePostFix"] = {
4+
prefix = "```bash",
5+
postfix = "```",
6+
},
7+
---@type EasyCommand.Command[]
8+
myCommands = {
9+
{
10+
name = "EasyCommand",
11+
callback = 'lua vim.print("easy command user command")',
12+
},
13+
},
14+
aliases = {
15+
{
16+
from = "GitListCommits",
17+
to = "GitLog",
18+
},
19+
},
20+
-- TODO: commandSpecificConfig
21+
commandSpecificConfig = {
22+
["CommandName"] = {},
23+
},
2224
}
2325

2426
local Config = {}
2527
Config.config = default_config
2628

2729
---@param msg string
2830
local function notifyErr(msg)
29-
vim.notify("easy-comands: \n" .. msg, vim.log.levels.ERROR, { title = "easy-commands.nvim" })
31+
vim.notify("easy-comands.nvim: \n" .. msg, vim.log.levels.ERROR, { title = "easy-commands.nvim" })
3032
end
3133

3234
---@param cmd EasyCommand.Command
35+
---@param err any | nil
3336
---@return string
34-
local function formErrorMsg(cmd)
35-
local msg = "Something went wrong when calling [" .. cmd.name .. "]\n"
36-
if cmd.dependencies then
37-
msg = msg .. "Please check dependencies firstly" .. "\n"
38-
for _, d in ipairs(cmd.dependencies) do
39-
msg = msg .. " - " .. d .. "\n"
40-
end
41-
end
42-
msg = msg .. "Run [InspectCommand " .. cmd.name .. "] to find the path of implemented and investgate more\n"
43-
msg = msg .. "Or raise a issue in https://github.com/LintaoAmons/easy-commands.nvim/issues"
44-
if cmd.errorInfo then
45-
msg = msg + "Here more info you can check: \n" .. " " .. "cmd.errorInfo"
46-
end
47-
return msg
37+
local function formErrorMsg(cmd, err)
38+
local msg = "Something went wrong when calling [" .. cmd.name .. "]\n"
39+
if cmd.dependencies then
40+
msg = msg .. "\nPlease check dependencies firstly" .. "\n"
41+
for _, d in ipairs(cmd.dependencies) do
42+
msg = msg .. " - " .. d .. "\n"
43+
end
44+
end
45+
msg = msg .. "\nRun `:InspectCommand " .. cmd.name .. "` to investgate the implementation \n"
46+
msg = msg .. "Or raise a issue in https://github.com/LintaoAmons/easy-commands.nvim/issues\n"
47+
msg = msg .. "\nError: \n" .. err
48+
49+
if cmd.errorInfo then
50+
msg = msg + "Here more info you can check: \n" .. " " .. cmd.errorInfo
51+
end
52+
msg = string.gsub(msg, "^I", " ")
53+
return msg
4854
end
4955

5056
---@param cmd EasyCommand.Command
5157
local function safeCallWithErrMsg(cmd)
52-
return function()
53-
local ok
54-
local callback = cmd.callback
55-
if type(callback) == "string" then
56-
---@cast callback string
57-
ok, _ = pcall(function()
58-
vim.cmd(callback)
59-
end)
60-
else
61-
---@cast callback fun():nil
62-
ok, _ = pcall(callback)
63-
end
64-
65-
if not ok then
66-
notifyErr(formErrorMsg(cmd))
67-
end
68-
end
58+
return function()
59+
local ok
60+
local err
61+
local callback = cmd.callback
62+
if type(callback) == "string" then
63+
---@cast callback string
64+
ok, err = pcall(function()
65+
vim.cmd(callback)
66+
end)
67+
else
68+
---@cast callback fun():nil
69+
ok, err = pcall(callback)
70+
end
71+
72+
if not ok then
73+
notifyErr(formErrorMsg(cmd, err))
74+
end
75+
end
6976
end
7077

7178
---@param implementation? EasyCommand.Command -- The implementation of the user command.
7279
---@param commandName string -- The name of the user command to be registered.
7380
local function registerUserCommand(implementation, commandName)
74-
if not implementation then
75-
vim.api.nvim_create_user_command(commandName, function()
76-
notifyErr(commandName .. " not implemented yet")
77-
end, {})
78-
return
79-
end
80-
81-
if not require("easy-commands._types").isCommand(implementation) then
82-
vim.api.nvim_create_user_command(commandName, function()
83-
notifyErr(commandName .. " not properly implemented")
84-
end, {})
85-
return
86-
end
87-
88-
---@cast implementation EasyCommand.Command
89-
-- TODO: check dependencies and show alart or show dependency when user call command failed
90-
if implementation.allow_visual_mode then
91-
-- https://github.com/ray-x/go.nvim/blob/711b3b84cf59d3c43a9d1b02fdf12152b397e7b1/lua/go/commands.lua#LL443C7-L443C20
92-
vim.api.nvim_create_user_command(
93-
commandName,
94-
safeCallWithErrMsg(implementation),
95-
{ range = true, desc = implementation.description }
96-
)
97-
return
98-
end
99-
100-
vim.api.nvim_create_user_command(
101-
commandName,
102-
safeCallWithErrMsg(implementation),
103-
{ desc = implementation.description }
104-
)
81+
if not implementation then
82+
vim.api.nvim_create_user_command(commandName, function()
83+
notifyErr(commandName .. " not implemented yet")
84+
end, {})
85+
return
86+
end
87+
88+
if not require("easy-commands._types").isCommand(implementation) then
89+
vim.api.nvim_create_user_command(commandName, function()
90+
notifyErr(commandName .. " not properly implemented")
91+
end, {})
92+
return
93+
end
94+
95+
---@cast implementation EasyCommand.Command
96+
-- TODO: check dependencies and show alart or show dependency when user call command failed
97+
if implementation.allow_visual_mode then
98+
-- https://github.com/ray-x/go.nvim/blob/711b3b84cf59d3c43a9d1b02fdf12152b397e7b1/lua/go/commands.lua#LL443C7-L443C20
99+
vim.api.nvim_create_user_command(
100+
commandName,
101+
safeCallWithErrMsg(implementation),
102+
{ range = true, desc = implementation.description }
103+
)
104+
return
105+
end
106+
107+
vim.api.nvim_create_user_command(
108+
commandName,
109+
safeCallWithErrMsg(implementation),
110+
{ desc = implementation.description }
111+
)
105112
end
106113

107114
local function registerUserCustomCommand()
108-
for _, c in pairs(Config.getConfig().myCommands) do
109-
registerUserCommand(c, c.name)
110-
end
115+
for _, c in pairs(Config.getConfig().myCommands) do
116+
registerUserCommand(c, c.name)
117+
end
111118
end
112119

113120
---@return {[string]: EasyCommand.Command}
114121
local function buildCommandMap()
115-
---@type EasyCommand.Command[]
116-
local commands = require("easy-commands.impl")
117-
local map = {}
118-
for _, c in ipairs(commands) do
119-
map[c.name] = c
120-
end
121-
return map
122+
---@type EasyCommand.Command[]
123+
local commands = require("easy-commands.impl")
124+
local map = {}
125+
for _, c in ipairs(commands) do
126+
map[c.name] = c
127+
end
128+
return map
122129
end
123130

124131
local function registerUserCommands(commandNames)
125-
local commandsMap = buildCommandMap()
126-
for _, command in pairs(commandNames) do
127-
local implementation = commandsMap[command]
128-
registerUserCommand(implementation, command)
129-
end
132+
local commandsMap = buildCommandMap()
133+
for _, command in pairs(commandNames) do
134+
local implementation = commandsMap[command]
135+
registerUserCommand(implementation, command)
136+
end
130137
end
131138

132139
---@param aliases {from: "string", to: "string"}[]
133140
local function registerAliases(aliases)
134-
for _, alias in ipairs(aliases) do
135-
vim.api.nvim_create_user_command(alias.to, alias.from, {})
136-
end
141+
for _, alias in ipairs(aliases) do
142+
vim.api.nvim_create_user_command(alias.to, alias.from, {})
143+
end
137144
end
138145

139146
---@param user_config? table
140147
Config.setup = function(user_config)
141-
Config.config = vim.tbl_deep_extend("force", default_config, user_config or {})
148+
Config.config = vim.tbl_deep_extend("force", default_config, user_config or {})
142149

143-
local commands_name = require("easy-commands.names")
144-
local inuse_commands = require("easy-commands.impl.util.base.table").getDifferenceSet(
145-
commands_name,
146-
Config.getConfig().disabledCommands
147-
)
150+
local commands_name = require("easy-commands.names")
151+
local inuse_commands = require("easy-commands.impl.util.base.table").getDifferenceSet(
152+
commands_name,
153+
Config.getConfig().disabledCommands
154+
)
148155

149-
registerUserCommands(inuse_commands)
156+
registerUserCommands(inuse_commands)
150157

151-
registerUserCustomCommand()
158+
registerUserCustomCommand()
152159

153-
registerAliases(Config.config.aliases)
160+
registerAliases(Config.config.aliases)
154161
end
155162

156163
Config.getConfig = function()
157-
return Config.config
164+
return Config.config
158165
end
159166

160167
return Config

lua/easy-commands/impl/lang/go.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ M.GoToTestFile = function()
1313
end
1414

1515
if vim.fn.filereadable(jump_to_file) == 0 then
16-
require("easy-commands.impl.util.base.fs").createFile(jump_to_file)
16+
require("easy-commands.impl.util.base.fs").create_file(jump_to_file)
1717
end
1818

1919
vim.cmd("e " .. jump_to_file)

lua/easy-commands/impl/navigation.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ local M = {
108108

109109
{
110110
name = "MaximiseWindow",
111-
callback = require("easy-commands.impl.util.editor").close_all_other_windows,
111+
callback = function()
112+
require("easy-commands.impl.util.editor").window.close_all_other_windows({
113+
"filesystem", -- neo-tree
114+
"Trouble",
115+
})
116+
end,
112117
},
113118
{
114119
name = "MaximiseWindowAsPopup",

0 commit comments

Comments
 (0)