Skip to content

Commit

Permalink
Closes dense-analysis#3019 - Implement default navigation
Browse files Browse the repository at this point in the history
Default navigation for commands that jump to new locations has been
implemented with the `ale_default_navigation` variable, and all commands
that jump to locations now support `-tab`, `-split`, or `-vsplit`
arguments for overriding the default navigation behavior.
  • Loading branch information
w0rp committed Apr 15, 2020
1 parent bbe5153 commit 82f734a
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 113 deletions.
39 changes: 39 additions & 0 deletions autoload/ale/definition.vim
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let s:go_to_definition_map = {}

" Enable automatic updates of the tagstack
let g:ale_update_tagstack = get(g:, 'ale_update_tagstack', 1)
let g:ale_default_navigation = get(g:, 'ale_default_navigation', 'buffer')

" Used to get the definition map in tests.
function! ale#definition#GetMap() abort
Expand Down Expand Up @@ -134,6 +135,10 @@ function! s:GoToLSPDefinition(linter, options, capability) abort
endfunction

function! ale#definition#GoTo(options) abort
if !get(g:, 'ale_ignore_2_7_warnings') && has_key(a:options, 'deprecated_command')
execute 'echom '':' . a:options.deprecated_command . ' is deprecated. Use `let g:ale_ignore_2_7_warnings = 1` to disable this message.'''
endif

for l:linter in ale#linter#Get(&filetype)
if !empty(l:linter.lsp)
call s:GoToLSPDefinition(l:linter, a:options, 'definition')
Expand All @@ -142,6 +147,10 @@ function! ale#definition#GoTo(options) abort
endfunction

function! ale#definition#GoToType(options) abort
if !get(g:, 'ale_ignore_2_7_warnings') && has_key(a:options, 'deprecated_command')
execute 'echom '':' . a:options.deprecated_command . ' is deprecated. Use `let g:ale_ignore_2_7_warnings = 1` to disable this message.'''
endif

for l:linter in ale#linter#Get(&filetype)
if !empty(l:linter.lsp)
" TODO: handle typeDefinition for tsserver if supported by the
Expand All @@ -154,3 +163,33 @@ function! ale#definition#GoToType(options) abort
endif
endfor
endfunction

function! ale#definition#GoToCommandHandler(command, ...) abort
let l:options = {}

if len(a:000) > 0
for l:option in a:000
if l:option is? '-tab'
let l:options.open_in = 'tab'
elseif l:option is? '-split'
let l:options.open_in = 'split'
elseif l:option is? '-vsplit'
let l:options.open_in = 'vsplit'
endif
endfor
endif

if !has_key(l:options, 'open_in')
let l:default_navigation = ale#Var(bufnr(''), 'default_navigation')

if index(['tab', 'split', 'vsplit'], l:default_navigation) >= 0
let l:options.open_in = l:default_navigation
endif
endif

if a:command is# 'type'
call ale#definition#GoToType(l:options)
else
call ale#definition#GoTo(l:options)
endif
endfunction
37 changes: 29 additions & 8 deletions autoload/ale/preview.vim
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
" Author: w0rp <[email protected]>
" Description: Preview windows for showing whatever information in.

if !has_key(s:, 'last_selection_list')
let s:last_selection_list = []
endif

if !has_key(s:, 'last_selection_open_in')
let s:last_selection_open_in = 'current-buffer'
endif

" Open a preview window and show some lines in it.
" A second argument can be passed as a Dictionary with options. They are...
"
Expand Down Expand Up @@ -67,32 +75,45 @@ function! ale#preview#ShowSelection(item_list, ...) abort

call ale#preview#Show(l:lines, {'filetype': 'ale-preview-selection'})
let b:ale_preview_item_list = a:item_list
let b:ale_preview_item_open_in = get(l:options, 'open_in', 'current-buffer')

" Remove the last preview
let s:last_selection_list = b:ale_preview_item_list
let s:last_selection_open_in = b:ale_preview_item_open_in
endfunction

function! s:Open(open_in_tab) abort
function! ale#preview#RepeatSelection() abort
if empty(s:last_selection_list)
return
endif

call ale#preview#ShowSelection(s:last_selection_list, {
\ 'open_in': s:last_selection_open_in,
\})
endfunction

function! s:Open(open_in) abort
let l:item_list = get(b:, 'ale_preview_item_list', [])
let l:item = get(l:item_list, getpos('.')[1] - 1, {})

if empty(l:item)
return
endif

if !a:open_in_tab
:q!
endif
:q!

call ale#util#Open(
\ l:item.filename,
\ l:item.line,
\ l:item.column,
\ {'open_in_tab': a:open_in_tab},
\ {'open_in': a:open_in},
\)
endfunction

function! ale#preview#OpenSelectionInBuffer() abort
call s:Open(0)
function! ale#preview#OpenSelection() abort
call s:Open(b:ale_preview_item_open_in)
endfunction

function! ale#preview#OpenSelectionInTab() abort
call s:Open(1)
call s:Open('tab')
endfunction
19 changes: 18 additions & 1 deletion autoload/ale/references.vim
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
let g:ale_default_navigation = get(g:, 'ale_default_navigation', 'buffer')

let s:references_map = {}

" Used to get the references map in tests.
Expand Down Expand Up @@ -99,7 +101,8 @@ function! s:OnReady(line, column, options, linter, lsp_details) abort
let l:request_id = ale#lsp#Send(l:id, l:message)

let s:references_map[l:request_id] = {
\ 'use_relative_paths': has_key(a:options, 'use_relative_paths') ? a:options.use_relative_paths : 0
\ 'use_relative_paths': has_key(a:options, 'use_relative_paths') ? a:options.use_relative_paths : 0,
\ 'open_in': get(a:options, 'open_in', 'current-buffer'),
\}
endfunction

Expand All @@ -110,10 +113,24 @@ function! ale#references#Find(...) abort
for l:option in a:000
if l:option is? '-relative'
let l:options.use_relative_paths = 1
elseif l:option is? '-tab'
let l:options.open_in = 'tab'
elseif l:option is? '-split'
let l:options.open_in = 'split'
elseif l:option is? '-vsplit'
let l:options.open_in = 'vsplit'
endif
endfor
endif

if !has_key(l:options, 'open_in')
let l:default_navigation = ale#Var(bufnr(''), 'default_navigation')

if index(['tab', 'split', 'vsplit'], l:default_navigation) >= 0
let l:options.open_in = l:default_navigation
endif
endif

let l:buffer = bufnr('')
let [l:line, l:column] = getpos('.')[1:2]
let l:column = min([l:column, len(getline(l:line))])
Expand Down
8 changes: 4 additions & 4 deletions autoload/ale/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,17 @@ endfunction
" options['open_in'] can be:
" current-buffer (default)
" tab
" vertical-split
" horizontal-split
" split
" vsplit
function! ale#util#Open(filename, line, column, options) abort
let l:open_in = get(a:options, 'open_in', 'current-buffer')
let l:args_to_open = '+' . a:line . ' ' . fnameescape(a:filename)

if l:open_in is# 'tab'
call ale#util#Execute('tabedit ' . l:args_to_open)
elseif l:open_in is# 'horizontal-split'
elseif l:open_in is# 'split'
call ale#util#Execute('split ' . l:args_to_open)
elseif l:open_in is# 'vertical-split'
elseif l:open_in is# 'vsplit'
call ale#util#Execute('vsplit ' . l:args_to_open)
elseif bufnr(a:filename) isnot bufnr('')
" Open another file only if we need to.
Expand Down
Loading

0 comments on commit 82f734a

Please sign in to comment.