Haskell nvim language server integration

I set up my neo vim for haskell by following this blog
https://jkuokkanen109157944.wordpress.com/2020/11/10/creating-a-haskell-development-environment-with-lsp-on-nixos/
the only things that are suggested from coc are lexicographical things (that coc can read in the file) but it doesn’t use the language server so it doesn’t give me hints like how the function fmap is declared.
Mine:
image
Instead of:
image
my neovim nix looks like this I think I installed every package that is needed for this autocompletion

{ pkgs, ... }:
let
  vimrc = pkgs.callPackage ./vimrc.nix {};
  plugin = pkgs.callPackage ./plugins.nix {};
in {
  environment.systemPackages = with pkgs; [
    ghc
    cabal2nix
    haskellPackages.haskell-language-server
    cabal-install
    hlint
    (neovim.override {
      vimAlias = true;
      withNodeJs = true;
      configure = {
        packages.myPlugins = with pkgs.vimPlugins; {
          start = [
                    coc-nvim
                    haskell-vim
                    ghcid
                    vim-hindent
                    coc-json
                    coc-vimlsp
                  ];
          opt = [];
        };
        customRC = vimrc;
      };
    }
  )];
}


my Cocconfig doesn’t fail and it starts the language server

{"languageserver": {
  "haskell": {
    "command": "haskell-language-server-wrapper",
    "args": ["--lsp"],
    "filetypes": [ "lhaskell", "haskell" ],
     "initializationOptions": {
        "languageServerHaskell": {
          "hlintOn": true,
          "maxNumberOfProblems": 10,
          "completionSnippetsOn": true
        }
     }
  }
  }
}

Do I Need a plugin or nix package, that the autocompletion uses the language server?

If this is the wrong Forum I am sorry, I thought the problem lies in the nix config and not in the vimrc (Feel free to suggest the right Forum.)
For Completeness my vimrc


" Helps force plugins to load correctly when it is turned back on below
filetype off
" Turn on syntax highlighting
syntax enable


" For plugins to load correctly
filetype plugin indent on

" Security
set modelines=0

" Show line numbers
set number relativenumber

" Show file stats
set ruler

" Blink cursor on error instead of beeping (grr)
set visualbell

" Encoding
set encoding=utf-8
set autoread

" Whitespace
set wrap
set textwidth=79
set formatoptions=tcqrn1
set tabstop=2
set shiftwidth=2
set softtabstop=2
set expandtab
set noshiftround

" Cursor motion
set scrolloff=3
set backspace=indent,eol,start
set matchpairs+=<:> " use % to jump between pairs
runtime! macros/matchit.vim

" Move up/down editor lines
nnoremap j gj
nnoremap k gk

" Allow hidden buffers
set hidden

" Rendering
set ttyfast

" Status bar
set laststatus=2

" Last line
set showmode
set showcmd

" Searching
nnoremap / /\v
vnoremap / /\v
set hlsearch
set incsearch
set ignorecase
set smartcase
set showmatch
map <leader><space> :let @/=''<cr> " clear search

" Formatting
map <leader>q gqip

" Visualize tabs and newlines
set listchars=tab:▸\ ,eol:¬

map <leader>l :set list!<CR> " Toggle tabs and EOL

" use <tab> for trigger completion and navigate to the next complete item
function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~ '\s'
endfunction

inoremap <silent><expr> <Tab>
      \ pumvisible() ? "\<C-n>" :
      \ <SID>check_back_space() ? "\<Tab>" :
      \ coc#refresh()

Hey @flix59,

I run hls+coc+nvim, so it should be doable.

First question when debugging hls: Does running haskell-language-server and/or haskell-language-server-wrapper in your project report any errors?

For reference this is my nvim+coc config:

For good measure I will also point to the hls section in the haskell nixpkgs manual, though it might not address your specific problem:
https://haskell4nix.readthedocs.io/nixpkgs-users-guide.html#how-to-install-haskell-language-server

Also have you checked in a task manager that haskell-language-server is being started?

1 Like

Ok the language server failed in the consol but vim didn’t show it.
The problem was that i didn’t realize, the hls needs all the dependencies from the nix-shell of my project so I need to start vim from within the shell of course. Thanks for the fast help

1 Like

Ok I understand why I got lucky yesterday, I put the one Haskell file as executional in my cabal file. Currently I just have little scripts that I start with ghci and just get a number as output is it possible to get the prelude definitions without the setup of a project where I define every file?

I am not sure what you mean. If you just run little scripts without cabal or stack you still always have Prelude imported by default. Also you can use all libraries listed in the ghc release notes, (i.e. base, time, and some others) simply by importing their modules. For me hls also works without a cabal file one simple script files.

ok then where is something wrong… when I run the hls in a nixshell i get errors on all .hs files

File:    haskell/nixosTest/myscript.hs
Hidden:   no
Range:    1:0-2:0
Source:   cradle
Severity: DsError
Message: 
  Failed to parse result of calling cabal
  
  cabal: Unknown target '/home/felix/Documents/haskell/nixosTest/myscript.hs'.
  The package nixosTest has no file target 'myscript.hs'.

on all files, which aren’t in the cabal project of that nix-shell

Ah, okay. Yeah, that is to be expected. I think if you want not-cabal-tracked files to work with hls in a cabal project you probably need to create a manual hie.yaml, possibly with a multi-cradle or something.