Skip to content
14 changes: 11 additions & 3 deletions autoload/ale/definition.vim
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,22 @@ function! ale#definition#FormatLSPResponse(response_item, options) abort
let l:column = a:response_item.range.start.character + 1
endif

let l:filename = ale#util#ToResource(l:uri)
let l:content = ale#util#SafeReadFileLine(l:filename, l:line)

if get(a:options, 'open_in') is# 'quickfix'
return {
\ 'filename': ale#util#ToResource(l:uri),
\ 'filename': l:filename,
\ 'lnum': l:line,
\ 'col': l:column,
\ 'text': l:content,
\}
else
return {
\ 'filename': ale#util#ToResource(l:uri),
\ 'filename': l:filename,
\ 'line': l:line,
\ 'column': l:column,
\ 'match': l:content,
\}
endif
endfunction
Expand Down Expand Up @@ -238,6 +243,7 @@ function! s:OnReady(line, column, options, capability, linter, lsp_details) abor
let l:request_id = ale#lsp#Send(l:id, l:message)

let s:go_to_definition_map[l:request_id] = {
\ 'use_relative_paths': get(a:options, 'use_relative_paths', 0),
\ 'open_in': get(a:options, 'open_in', 'current-buffer'),
\}
endfunction
Expand Down Expand Up @@ -277,7 +283,9 @@ function! ale#definition#GoToCommandHandler(command, ...) abort

if len(a:000) > 0
for l:option in a:000
if l:option is? '-tab'
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'
Expand Down
19 changes: 13 additions & 6 deletions autoload/ale/references.vim
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,24 @@ function! ale#references#HandleTSServerResponse(conn_id, response) abort
endfunction

function! ale#references#FormatLSPResponseItem(response_item, options) abort
let l:filename = ale#util#ToResource(a:response_item.uri)
let l:line = a:response_item.range.start.line + 1
let l:col = a:response_item.range.start.character + 1
let l:content = ale#util#SafeReadFileLine(l:filename, l:line)

if get(a:options, 'open_in') is# 'quickfix'
return {
\ 'filename': ale#util#ToResource(a:response_item.uri),
\ 'lnum': a:response_item.range.start.line + 1,
\ 'col': a:response_item.range.start.character + 1,
\ 'filename': l:filename,
\ 'lnum': l:line,
\ 'col': l:col,
\ 'text': l:content,
\}
else
return {
\ 'filename': ale#util#ToResource(a:response_item.uri),
\ 'line': a:response_item.range.start.line + 1,
\ 'column': a:response_item.range.start.character + 1,
\ 'filename': l:filename,
\ 'line': l:line,
\ 'column': l:col,
\ 'match': l:content,
\}
endif
endfunction
Expand Down
9 changes: 9 additions & 0 deletions autoload/ale/util.vim
Original file line number Diff line number Diff line change
Expand Up @@ -583,3 +583,12 @@ function! ale#util#ToResource(uri) abort

return l:resource
endfunction

function! ale#util#SafeReadFileLine(filename, line)
let l:content = ''
if filereadable(a:filename)
let l:content = readfile(a:filename, '', a:line)[-1]
Copy link

Copilot AI Jun 13, 2025

Choose a reason for hiding this comment

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

If performance becomes an issue for large files, consider using a method that reads only the target line rather than reading all lines up to it.

Suggested change
let l:content = readfile(a:filename, '', a:line)[-1]
let l:content = getline(a:line, a:filename)

Copilot uses AI. Check for mistakes.
endif

return l:content
endfunction
2 changes: 2 additions & 0 deletions test/test_find_references.vader
Original file line number Diff line number Diff line change
Expand Up @@ -378,11 +378,13 @@ Execute(LSP reference responses should be handled):
\ 'filename': ale#path#Simplify(g:dir . '/completion_dummy_file'),
\ 'line': 3,
\ 'column': 8,
\ 'match': '',
\ },
\ {
\ 'filename': ale#path#Simplify(g:dir . '/other_file'),
\ 'line': 8,
\ 'column': 16,
\ 'match': '',
\ },
\ ],
\ g:item_list
Expand Down
22 changes: 11 additions & 11 deletions test/test_go_to_definition.vader
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ Execute(tsserver definition requests should be sent):
\ [0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}]
\ ],
\ g:message_list
AssertEqual {'42': {'open_in': 'current-buffer'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'current-buffer', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(tsserver type definition requests should be sent):
runtime ale_linters/typescript/tsserver.vim
Expand All @@ -264,7 +264,7 @@ Execute(tsserver type definition requests should be sent):
\ [0, 'ts@typeDefinition', {'file': expand('%:p'), 'line': 2, 'offset': 5}]
\ ],
\ g:message_list
AssertEqual {'42': {'open_in': 'current-buffer'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'current-buffer', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(tsserver implementation requests should be sent):
runtime ale_linters/typescript/tsserver.vim
Expand All @@ -288,7 +288,7 @@ Execute(tsserver implementation requests should be sent):
\ [0, 'ts@implementation', {'file': expand('%:p'), 'line': 2, 'offset': 5}]
\ ],
\ g:message_list
AssertEqual {'42': {'open_in': 'current-buffer'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'current-buffer', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(tsserver tab definition requests should be sent):
runtime ale_linters/typescript/tsserver.vim
Expand All @@ -312,7 +312,7 @@ Execute(tsserver tab definition requests should be sent):
\ [0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}]
\ ],
\ g:message_list
AssertEqual {'42': {'open_in': 'tab'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'tab', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(The default navigation type should be used):
runtime ale_linters/typescript/tsserver.vim
Expand All @@ -337,7 +337,7 @@ Execute(The default navigation type should be used):
\ [0, 'ts@definition', {'file': expand('%:p'), 'line': 2, 'offset': 5}]
\ ],
\ g:message_list
AssertEqual {'42': {'open_in': 'tab'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'tab', 'use_relative_paths': 0}}, ale#definition#GetMap()

Given python(Some Python file):
foo
Expand Down Expand Up @@ -502,7 +502,7 @@ Execute(LSP definition requests should be sent):
\ ],
\ g:message_list

AssertEqual {'42': {'open_in': 'current-buffer'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'current-buffer', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(LSP type definition requests should be sent):
runtime ale_linters/python/pylsp.vim
Expand Down Expand Up @@ -538,7 +538,7 @@ Execute(LSP type definition requests should be sent):
\ ],
\ g:message_list

AssertEqual {'42': {'open_in': 'current-buffer'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'current-buffer', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(LSP implementation requests should be sent):
runtime ale_linters/python/pylsp.vim
Expand Down Expand Up @@ -574,7 +574,7 @@ Execute(LSP implementation requests should be sent):
\ ],
\ g:message_list

AssertEqual {'42': {'open_in': 'current-buffer'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'current-buffer', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(LSP tab definition requests should be sent):
runtime ale_linters/python/pylsp.vim
Expand Down Expand Up @@ -610,7 +610,7 @@ Execute(LSP tab definition requests should be sent):
\ ],
\ g:message_list

AssertEqual {'42': {'open_in': 'tab'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'tab', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(LSP tab type definition requests should be sent):
runtime ale_linters/python/pylsp.vim
Expand Down Expand Up @@ -646,7 +646,7 @@ Execute(LSP tab type definition requests should be sent):
\ ],
\ g:message_list

AssertEqual {'42': {'open_in': 'tab'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'tab', 'use_relative_paths': 0}}, ale#definition#GetMap()

Execute(LSP tab implementation requests should be sent):
runtime ale_linters/python/pylsp.vim
Expand Down Expand Up @@ -682,4 +682,4 @@ Execute(LSP tab implementation requests should be sent):
\ ],
\ g:message_list

AssertEqual {'42': {'open_in': 'tab'}}, ale#definition#GetMap()
AssertEqual {'42': {'open_in': 'tab', 'use_relative_paths': 0}}, ale#definition#GetMap()
Loading