Skip to content

Commit

Permalink
Merge pull request neomake#1866 from neomake/stdin-cwd
Browse files Browse the repository at this point in the history
cwd: default to buffer's dir in stdin mode
  • Loading branch information
blueyed authored Feb 22, 2018
2 parents ad3aea3 + d719f58 commit 45af88d
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 178 deletions.
23 changes: 14 additions & 9 deletions autoload/neomake.vim
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,19 @@ function! s:MakeJob(make_id, options) abort
\ 'buffer_output': a:options.maker.buffer_output,
\ }, 'keep')

let [cd_error, cwd, cd_back_cmd] = s:cd_to_jobs_cwd(jobinfo)
if !empty(cd_error)
throw printf("Neomake: %s: could not change to maker's cwd (%s): %s.",
\ maker.name, cwd, cd_error)
endif

let error = ''
let cd_back_cmd = ''
try
let error = ''
let jobinfo.argv = maker._get_argv(jobinfo)
call neomake#utils#hook('NeomakeJobInit', {'jobinfo': jobinfo})

" Change to job's cwd (after args, which may use uses_stdin to set it).
let [cd_error, cwd, cd_back_cmd] = s:cd_to_jobs_cwd(jobinfo)
if !empty(cd_error)
throw printf("Neomake: %s: could not change to maker's cwd (%s): %s.",
\ maker.name, cwd, cd_error)
endif

call neomake#utils#hook('NeomakeJobInit', {'jobinfo': jobinfo})
if s:async
call neomake#utils#LoudMessage(printf('Starting async job: %s.', string(jobinfo.argv)), jobinfo)
else
Expand Down Expand Up @@ -1702,12 +1704,15 @@ function! s:cd_to_jobs_cwd(jobinfo) abort
let cwd = expand(cwd, 1)
endif
let a:jobinfo.cwd = substitute(fnamemodify(cwd, ':p'), '[\/]$', '', '')
elseif get(a:jobinfo, 'uses_stdin', 0)
call neomake#utils#DebugMessage("Using buffer's directory for cwd with uses_stdin.", a:jobinfo)
let a:jobinfo.cwd = neomake#utils#fnamemodify(a:jobinfo.bufnr, ':h')
else
let a:jobinfo.cwd = s:make_info[a:jobinfo.make_id].cwd
endif
endif
let cwd = a:jobinfo.cwd
if empty(cwd)
if empty(cwd) || cwd ==# '.'
return ['', '', '']
endif
let cwd = substitute(fnamemodify(cwd, ':p'), '[\/]$', '', '')
Expand Down
3 changes: 2 additions & 1 deletion doc/neomake.txt
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,8 @@ You can specify the default for this via |g:neomake_remove_invalid_entries|.

*neomake-makers-cwd*
The working directory of a maker defaults to the current working directory
of the make run (|getcwd()|).
of the make run (|getcwd()|), or the buffer's directory in case of using stdin
(see |neomake-makers-supports_stdin|).
The `cwd` property overrides this, and gets expanded in the context of the
current buffer. Special buffers (like fugitive blobs) get handled for values
starting with `%:` (typically used in this context), falling back to
Expand Down
1 change: 1 addition & 0 deletions tests/main.vader
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Include (Processing): processing.vader
Include (Serialize): serialize.vader
Include (Signs): signs.vader
Include (Statusline): statusline.vader
Include (Stdin): stdin.vader
Include (Temporary files): tempfiles.vader
Include (Toggling): toggle.vader
Include (Utils): utils.vader
Expand Down
188 changes: 188 additions & 0 deletions tests/stdin.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
Include: include/setup.vader

Execute (stdin maker):
let maker = {'exe': 'cat', 'supports_stdin': 1, 'errorformat': '%f:%m'}
new
" Stdin should be used also when tempfiles are disabled.
let b:neomake_tempfile_enabled = 0
normal! ifile1.test:line1
normal! ofile1.test:line2
call neomake#Make(1, [maker])
AssertNeomakeMessage "Using buffer's directory for cwd with uses_stdin.", 3
if neomake#has_async_support()
AssertNeomakeMessage "Starting async job: ['cat', '-'].", 2
NeomakeTestsWaitForFinishedJobs
endif
AssertNeomakeMessage 'cwd: .', 3
AssertNeomakeMessage '\mstdout: unnamed_maker: [''file1.test:line1.*'
AssertEqual map(getloclist(0), 'v:val.text'), ['line1', 'line2']
bwipe!

Execute (stdin maker (args as string)):
let maker = {'exe': 'cat', 'args': '', 'supports_stdin': 1, 'errorformat': '%f:%m'}
new
" Stdin should be used also when tempfiles are disabled.
let b:neomake_tempfile_enabled = 0
normal! ifile1.test:line1
normal! ofile1.test:line2
call neomake#Make(1, [maker])
if neomake#has_async_support()
if has('nvim')
AssertNeomakeMessage "Starting async job: 'cat \\-'.", 2
else
let shell_argv = "'".join(split(&shell) + split(&shellcmdflag), "', '")."'"
AssertNeomakeMessage "Starting async job: [".shell_argv.", 'cat \\-'].", 2
endif
NeomakeTestsWaitForFinishedJobs
endif
AssertNeomakeMessage '\mstdout: unnamed_maker: [''file1.test:line1.*'
AssertEqual map(getloclist(0), 'v:val.text'), ['line1', 'line2']
bwipe!

Execute (stdin maker (disabled tempfiles)):
let maker = {'exe': 'cat', 'supports_stdin': 1, 'tempfile_name': '-'}
new
" Stdin should be used also when tempfiles are disabled.
let b:neomake_tempfile_enabled = 0
normal! iline1
normal! oline2
call neomake#Make(1, [maker])
AssertNeomakeMessage 'Using stdin for unnamed buffer.'
if neomake#has_async_support()
AssertNeomakeMessage "Starting async job: ['cat', '-'].", 2
NeomakeTestsWaitForFinishedJobs
else
AssertNeomakeMessage 'Starting: cat -.', 2
endif
AssertNeomakeMessage '\mstdout: unnamed_maker: [''line1.*'
AssertEqual map(getloclist(0), 'v:val.text'), ['line1', 'line2']
bwipe!

Execute (stdin maker (project mode: append_file)):
let maker = {'exe': 'cat', 'supports_stdin': 1, 'tempfile_name': '-',
\ 'append_file': 1}
new
" Stdin should be used also when tempfiles are disabled.
let b:neomake_tempfile_enabled = 0
normal! iline1
normal! oline2
call neomake#Make(0, [maker])
if neomake#has_async_support()
AssertNeomakeMessage "Starting async job: ['cat', '-'].", 2
NeomakeTestsWaitForFinishedJobs
else
AssertNeomakeMessage 'Starting: cat -.', 2
endif
AssertNeomakeMessage '\mstdout: unnamed_maker: [''line1.*'
AssertEqual map(getqflist(), 'v:val.text'), ['line1', 'line2']
bwipe!

Execute (stdin maker (project mode: uses_filename)):
let maker = {'exe': 'cat', 'args': '$NEOMAKE_FILE', 'supports_stdin': 1,
\ 'uses_filename': 1}
new
" Stdin should be used also when tempfiles are disabled.
let b:neomake_tempfile_enabled = 0
normal! iline1
normal! oline2
call neomake#Make(0, [maker])
AssertNeomakeMessage 'Using stdin for unnamed buffer.'
if neomake#has_async_support()
if has('nvim')
AssertNeomakeMessage "Starting async job: 'cat $NEOMAKE_FILE'.", 2
else
let shell_argv = "'".join(split(&shell) + split(&shellcmdflag), "', '")."'"
AssertNeomakeMessage "Starting async job: [".shell_argv.", 'cat $NEOMAKE_FILE'].", 2
endif
NeomakeTestsWaitForFinishedJobs
else
AssertNeomakeMessage 'Starting: cat $NEOMAKE_FILE.', 2
endif
AssertNeomakeMessage '\mstdout: unnamed_maker: [''line1.*'
AssertEqual map(getqflist(), 'v:val.text'), ['line1', 'line2']
bwipe!

Execute (stdin maker: supports_stdin can be a callback adding args):
let maker = {'exe': 'printf', 'args': ['%s\n']}
function maker.supports_stdin(jobinfo)
let self.args += ['added_arg']
return 1
endfunction
new
set buftype=nofile
CallNeomake 1, [maker]
AssertNeomakeMessage 'Using stdin for unnamed buffer.'
AssertEqual map(getloclist(0), 'v:val.text'), ['added_arg', '-']
bwipe

Execute (stdin maker: supports_stdin can be a callback adding args, and setting tempfile_name):
let maker = {'exe': 'printf', 'args': ['%s\n']}
function maker.supports_stdin(jobinfo)
let self.args += ['added_arg']
let self.tempfile_name = 'custom_tempfile'
return 1
endfunction
new
set buftype=nofile
CallNeomake 1, [maker]
AssertNeomakeMessage 'Using stdin for unnamed buffer.'
AssertEqual map(getloclist(0), 'v:val.text'), ['added_arg', 'custom_tempfile']
bwipe

Execute (stdin maker: supports_stdin can be a callback returning 0):
let maker = {'exe': 'printf', 'args': ['%s\n']}
function maker.supports_stdin(jobinfo)
return 0
endfunction
new
set buftype=nofile
CallNeomake 1, [maker]
AssertNeomakeMessage '\v^Using tempfile for unnamed buffer: "(.*)".$'
let tempfile_name = g:neomake_test_matchlist[1]
AssertEqual map(getloclist(0), 'v:val.text'), [tempfile_name]
bwipe

Execute (stdin maker: supports_stdin can be a callback returning 0 and change args):
let maker = {'exe': 'printf', 'args': ['%s\n']}
function maker.supports_stdin(jobinfo)
let self.args += ['added_arg']
return 0
endfunction
new
set buftype=nofile
CallNeomake 1, [maker]
AssertNeomakeMessage '\v^Using tempfile for unnamed buffer: "(.*)".$'
let tempfile_name = g:neomake_test_matchlist[1]
AssertEqual map(getloclist(0), 'v:val.text'), ['added_arg', tempfile_name]
bwipe

Execute (stdin maker: uses neomake#utils#get_buffer_lines for buffer_lines):
let maker = {'exe': 'cat', 'supports_stdin': 1}
new
set buftype=nofile
CallNeomake 1, [maker]
AssertNeomakeMessage '\v^Using tempfile for unnamed buffer: "(.*)".$'
let tempfile_name = g:neomake_test_matchlist[1]
AssertEqual map(getloclist(0), 'v:val.text'), []

normal! o
CallNeomake 1, [maker]
AssertEqual map(getloclist(0), 'v:val.text'), ['', '']
bwipe

Execute (stdin maker: uses buffer's cwd by default):
let maker = {'exe': 'true', 'supports_stdin': 1}
let fname = tempname()
new
exe 'file '.fname

CallNeomake 1, [maker]
AssertNeomakeMessage printf('cwd: %s (changed).', fnamemodify(fname, ':h')), 3

CallNeomake 0, [extend(copy(maker), {'uses_filename': 1})]
AssertNeomakeMessage printf('cwd: %s (changed).', fnamemodify(fname, ':h')), 3

let maker.cwd = '.'
CallNeomake 1, [maker]
AssertNeomakeMessage printf('cwd: %s.', getcwd()), 3
bwipe
Loading

0 comments on commit 45af88d

Please sign in to comment.