Skip to content

Commit

Permalink
chore: reorganise commands
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jul 26, 2024
1 parent e539d16 commit 314740f
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 146 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Merge `HsRepl*` commands into `Haskell repl *` subcommands.
- Merge `HsProjectFile` `HsPackageYaml` and `HsPackageCabal` commands
into `Haskell {projectFile|packageYaml|packageCabal}` subcommands.
- Merge logging commands into respective `Haskell` and `Hls` subcommands.
- Merge logging commands into respective `Haskell log` subcommands.

### Added

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -697,9 +697,9 @@ vim.g.haskell_tools = {

You can also temporarily set the log level by calling

| Command | Argument |
| ---------------------- | -------------------------------------------------- |
| `:Haskell setLogLevel` | One of `debug` `error` `warn` `info` `trace` `off` |
| Command | Argument |
| ----------------------- | -------------------------------------------------- |
| `:Haskell log setLevel` | One of `debug` `error` `warn` `info` `trace` `off` |

or

Expand All @@ -719,8 +719,8 @@ You can find the log files by calling
or open them by calling

```lua
:lua require('haskell-tools').log.nvim_open_logfile() -- or :Hls openLog
:lua require('haskell-tools').log.nvim_open_hls_logfile() -- or :Hls openLog
:lua require('haskell-tools').log.nvim_open_logfile() -- or :Haskell log openLog
:lua require('haskell-tools').log.nvim_open_hls_logfile() -- or :Haskell log openHlsLog
```

## :link: Recommendations
Expand Down
6 changes: 3 additions & 3 deletions doc/haskell-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -533,9 +533,9 @@ haskell-tools Logging *haskell-tools.log*

The following commands are available:

* `:Haskell setLogLevel` - Set the haskell-tools.nvim and LSP client log level.
* `:Haskell openLog` - Open the haskell-tools.nvim log file.
* `:Hls openLog` - Open the haskell-language-server log file.
* `:Haskell log setLevel` - Set the haskell-tools.nvim and LSP client log level.
* `:Haskell log openLog` - Open the haskell-tools.nvim log file.
* `:Haskell log openHlsLog` - Open the haskell-language-server log file.

haskell-tools.Log *haskell-tools.Log*

Expand Down
181 changes: 134 additions & 47 deletions lua/haskell-tools/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,42 @@ local ht = require('haskell-tools')
---Whether this command supports a bang!
---@field bang? boolean

---@param arg_lead string
local function complete_haskell_files(arg_lead)
vim.print(arg_lead)
return vim
.iter(vim.list_extend(vim.fn.getcompletion(arg_lead, 'file'), vim.fn.getcompletion(arg_lead, 'buffer')))
:filter(function(file_path)
local ext = vim.fn.fnamemodify(file_path, ':e')
return ext == 'hs' or ext == ''
end)
:totable()
end

---@param args? string[]
---@return string?
local function get_single_opt_arg(args)
if type(args) ~= 'table' then
return
end
if #args > 1 then
require('haskell-tools.log.internal').warn { 'Too many arguments!', args }
end
return #args > 0 and args[1] or nil
end

---@param args? string[]
---@return string
local function get_filepath_arg(args)
if not args or #args == 0 then
return vim.api.nvim_buf_get_name(0)
end
vim.validate('filepath', args[1], 'string')
local filepath = vim.fn.expand(args[1])
---@cast filepath string
return filepath
end

---@type table<string, haskell-tools.Subcommand>
local command_tbl = {
packageYaml = {
Expand All @@ -31,35 +67,11 @@ local command_tbl = {
ht.project.open_project_file()
end,
},
openLog = {
impl = function()
require('haskell-tools').log.nvim_open_logfile()
end,
},
setLogLevel = {
impl = function(args)
local level = vim.fn.expand(args[1])
---@cast level string
require('haskell-tools').log.set_level(tonumber(level) or level)
end,
complete = function(arg_lead)
local levels = vim.tbl_keys(vim.log.levels)
return vim.tbl_filter(function(command)
return command:find(arg_lead) ~= nil
end, levels)
end,
},
}

---@param name string The name of the subcommand
---@param cmd haskell-tools.Subcommand The implementation and optional completions
function HtCommands.register_subcommand(name, cmd)
command_tbl[name] = cmd
end

---@param name string The name of the subcommand
---@param subcmd_tbl table<string, haskell-tools.Subcommand> The subcommand's subcommand table
function HtCommands.register_subcommand_tbl(name, subcmd_tbl)
local function register_subcommand_tbl(name, subcmd_tbl)
command_tbl[name] = {
impl = function(args, ...)
local subcmd = subcmd_tbl[table.remove(args, 1)]
Expand All @@ -84,6 +96,79 @@ function HtCommands.register_subcommand_tbl(name, subcmd_tbl)
}
end

---@type table<string, haskell-tools.Subcommand>
local repl_subcommands = {
toggle = {
impl = function(args)
local filepath = get_filepath_arg(args)
ht.repl.toggle(filepath)
end,
complete = complete_haskell_files,
},
load = {
impl = function(args)
local filepath = get_filepath_arg(args)
ht.repl.load_file(filepath)
end,
complete = complete_haskell_files,
},
quit = {
impl = ht.repl.quit,
},
reload = {
impl = ht.repl.reload,
},
paste_type = {
impl = function(args)
local reg = get_single_opt_arg(args)
ht.repl.paste_type(reg)
end,
},
cword_type = {
impl = ht.repl.cword_type,
},
paste_info = {
impl = function(args)
local reg = get_single_opt_arg(args)
ht.repl.paste_info(reg)
end,
},
cword_info = {
impl = ht.repl.cword_info,
},
}

-- TODO: Smarter completions. load, quit and reload should only be suggested when a repl is active
register_subcommand_tbl('repl', repl_subcommands)

local log_command_tbl = {
openHlsLog = {
impl = function()
ht.log.nvim_open_hls_logfile()
end,
},
openLog = {
impl = function()
require('haskell-tools').log.nvim_open_logfile()
end,
},
setLevel = {
impl = function(args)
local level = vim.fn.expand(args[1])
---@cast level string
require('haskell-tools').log.set_level(tonumber(level) or level)
end,
complete = function(arg_lead)
local levels = vim.tbl_keys(vim.log.levels)
return vim.tbl_filter(function(command)
return command:find(arg_lead) ~= nil
end, levels)
end,
},
}

register_subcommand_tbl('log', log_command_tbl)

---@generic K, V
---@param predicate fun(V):boolean
---@param tbl table<K, V>
Expand All @@ -110,27 +195,29 @@ local function haskell_cmd(opts)
command.impl(args, opts)
end

vim.api.nvim_create_user_command('Haskell', haskell_cmd, {
nargs = '+',
desc = 'haskell-tools.nvim commands',
complete = function(arg_lead, cmdline, _)
local commands = cmdline:match("^['<,'>]*Haskell!") ~= nil
-- bang!
and tbl_keys_by_value_filter(function(command)
return command.bang == true
end, command_tbl)
or vim.tbl_keys(command_tbl)
local subcmd, subcmd_arg_lead = cmdline:match("^['<,'>]*Haskell[!]*%s(%S+)%s(.*)$")
if subcmd and subcmd_arg_lead and command_tbl[subcmd] and command_tbl[subcmd].complete then
return command_tbl[subcmd].complete(subcmd_arg_lead)
end
if cmdline:match("^['<,'>]*Haskell[!]*%s+%w*$") then
return vim.tbl_filter(function(command)
return command:find(arg_lead) ~= nil
end, commands)
end
end,
bang = false, -- might change
})
function HtCommands.init()
vim.api.nvim_create_user_command('Haskell', haskell_cmd, {
nargs = '+',
desc = 'haskell-tools.nvim commands',
complete = function(arg_lead, cmdline, _)
local commands = cmdline:match("^['<,'>]*Haskell!") ~= nil
-- bang!
and tbl_keys_by_value_filter(function(command)
return command.bang == true
end, command_tbl)
or vim.tbl_keys(command_tbl)
local subcmd, subcmd_arg_lead = cmdline:match("^['<,'>]*Haskell[!]*%s(%S+)%s(.*)$")
if subcmd and subcmd_arg_lead and command_tbl[subcmd] and command_tbl[subcmd].complete then
return command_tbl[subcmd].complete(subcmd_arg_lead)
end
if cmdline:match("^['<,'>]*Haskell[!]*%s+%w*$") then
return vim.tbl_filter(function(command)
return command:find(arg_lead) ~= nil
end, commands)
end
end,
bang = false, -- might change
})
end

return HtCommands
9 changes: 9 additions & 0 deletions lua/haskell-tools/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,17 @@ local function dap_discover()
require('haskell-tools').dap.discover_configurations(bufnr, auto_discover)
end

local function init()
if vim.g.haskell_tools_loaded then
return
end
vim.g.haskell_tools_loaded = true
require('haskell-tools.commands').init()
end

---ftplugin implementation
function Api.ftplugin()
init()
start_or_attach()
dap_discover()
end
Expand Down
6 changes: 3 additions & 3 deletions lua/haskell-tools/log/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
---@brief [[
--- The following commands are available:
---
--- * `:Haskell setLogLevel` - Set the haskell-tools.nvim and LSP client log level.
--- * `:Haskell openLog` - Open the haskell-tools.nvim log file.
--- * `:Hls openLog` - Open the haskell-language-server log file.
--- * `:Haskell log setLevel` - Set the haskell-tools.nvim and LSP client log level.
--- * `:Haskell log openLog` - Open the haskell-tools.nvim log file.
--- * `:Haskell log openHlsLog` - Open the haskell-language-server log file.
---@brief ]]

---@class haskell-tools.Log
Expand Down
7 changes: 1 addition & 6 deletions lua/haskell-tools/lsp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ local HlsCmd = {
stop = 'stop',
restart = 'restart',
evalAll = 'evalAll',
openLog = 'openLog',
}

local function hls_command(opts)
Expand All @@ -246,9 +245,6 @@ local function hls_command(opts)
Hls.restart()
elseif cmd == HlsCmd.evalAll then
Hls.buf_eval_all()
elseif cmd == HlsCmd.openLog then
local ht = require('haskell-tools')
ht.log.nvim_open_hls_logfile()
end
end

Expand All @@ -260,11 +256,10 @@ vim.api.nvim_create_user_command('Hls', hls_command, {
---@type haskell-tools.HlsCmd[]
local available_commands = #clients == 0 and { 'start' } or { 'stop', 'restart', 'evalAll' }
---@type haskell-tools.HlsCmd[]
local always_available_commands = { 'openLog' }
if cmdline:match('^Hls%s+%w*$') then
return vim.tbl_filter(function(command)
return command:find(arg_lead) ~= nil
end, vim.list_extend(always_available_commands, available_commands))
end, available_commands)
end
end,
})
Expand Down
Loading

0 comments on commit 314740f

Please sign in to comment.