|
1 | 1 | 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 | + }, |
22 | 24 | }
|
23 | 25 |
|
24 | 26 | local Config = {}
|
25 | 27 | Config.config = default_config
|
26 | 28 |
|
27 | 29 | ---@param msg string
|
28 | 30 | 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" }) |
30 | 32 | end
|
31 | 33 |
|
32 | 34 | ---@param cmd EasyCommand.Command
|
| 35 | +---@param err any | nil |
33 | 36 | ---@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 |
48 | 54 | end
|
49 | 55 |
|
50 | 56 | ---@param cmd EasyCommand.Command
|
51 | 57 | 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 |
69 | 76 | end
|
70 | 77 |
|
71 | 78 | ---@param implementation? EasyCommand.Command -- The implementation of the user command.
|
72 | 79 | ---@param commandName string -- The name of the user command to be registered.
|
73 | 80 | 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 | + ) |
105 | 112 | end
|
106 | 113 |
|
107 | 114 | 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 |
111 | 118 | end
|
112 | 119 |
|
113 | 120 | ---@return {[string]: EasyCommand.Command}
|
114 | 121 | 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 |
122 | 129 | end
|
123 | 130 |
|
124 | 131 | 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 |
130 | 137 | end
|
131 | 138 |
|
132 | 139 | ---@param aliases {from: "string", to: "string"}[]
|
133 | 140 | 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 |
137 | 144 | end
|
138 | 145 |
|
139 | 146 | ---@param user_config? table
|
140 | 147 | 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 {}) |
142 | 149 |
|
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 | + ) |
148 | 155 |
|
149 |
| - registerUserCommands(inuse_commands) |
| 156 | + registerUserCommands(inuse_commands) |
150 | 157 |
|
151 |
| - registerUserCustomCommand() |
| 158 | + registerUserCustomCommand() |
152 | 159 |
|
153 |
| - registerAliases(Config.config.aliases) |
| 160 | + registerAliases(Config.config.aliases) |
154 | 161 | end
|
155 | 162 |
|
156 | 163 | Config.getConfig = function()
|
157 |
| - return Config.config |
| 164 | + return Config.config |
158 | 165 | end
|
159 | 166 |
|
160 | 167 | return Config
|
0 commit comments