summaryrefslogtreecommitdiffstats
path: root/autoload/nim.vim
blob: c913af9936c259813ee9384aa3a60a3eb6dd4424 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
if !has_key(g:polyglot_is_disabled, 'nim')
  finish
endif

let g:nim_log = []
let s:plugin_path = escape(expand('<sfile>:p:h'), '\')

if !exists('g:nim_caas_enabled')
  let g:nim_caas_enabled = 0
endif

if !executable('nim')
  echoerr "the Nim compiler must be in your system's PATH"
endif

if has('pythonx')
  exe 'pyxfile ' . fnameescape(s:plugin_path) . '/nim_vim.py'
endif

fun! nim#init() abort
  let cmd = printf('nim --dump.format:json --verbosity:0 dump %s', s:CurrentNimFile())
  let raw_dumpdata = system(cmd)
  if !v:shell_error && expand('%:e') ==# 'nim'
    let false = 0 " Needed for eval of json
    let true = 1 " Needed for eval of json
    let dumpdata = eval(substitute(raw_dumpdata, "\n", '', 'g'))
    
    let b:nim_project_root = dumpdata['project_path']
    let b:nim_defined_symbols = dumpdata['defined_symbols']
    let b:nim_caas_enabled = g:nim_caas_enabled || index(dumpdata['defined_symbols'], 'forcecaas') != -1

    for path in dumpdata['lib_paths']
      if finddir(path) ==# path
        let &l:path = path . ',' . &l:path
      endif
    endfor
  else
    let b:nim_caas_enabled = 0
  endif
endf

fun! s:UpdateNimLog() abort
  setlocal buftype=nofile
  setlocal bufhidden=hide
  setlocal noswapfile

  for entry in g:nim_log
    call append(line('$'), split(entry, "\n"))
  endfor

  let g:nim_log = []

  match Search /^nim\ .*/
endf

augroup NimVim
  au!
  au BufEnter log://nim call s:UpdateNimLog()
  if has('pythonx')
    " au QuitPre * :pyx nimTerminateAll()
    au VimLeavePre * :pyx nimTerminateAll()
  endif
augroup END

command! NimLog :e log://nim

command! NimTerminateService
  \ :exe printf("pyx nimTerminateService('%s')", b:nim_project_root)

command! NimRestartService
  \ :exe printf("pyx nimRestartService('%s')", b:nim_project_root)

fun! s:CurrentNimFile() abort
  let save_cur = getpos('.')
  call cursor(0, 0, 0)
  
  let PATTERN = '\v^\#\s*included from \zs.*\ze'
  let l = search(PATTERN, 'n')

  if l != 0
    let f = matchstr(getline(l), PATTERN)
    let l:to_check = expand('%:h') . '/' . f
  else
    let l:to_check = expand('%')
  endif

  call setpos('.', save_cur)
  return l:to_check
endf

let g:nim_symbol_types = {
  \ 'skParam': 'v',
  \ 'skVar': 'v',
  \ 'skLet': 'v',
  \ 'skTemp': 'v',
  \ 'skForVar': 'v',
  \ 'skConst': 'v',
  \ 'skResult': 'v',
  \ 'skGenericParam': 't',
  \ 'skType': 't',
  \ 'skField': 'm',
  \ 'skProc': 'f',
  \ 'skMethod': 'f',
  \ 'skIterator': 'f',
  \ 'skConverter': 'f',
  \ 'skMacro': 'f',
  \ 'skTemplate': 'f',
  \ 'skEnumField': 'v',
  \ }

fun! NimExec(op) abort
  let isDirty = getbufvar(bufnr('%'), '&modified')
  if isDirty
    let tmp = tempname() . bufname('%') . '_dirty.nim'
    silent! exe ':w ' . tmp

    let cmd = printf('idetools %s --trackDirty:"%s,%s,%d,%d" "%s"',
      \ a:op, tmp, expand('%:p'), line('.'), col('.')-1, s:CurrentNimFile())
  else
    let cmd = printf('idetools %s --track:"%s,%d,%d" "%s"',
      \ a:op, expand('%:p'), line('.'), col('.')-1, s:CurrentNimFile())
  endif

  if b:nim_caas_enabled
    exe printf("pyx nimExecCmd('%s', '%s', False)", b:nim_project_root, cmd)
    let output = get(l:, 'py_res', '')
  else
    let output = system('nim ' . cmd)
  endif

  call add(g:nim_log, 'nim ' . cmd . "\n" . output)
  return output
endf

fun! NimExecAsync(op, Handler) abort
  let result = NimExec(a:op)
  call a:Handler(result)
endf

fun! NimComplete(findstart, base) abort
  if b:nim_caas_enabled ==# 0
    return -1
  endif

  if a:findstart
    if synIDattr(synIDtrans(synID(line('.'),col('.'),1)), 'name') ==# 'Comment'
      return -1
    endif
    let line = getline('.')
    let start = col('.') - 1
    while start > 0 && line[start - 1] =~? '\w'
      let start -= 1
    endwhile
    return start
  else
    let result = []
    let sugOut = NimExec('--suggest')
    for line in split(sugOut, '\n')
      let lineData = split(line, '\t')
      if len(lineData) > 0 && lineData[0] ==# 'sug'
        let word = split(lineData[2], '\.')[-1]
        if a:base ==? '' || word =~# '^' . a:base
          let kind = get(g:nim_symbol_types, lineData[1], '')
          let c = { 'word': word, 'kind': kind, 'menu': lineData[3], 'dup': 1 }
          call add(result, c)
        endif
      endif
    endfor
    return result
  endif
endf

if !exists('g:neocomplcache_omni_patterns')
  let g:neocomplcache_omni_patterns = {}
endif
let g:neocomplcache_omni_patterns['nim'] = '[^. *\t]\.\w*'

if !exists('g:neocomplete#sources#omni#input_patterns')
  let g:neocomplete#sources#omni#input_patterns = {}
endif
let g:neocomplete#sources#omni#input_patterns['nim'] = '[^. *\t]\.\w*'

let g:nim_completion_callbacks = {}

fun! NimAsyncCmdComplete(cmd, output) abort
  call add(g:nim_log, a:output)
  echom g:nim_completion_callbacks
  if has_key(g:nim_completion_callbacks, a:cmd)
    let Callback = get(g:nim_completion_callbacks, a:cmd)
    call Callback(a:output)
    " remove(g:nim_completion_callbacks, a:cmd)
  else
    echom 'ERROR, Unknown Command: ' . a:cmd
  endif
  return 1
endf

fun! GotoDefinition_nim_ready(def_output) abort
  if v:shell_error
    echo 'nim was unable to locate the definition. exit code: ' . v:shell_error
    " echoerr a:def_output
    return 0
  endif
  
  let rawDef = matchstr(a:def_output, 'def\t\([^\n]*\)')
  if rawDef == ''
    echo 'the current cursor position does not match any definitions'
    return 0
  endif
  
  let defBits = split(rawDef, '\t')
  let file = defBits[4]
  let line = defBits[5]
  exe printf('e +%d %s', line, file)
  return 1
endf

fun! GotoDefinition_nim() abort
  call NimExecAsync('--def', function('GotoDefinition_nim_ready'))
endf

fun! FindReferences_nim() abort
  "setloclist()
endf

" Syntastic syntax checking
fun! SyntaxCheckers_nim_nim_GetLocList() abort
  let makeprg = 'nim check --hints:off --listfullpaths ' . s:CurrentNimFile()
  let errorformat = &errorformat
  
  return SyntasticMake({ 'makeprg': makeprg, 'errorformat': errorformat })
endf

function! SyntaxCheckers_nim_nim_IsAvailable() abort
  return executable('nim')
endfunction

if exists('g:SyntasticRegistry')
  call g:SyntasticRegistry.CreateAndRegisterChecker({
      \ 'filetype': 'nim',
      \ 'name': 'nim'})
endif