let g:ale_linters = {
\ 'haskell': ['hls'],
\}
let b:ale_fixers = {
\ 'haskell': ['hls', 'hlint']
\}
let g:ale_command_wrapper = 'nice -n5 %*'
let g:ale_haskell_hls_executable = 'haskell-language-server-wrapper'

let g:ale_close_preview_on_insert = 1
let g:ale_cursor_detail = 0
let g:ale_keep_list_window_open = 0
let g:ale_lint_delay = 200
let g:ale_lint_on_enter = 0
let g:ale_lint_on_insert_leave = 0
let g:ale_lint_on_save = 1
let g:ale_lint_on_text_changed = 'never'
let g:ale_list_vertical = 0
let g:ale_list_window_size = 5
let g:ale_lsp_show_message_severity = 'information'
let g:ale_lsp_suggestions = 1
let g:ale_maximum_file_size = 1000000
let g:ale_open_list = 1
let g:ale_set_balloons = 1
let g:ale_set_highlights = 1
let g:ale_set_loclist = 1
let g:ale_set_quickfix = 0
let g:ale_sign_column_always = 1
let g:ale_sign_error = '!!'
let g:ale_sign_warning = ' !'
let g:ale_virtualtext_cursor = 1
let g:ale_virtualtext_delay = 10

" Unfortunately, this does not update the loclist position
"nmap <silent> <C-j> :ALEPrevious<CR>
"nmap <silent> <C-l> :ALENext<CR>
nmap <silent> <C-w> :ALEPrevious -wrap -error<CR>
nmap <silent> <C-e> :ALENext -wrap -error<CR>
nmap gd :ALEGoToDefinition<CR>
nmap gm :ALECodeAction<CR>
"nmap <silent> <C-k> <Plug>(ale_previous_wrap)
"nmap <silent> <C-l> <Plug>(ale_next_wrap)

function! LinterStatus() abort
  let l:counts = ale#statusline#Count(bufnr(''))
  let l:all_errors = l:counts.error + l:counts.style_error
  let l:all_non_errors = l:counts.total - l:all_errors
  if !get(g:, 'ale_enabled', 0)
    return ''
  elseif index(g:ale_filetype_blacklist, &filetype) != -1
    return ''
  elseif ale#engine#IsCheckingBuffer(bufnr('')) == 1
    return '...'
  elseif l:counts.total == 0
    return 'OK'
  else
    return printf('%dW %dE', all_non_errors, all_errors)
  fi
endfunction
" Refresh the statusline
augroup RefreshStatusline
  autocmd!
  autocmd CursorHold,BufWritePost * :redrawstatus
  autocmd User ALEJobStarted,ALELintPost :redrawstatus
augroup END

" Close the loclist window automatically when the buffer is closed
augroup CloseLoclistWindowGroup
  autocmd!
  autocmd QuitPre * if empty(&buftype) | lclose | endif
augroup END

hi ALEWarningSign ctermbg=darkyellow ctermfg=black
hi ALEVirtualTextError ctermfg=red
hi ALEVirtualTextWarning ctermfg=yellow
hi ALEVirtualTextInfo ctermfg=blue
"hi clear ALEWarning

let g:ale_completion_enabled = 0
"set omnifunc=ale#completion#OmniFunc
"au FileType haskell set omnifunc=ale#completion#OmniFunc

imap <C-p> <C-x><C-o>
imap <silent><expr> <TAB>
 \ pumvisible() ? "\<C-n>" :
 \ <SID>check_back_space() ? "\<TAB>" :
 \ "\<C-p>"
function! s:check_back_space() abort
	let col = col('.') - 1
	return !col || getline('.')[col - 1] =~# '\s'
endfunction
let g:ale_completion_symbols = {
\ 'text': '',
\ 'method': '',
\ 'function': 'fun',
\ 'constructor': 'cons',
\ 'field': '',
\ 'variable': 'var',
\ 'class': 'type',
\ 'interface': '',
\ 'module': 'mod',
\ 'property': 'prop',
\ 'unit': 'unit',
\ 'value': 'val',
\ 'enum': '',
\ 'keyword': 'keyword',
\ 'snippet': '',
\ 'color': 'color',
\ 'file': '',
\ 'reference': 'ref',
\ 'folder': '',
\ 'enum member': '',
\ 'constant': '',
\ 'struct': '',
\ 'event': 'event',
\ 'operator': '',
\ 'type_parameter': 'type param',
\ '<default>': 'v'
\ }