@codua/codeoutline-win32-x64 0.2.6 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (73) hide show
  1. package/README.md +205 -3
  2. package/README.zh-CN.md +40 -2
  3. package/bin/xnet.exe +0 -0
  4. package/build-info.json +1 -1
  5. package/package.json +1 -1
  6. package/scripts/codeoutline/affected.lua +145 -0
  7. package/scripts/codeoutline/automatic_events.lua +202 -0
  8. package/scripts/codeoutline/cache_repairs.lua +47 -0
  9. package/scripts/codeoutline/callers.lua +56 -16
  10. package/scripts/codeoutline/cargo_toml.lua +125 -0
  11. package/scripts/codeoutline/command.lua +26 -4
  12. package/scripts/codeoutline/common.lua +48 -7
  13. package/scripts/codeoutline/completion.lua +126 -22
  14. package/scripts/codeoutline/coverage.lua +63 -0
  15. package/scripts/codeoutline/cpp_bindings.lua +46 -11
  16. package/scripts/codeoutline/cpp_fragments.lua +143 -0
  17. package/scripts/codeoutline/cpp_implementations.lua +340 -0
  18. package/scripts/codeoutline/cpp_includes.lua +156 -0
  19. package/scripts/codeoutline/cpp_macros.lua +279 -0
  20. package/scripts/codeoutline/cpp_receivers.lua +342 -0
  21. package/scripts/codeoutline/cpp_types.lua +285 -29
  22. package/scripts/codeoutline/definition_candidates.lua +186 -0
  23. package/scripts/codeoutline/dispatch_aliases.lua +10 -5
  24. package/scripts/codeoutline/documents.lua +22 -9
  25. package/scripts/codeoutline/explore.lua +243 -122
  26. package/scripts/codeoutline/external_headers.lua +176 -0
  27. package/scripts/codeoutline/foreign.lua +23 -3
  28. package/scripts/codeoutline/function_values.lua +69 -0
  29. package/scripts/codeoutline/go_methods.lua +931 -0
  30. package/scripts/codeoutline/go_modules.lua +329 -0
  31. package/scripts/codeoutline/graph.lua +322 -67
  32. package/scripts/codeoutline/implementations.lua +403 -0
  33. package/scripts/codeoutline/index.lua +161 -28
  34. package/scripts/codeoutline/index_worker.lua +11 -4
  35. package/scripts/codeoutline/indirect.lua +4 -2
  36. package/scripts/codeoutline/js_evidence.lua +2 -1
  37. package/scripts/codeoutline/js_resolution.lua +40 -12
  38. package/scripts/codeoutline/js_workspace.lua +325 -0
  39. package/scripts/codeoutline/lang/c.lua +15 -2
  40. package/scripts/codeoutline/lang/cpp.lua +32 -18
  41. package/scripts/codeoutline/lang/csharp.lua +1 -1
  42. package/scripts/codeoutline/lang/js.lua +56 -20
  43. package/scripts/codeoutline/lang/rust.lua +9 -1
  44. package/scripts/codeoutline/language_detect.lua +100 -0
  45. package/scripts/codeoutline/lsp.lua +173 -23
  46. package/scripts/codeoutline/lua_bindings.lua +151 -47
  47. package/scripts/codeoutline/lua_callback_loops.lua +210 -0
  48. package/scripts/codeoutline/main.lua +32 -7
  49. package/scripts/codeoutline/mcp.lua +52 -9
  50. package/scripts/codeoutline/navigation.lua +351 -65
  51. package/scripts/codeoutline/on_demand.lua +93 -0
  52. package/scripts/codeoutline/ordinary_bindings.lua +210 -34
  53. package/scripts/codeoutline/parse.lua +18 -2
  54. package/scripts/codeoutline/parse_pool.lua +1 -1
  55. package/scripts/codeoutline/parse_worker.lua +1 -1
  56. package/scripts/codeoutline/python_bindings.lua +2 -2
  57. package/scripts/codeoutline/query_filters.lua +65 -0
  58. package/scripts/codeoutline/query_report.lua +56 -19
  59. package/scripts/codeoutline/resolution.lua +23 -0
  60. package/scripts/codeoutline/rust_packages.lua +203 -0
  61. package/scripts/codeoutline/rust_resolution.lua +301 -0
  62. package/scripts/codeoutline/search_ranking.lua +70 -0
  63. package/scripts/codeoutline/service.lua +47 -7
  64. package/scripts/codeoutline/shared_events.lua +369 -0
  65. package/scripts/codeoutline/static_dispatch.lua +46 -11
  66. package/scripts/codeoutline/text.lua +3 -2
  67. package/scripts/codeoutline/ts_implementations.lua +81 -0
  68. package/scripts/codeoutline/type_bases.lua +61 -0
  69. package/scripts/codeoutline/type_hierarchy.lua +324 -0
  70. package/scripts/codeoutline/type_references.lua +291 -0
  71. package/scripts/codeoutline/type_uses.lua +260 -0
  72. package/scripts/codeoutline/update_sequence.lua +1 -1
  73. package/scripts/codeoutline/version.lua +1 -1
@@ -0,0 +1,291 @@
1
+ -- Query explicit type sites without a separate resident type-use graph.
2
+ local graph, control = require('codeoutline.graph'), require('codeoutline.control')
3
+ local M = { MAX_FILES = 64, MAX_BYTES = 8 * 1024 * 1024, MAX_NAMES = 32, MAX_SITES = 4096,
4
+ MAX_SCAN_FILES = 20000, MAX_SCAN_BYTES = 256 * 1024 * 1024, TIME_MS = 5000 }
5
+ M.kinds = { class = true, struct = true, union = true, interface = true, enum = true, record = true, typedef = true, annotation_type = true, trait = true }
6
+ local languages = require('codeoutline.type_uses').languages
7
+ function M.validate(query, opts)
8
+ local budget = require('codeoutline.explore').validate(query, opts)
9
+ assert(require('codeoutline.query_filters').parse(query).text ~= '', 'type references require an exact type name')
10
+ local cap = opts and opts.max_nodes or 200
11
+ assert(type(cap) == 'number' and cap % 1 == 0 and cap >= 1 and cap <= 2000, 'max_nodes must be an integer between 1 and 2000')
12
+ return budget
13
+ end
14
+ function M.resolve(G, f, from, site, ctx, details, navigation)
15
+ if site.value_shadowed and not site.elaborated then return {}, 'value binding' end
16
+ local language, written = G.files[f].language, site.written
17
+ local data, head = details or ctx.ordinary.data(f), written:match('^([%w_$]+)')
18
+ for _, p in ipairs(data.type_parameters or {}) do
19
+ if p.name == head and p.first <= site.at and site.at <= p.last then return {}, 'type parameter' end
20
+ end
21
+ local scope = from and from > 0 and G.nodes[from] and G.nodes[from].qualified or ''
22
+ local ids
23
+ if language == 'cpp' then
24
+ ids = require('codeoutline.cpp_types').resolve(G, f, written, { context = ctx, scope = scope,
25
+ at = site.at, accept = M.kinds, definitions = true, navigation_alias = navigation })
26
+ elseif language == 'c' then
27
+ local visible = graph.visible_dependencies(G, f, ctx); ids = {}
28
+ local own = {}
29
+ for _, id in ipairs(G.by_name[written] or {}) do
30
+ local cf = G.node_file[id]
31
+ local kind = G.nodes[id].kind
32
+ if (cf == f or visible[cf]) and (site.elaborated and kind == site.elaborated or not site.elaborated and kind == 'typedef')
33
+ and (G.files[cf].language == 'c' or G.files[cf].language == 'cpp') then
34
+ if cf == f and G.nodes[id].line <= site.line then own[#own + 1] = id
35
+ elseif cf ~= f then ids[#ids + 1] = id end
36
+ end
37
+ end
38
+ if #own > 0 then ids = own end
39
+ elseif language == 'go' then ids = require('codeoutline.go_methods').context(G, ctx).types(f, written)
40
+ elseif language == 'rust' then
41
+ local resolver = require('codeoutline.rust_resolution')
42
+ local entry = resolver.path(G, f, written, ctx, resolver.scope(G, f, from)); ids = entry and entry.ids or {}
43
+ if not entry and not require('codeoutline.rust_packages').owner(G, f, ctx) then
44
+ for _, id in ipairs(G.by_name[written] or {}) do
45
+ if G.node_file[id] == f and G.nodes[id].qualified == written then ids[#ids + 1] = id end
46
+ end
47
+ end
48
+ elseif language == 'typescript' or language == 'arkts' then
49
+ ids = require('codeoutline.ts_implementations').types(G, f, written, ctx)
50
+ if #ids == 0 then
51
+ for _, imp in ipairs(G.files[f].js_imports or {}) do
52
+ if imp.local_name == written and imp.imported ~= '*' then ids = require('codeoutline.js_resolution').imported(G, f, imp.path, imp.imported, ctx); break end
53
+ end
54
+ if #ids == 0 then
55
+ for _, id in ipairs(G.by_name[written] or {}) do if G.node_file[id] == f and not G.nodes[id].parent then ids[#ids + 1] = id end end
56
+ end
57
+ end
58
+ else ids = require('codeoutline.implementations').types(G, f, written, ctx, scope, site.at) end
59
+ local result = {}
60
+ for _, id in ipairs(ids or {}) do if M.kinds[G.nodes[id].kind] then result[#result + 1] = id end end
61
+ return result, #result > 1 and 'ambiguous type' or #result == 0 and 'unresolved type' or nil
62
+ end
63
+ local function describe(G, id)
64
+ return { id = id, name = G.nodes[id].qualified, file = G.files[G.node_file[id]].path, line = G.nodes[id].line }
65
+ end
66
+ function M.query(G, idx, targets, opts)
67
+ opts = opts or {}
68
+ local result = { targets = {}, references = {}, unresolved_sites = 0, ambiguous_sites = 0,
69
+ query_stats = { scanned_files = 0, scanned_sites = 0, source_files = 0, source_bytes = 0, incomplete_reasons = {} } }
70
+ local stats, deadline, stop = result.query_stats, xtimer.now_ms() + (opts.time_limit_ms or M.TIME_MS), {}
71
+ if #targets > (opts.max_nodes or 200) then
72
+ local selected = {}
73
+ for i = 1, opts.max_nodes or 200 do selected[i] = targets[i] end
74
+ targets = selected
75
+ result.seed_limit_reached, result.relationships_incomplete = true, true
76
+ stats.incomplete_reasons.type_reference_target_limit = true
77
+ end
78
+ local roots, root_files, names, name_count, loaded, positions = {}, {}, {}, 0, {}, {}
79
+ local function limited(reason) stats.incomplete_reasons[reason], result.relationships_incomplete = true, true; error(stop, 0) end
80
+ local function check()
81
+ control.check()
82
+ if xtimer.now_ms() >= deadline then result.time_limit_reached = true; limited('type_reference_time_limit') end
83
+ end
84
+ local function add_name(name)
85
+ if names[name] then return end
86
+ if name_count >= M.MAX_NAMES then limited('type_reference_alias_limit') end
87
+ names[name], name_count = true, name_count + 1
88
+ end
89
+ if #targets == 0 then return result end
90
+ local ctx = require('codeoutline.js_resolution').context(G); ctx.include_paths = opts.include_paths
91
+ local function source(f)
92
+ if not loaded[f] then
93
+ if stats.source_files >= (opts.max_files or M.MAX_FILES) then limited('type_reference_file_limit') end
94
+ if stats.source_bytes + (G.files[f].size or 0) > M.MAX_BYTES then limited('type_reference_source_limit') end
95
+ loaded[f] = true; stats.source_files = stats.source_files + 1; stats.source_bytes = stats.source_bytes + (G.files[f].size or 0)
96
+ end
97
+ if idx then
98
+ local raw, repaired = idx:query_source(G.files[f].path)
99
+ if repaired then error(opts.retry, 0) end
100
+ return assert(raw, 'cannot read type reference source')
101
+ end
102
+ end
103
+ ctx.ordinary = require('codeoutline.ordinary_bindings').context(G, ctx,
104
+ (idx or G.root) and function(f) return source(f) end or nil, opts.cache and { cache = opts.cache } or nil)
105
+ stats.ordinary = ctx.ordinary.stats
106
+ local summaries, summary_bytes = {}, 0
107
+ local function data(f)
108
+ check()
109
+ if summaries[f] then return summaries[f] end
110
+ if not loaded[f] then source(f) end
111
+ local full = ctx.ordinary.data(f)
112
+ local count = 0; for _ in pairs(full.type_sites or {}) do count = count + 1 end
113
+ local weight = count * 256 + #(full.type_parameters or {}) * 96
114
+ if summary_bytes + weight > M.MAX_BYTES then limited('type_reference_evidence_limit') end
115
+ summary_bytes = summary_bytes + weight; stats.evidence_bytes = summary_bytes
116
+ summaries[f] = { type_sites = full.type_sites, type_parameters = full.type_parameters }
117
+ return summaries[f]
118
+ end
119
+ local function matches(ids) for _, id in ipairs(ids) do if roots[id] then return roots[id] end end end
120
+ -- Type occurrences are not in the persistent graph. Scan candidate text,
121
+ -- then parse only relevant files through the validated shared FIFO.
122
+ local function candidate(f)
123
+ check()
124
+ local rec = G.files[f]
125
+ local cached = summaries[f] or rec
126
+ if cached.type_sites then
127
+ for _, site in ipairs(cached.type_sites) do if names[site.name] then return true end end
128
+ return false
129
+ end
130
+ if (stats.candidate_files or 0) >= M.MAX_SCAN_FILES then limited('type_reference_scan_file_limit') end
131
+ if (stats.candidate_bytes or 0) + (rec.size or 0) > M.MAX_SCAN_BYTES then limited('type_reference_scan_byte_limit') end
132
+ local raw, repaired
133
+ if idx and idx.abs then
134
+ local file = assert(io.open(idx:abs(rec.path), 'rb'), 'cannot read type candidate: ' .. rec.path)
135
+ raw = file:read(M.MAX_SCAN_BYTES - (stats.candidate_bytes or 0) + 1); file:close()
136
+ local crc = xcompress and xcompress.crc32 and xcompress.crc32(raw) or xutils.sha256_hex(raw)
137
+ assert(#raw == rec.size and crc == rec.crc, 'source snapshot changed during type query: ' .. rec.path)
138
+ elseif idx then raw, repaired = idx:query_source(rec.path)
139
+ elseif G.root then
140
+ local file = assert(io.open(G.root .. '/' .. rec.path, 'rb')); raw = file:read('a'); file:close()
141
+ local crc = xcompress and xcompress.crc32 and xcompress.crc32(raw) or xutils.sha256_hex(raw)
142
+ assert(#raw == rec.size and crc == rec.crc, 'source snapshot changed during type query: ' .. rec.path)
143
+ else
144
+ for _, site in ipairs(data(f).type_sites or {}) do if names[site.name] then return true end end
145
+ return false
146
+ end
147
+ if repaired then error(opts.retry, 0) end
148
+ assert(raw, 'cannot read type candidate: ' .. rec.path)
149
+ stats.candidate_files, stats.candidate_bytes = (stats.candidate_files or 0) + 1, (stats.candidate_bytes or 0) + #raw
150
+ if stats.candidate_bytes > M.MAX_SCAN_BYTES then limited('type_reference_scan_byte_limit') end
151
+ for name in pairs(names) do
152
+ local escaped = name:gsub('([^%w])', '%%%1')
153
+ if raw:find('%f[%w_$]' .. escaped .. '%f[^%w_$]') then return true end
154
+ end
155
+ return false
156
+ end
157
+ local ok, err = pcall(function()
158
+ for _, id in ipairs(targets) do roots[id] = id; root_files[G.node_file[id]] = true; add_name(G.nodes[id].name) end
159
+ for _, id in ipairs(targets) do
160
+ data(G.node_file[id])
161
+ result.targets[#result.targets + 1] = describe(G, id)
162
+ end
163
+ for _, id in ipairs(targets) do
164
+ if G.nodes[id].kind == 'typedef' then
165
+ local f, n = G.node_file[id], id - G.id_of[G.node_file[id]][1] + 1
166
+ for i, site in pairs(data(f).type_sites or {}) do
167
+ if site.from == n and site.role == 'alias' and site.alias_name then
168
+ local resolved = M.resolve(G, f, id, site, ctx, data(f))
169
+ if #resolved == 1 then roots[resolved[1]] = id; root_files[G.node_file[resolved[1]]] = true end
170
+ end
171
+ end
172
+ end
173
+ end
174
+ for round = 1, M.MAX_NAMES do
175
+ local before = name_count
176
+ for number, f in ipairs(G.file_order) do
177
+ if number % 128 == 0 then check() end
178
+ local rec = G.files[f]
179
+ if languages[rec.language] then
180
+ for _, export in ipairs(rec.js_exports or {}) do
181
+ if export.local_name and names[export.local_name] and export.name ~= 'default' then add_name(export.name) end
182
+ end
183
+ for _, imp in ipairs(rec.js_imports or rec.imports or {}) do
184
+ local tail = (imp.imported or imp.path or ''):match('([%w_$]+)$')
185
+ local alias = imp.local_name or imp.alias
186
+ if alias and names[tail] then add_name(alias) end
187
+ for _, hint in ipairs(imp.type_aliases or {}) do if names[hint.name] then add_name(hint.alias) end end
188
+ if imp.imported == 'default' and alias then
189
+ local imported = require('codeoutline.js_resolution').imported(G, f, imp.path, imp.imported, ctx)
190
+ if matches(imported) then add_name(alias) end
191
+ end
192
+ end
193
+ end
194
+ end
195
+ if before == name_count then break end
196
+ end
197
+ for pass = 1, M.MAX_NAMES do
198
+ local before = name_count
199
+ local eligible = {}
200
+ local dirs, ordered, near, far = {}, {}, {}, {}
201
+ for f in pairs(root_files) do dirs[G.file_dir[f]] = true end
202
+ for _, f in ipairs(G.file_order) do
203
+ if languages[G.files[f].language] then
204
+ local bucket = root_files[f] and ordered or dirs[G.file_dir[f]] and near or far
205
+ bucket[#bucket + 1] = f
206
+ end
207
+ end
208
+ for _, f in ipairs(near) do ordered[#ordered + 1] = f end
209
+ for _, f in ipairs(far) do ordered[#ordered + 1] = f end
210
+ for _, f in ipairs(ordered) do
211
+ if languages[G.files[f].language] then
212
+ stats.scanned_files = stats.scanned_files + 1
213
+ if stats.scanned_files % 128 == 0 then check() end
214
+ local rec = G.files[f]
215
+ if candidate(f) then
216
+ if rec.language == 'c' or rec.language == 'cpp' then
217
+ eligible[f] = root_files[f] or false
218
+ if not eligible[f] then
219
+ local visible, paired = graph.visible_dependencies(G, f, ctx)
220
+ for root in pairs(root_files) do if visible[root] or paired[root] then eligible[f] = true; break end end
221
+ end
222
+ else eligible[f] = true end
223
+ if eligible[f] then
224
+ for i, site in ipairs(data(f).type_sites or {}) do
225
+ if i % 1024 == 0 then check() end
226
+ local key = f .. ':' .. i
227
+ if names[site.name] and not positions[key] then
228
+ if stats.scanned_sites >= M.MAX_SITES then limited('type_reference_site_limit') end
229
+ positions[key] = true; stats.scanned_sites = stats.scanned_sites + 1
230
+ local from = site.from > 0 and G.id_of[f][site.from] or nil
231
+ local ids, reason = M.resolve(G, f, from, site, ctx, data(f))
232
+ if ctx.include_incomplete or ctx.workspace_incomplete or ctx.go_incomplete or ctx.go_methods_incomplete or ctx.rust_incomplete then limited('type_reference_binding_incomplete') end
233
+ local target = matches(ids)
234
+ if #ids == 1 and target then
235
+ if #result.references >= (opts.max_nodes or 200) then result.node_limit_reached = true; limited('type_reference_result_limit') end
236
+ result.references[#result.references + 1] = { file = rec.path, line = site.line, first = site.first, last = site.last,
237
+ role = site.role, written = site.written, target = target, source = from }
238
+ local node = site.from > 0 and rec.nodes[site.from]
239
+ if site.role == 'alias' and site.alias_name and node and node.kind == 'typedef' then
240
+ roots[from] = target; root_files[f] = true; add_name(node.name)
241
+ end
242
+ elseif #ids > 1 then result.ambiguous_sites = result.ambiguous_sites + 1
243
+ elseif reason ~= 'type parameter' and reason ~= 'value binding' then result.unresolved_sites = result.unresolved_sites + 1 end
244
+ end
245
+ end
246
+ end
247
+ end
248
+ end
249
+ end
250
+ if before == name_count then break end
251
+ end
252
+ end)
253
+ if not ok and err ~= stop then error(err, 0) end
254
+ if ctx.include_incomplete or ctx.workspace_incomplete or ctx.go_incomplete or ctx.go_methods_incomplete or ctx.rust_incomplete then
255
+ result.relationships_incomplete = true; stats.incomplete_reasons.type_reference_binding_incomplete = true
256
+ end
257
+ result.incomplete = result.relationships_incomplete or result.unresolved_sites > 0 or result.ambiguous_sites > 0
258
+ table.sort(result.references, function(a, b) return a.file < b.file or a.file == b.file and a.first < b.first end)
259
+ return result
260
+ end
261
+ local function run(G, idx, query, opts, retry)
262
+ local budget = M.validate(query, opts)
263
+ local filters = require('codeoutline.query_filters').parse(query)
264
+ local ids = {}
265
+ for number, id in ipairs(G.by_qualified[filters.text] or G.by_name[filters.text] or {}) do
266
+ if number % 256 == 0 then control.check() end
267
+ if M.kinds[G.nodes[id].kind] and languages[G.files[G.node_file[id]].language] and require('codeoutline.query_filters').id(filters, G, id) then
268
+ ids[#ids + 1] = id
269
+ if #ids > (opts.max_nodes or 200) then break end
270
+ end
271
+ end
272
+ table.sort(ids, function(a, b) return graph.before(G, a, b) end)
273
+ local options = {}; for key, value in pairs(opts or {}) do options[key] = value end; options.retry = retry
274
+ local result = M.query(G, idx, ids, options)
275
+ local lines = { string.format('Matched %d type(s); %d explicit type references.', #result.targets, #result.references),
276
+ 'Scope: explicit type positions parsed on demand in indexed files; no inference, runtime reflection or complete build coverage is claimed.' }
277
+ for _, target in ipairs(result.targets) do lines[#lines + 1] = string.format('Target: %s (%s:%d)', target.name, target.file, target.line) end
278
+ for _, ref in ipairs(result.references) do lines[#lines + 1] = string.format('- %s:%d [%s] %s', ref.file, ref.line, ref.role, ref.written) end
279
+ if #ids == 0 then lines[#lines + 1] = 'No supported type matched; use an exact type name or qualified name.' end
280
+ if result.incomplete then lines[#lines + 1] = string.format('Coverage incomplete; %d unresolved and %d ambiguous candidate sites.', result.unresolved_sites, result.ambiguous_sites) end
281
+ local output, cut = require('codeoutline.text').limit(table.concat(lines, '\n'), budget, result.node_limit_reached)
282
+ result.truncated = cut; return output, result
283
+ end
284
+ function M.run(G, idx, query, opts)
285
+ local retry = {}
286
+ local ok, output, info = pcall(run, G, idx, query, opts or {}, retry)
287
+ if not ok and output == retry then return nil, { encoding_retry = true } end
288
+ if not ok then error(output, 0) end
289
+ return output, info
290
+ end
291
+ return M
@@ -0,0 +1,260 @@
1
+ -- Explicit type sites; detailed binding and ranges belong to queries only.
2
+ local M = { languages = { c = true, cpp = true, csharp = true, java = true,
3
+ kotlin = true, typescript = true, arkts = true, go = true, rust = true } }
4
+ local builtin = {}
5
+ for word in ([[void bool boolean byte sbyte char wchar_t char16_t char32_t short int long float double decimal
6
+ signed unsigned const volatile static extern inline virtual public private protected internal abstract override
7
+ final sealed readonly ref out in params mut dyn impl auto var val let new class struct union enum interface
8
+ record typename template typedef using extends implements throws where async await return yield get set
9
+ string object any unknown never number symbol bigint undefined null true false self Self super this
10
+ int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex64 complex128
11
+ rune error chan map func type pub fn trait i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize f32 f64
12
+ Unit Nothing Any Boolean Byte Short Int Long Float Double Char String dynamic]]):gmatch('%S+') do builtin[word] = true end
13
+ local callable = { ['function'] = true, method = true, prototype = true, constructor = true, destructor = true }
14
+ local containers = { class = true, struct = true, union = true, interface = true, record = true, enum = true }
15
+
16
+ function M.collect(r, T)
17
+ if not M.languages[r.language] or r.cpp_aliases_only or r.dispatch_only or r.collect_type_uses == false
18
+ or not (r.ordinary_bindings or r.ranges) then return end
19
+ local language, sites, names, seen = r.language, {}, {}, {}
20
+ local details = r.ordinary_bindings or r.ranges
21
+ local suffix = language == 'typescript' or language == 'arkts' or language == 'kotlin' or language == 'rust'
22
+ local prefix = language == 'c' or language == 'cpp' or language == 'java' or language == 'csharp'
23
+ local separator = (language == 'c' or language == 'cpp' or language == 'rust') and '::' or '.'
24
+ for _, n in ipairs(r.nodes) do if n.type_token then names[n.type_token] = true end end
25
+ local function add(t, role, first, last)
26
+ local name = T:text(t)
27
+ if seen[t] or names[t] or T.k[t] ~= 'id' or not name:match('^[%a_$][%w_$]*$') then return end
28
+ seen[t] = true
29
+ local a = t
30
+ while a > (first or 1) and T:text(a - 1) == separator and T.k[a - 2] == 'id' do a = a - 2 end
31
+ if T:text(t + 1) == separator and t + 1 <= (last or T.n) then return end
32
+ local written = T.src:sub(T.s[a], T.e[t]):gsub('%s+', '')
33
+ local site = { tok = t, name = name, kind = 'type', line = T:line(t), type_role = role,
34
+ recv = a < t and written:sub(1, #written - #name - #separator) or nil }
35
+ sites[#sites + 1] = site
36
+ if details then
37
+ site.type_use = { role = role, written = written, line = site.line, first = T.s[t] - 1, last = T.e[t], at = T.s[a] - 1 }
38
+ local tag = T:text(a - 1)
39
+ if tag == 'struct' or tag == 'union' or tag == 'enum' or tag == 'class' then site.type_use.elaborated = tag end
40
+ end
41
+ end
42
+ local function range(a, b, role, exclude)
43
+ if not a or not b or a > b then return end
44
+ for _, t in ipairs(T:idents(a, b)) do if t ~= exclude then add(t, role, a, b) end end
45
+ end
46
+ local function cut(a, b)
47
+ local depth = 0
48
+ for i = a, b do
49
+ local s = T:text(i)
50
+ if s == '<' then depth = depth + 1 elseif s == '>' then depth = math.max(0, depth - 1) end
51
+ if depth == 0 and (s == '=' or s == '{' or s == 'where') then return i - 1 end
52
+ end
53
+ return b
54
+ end
55
+ local function angle(a, b, owner)
56
+ if T:text(a) ~= '<' and not (language == 'go' and T:text(a) == '[') then return a end
57
+ local open, close = T:text(a), T:text(a) == '[' and ']' or '>'
58
+ local depth, expect = 1, true
59
+ for i = a + 1, math.min(b, a + 256) do
60
+ local s = T:text(i)
61
+ if s == open then depth = depth + 1 elseif s == close then depth = depth - 1 end
62
+ if depth == 0 then return i + 1 end
63
+ if depth == 1 then
64
+ if s == ',' then expect = true
65
+ elseif T.k[i] == 'id' and expect then
66
+ names[i], expect = true, false
67
+ if r.ordinary_bindings or r.ranges then
68
+ r.type_parameters = r.type_parameters or {}
69
+ r.type_parameters[#r.type_parameters + 1] = { name = s, first = T.s[owner.ti] - 1, last = T.e[owner.tj] }
70
+ end
71
+ elseif s == 'extends' or s == ':' then
72
+ local j = i + 1
73
+ while j <= b and T:text(j) ~= ',' and T:text(j) ~= close do j = j + 1 end
74
+ range(i + 1, j - 1, 'constraint')
75
+ end
76
+ end
77
+ end
78
+ return b + 1
79
+ end
80
+ local function parameter(a, b, role)
81
+ b = cut(a, b)
82
+ if a > b then return end
83
+ if suffix then
84
+ for i = a, b do if T:text(i) == ':' then range(i + 1, b, role); return end end
85
+ return
86
+ end
87
+ if language == 'go' then
88
+ if T.k[a] == 'id' and T:text(a + 1) ~= '.' and a < b then
89
+ local i = a + 1
90
+ while T:text(i) == ',' and T.k[i + 1] == 'id' do i = i + 2 end
91
+ range(i, b, role)
92
+ else range(a, b, role) end
93
+ return
94
+ end
95
+ local last, count, depth, primitive = nil, 0, 0, false
96
+ for i = a, b do
97
+ local s = T:text(i)
98
+ if s == '<' then depth = depth + 1 elseif s == '>' then depth = math.max(0, depth - 1) end
99
+ if depth == 0 then
100
+ if T.k[i] == 'id' then last, count = i, count + 1 end
101
+ if s == 'int' or s == 'char' or s == 'void' or s == 'string' or s == 'bool' then primitive = true end
102
+ end
103
+ end
104
+ local exclude = last and (count > 1 or primitive) and T:text(last - 1) ~= separator
105
+ and T:text(last + 1) ~= '<' and last or nil
106
+ range(a, b, role, exclude)
107
+ end
108
+ local function parameters(a, b, role)
109
+ local start, i, depth = a, a, 0
110
+ while i <= b + 1 do
111
+ local s = T:text(i)
112
+ if i == b + 1 or s == ',' and depth == 0 then parameter(start, i - 1, role); start = i + 1
113
+ elseif s == '<' then depth = depth + 1
114
+ elseif s == '>' then depth = math.max(0, depth - 1)
115
+ elseif T.m[i] and T.m[i] > i then i = T.m[i] end
116
+ i = i + 1
117
+ end
118
+ end
119
+ local declaration_prefixes = {}
120
+ for index, n in ipairs(r.nodes) do
121
+ if index % 128 == 0 then require('codeoutline.control').check() end
122
+ local ni = n.type_token
123
+ if ni and (callable[n.kind] or containers[n.kind] or n.kind == 'typedef') then
124
+ local a = n.ti
125
+ while builtin[T:text(a)] and T:text(a) ~= 'template' and T:text(a) ~= 'void' do a = a + 1 end
126
+ if language == 'java' and T:text(a) == '<' then angle(a, n.tj, n) end
127
+ if language == 'cpp' then
128
+ for j = n.ti, math.max(1, n.ti - 256), -1 do
129
+ local s = T:text(j)
130
+ if s == 'template' and T:text(j + 1) == '<' then angle(j + 1, n.tj, { ti = j, tj = n.tj }); break end
131
+ if j < n.ti and (s == ';' or s == '{' or s == '}') then break end
132
+ end
133
+ end
134
+ end
135
+ if ni and callable[n.kind] then
136
+ local p = angle(ni + 1, n.tj, n)
137
+ if T:text(p) == '(' and T.m[p] then
138
+ local ending = T.m[p]
139
+ parameters(p + 1, ending - 1, 'parameter')
140
+ if prefix then range(n.ti, ni - 1, 'return') end
141
+ local a = ending + 1
142
+ if language == 'go' then
143
+ local b = a
144
+ while b <= n.tj and T:text(b) ~= '{' and T:text(b) ~= ';' and T.k[b] ~= 'nl' do b = b + 1 end
145
+ if T:text(a) == '(' and T.m[a] then parameters(a + 1, T.m[a] - 1, 'return') else range(a, b - 1, 'return') end
146
+ if T:text(n.ti + 1) == '(' and T.m[n.ti + 1] then parameters(n.ti + 2, T.m[n.ti + 1] - 1, 'receiver') end
147
+ elseif T:text(a) == ':' or T:text(a) == '->' then range(a + 1, cut(a + 1, n.tj), 'return') end
148
+ end
149
+ elseif ni and containers[n.kind] then
150
+ local a, mode = angle(ni + 1, n.tj, n), nil
151
+ if T:text(a) == '(' and T.m[a] then parameters(a + 1, T.m[a] - 1, 'parameter'); a = T.m[a] + 1 end
152
+ for i = a, n.tj do
153
+ local s = T:text(i)
154
+ if s == '{' then break end
155
+ if s == ':' or s == 'extends' or s == 'implements' then mode = i + 1 end
156
+ if mode and T.k[i] == 'id' then add(i, 'inheritance', mode, n.tj) end
157
+ end
158
+ elseif ni and (n.kind == 'field' or n.kind == 'property' or n.kind == 'variable' or n.kind == 'constant') then
159
+ if prefix then
160
+ local span = declaration_prefixes[n.tj]
161
+ if not span then
162
+ local a = n.ti
163
+ if a >= ni then
164
+ a = ni - 1
165
+ while a > 0 and T:text(a) ~= ';' and T:text(a) ~= '{' and T:text(a) ~= '}' do a = a - 1 end
166
+ a = a + 1
167
+ end
168
+ span = { a, ni - 1 }; declaration_prefixes[n.tj] = span
169
+ end
170
+ range(span[1], span[2], n.kind == 'field' and 'field' or 'variable')
171
+ elseif suffix and T:text(ni + 1) == ':' then range(ni + 2, cut(ni + 2, n.tj), (n.kind == 'field' or n.kind == 'property') and 'field' or 'variable')
172
+ elseif language == 'go' then
173
+ if n.embedded then range(n.ti, n.tj, 'inheritance') else range(ni + 1, cut(ni + 1, n.tj), n.kind == 'field' and 'field' or 'variable') end
174
+ end
175
+ elseif ni and n.kind == 'typedef' then
176
+ local first_site, a, b = #sites + 1
177
+ if prefix then
178
+ if T:text(n.ti) == 'using' then a, b = ni + 2, n.tj - 1 else a, b = n.ti + 1, ni - 1 end
179
+ else
180
+ a = angle(ni + 1, n.tj, n); if T:text(a) == '=' then a = a + 1 end
181
+ b = cut(a, n.tj)
182
+ end
183
+ range(a, b, 'alias')
184
+ if details then
185
+ local first, last = a, b
186
+ if T:text(last) == ';' then last = last - 1 end
187
+ local tag = T:text(first)
188
+ if tag == 'struct' or tag == 'class' or tag == 'union' or tag == 'enum' then first = first + 1 end
189
+ local plain, identifier = first <= last, true
190
+ for j = first, last do
191
+ if identifier then plain = plain and T.k[j] == 'id'
192
+ else plain = plain and (T:text(j) == '.' or T:text(j) == '::') end
193
+ identifier = not identifier
194
+ end
195
+ -- Containers, unions, function types and pointer decoration are
196
+ -- type uses, but do not make the alias identical to an element type.
197
+ if plain and not identifier and (language ~= 'go' or T:text(ni + 1) == '=') then
198
+ for j = first_site, #sites do sites[j].type_use.alias_name = n.name end
199
+ elseif language == 'go' and T:text(ni + 1) ~= '=' then
200
+ for j = first_site, #sites do sites[j].type_use.role = 'underlying' end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ local function import_alias(t)
206
+ for i = t - 2, math.max(1, t - 128), -1 do
207
+ local s = T:text(i)
208
+ if s == 'import' or s == 'use' then return true end
209
+ if s == ';' or s == '=' or s == 'return' or s == 'function' or s == 'fn' then return false end
210
+ if s == '{' then
211
+ for j = i - 1, math.max(1, i - 4), -1 do if T:text(j) == 'import' or T:text(j) == 'use' then return true end end
212
+ return false
213
+ end
214
+ end
215
+ return false
216
+ end
217
+ for _, i in ipairs(T:idents(1, T.n)) do
218
+ local prev = T:text(i - 1)
219
+ if prev == 'new' or prev == 'as' or prev == 'is' or prev == 'instanceof' then
220
+ local j = i
221
+ while T:text(j + 1) == separator and T.k[j + 2] == 'id' do j = j + 2 end
222
+ if prev ~= 'as' or not import_alias(i) then range(i, j, prev == 'new' and 'construction' or 'cast') end
223
+ elseif prev == '<' and (T:text(i - 2) == 'static_cast' or T:text(i - 2) == 'dynamic_cast'
224
+ or T:text(i - 2) == 'const_cast' or T:text(i - 2) == 'reinterpret_cast') then
225
+ local j = i; while j <= math.min(T.n, i + 128) and T:text(j) ~= '>' do j = j + 1 end
226
+ if T:text(j) == '>' then range(i, j - 1, 'cast') end
227
+ elseif (language == 'go' or language == 'rust') and T:text(i + 1) == '{'
228
+ and (prev == '=' or prev == ':=' or prev == 'return' or prev == '&') then add(i, 'construction')
229
+ elseif prefix and (prev == 'struct' or prev == 'class' or prev == 'union' or prev == 'enum') then add(i, 'variable')
230
+ elseif prefix and (prev == ';' or prev == '{' or prev == '}' or prev == 'const' or prev == 'static') then
231
+ local j = i
232
+ while T:text(j + 1) == separator and T.k[j + 2] == 'id' do j = j + 2 end
233
+ local p = j + 1
234
+ if T:text(p) == '<' then
235
+ local depth = 1; p = p + 1
236
+ while p <= math.min(T.n, j + 128) and depth > 0 do
237
+ if T:text(p) == '<' then depth = depth + 1 elseif T:text(p) == '>' then depth = depth - 1 end
238
+ p = p + 1
239
+ end
240
+ end
241
+ while T:text(p) == '*' or T:text(p) == '&' or T:text(p) == '&&' or T:text(p) == 'const' do p = p + 1 end
242
+ if T.k[p] == 'id' and (T:text(p + 1) == '=' or T:text(p + 1) == ';' or T:text(p + 1) == ','
243
+ or T:text(p + 1) == '[' or T:text(p + 1) == '{') then range(i, p - 1, 'variable') end
244
+ elseif (suffix or language == 'go') and (prev == 'let' or prev == 'var' or prev == 'val' or prev == 'const') then
245
+ local p = i + 1
246
+ if suffix and T:text(p) == ':' then p = p + 1 elseif suffix then p = T.n + 1 end
247
+ local b = p
248
+ while b <= T.n and T:text(b) ~= '=' and T:text(b) ~= ';' and T:text(b) ~= '{'
249
+ and T.k[b] ~= 'nl' and T:line(b) == T:line(i) do b = b + 1 end
250
+ range(p, b - 1, 'variable')
251
+ end
252
+ end
253
+ for _, i in ipairs(T:calls(1, T.n)) do
254
+ local a = i
255
+ while T:text(a - 1) == separator and T.k[a - 2] == 'id' do a = a - 2 end
256
+ if T:text(a - 1) == 'new' then add(i, 'construction', a, i) end
257
+ end
258
+ r.type_use_refs = sites
259
+ end
260
+ return M
@@ -1 +1 @@
1
- return 15
1
+ return 19
@@ -1 +1 @@
1
- return '0.2.6'
1
+ return '0.3.0'