Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax checker should respect beancount_root variable #35

Open
wzyboy opened this issue Dec 2, 2016 · 2 comments
Open

Syntax checker should respect beancount_root variable #35

wzyboy opened this issue Dec 2, 2016 · 2 comments

Comments

@wzyboy
Copy link
Contributor

wzyboy commented Dec 2, 2016

A year ago @yegle added syntax checker feature to vim-beancount (#8). It suddenly occurred to me that this syntax checker does not respect beancount_root variable. That is to say, if you split your leger into multiple files, that single bean-check command has no way to find all account names, leading to "Invalid account references" errors.

Since I am a n00b to VimL. Could you help?

@yegle
Copy link
Contributor

yegle commented Dec 2, 2016

I'm a noob on VimL too. But I suspect you can workaround this issue by including necessary file (in your case, the file that contains all open directives) in all your ledger files.

This won't work if you don't have a separate file containing all your open directives.

@jonsmithers
Copy link
Contributor

jonsmithers commented Sep 27, 2020

I fixed this for the ALE linter with some changes to my vimrc. Maybe I should put together a pull request.

augroup vimrc_beancount
  au!
  autocmd FileType beancount let b:beancount_root='main.beancount'

  " override ALE linter definition from nathangrigg/vim-beancount
  autocmd FileType beancount call ale#linter#PreventLoading('beancount')
  autocmd FileType beancount call ale#linter#Define('beancount', {
        \   'name': 'bean_check',
        \   'output_stream': 'stderr',
        \   'executable': 'bean-check',
        \   'command': 'bean-check ' . (exists('b:beancount_root') ? b:beancount_root : '%s'),
        \   'callback': function('<SID>HandleBeancountLint'),
        \})
augroup END

" this function is a modification of ale#handlers#unix#HandleAsError
function! <SID>HandleBeancountLint(buffer, lines) abort
  let l:pattern = '\v^([^:]+):(\d+):?(\d+)?:? ?(.+)$'
  let l:output = []

  let l:matches = ale#util#GetMatches(a:lines, l:pattern)

  for l:match in l:matches
    let l:file = l:match[1]
    let l:lnum = l:match[2] + 0
    let l:text = trim(l:match[4])
    if (l:file != expand('%:p'))
      " This error is for a different file, so assign it to line 0 and prepend
      " the culprit filename to the lint text
      let l:lnum = 0
      let l:relativefile = fnamemodify(l:file, ':.')
      let l:text = '('.l:relativefile.') '.l:text
    endif
    call add(l:output, {
          \   'lnum': l:lnum,
          \   'col': l:match[3] + 0,
          \   'text': l:text,
          \   'type': 'E',
          \})
  endfor

  return l:output
endfunction

jonsmithers added a commit to jonsmithers/vim-beancount that referenced this issue Oct 16, 2020
The bean_check ale linter now runs against the root beancount file
defined by b:beancount_root if it is set. Errors from bean-check that do
not correspond to the current file will be reported on line 0.

Addresses nathangrigg#35
xentac pushed a commit that referenced this issue Mar 6, 2021
The bean_check ale linter now runs against the root beancount file
defined by b:beancount_root if it is set. Errors from bean-check that do
not correspond to the current file will be reported on line 0.

Addresses #35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants