Skip to content
This repository was archived by the owner on Jul 6, 2021. It is now read-only.

Commit feb54d4

Browse files
authored
Merge pull request #8 from chmln/master
Simplify jumpLoc and add cycled next diagnostic feature
2 parents 783f389 + c0a32b6 commit feb54d4

3 files changed

Lines changed: 64 additions & 121 deletions

File tree

lua/diagnostic.lua

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,6 @@ function M.publish_diagnostics(bufnr)
5050
end
5151
M.diagnostics_loclist(result)
5252
vim.api.nvim_command("doautocmd User LspDiagnosticsChanged")
53-
local loc = require 'jumpLoc'
54-
-- loc.init will be set to false when BufEnter
55-
if loc.init == false then
56-
loc.initLocation()
57-
else
58-
loc.updateLocation()
59-
end
6053
end
6154

6255
function M.refresh_diagnostics()
@@ -71,7 +64,6 @@ end
7164
M.on_attach = function(_, _)
7265
-- Setup autocmd
7366
vim.api.nvim_command [[augroup DiagnosticRefresh]]
74-
vim.api.nvim_command [[autocmd InsertLeave <buffer> lua require'jumpLoc'.initLocation()]]
7567
vim.api.nvim_command [[autocmd BufWinEnter,TabEnter <buffer> lua require'diagnostic'.refresh_diagnostics()]]
7668
vim.api.nvim_command [[augroup end]]
7769

@@ -83,4 +75,3 @@ M.on_attach = function(_, _)
8375
end
8476

8577
return M
86-

lua/jumpLoc.lua

Lines changed: 59 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -4,143 +4,92 @@ local util = require'diagnostic.util'
44
local diagnostic = require'diagnostic'
55
local M = {}
66

7-
8-
---------------------------------
9-
-- local function declartion --
10-
---------------------------------
11-
12-
-- currentLocation is an indicator that user has perform a jump
13-
-- need to re-adjust prevLocationIndex and nextLocationIndex to prevent issues
14-
local adjustLocation = function(row, col)
15-
if row > M.location[M.currentLocationIndex]['lnum'] or (row == M.location[M.currentLocationIndex]['lnum'] and col > M.location[M.currentLocationIndex]['col']) then
16-
M.prevLocationIndex = M.currentLocationIndex
17-
else
18-
M.nextLocationIndex = M.currentLocationIndex
7+
function M.get_next_loc()
8+
M.location = api.nvim_call_function('getloclist', {0})
9+
if #M.location <= 0 then
10+
return -1
1911
end
20-
M.currentLocationIndex = -1
21-
end
2212

23-
local checkCurrentLocation = function(row, col)
24-
if row == M.location[M.currentLocationIndex]['lnum'] and col == M.location[M.currentLocationIndex]['col'] then
25-
return true
26-
-- handle C/C++ edge case
27-
elseif row == M.location[M.currentLocationIndex]['lnum'] then
28-
local line = api.nvim_get_current_line()
29-
if M.location[M.currentLocationIndex]['col'] > #line and col == #line then
30-
return true
31-
end
32-
end
33-
return false
34-
end
13+
local cur_row = api.nvim_call_function('line', {"."})
14+
local cur_col = api.nvim_call_function('col', {"."})
3515

36-
local checkPrevLocation = function()
37-
if M.currentLocationIndex == -1 then
38-
return
39-
end
40-
while M.location[M.currentLocationIndex]['lnum'] == M.location[M.prevLocationIndex]['lnum'] and M.location[M.currentLocationIndex]['col'] == M.location[M.prevLocationIndex]['col'] do
41-
M.currentLocationIndex = M.currentLocationIndex - 1
42-
M.prevLocationIndex = M.prevLocationIndex - 1
43-
M.nextLocationIndex = M.nextLocationIndex - 1
44-
end
45-
end
46-
47-
local checkNextLocation = function()
48-
if M.currentLocationIndex == -1 then
49-
return
50-
end
51-
while M.location[M.currentLocationIndex]['lnum'] == M.location[M.nextLocationIndex]['lnum'] and M.location[M.currentLocationIndex]['col'] == M.location[M.nextLocationIndex]['col'] do
52-
M.currentLocationIndex = M.currentLocationIndex + 1
53-
M.prevLocationIndex = M.prevLocationIndex + 1
54-
M.nextLocationIndex = M.nextLocationIndex + 1
16+
for i, v in ipairs(M.location) do
17+
if v['lnum'] > cur_row or (v['lnum'] == cur_row and v['col'] > cur_col + 1) then
18+
return i
19+
end
5520
end
21+
return -1
5622
end
5723

58-
59-
----------------------------------
60-
-- member function declartion --
61-
----------------------------------
62-
63-
-- Init variable
64-
M.init = false
65-
M.prevLocationIndex = -1
66-
M.currentLocationIndex = -1
67-
M.nextLocationIndex = -1
68-
69-
-- Initialize location and set jump index
70-
function M.initLocation()
71-
-- TODO
24+
function M.get_prev_loc()
7225
M.location = api.nvim_call_function('getloclist', {0})
7326
if #M.location == 0 then
74-
-- let both index be invalid
75-
M.prevLocationIndex = -1
76-
M.nextLocationIndex = -1
77-
return
27+
return -1
7828
end
79-
local current_row = api.nvim_call_function('line', {"."})
80-
local current_col = api.nvim_call_function('col', {"."})
8129

82-
if M.currentLocationIndex ~= -1 then
83-
if M.currentLocationIndex > #M.location then
84-
M.currentLocationIndex = -1
85-
elseif checkCurrentLocation(current_row, current_col) then
86-
return
87-
else
88-
adjustLocation(current_row, current_col)
89-
end
90-
end
30+
local cur_row = api.nvim_call_function('line', {"."})
31+
local cur_col = api.nvim_call_function('col', {"."})
32+
9133
for i, v in ipairs(M.location) do
92-
if v['lnum'] > current_row or (v['lnum'] == current_row and v['col'] > current_col) then
93-
M.nextLocationIndex = i
94-
M.prevLocationIndex = i-1
95-
return
34+
local is_next = v['lnum'] > cur_row or (v['lnum'] == cur_row and v['col'] > cur_col);
35+
local is_prev = v['lnum'] == cur_row and cur_col > v['col'];
36+
local same_pos = v['lnum'] == cur_row and v['col'] == cur_col;
37+
if is_next or same_pos then
38+
return i - 1
39+
elseif is_prev then
40+
return i
9641
end
9742
end
9843

99-
-- TODO Documentation
100-
M.nextLocationIndex = #M.location+1
101-
M.prevLocationIndex = #M.location
102-
M.init = true
44+
return -1
10345
end
10446

105-
-- Update location and jump index upon changing location list
106-
function M.updateLocation()
107-
M.location = api.nvim_call_function('getloclist', {0})
108-
if #M.location == 0 then
109-
M.prevLocationIndex = -1
110-
M.nextLocationIndex = -1
47+
function jumpToLocation(i)
48+
if i >= 1 and i <= #M.location then
49+
api.nvim_command("silent! ll"..i)
50+
M.openLineDiagnostics()
11151
end
112-
M.initLocation()
11352
end
11453

11554
-- Jump to next location
11655
-- Show warning text when no next location is available
11756
function M.jumpNextLocation()
118-
M.initLocation()
119-
if M.nextLocationIndex > #M.location or M.nextLocationIndex == -1 then
120-
api.nvim_command("echohl WarningMsg | echo 'no next diagnostic' | echohl None")
57+
local i = M.get_next_loc()
58+
if i >= 1 then
59+
jumpToLocation(i)
12160
else
122-
checkNextLocation()
123-
api.nvim_command("silent! ll"..M.nextLocationIndex)
124-
M.currentLocationIndex = M.nextLocationIndex
125-
M.nextLocationIndex = M.currentLocationIndex + 1
126-
M.prevLocationIndex = M.currentLocationIndex - 1
127-
M.openLineDiagnostics()
61+
api.nvim_command("echohl WarningMsg | echo 'no next diagnostic' | echohl None")
12862
end
12963
end
13064

131-
-- Jump to previous location
132-
-- Show warning text when no previous location is available
13365
function M.jumpPrevLocation()
134-
M.initLocation()
135-
if M.prevLocationIndex == 0 or M.prevLocationIndex == -1 then
136-
api.nvim_command("echohl WarningMsg | echo 'no previous diagnostic' | echohl None")
66+
local i = M.get_prev_loc()
67+
if i >= 1 then
68+
jumpToLocation(i)
13769
else
138-
checkPrevLocation()
139-
api.nvim_command("silent! ll"..M.prevLocationIndex)
140-
M.currentLocationIndex = M.prevLocationIndex
141-
M.nextLocationIndex = M.currentLocationIndex + 1
142-
M.prevLocationIndex = M.currentLocationIndex - 1
143-
M.openLineDiagnostics()
70+
api.nvim_command("echohl WarningMsg | echo 'no prev diagnostic' | echohl None")
71+
end
72+
end
73+
74+
function M.jumpNextLocationCycle()
75+
local next_i = M.get_next_loc()
76+
if next_i > 0 then
77+
jumpToLocation(next_i)
78+
elseif M.get_prev_loc() >= 0 then
79+
jumpToLocation(1)
80+
else
81+
return api.nvim_command("echohl WarningMsg | echo 'No diagnostics found' | echohl None")
82+
end
83+
end
84+
85+
function M.jumpPrevLocationCycle()
86+
local prev_i = M.get_prev_loc()
87+
if prev_i > 0 then
88+
jumpToLocation(prev_i)
89+
elseif M.get_next_loc() >= 0 then
90+
jumpToLocation(#M.location)
91+
else
92+
return api.nvim_command("echohl WarningMsg | echo 'No diagnostics found' | echohl None")
14493
end
14594
end
14695

@@ -157,6 +106,7 @@ function M.openLineDiagnostics()
157106
end))
158107
end
159108
end
109+
160110
-- Open location window and jump back to current window
161111
function M.openDiagnostics()
162112
api.nvim_command("lopen")

plugin/diagnostic.vim

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ if exists('g:loaded_diagnostic') | finish | endif
33
let s:save_cpo = &cpo
44
set cpo&vim
55

6-
command! PrevDiagnostic lua require'jumpLoc'.jumpPrevLocation()
7-
command! NextDiagnostic lua require'jumpLoc'.jumpNextLocation()
8-
command! OpenDiagnostic lua require'jumpLoc'.openDiagnostics()
6+
command! PrevDiagnostic lua require'jumpLoc'.jumpPrevLocation()
7+
command! PrevDiagnosticCycle lua require'jumpLoc'.jumpPrevLocationCycle()
8+
command! NextDiagnostic lua require'jumpLoc'.jumpNextLocation()
9+
command! NextDiagnosticCycle lua require'jumpLoc'.jumpNextLocationCycle()
10+
command! OpenDiagnostic lua require'jumpLoc'.openDiagnostics()
911

1012
" lua require'diagnostic'.modifyCallback()
1113

0 commit comments

Comments
 (0)