forked from pwntester/codeql.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefs.lua
More file actions
160 lines (137 loc) · 4.36 KB
/
Copy pathdefs.lua
File metadata and controls
160 lines (137 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
local util = require "codeql.util"
local vim = vim
local M = {}
M.cache = {
definitions = {},
references = {},
}
local function add_to_cache(kind, fname, lnum, range, location)
local key = string.format("%s::%d", fname, lnum)
local entry = M.cache[kind][key]
if not entry then
entry = {}
end
entry[range] = location
M.cache[kind][key] = entry
end
function M.process_refs(results_path)
M.process("references", results_path)
end
function M.process_defs(results_path)
M.process("definitions", results_path)
end
function M.process(kind, results_path)
-- no results available, exiting
if not util.is_file(results_path) then
return
end
util.message(string.format("Processing %s", kind))
local results = util.read_json_file(results_path)
-- no results, exiting
if not results or vim.tbl_count(results) == 0 then
return
end
local tuples = results["#select"]["tuples"]
--clear_path_from_cache(path)
for _, tuple in ipairs(tuples) do
local src = tuple[1]
local dst = tuple[2]
local src_fname = util.uri_to_fname(src["url"]["uri"])
local src_lnum = src["url"]["startLine"]
local src_range = { src["url"]["startColumn"], src["url"]["endColumn"] }
local src_label = src["label"]
local dst_fname = util.uri_to_fname(dst["url"]["uri"])
local dst_lnum = dst["url"]["startLine"]
local dst_range = { dst["url"]["startColumn"], dst["url"]["endColumn"] }
local dst_label = dst["label"]
if kind == "definitions" then
add_to_cache(kind, src_fname, src_lnum, src_range, {
fname = dst_fname,
lnum = dst_lnum,
range = dst_range,
label = dst_label,
})
elseif kind == "references" then
add_to_cache(kind, dst_fname, dst_lnum, dst_range, {
fname = src_fname,
lnum = src_lnum,
range = src_range,
label = src_label,
})
end
end
end
function M.find_at_cursor(kind)
if not M.cache then
return
end
local bufnr = vim.api.nvim_get_current_buf()
local winid = vim.api.nvim_get_current_win()
local bufname = vim.api.nvim_buf_get_name(bufnr)
local _, row, col = unpack(vim.fn.getpos ".")
if not vim.startswith(bufname, "ql:/") then
return
end
local prefix = vim.split(bufname, "://")[1]
local fname = vim.split(bufname, "://")[2]
local word_at_cursor = ""
local key = string.format("/%s::%d", fname, row)
local entry = M.cache[kind][key]
if not entry or vim.tbl_count(entry) == 0 then
util.message(string.format("Cannot find %s for %s", kind, key))
return
end
local matching_locs = {}
for range, location in pairs(entry) do
if col >= range[1] and col <= range[2] then
table.insert(matching_locs, location)
end
end
if #matching_locs == 0 then
util.message(string.format("Cannot find matching %s for %s", kind, word_at_cursor))
return
elseif #matching_locs == 1 then
-- jump to location (def/ref)
local location = matching_locs[1]
-- mark current position so it can be jumped back to
vim.api.nvim_command "mark '"
-- push a new item into tagstack
local from = { bufnr, vim.fn.line ".", vim.fn.col ".", 0 }
local items = { { tagname = vim.fn.expand "<cword>", from = from } }
vim.fn.settagstack(vim.fn.win_getid(), { items = items }, "t")
local def_bufname = string.format("ql://%s", string.sub(location.fname, 2))
local opts = {
line = location.lnum,
target_winid = winid,
}
if vim.fn.bufnr(def_bufname) > -1 then
vim.api.nvim_command(string.format("buffer %s", def_bufname))
if opts.line then
util.jump_to_line(opts)
end
else
local def_bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_name(def_bufnr, def_bufname)
local path = string.sub(location.fname, 2)
util.open_from_archive(def_bufnr, path, opts)
end
elseif #matching_locs > 1 then
local items = {}
for _, location in ipairs(matching_locs) do
local rel_fname = string.sub(location.fname, 2)
table.insert(items, {
filename = string.format("%s://%s", prefix, rel_fname),
module = location.fname,
lnum = location.lnum,
col = location.range[1],
text = location.label,
})
end
vim.fn.setqflist({}, " ", {
title = string.format("CodeQL %s", kind),
items = items,
})
vim.api.nvim_command "botright copen"
end
end
return M