Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/github_actions/actions/checkout-4
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Sep 4, 2023
2 parents 4b04f70 + 6e6eaf6 commit 7785175
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 26 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Automatically discover debug adapter launch configurations if `nvim-dap` and `haskell-debug-adapter`
are detected.
This can be disabled by setting the `vim.g.haskell_tools.dap.auto_discover` option to `false`.

## [2.0.2] - 2023-09-02
### Fixed
- Hover: Decode url-encoded (type-)definition paths in hover actions ([#238](https://github.com/mrcjkb/haskell-tools.nvim/issues/238)).
Expand Down
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,6 @@ vim.keymap.set('n', '<leader>rf', function()
ht.repl.toggle(vim.api.nvim_buf_get_name(0))
end, def_opts)
vim.keymap.set('n', '<leader>rq', ht.repl.quit, opts)

-- Detect nvim-dap launch configurations
-- (requires nvim-dap and haskell-debug-adapter)
ht.dap.discover_configurations(bufnr)
```

>**Note**
Expand Down Expand Up @@ -249,10 +245,10 @@ this plugin will set up `autocmd`s to automatically generate tags:

This feature can be tweaked or disabled in the [advanced configuration](#advanced-configuration).

- [x] **Auto-detect `haskell-debug-adapter` configurations**
- [x] **Auto-discover `haskell-debug-adapter` configurations**

If the [`nvim-dap`](https://github.com/mfussenegger/nvim-dap) plugin is installed,
you can use `haskell-tools.nvim` to auto-detect [`haskell-debug-adapter`](https://hackage.haskell.org/package/haskell-debug-adapter)
`haskell-tools.nvim` will automatically discover [`haskell-debug-adapter`](https://hackage.haskell.org/package/haskell-debug-adapter)
configurations.

![dap](https://user-images.githubusercontent.com/12857160/232348888-4fea5393-d624-417e-b994-6eb44113a3d9.gif)
Expand Down Expand Up @@ -543,6 +539,12 @@ local ht = require('haskell-tools')
ht.dap.discover_configurations(bufnr, opts)
```

> **Note**
>
> `haskell-tools.nvim` will discover DAP launch configurations automatically,
> if `nivm-dap` is installed and the debug adapter server is executable.
> There is typically no need to call this function manually.
### Telescope extension

If [`telescope.nvim`](https://github.com/nvim-telescope/telescope.nvim) is installed,
Expand Down
7 changes: 4 additions & 3 deletions doc/haskell-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,10 @@ HaskellLspClientOpts *HaskellLspClientOpts*
HTDapOpts *HTDapOpts*

Fields: ~
{cmd} (string[]|nil) The command to start haskell-debug-adapter with.
{logFile} (string|nil) Log file path for detected configurations.
{logLevel} (HaskellDebugAdapterLogLevel|nil) The log level for detected configurations.
{cmd} (string[]|nil) The command to start the debug adapter server with.
{logFile} (string|nil) Log file path for detected configurations.
{logLevel} (HaskellDebugAdapterLogLevel|nil) The log level for detected configurations.
{auto_discover} (boolean|AddDapConfigOpts) Set to `false` to disable auto-discovery of launch configurations. `true` uses the default configurations options`.


HaskellDebugAdapterLogLevel *HaskellDebugAdapterLogLevel*
Expand Down
1 change: 1 addition & 0 deletions ftplugin/cabal.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
local ht = require('haskell-tools.internal')
ht.start_or_attach()
ht.dap_discover()
1 change: 1 addition & 0 deletions ftplugin/cabalproject.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
local ht = require('haskell-tools.internal')
ht.start_or_attach()
ht.dap_discover()
1 change: 1 addition & 0 deletions ftplugin/haskell.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
local ht = require('haskell-tools.internal')
ht.start_or_attach()
ht.dap_discover()
1 change: 1 addition & 0 deletions ftplugin/lhaskell.lua
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
local ht = require('haskell-tools.internal')
ht.start_or_attach()
ht.dap_discover()
3 changes: 2 additions & 1 deletion lua/haskell-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,10 @@ vim.g.haskell_tools = vim.g.haskell_tools
---@see https://haskell-language-server.readthedocs.io/en/latest/configuration.html.

---@class HTDapOpts
---@field cmd string[] | nil The command to start haskell-debug-adapter with.
---@field cmd string[] | nil The command to start the debug adapter server with.
---@field logFile string | nil Log file path for detected configurations.
---@field logLevel HaskellDebugAdapterLogLevel | nil The log level for detected configurations.
---@field auto_discover boolean | AddDapConfigOpts Set to `false` to disable auto-discovery of launch configurations. `true` uses the default configurations options`.

---@alias HaskellDebugAdapterLogLevel 'Debug' | 'Info' | 'Warning' | 'Error'

Expand Down
28 changes: 28 additions & 0 deletions lua/haskell-tools/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,34 @@ function HtConfigCheck.validate(cfg)
if not ok then
return false, err
end
local dap = cfg.dap
local valid_dap_log_levels = { 'Debug', 'Info', 'Warning', 'Error' }
ok, err = validate('dap', {
auto_discover = { dap.auto_discover, { 'boolean', 'table' } },
cmd = { dap.cmd, { 'function', 'table' } },
logFile = { dap.logFile, 'string' },
logLevel = {
dap.logLevel,
function(level)
return type(level) == 'string' and vim.tbl_contains(valid_dap_log_levels, level)
end,
'one of ' .. vim.inspect(valid_backends),
},
})
if not ok then
return false, err
end
local auto_discover = dap.auto_discover
if type(auto_discover) == 'table' then
---@cast auto_discover AddDapConfigOpts
ok, err = validate('dap.auto_discover', {
autodetect = { auto_discover.autodetect, 'boolean' },
settings_file_pattern = { auto_discover.settings_file_pattern, 'string' },
})
if not ok then
return false, err
end
end
return true
end

Expand Down
4 changes: 3 additions & 1 deletion lua/haskell-tools/config/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,14 @@ local HTDefaultConfig = {
},
---@class HTDapConfig debug adapter config for nvim-dap.
dap = {
---@type string[] The command to start haskell-debug-adapter with.
---@type string[] | (fun():string[]) The command to start the debug adapter server with.
cmd = { 'haskell-debug-adapter' },
---@type string Log file path for detected configurations.
logFile = vim.fn.stdpath('data') .. '/haskell-dap.log',
---@type HaskellDebugAdapterLogLevel The log level for detected configurations.
logLevel = 'Warning',
---@type boolean | AddDapConfigOpts Set to `false` to disable auto-discovery of launch configurations. `true` uses the default configurations options`.
auto_discover = true,
},
}

Expand Down
35 changes: 20 additions & 15 deletions lua/haskell-tools/dap.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---@mod haskell-tools.dap haskell-tools nvim-dap setup

local deps = require('haskell-tools.deps')
local Types = require('haskell-tools.types.internal')

---@param root_dir string
local function get_ghci_dap_cmd(root_dir)
Expand Down Expand Up @@ -40,7 +41,7 @@ local function find_json_configurations(root_dir, opts)
end

---@param root_dir string
---@return table
---@return HsDapLaunchConfiguration[]
local function detect_launch_configurations(root_dir)
local launch_configurations = {}
local Path = deps.require_plenary('plenary.path')
Expand Down Expand Up @@ -83,9 +84,7 @@ local _configuration_cache = {}
if not deps.has('dap') then
---@type HsDapTools
local NullHsDapTools = {
discover_configurations = function(_)
vim.notify_once('haskell-tools.dap: Failed to initialise. Is nvim-dap installed?', vim.log.levels.ERROR)
end,
discover_configurations = function(_) end,
}
return NullHsDapTools
end
Expand All @@ -95,18 +94,11 @@ local dap = require('dap')
---@class HsDapTools
local HsDapTools = {}

local HTConfig = require('haskell-tools.config.internal')
local dap_opts = HTConfig.dap
dap.adapters.ghc = {
type = 'executable',
command = table.concat(dap_opts.cmd, ' '),
}

---@class AddDapConfigOpts
local DefaultAutoDapConfigOpts = {
---@type boolean Whether to automatically detect launch configurations for the project
---@type boolean Whether to automatically detect launch configurations for the project.
autodetect = true,
---@type string File name or pattern to search for. Defaults to 'launch.json'
---@type string File name or pattern to search for. Defaults to 'launch.json'.
settings_file_pattern = 'launch.json',
}

Expand All @@ -115,19 +107,32 @@ local DefaultAutoDapConfigOpts = {
---@param opts AddDapConfigOpts|nil
---@return nil
HsDapTools.discover_configurations = function(bufnr, opts)
local async = deps.require_plenary('plenary.async')
local HTConfig = require('haskell-tools.config.internal')
local HTDapConfig = HTConfig.dap
local log = require('haskell-tools.log.internal')
local dap_cmd = Types.evaluate(HTDapConfig.cmd) or {}
if #dap_cmd == 0 or vim.fn.executable(dap_cmd[1]) ~= 1 then
log.debug { 'DAP server executable not found.', dap_cmd }
return
end
---@cast dap_cmd string[]
dap.adapters.ghc = {
type = 'executable',
command = table.concat(dap_cmd, ' '),
}
local async = deps.require_plenary('plenary.async')
async.run(function()
bufnr = bufnr or 0 -- Default to current buffer
opts = vim.tbl_deep_extend('force', {}, DefaultAutoDapConfigOpts, opts or {})
local filename = vim.api.nvim_buf_get_name(bufnr)
local HtProjectHelpers = require('haskell-tools.project.helpers')
local project_root = HtProjectHelpers.match_project_root(filename)
if not project_root then
log.warn('haskell-tools.dap: Unable to detect project root for file ' .. filename)
log.warn('dap: Unable to detect project root for file ' .. filename)
return
end
if _configuration_cache[project_root] then
log.debug('dap: Found cached configuration. Skipping.')
return
end
local discovered_configurations = {}
Expand Down
13 changes: 13 additions & 0 deletions lua/haskell-tools/internal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,17 @@ function InternalApi.start_or_attach()
end
end

---Auto-discover nvim-dap launch configurations (if auto-discovery is enabled)
function InternalApi.dap_discover()
local auto_discover = HTConfig.dap.auto_discover
if not auto_discover then
return
elseif type(auto_discover) == 'boolean' then
return require('haskell-tools').dap.discover_configurations()
end
---@cast auto_discover AddDapConfigOpts
local bufnr = vim.api.nvim_get_current_buf()
require('haskell-tools').dap.discover_configurations(bufnr, auto_discover)
end

return InternalApi

0 comments on commit 7785175

Please sign in to comment.