Skip to content

Commit 6c52a6e

Browse files
committed
Create new namespace for version utilities
* Move version parsing and checking to its own namespace * Store 'ghci' or 'intero' marker along with the version * Add version parsing tests for Intero, new GHCi, and an old GHCi
1 parent 8df0131 commit 6c52a6e

File tree

4 files changed

+841
-22
lines changed

4 files changed

+841
-22
lines changed

autoload/intero/process.vim

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@ let g:intero_started = 0
1818
" Whether Intero has done its initialization yet
1919
let s:intero_initialized = 0
2020

21-
let s:no_version = [0, 0, 0]
2221
" The version of GHCi, parsed on startup.
23-
let g:intero_ghci_version = s:no_version
22+
let g:intero_ghci_version = g:intero#process#version#no_version
2423

2524
" If true, echo the next response. Reset after each response.
2625
let g:intero_echo_next = 0
@@ -293,27 +292,9 @@ function! s:on_stdout(jobid, lines, event) abort
293292
endfor
294293
endfunction
295294

296-
function! s:parse_ghci_version(output) abort
297-
for l:l in a:output
298-
" Try parsing regular GHCi version.
299-
let l:matches = matchlist(l:l, 'GHCi, version \(\d*\)\.\(\d*\).\(\d*\):')
300-
if !empty(l:matches)
301-
return [l:matches[1] + 0, l:matches[2] + 0, l:matches[3] + 0]
302-
else
303-
" Fallback to parsing Intero-style version.
304-
let l:matches = matchlist(l:l, '(GHC \(\d*\)\.\(\d*\).\(\d*\))')
305-
if !empty(l:matches)
306-
return [l:matches[1] + 0, l:matches[2] + 0, l:matches[3] + 0]
307-
endif
308-
endif
309-
endfor
310-
311-
return g:no_version
312-
endfunction
313-
314295
function! s:on_initial_compile(output) abort
315-
if g:intero_ghci_version == [0, 0, 0]
316-
let g:intero_ghci_version = s:parse_ghci_version(a:output)
296+
if g:intero_ghci_version == g:intero#process#version#no_version
297+
let g:intero_ghci_version = g:intero#process#version#parse_lines(a:output)
317298
endif
318299

319300
" Trigger Neomake's parsing of the compilation errors

autoload/intero/process/version.vim

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""""""""""
2+
" Version:
3+
"
4+
" This file contains functions for parsing and checking GHCi versions.
5+
"""""""""""
6+
7+
" A parsed version is a list of 4 elements, containing one string (either
8+
" 'intero' or 'ghci'), and three numbers, for the MAJOR, MINOR, and PATCH
9+
" versions.
10+
11+
" An empty version value that can be used as a default, or to signal that
12+
" parsing failed.
13+
let g:intero#process#version#no_version = ['', 0, 0, 0]
14+
15+
" Tries to parse the GHCi or Intero version from a list of lines.
16+
function! intero#process#version#parse_lines(output) abort
17+
for l:l in a:output
18+
" Try parsing regular GHCi version.
19+
let l:matches = matchlist(l:l, 'GHC\%(i\|\sInteractive\), version \(\d*\)\.\(\d*\).\(\d*\)[:,]')
20+
if !empty(l:matches)
21+
return ['ghci', l:matches[1] + 0, l:matches[2] + 0, l:matches[3] + 0]
22+
else
23+
" Fallback to parsing Intero-style version.
24+
let l:matches = matchlist(l:l, 'Intero .* (GHC \(\d*\)\.\(\d*\).\(\d*\))')
25+
if !empty(l:matches)
26+
return ['intero', l:matches[1] + 0, l:matches[2] + 0, l:matches[3] + 0]
27+
endif
28+
endif
29+
endfor
30+
31+
return g:intero#process#version#no_version
32+
endfunction
33+
34+
function! intero#process#version#is_intero(version) abort
35+
return a:version[0] ==# 'intero'
36+
endfunction
37+
38+
function! intero#process#version#is_ghci(version) abort
39+
return a:version[0] ==# 'ghci'
40+
endfunction

test/intero/process/version.vader

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Execute (parse_lines handles GHCi output):
2+
AssertEqual intero#process#version#parse_lines([ "GHCi, version 8.2.1: http://www.haskell.org/ghc/ :? for help",
3+
\ "Loaded GHCi configuration from /home/user/.ghci",
4+
\ "Prelude>"]), ["ghci", 8, 2, 1]
5+
6+
Execute (parse_lines handles older GHCi output):
7+
AssertEqual intero#process#version#parse_lines([ " ___ ___ _",
8+
\ " / _ \ /\ /\/ __(_)",
9+
\ " / /_\// /_/ / / | | GHC Interactive, version 6.0.1, for Haskell 98.",
10+
\ "/ /_\\/ __ / /___| | http://www.haskell.org/ghc/",
11+
\ "\____/\/ /_/\____/|_| Type :? for help."]), ["ghci", 6, 0, 1]
12+
13+
Execute (parse_lines handles Intero output):
14+
AssertEqual intero#process#version#parse_lines([ "Intero 0.1.21 (GHC 8.0.2)",
15+
\ "Type :intro and press enter for an introduction of the standard commands.",
16+
\ "Prelude>"]), ["intero", 8, 0, 2]

0 commit comments

Comments
 (0)