Skip to content

Commit 8ec2edf

Browse files
committed
Add go-back functionality to go-to-declaration
1 parent 2c1c8bc commit 8ec2edf

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Key|Action
3232
*Ctrl+{*|Open brace and automatically indent next line
3333
*Ctrl+Enter*|Autocomplete
3434
*Ctrl+Shift+G*|Go to declaration of symbol at cursor
35+
*Ctrl+Alt+Shift+G*|Go back after jumping to declaration
3536
*Ctrl+Shift+M*|Display symbol index for current file
3637
*Ctrl+;*|Go to end of line, insert semicolon
3738
*Shift+Enter*|Go to end of line, insert semicolon, insert newline

dmd/init.lua

+33-6
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,38 @@ local function showDoc()
128128
end
129129
end
130130

131-
local function gotoDeclaration()
131+
132+
M.gotoStack = {}
133+
134+
function M.goBack()
135+
if #M.gotoStack == 0 then return end
136+
local top = M.gotoStack[#M.gotoStack]
137+
ui.goto_view(top.view)
138+
if top.file ~= nil then
139+
ui.goto_file(top.file)
140+
else
141+
if top.buffer > _BUFFERS then
142+
table.remove(M.gotoStack)
143+
return
144+
end
145+
view:goto_buffer(top.buffer)
146+
end
147+
buffer:goto_line(top.line)
148+
buffer:vertical_centre_caret()
149+
table.remove(M.gotoStack) -- pop last item
150+
end
151+
152+
function M.gotoDeclaration()
132153
local r = runDCDClient("-l")
133154
if r ~= "Not found\n" then
134155
path, position = r:match("^(.-)\t(%d+)")
135156
if (path ~= nil and position ~= nil) then
157+
table.insert(M.gotoStack, {
158+
line = buffer:line_from_position(buffer.current_pos),
159+
view = _VIEWS[_G.view],
160+
file = buffer.filename,
161+
buffer = _BUFFERS[_G.buffer]
162+
})
136163
if (path ~= "stdin") then
137164
io.open_file(path)
138165
end
@@ -253,12 +280,11 @@ end
253280
events.connect(events.CHAR_ADDED, function(ch)
254281
if buffer:get_lexer() ~= "dmd" or ch > 255 then return end
255282
if string.char(ch) == '(' or string.char(ch) == '.' or string.char(ch) == ':' then
256-
-- local setting = buffer.auto_c_choose_single
257-
-- buffer.auto_c_choose_single = false
283+
local setting = buffer.auto_c_choose_single
284+
buffer.auto_c_choose_single = false
258285
autocomplete(ch)
259-
-- buffer.auto_c_choose_single = setting
286+
buffer.auto_c_choose_single = setting
260287
end
261-
262288
end)
263289

264290
-- Run dscanner's static analysis after saves and print the warnings and errors
@@ -337,7 +363,8 @@ keys.dmd = {
337363
['cH'] = {showDoc},
338364
['down'] = {cycleCalltips, 1},
339365
['up'] = {cycleCalltips, -1},
340-
['cG'] = {gotoDeclaration},
366+
['cG'] = {M.gotoDeclaration},
367+
['caG'] = {M.goBack},
341368
['cM'] = {symbolIndex},
342369
}
343370

0 commit comments

Comments
 (0)