@codua/codeoutline-win32-x64 0.1.5 → 0.1.9

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.
@@ -2,10 +2,15 @@
2
2
  -- file dependencies (#include / import), and call edges between nodes.
3
3
  --
4
4
  -- local G = graph.build(idx)
5
- -- G.nodes[id] -> { f = file index, node = <node table>, id = id }
5
+ -- G.nodes[id] -> <node table> (shared with the file record)
6
+ -- G.node_file[id] -> file index
6
7
  -- G.files[f] -> file record (path, language, nodes, refs, imports)
7
8
  -- G.by_name[name] -> { id, ... }
8
- -- G.out[id], G.inn[id] -> { {id = other, line = call line, kind = ref kind}, ... }
9
+ -- G.out[id], G.inn[id] -> { other, line, kind, other, line, kind, ... }
10
+ --
11
+ -- Edge lists are flat, EDGE values per edge, and nodes carry no wrapper
12
+ -- table: on large trees per-edge and per-node tables cost more than the
13
+ -- data they hold. Walk an edge list with `for i = 1, #list, graph.EDGE`.
9
14
  --
10
15
  -- Resolution is name-based with locality scoring, the same model CodeGraph
11
16
  -- uses for dynamic languages: a call binds to the best-placed definition of
@@ -16,6 +21,7 @@
16
21
  local control = require('codeoutline.control')
17
22
  local M = {}
18
23
 
24
+ M.EDGE = 3
19
25
  M.MAX_TIES = 3
20
26
  M.MAX_CANDIDATES = 200
21
27
 
@@ -44,6 +50,13 @@ local function push(t, k, v)
44
50
  list[#list + 1] = v
45
51
  end
46
52
 
53
+ local function push3(t, k, a, b, c)
54
+ local list = t[k]
55
+ if not list then list = {}; t[k] = list end
56
+ local n = #list
57
+ list[n + 1], list[n + 2], list[n + 3] = a, b, c
58
+ end
59
+
47
60
  -- Files a file depends on, resolved from its #include / import records.
48
61
  local function resolve_deps(G, f)
49
62
  local rec = G.files[f]
@@ -175,7 +188,7 @@ local function resolve_deps(G, f)
175
188
  if path:sub(-2) == '.*' then
176
189
  for _, g in ipairs(G.pkg_files[path:sub(1, -3)] or {}) do add(g) end
177
190
  else
178
- for _, id in ipairs(G.by_qualified[path] or {}) do add(G.nodes[id].f) end
191
+ for _, id in ipairs(G.by_qualified[path] or {}) do add(G.node_file[id]) end
179
192
  end
180
193
  end
181
194
  end
@@ -210,79 +223,92 @@ end
210
223
 
211
224
  -- Innermost class-like container of a node (for self./this. calls).
212
225
  local function owner_class(G, id)
213
- local n = G.nodes[id]
214
- local rec = G.files[n.f]
215
- local p = n.node.parent
226
+ local f = G.node_file[id]
227
+ local rec = G.files[f]
228
+ local p = G.nodes[id].parent
216
229
  while p do
217
230
  local pn = rec.nodes[p]
218
231
  if pn.kind == 'class' or pn.kind == 'record' or pn.kind == 'interface' or pn.kind == 'enum'
219
232
  or pn.kind == 'struct' or pn.kind == 'union' then
220
- return G.id_of[n.f][p]
233
+ return G.id_of[f][p]
221
234
  end
222
235
  p = pn.parent
223
236
  end
224
237
  return nil
225
238
  end
226
239
 
227
- local function score(G, f, src_id, ref, cid, deps, paired)
240
+ -- Placement of a candidate in file cf relative to the ref's file f: 0 when
241
+ -- unrelated, nil when invisible (a C static of another file).
242
+ local function locality(G, f, cf, c, deps, paired)
243
+ if cf == f then return 100 end
244
+ if c.static then return nil end
245
+ if deps[cf] then return 50 end
246
+ if paired[cf] then return 40 end
247
+ if G.file_dir[cf] == G.file_dir[f] then return 20 end
248
+ return 0
249
+ end
250
+
251
+ -- owner: the innermost class of src_id, passed in by resolve_ref when the
252
+ -- ref kind needs it (computed once per ref, not per candidate).
253
+ local function score(G, f, src_id, ref_kind, recv, cid, deps, paired, owner)
228
254
  local c = G.nodes[cid]
229
- local cf = c.f
230
- local crec, frec = G.files[cf], G.files[f]
231
- if family(crec.language) ~= family(frec.language) then return nil end
232
- local s = 0
233
- if cf == f then
234
- s = s + 100
235
- elseif c.node.static then
236
- return nil -- C static: invisible outside its file
237
- elseif deps[cf] then
238
- s = s + 50
239
- elseif paired[cf] then
240
- s = s + 40
241
- elseif dirname(crec.path) == dirname(frec.path) then
242
- s = s + 20
243
- end
244
- local kind = c.node.kind
255
+ local cf = G.node_file[cid]
256
+ if family(G.files[cf].language) ~= family(G.files[f].language) then return nil end
257
+ local s = locality(G, f, cf, c, deps, paired)
258
+ if not s then return nil end
259
+ local kind = c.kind
245
260
  if kind == 'prototype' then s = s - 3 end
246
- if ref.kind == 'member_call' and (ref.recv == 'self' or ref.recv == 'this' or ref.recv == 'cls') and src_id then
247
- local owner = owner_class(G, src_id)
248
- if owner and G.nodes[cid].node.parent and G.id_of[cf][G.nodes[cid].node.parent] == owner then
261
+ if ref_kind == 'member_call' and (recv == 'self' or recv == 'this' or recv == 'cls') and src_id then
262
+ if owner and c.parent and G.id_of[cf][c.parent] == owner then
249
263
  s = s + 80
250
264
  elseif kind ~= 'method' then
251
265
  s = s - 20
252
266
  end
253
- elseif ref.kind == 'member_call' and s <= 0 then
267
+ elseif ref_kind == 'member_call' and s <= 0 then
254
268
  -- obj.name() on an unknown receiver: binding it to any same-named
255
269
  -- method project-wide (str.format -> some format()) is mostly wrong,
256
270
  -- so only local candidates qualify.
257
271
  return nil
258
- elseif ref.kind == 'call' and frec.language == 'java' and src_id then
272
+ elseif ref_kind == 'call' and G.files[f].language == 'java' and src_id then
259
273
  -- unqualified call inside a class: implicit this
260
- local owner = owner_class(G, src_id)
261
- if owner and c.node.parent and G.id_of[cf][c.node.parent] == owner then s = s + 30 end
274
+ if owner and c.parent and G.id_of[cf][c.parent] == owner then s = s + 30 end
262
275
  end
263
276
  return s
264
277
  end
265
278
 
266
- local function resolve_ref(G, f, src_id, ref, deps, paired)
267
- if ref.kind == 'annotation' then return nil end
279
+ local function is_decl(node) return node.kind == 'prototype' or node.decl end
280
+
281
+ -- Scored candidates of the current resolve_ref call, reused across calls to
282
+ -- keep the hot loop allocation-free; only the first n entries are live.
283
+ local scored_ids, scored_vals = {}, {}
284
+
285
+ local function resolve_ref(G, f, src_id, name, kind, recv, deps, paired)
286
+ if kind == 'annotation' then return nil end
268
287
  local accept = CALLABLE
269
- if ref.kind == 'new' then accept = NEWABLE elseif ref.kind == 'ref' then accept = VALUE end
270
- local cands = G.by_name[ref.name]
288
+ if kind == 'new' then accept = NEWABLE elseif kind == 'ref' then accept = VALUE end
289
+ local cands = G.by_name[name]
271
290
  if not cands then return nil end
272
291
  -- Score first: score() rejects other languages and statics of other
273
292
  -- files, so "is there a real definition?" is only asked among the
274
293
  -- candidates this call could actually reach. (Asking it over all
275
294
  -- same-named nodes let a Python `def work` hide the C prototype of
276
295
  -- `work`, after which the Python def was filtered out too.)
277
- local function is_decl(node) return node.kind == 'prototype' or node.decl end
278
- local scored, has_def = {}, false
296
+ local self_call = kind == 'member_call' and src_id and (recv == 'self' or recv == 'this' or recv == 'cls')
297
+ local owner = (self_call or (kind == 'call' and src_id and G.files[f].language == 'java'))
298
+ and owner_class(G, src_id) or nil
299
+ -- score() rejects an unknown-receiver member call to an unrelated file;
300
+ -- checking placement first skips most candidates of common method names.
301
+ local local_only = kind == 'member_call' and not self_call
302
+ local n, has_def = 0, false
279
303
  for i = 1, math.min(#cands, M.MAX_CANDIDATES) do
280
304
  local cid = cands[i]
281
- local node = G.nodes[cid].node
282
- if cid ~= src_id and accept[node.kind] then
283
- local s = score(G, f, src_id, ref, cid, deps, paired)
305
+ local node = G.nodes[cid]
306
+ if cid ~= src_id and accept[node.kind]
307
+ and (not local_only or (locality(G, f, G.node_file[cid], node, deps, paired) or 0) > 0) then
308
+ local s = score(G, f, src_id, kind, recv, cid, deps, paired, owner)
284
309
  if s then
285
- scored[#scored + 1] = { cid, s }
310
+ n = n + 1
311
+ scored_ids[n], scored_vals[n] = cid, s
286
312
  if not is_decl(node) then has_def = true end
287
313
  end
288
314
  end
@@ -290,9 +316,9 @@ local function resolve_ref(G, f, src_id, ref, deps, paired)
290
316
  -- Prefer definitions: drop prototypes / in-class declarations when a
291
317
  -- reachable definition exists.
292
318
  local best, ties = nil, {}
293
- for _, c in ipairs(scored) do
294
- local cid, s = c[1], c[2]
295
- if not (has_def and is_decl(G.nodes[cid].node)) then
319
+ for i = 1, n do
320
+ local cid, s = scored_ids[i], scored_vals[i]
321
+ if not (has_def and is_decl(G.nodes[cid])) then
296
322
  if not best or s > best then
297
323
  best, ties = s, { cid }
298
324
  elseif s == best and #ties < M.MAX_TIES then
@@ -308,7 +334,7 @@ end
308
334
 
309
335
  function M.build(idx)
310
336
  local G = {
311
- files = {}, file_index = {}, nodes = {}, id_of = {},
337
+ files = {}, file_index = {}, file_dir = {}, nodes = {}, node_file = {}, id_of = {},
312
338
  by_name = {}, by_lname = {}, by_qualified = {}, by_base = {}, by_stem = {},
313
339
  pkg_files = {}, children = {}, out = {}, inn = {}, deps = {}, dir_files = {}, ns_files = {},
314
340
  generation = idx.generation,
@@ -316,6 +342,9 @@ function M.build(idx)
316
342
  local paths = {}
317
343
  for rel in pairs(idx.files) do paths[#paths + 1] = rel end
318
344
  table.sort(paths)
345
+ -- Directory ids per file: placement scoring compares numbers instead of
346
+ -- slicing two paths per candidate.
347
+ local dir_ids = {}
319
348
  -- Checkpoints per file: LuaJIT does not run count hooks inside compiled
320
349
  -- loops, so cancellation relies on these (as in Index:refresh).
321
350
  for f, rel in ipairs(paths) do
@@ -323,6 +352,10 @@ function M.build(idx)
323
352
  local rec = idx.files[rel]
324
353
  G.files[f] = rec
325
354
  G.file_index[rel] = f
355
+ local dir = dirname(rel)
356
+ local did = dir_ids[dir]
357
+ if not did then did = f; dir_ids[dir] = did end
358
+ G.file_dir[f] = did
326
359
  push(G.by_base, basename(rel), f)
327
360
  push(G.by_stem, stem(rel), f)
328
361
  push(G.dir_files, dirname(rel), f)
@@ -331,7 +364,7 @@ function M.build(idx)
331
364
  G.id_of[f] = ids
332
365
  for n, node in ipairs(rec.nodes) do
333
366
  local id = #G.nodes + 1
334
- G.nodes[id] = { id = id, f = f, n = n, node = node }
367
+ G.nodes[id], G.node_file[id] = node, f
335
368
  ids[n] = id
336
369
  push(G.by_name, node.name, id)
337
370
  push(G.by_lname, node.name:lower(), id)
@@ -353,14 +386,17 @@ function M.build(idx)
353
386
  control.check()
354
387
  local deps, paired = resolve_deps(G, f)
355
388
  G.deps[f] = deps
356
- for _, ref in ipairs(rec.refs) do
357
- if ref.from and ref.from > 0 then
358
- local src = G.id_of[f][ref.from]
359
- local targets = resolve_ref(G, f, src, ref, deps, paired)
389
+ local refs, ids = rec.refs, G.id_of[f]
390
+ local names, froms, kinds, lines, recvs = refs.name, refs.from, refs.kind, refs.line, refs.recv
391
+ for i = 1, #names do
392
+ local from = froms[i]
393
+ if from > 0 then
394
+ local src, kind, line = ids[from], kinds[i], lines[i]
395
+ local targets = resolve_ref(G, f, src, names[i], kind, recvs[i] or nil, deps, paired)
360
396
  if targets then
361
397
  for _, dst in ipairs(targets) do
362
- push(G.out, src, { id = dst, line = ref.line, kind = ref.kind })
363
- push(G.inn, dst, { id = src, line = ref.line, kind = ref.kind })
398
+ push3(G.out, src, dst, line, kind)
399
+ push3(G.inn, dst, src, line, kind)
364
400
  edges = edges + 1
365
401
  end
366
402
  end
@@ -373,8 +409,7 @@ end
373
409
 
374
410
  -- Location string for a node: "path:line".
375
411
  function M.where(G, id)
376
- local n = G.nodes[id]
377
- return G.files[n.f].path .. ':' .. n.node.line
412
+ return G.files[G.node_file[id]].path .. ':' .. G.nodes[id].line
378
413
  end
379
414
 
380
415
  return M
@@ -145,6 +145,11 @@ function M.start(config, submit, cancel, codec)
145
145
  if now - state.started > (state.dispatched and 125000 or 15000) then conn:close('timeout') end
146
146
  end
147
147
  end,
148
+ -- A session waits on a client reply (roots/list) or a queued tool call.
149
+ busy = function()
150
+ for _, session in pairs(sessions) do if next(session.pending) then return true end end
151
+ return false
152
+ end,
148
153
  close = function()
149
154
  for sid in pairs(sessions) do remove_session(sid) end
150
155
  for conn in pairs(connections) do conn:close('shutdown') end