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

Add support for Vim's tagstack to ALEGoToDefinition #2448

Merged
merged 2 commits into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
FROM tweekmonster/vim-testbed:latest

RUN install_vim -tag v8.0.0027 -build \
-tag v8.1.0204 -build \
-tag v8.1.0519 -build \
-tag neovim:v0.2.0 -build \
-tag neovim:v0.3.0 -build

Expand Down
19 changes: 19 additions & 0 deletions autoload/ale/definition.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

let s:go_to_definition_map = {}

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

" Used to get the definition map in tests.
function! ale#definition#GetMap() abort
return deepcopy(s:go_to_definition_map)
Expand All @@ -17,6 +20,20 @@ function! ale#definition#ClearLSPData() abort
let s:go_to_definition_map = {}
endfunction

function! ale#definition#UpdateTagStack() abort
let l:should_update_tagstack = exists('*gettagstack') && exists('*settagstack') && g:ale_update_tagstack

if l:should_update_tagstack
" Grab the old location (to jump back to) and the word under the
" cursor (as a label for the tagstack)
let l:old_location = [bufnr('%'), line('.'), col('.'), 0]
let l:tagname = expand('<cword>')

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above - the values I'm pushing on to the stack are based on the old location. So I don't think I need to wait for the buffer to open; assuming I've understood this comment correctly?

let l:winid = win_getid()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get the window ID from the buffer number, instead of the current window.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like it's probably really relevant and important - but I'm afraid I'm not an expert on the Vim functions involving windows and buffers. Any chance you can provide some pointers to the relevant docs?

call settagstack(l:winid, {'items': [{'from': l:old_location, 'tagname': l:tagname}]}, 'a')
call settagstack(l:winid, {'curidx': len(gettagstack(l:winid)['items']) + 1})
endif
endfunction

function! ale#definition#HandleTSServerResponse(conn_id, response) abort
if get(a:response, 'command', '') is# 'definition'
\&& has_key(s:go_to_definition_map, a:response.request_seq)
Expand All @@ -27,6 +44,7 @@ function! ale#definition#HandleTSServerResponse(conn_id, response) abort
let l:line = a:response.body[0].start.line
let l:column = a:response.body[0].start.offset

call ale#definition#UpdateTagStack()
call ale#util#Open(l:filename, l:line, l:column, l:options)
endif
endif
Expand All @@ -51,6 +69,7 @@ function! ale#definition#HandleLSPResponse(conn_id, response) abort
let l:line = l:item.range.start.line + 1
let l:column = l:item.range.start.character + 1

call ale#definition#UpdateTagStack()
call ale#util#Open(l:filename, l:line, l:column, l:options)
break
endfor
Expand Down
11 changes: 11 additions & 0 deletions doc/ale.txt
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,9 @@ information returned by LSP servers. The following commands are supported:
|ALEGoToDefinitionInSplit| - The same, but in a new split.
|ALEGoToDefinitionInVSplit| - The same, but in a new vertical split.

ALE will update Vim's |tagstack| automatically unless |g:ale_update_tagstack| is
set to `0`.

-------------------------------------------------------------------------------
5.3 Go To Type Definition *ale-go-to-type-definition*

Expand Down Expand Up @@ -1494,6 +1497,14 @@ g:ale_sign_warning *g:ale_sign_warning*
The sign for warnings in the sign gutter.


g:ale_update_tagstack *g:ale_update_tagstack*
*b:ale_update_tagstack*
Type: |Number|
Default: `1`

This option can be set to disable updating Vim's |tagstack| automatically.


g:ale_type_map *g:ale_type_map*
*b:ale_type_map*
Type: |Dictionary|
Expand Down
9 changes: 9 additions & 0 deletions test/test_go_to_definition.vader
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ After:
Execute(Other messages for the tsserver handler should be ignored):
call ale#definition#HandleTSServerResponse(1, {'command': 'foo'})

Execute(Tagstack should be incremented if supported):
if exists('*gettagstack') && exists('*settagstack')
let original_stack_depth = gettagstack().length
endif
call ale#definition#UpdateTagStack()
if exists('*gettagstack') && exists('*settagstack')
AssertEqual original_stack_depth + 1, gettagstack().length
endif

Execute(Failed definition responses should be handled correctly):
call ale#definition#SetMap({3: {'open_in': 'current-buffer'}})
call ale#definition#HandleTSServerResponse(
Expand Down