Skip to content

Commit

Permalink
fix(repl): prefer stack if installed and stack files exist (#213)
Browse files Browse the repository at this point in the history
###### Things done

- [x] Tested, as applicable:
  - [x] Manually
  - [x] Added plenary specs
- [x] Updated
[CHANGELOG.md](https://github.com/MrcJkb/haskell-tools.nvim/blob/master/CHANGELOG.md)
(if applicable).
- [x] Fits
[CONTRIBUTING.md](https://github.com/MrcJkb/haskell-tools.nvim/blob/master/CONTRIBUTING.md)
  • Loading branch information
mrcjkb committed Jul 5, 2023
1 parent 415e184 commit 2c8cb26
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 1 deletion.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ 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]
## [1.11.0] - 2023-07-05
### Changed
- Improvements to type signature detection from `textDocument/hover` docs.

### Added
- Hover: Hoogle search entries for all detected type signatures.

### Fixed
- repl: If both stack and cabal files are presend, prefer stack if it is installed.
This is configurable with the option `tools.repl.prefer`.

## [1.10.2] - 2023-05-22
### Fixed
- Do not use deprecated health check API in neovim > 0.9.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,8 @@ require('haskell-tools').start_or_attach {
-- 'builtin': Use the simple builtin repl
-- 'toggleterm': Use akinsho/toggleterm.nvim
handler = 'builtin',
-- Which backend to prefer if both stack and cabal files are present
prefer = vim.fn.executable('stack') and 'stack' or 'cabal',
builtin = {
create_repl_window = function(view)
-- create_repl_split | create_repl_vsplit | create_repl_tabnew | create_repl_cur_win
Expand Down
7 changes: 7 additions & 0 deletions doc/haskell-tools.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,17 @@ DefinitionOpts *DefinitionOpts*
{hoogle_signature_fallback} (boolean) (default: `false`) Configure `vim.lsp.definition` to fall back to hoogle search (does not affect `vim.lsp.tagfunc`)


repl_backend *repl_backend*

Type: ~
"cabal"|"stack"


ReplOpts *ReplOpts*

Fields: ~
{handler} (string) `'builtin'`: Use the simple builtin repl. `'toggleterm'`: Use akinsho/toggleterm.nvim
{prefer} (repl_backend) Prefer cabal or stack when both stack and cabal project files are present?
{builtin} (BuiltinReplOpts) Configuration for the builtin repl
{auto_focus} (boolean|nil) Whether to auto-focus the repl on toggle or send. The default value of `nil` means the handler decides.

Expand Down
4 changes: 4 additions & 0 deletions lua/haskell-tools/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,11 @@
---@class DefinitionOpts
---@field hoogle_signature_fallback boolean (default: `false`) Configure `vim.lsp.definition` to fall back to hoogle search (does not affect `vim.lsp.tagfunc`)

---@alias repl_backend 'cabal' | 'stack'

---@class ReplOpts
---@field handler string `'builtin'`: Use the simple builtin repl. `'toggleterm'`: Use akinsho/toggleterm.nvim
---@field prefer repl_backend Prefer cabal or stack when both stack and cabal project files are present?
---@field builtin BuiltinReplOpts Configuration for the builtin repl
---@field auto_focus boolean|nil Whether to auto-focus the repl on toggle or send. The default value of `nil` means the handler decides.

Expand Down Expand Up @@ -123,6 +126,7 @@ config.defaults = {
---@type ReplOpts
repl = {
handler = 'builtin',
prefer = vim.fn.executable('stack') == 1 and 'stack' or 'cabal',
---@type BuiltinReplOpts
builtin = {
create_repl_window = function(view)
Expand Down
8 changes: 8 additions & 0 deletions lua/haskell-tools/config/check.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function config_check.validate()
end
local repl = tools.repl
local valid_handlers = { 'builtin', 'toggleterm' }
local valid_backends = { 'cabal', 'stack' }
ok, err = validate('tools.repl', {
auto_focus = { repl.auto_focus, 'boolean', true },
builtin = { repl.builtin, 'table' },
Expand All @@ -106,6 +107,13 @@ function config_check.validate()
end,
'one of ' .. vim.inspect(valid_handlers),
},
prefer = {
repl.prefer,
function(backend)
return vim.tbl_contains(valid_backends, backend)
end,
'one of ' .. vim.inspect(valid_backends),
},
})
if not ok then
return false, err
Expand Down
4 changes: 4 additions & 0 deletions lua/haskell-tools/repl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ function repl.mk_repl_cmd(file)
return nil
end
end
local opts = ht.config.options.tools.repl
if opts.prefer == 'stack' and project.is_stack_project(chk_path) then
return mk_stack_repl_cmd(file)
end
if project.is_cabal_project(chk_path) then
return mk_cabal_repl_cmd(file)
end
Expand Down
5 changes: 5 additions & 0 deletions nix/test-overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ in {
extraPkgs = [final.pkgs.haskellPackages.hoogle];
};

haskell-tools-test-with-stack = mkPlenaryTest {
name = "haskell-tools-with-stack";
extraPkgs = [final.pkgs.stack];
};

haskell-tools-test-nightly = mkPlenaryTest {
nvim = nvim-nightly;
name = "haskell-tools-nightly";
Expand Down
6 changes: 6 additions & 0 deletions tests/fixtures/stack/single-package/simple.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
cabal-version: 3.0
name: simple
version: 0.1.0.0
build-type: Simple
common warnings
ghc-options: -Wall
26 changes: 26 additions & 0 deletions tests/repl_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,29 @@ if deps.has_toggleterm() then
mk_repl_setup_test('toggleterm', true)
mk_repl_setup_test('toggleterm', false)
end

local repl = assert(ht.repl, 'repl not set up')
local cwd = vim.fn.getcwd()

describe('mk_repl_cmd', function()
local stack_project_file = cwd .. '/tests/fixtures/stack/single-package/Abc.hs'
if vim.fn.executable('stack') == 1 then
it('prefers stack if stack.yml exists', function()
local cmd = assert(repl.mk_repl_cmd(stack_project_file))
assert(#cmd > 1)
assert.same('stack', cmd[1])
end)
else
it('prefers cabal even if stack.yml exists if cabal files exist', function()
local cmd = assert(repl.mk_repl_cmd(stack_project_file))
assert(#cmd > 1)
assert.same('cabal', cmd[1])
end)
end
local cabal_project_file = cwd .. '/tests/fixtures/cabal/single-package/Abc.hs'
it('prefers cabal if no stack.yml exists and cabal files exist', function()
local cmd = assert(repl.mk_repl_cmd(cabal_project_file))
assert(#cmd > 1)
assert.same('cabal', cmd[1])
end)
end)

0 comments on commit 2c8cb26

Please sign in to comment.