@codua/codeoutline-win32-x64 0.3.6 → 0.3.8

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.
@@ -3,6 +3,7 @@ local M = {}
3
3
  local control = require('codeoutline.shared.control')
4
4
  local loaders = { require = true, dofile = true, loadfile = true }
5
5
  local EXPORT_OWNER = '__exports'
6
+ local EMPTY = {}
6
7
  -- Loader call at token j: a known loader name, or any dotted/plain name whose
7
8
  -- last segment ends in `import` (`ks.import`, `skynet_import`). Returns the
8
9
  -- kind recorded in lua_imports, the callee's last token, and the base name
@@ -41,6 +42,60 @@ local function binding_at(bindings, name, token)
41
42
  return best
42
43
  end
43
44
 
45
+ -- A nil-initialized local can receive a module in a sibling if block. Keep
46
+ -- possible imports at later uses, and stop on a write we cannot model. Only the
47
+ -- last write of each branch survives; sibling branches keep their source order.
48
+ local function conditional_hint(bindings, name, base, token, T, offset)
49
+ if not base or not base.nil_local then return nil end
50
+ local writes = bindings[name]
51
+ if #writes > 32 then return nil end
52
+ local branches, last, seen = {}, {}, {}
53
+ for _, write in ipairs(writes) do
54
+ local finished = token and write.b < token or offset and T.e[write.b] <= offset
55
+ if base.token < write.token and finished then
56
+ if write.assignment then
57
+ if not write.branch or write.outer ~= base.a then return nil end
58
+ local arm = write.arm or write.a
59
+ if not seen[arm] then seen[arm], branches[#branches + 1] = true, arm end
60
+ last[arm] = write
61
+ elseif write.if_local and write.outer == base.a then
62
+ -- Branch-local declarations can hide a different variable.
63
+ return nil
64
+ end
65
+ end
66
+ end
67
+ local imports = {}
68
+ for _, scope in ipairs(branches) do
69
+ local import = last[scope].branch_import
70
+ if import then imports[#imports + 1] = import end
71
+ end
72
+ if #imports == 1 then return 'lua:' .. imports[1] end
73
+ if #imports > 1 then return 'lua-conditional:' .. table.concat(imports, ',') end
74
+ end
75
+
76
+ local function metatable_import(T, bindings, bound)
77
+ if not bound then return nil end
78
+ local call = bound.token + 2
79
+ if T:text(bound.token + 1) ~= '=' or T:text(call) ~= 'setmetatable'
80
+ or binding_at(bindings, 'setmetatable', call) or T:text(call + 1) ~= '('
81
+ or T:text(call + 2) ~= '{' then return nil end
82
+ local object_end = T.m[call + 2]
83
+ if not object_end or T:text(object_end + 1) ~= ',' or T:text(object_end + 2) ~= '{' then return nil end
84
+ local key = object_end + 3
85
+ if T:text(key) ~= '__index' or T:text(key + 1) ~= '=' or T.k[key + 2] ~= 'id' then return nil end
86
+ local close = key + 3
87
+ if T:text(close) == ',' or T:text(close) == ';' then close = close + 1 end
88
+ if T.m[object_end + 2] ~= close or T.m[call + 1] ~= close + 1
89
+ or not value_end(T, close + 2) then return nil end
90
+ local base = binding_at(bindings, T:text(key + 2), key + 2)
91
+ return base and tonumber(base.hint:match('^lua:(%d+)$'))
92
+ end
93
+
94
+ local function declared_local(T, params, token)
95
+ return T:text(token - 1) == 'local' or params[token] ~= nil
96
+ or T:text(token - 1) == 'function' and T:text(token - 2) == 'local'
97
+ end
98
+
44
99
  -- Shared lexical table-member lookup for normal references and query-only
45
100
  -- configured callback aliases. Results are local parser node indices.
46
101
  function M.local_members(nodes, scopes, name, token, scope, finish)
@@ -58,6 +113,9 @@ function M.collect(r, T, bm)
58
113
  r.lua_imports, r.lua_exports, r.lua_locals = {}, {}, {}
59
114
  local details = not r.defer_analysis and (r.ranges or r.ordinary_bindings)
60
115
  local scopes, stack, params, bindings, function_scopes = {}, {}, {}, {}, {}
116
+ -- Query-only: `then`/`else` do not open a block, so every arm of one `if`
117
+ -- shares its scope. Arm openings tell sequential writes from alternatives.
118
+ local arms = {}
61
119
  for i = 1, T.n do
62
120
  if stack[#stack] and bm[stack[#stack]] == i then stack[#stack] = nil end
63
121
  scopes[i] = stack[#stack] or 0
@@ -65,13 +123,21 @@ function M.collect(r, T, bm)
65
123
  function_scopes[i] = T:text(i) == 'function' or function_scopes[scopes[i]]
66
124
  stack[#stack + 1] = i
67
125
  end
126
+ if details and T.k[i] == 'kw' then
127
+ local text = T:text(i)
128
+ if text == 'then' or text == 'else' then
129
+ local list = arms[scopes[i]]
130
+ if not list then list = {}; arms[scopes[i]] = list end
131
+ list[#list + 1] = i
132
+ end
133
+ end
68
134
  if T:text(i) == 'function' and T.k[i] == 'kw' then
69
135
  local p = i + 1
70
136
  while p < (bm[i] or i) and T:text(p) ~= '(' do p = p + 1 end
71
137
  for j = p + 1, (T.m[p] or p) - 1 do if T.k[j] == 'id' then params[j] = i end end
72
138
  end
73
139
  end
74
- local function add(name, token, scope, hint, imported)
140
+ local function add(name, token, scope, hint, imported, finish)
75
141
  bindings[name] = bindings[name] or {}
76
142
  -- Reassignment/parameter shadowing must not revive a global name
77
143
  -- after a loader value was bound under this lexical name.
@@ -80,7 +146,31 @@ function M.collect(r, T, bm)
80
146
  if b.imported and b.token < token and b.a <= token and b.b >= token then imported = true end
81
147
  end
82
148
  end
83
- bindings[name][#bindings[name] + 1] = { token = token, a = scope, b = bm[scope] or T.n, hint = hint, imported = imported }
149
+ local binding = { token = token, a = scope, b = bm[scope] or T.n, hint = hint, imported = imported }
150
+ bindings[name][#bindings[name] + 1] = binding
151
+ if details then
152
+ local local_decl = T:text(token - 1) == 'local'
153
+ local next = T:text(token + 1)
154
+ binding.local_declaration = declared_local(T, params, token)
155
+ binding.nil_local = local_decl and (next == '=' and T:text(token + 2) == 'nil' and value_end(T, token + 3)
156
+ or next ~= '=' and next ~= ',' and (T.k[token + 1] ~= 'op' or next == ';'))
157
+ binding.assignment = not local_decl and not params[token] and next == '='
158
+ if T:text(scope) == 'if' then
159
+ binding.outer = scopes[scope]
160
+ binding.if_local = local_decl or T:text(token - 2) == 'local' and T:text(token - 1) == 'function'
161
+ local index = hint:match('^lua:(%d+)$')
162
+ local last = index and finish or T:text(token + 2) == 'nil' and token + 2
163
+ -- Any top-level write in the branch counts; the scope check
164
+ -- already excludes writes nested in a deeper block.
165
+ if binding.assignment and last and value_end(T, last + 1) then
166
+ binding.branch = true
167
+ binding.branch_import = index
168
+ for _, opening in ipairs(arms[scope] or EMPTY) do
169
+ if opening < token then binding.arm = opening else break end
170
+ end
171
+ end
172
+ end
173
+ end
84
174
  end
85
175
  for i = 1, T.n do
86
176
  if i % 4096 == 0 then control.check() end
@@ -91,7 +181,7 @@ function M.collect(r, T, bm)
91
181
  elseif prev == 'local' or (next == '=' and prev ~= '.' and prev ~= ':' and prev ~= '{' and prev ~= ',') then
92
182
  local hint = '?'
93
183
  if next == '=' and T:text(i + 2) == '{' then hint = 'lua-local:' .. T:text(i) end
94
- local kind, call, base
184
+ local kind, call, base, finish
95
185
  if next == '=' then kind, call, base = loader_call(T, i + 2) end
96
186
  local imported = kind ~= nil
97
187
  if kind then
@@ -112,6 +202,7 @@ function M.collect(r, T, bm)
112
202
  r.lua_imports[#r.lua_imports + 1] = { path = path, kind = kind,
113
203
  member = member and T:text(member) or nil }
114
204
  hint = (member and 'lua-value:' or 'lua:') .. #r.lua_imports
205
+ finish = close
115
206
  if kind == 'loadfile' then hint = '?' end
116
207
  if member and kind ~= 'loadfile' then
117
208
  r.refs[#r.refs + 1] = { tok = member, name = T:text(member), kind = 'ref',
@@ -122,7 +213,7 @@ function M.collect(r, T, bm)
122
213
  end
123
214
  end
124
215
  -- Keep ordinary local function/table lookup; block unknown imported receivers.
125
- add(T:text(i), i, scopes[i], hint, imported)
216
+ add(T:text(i), i, scopes[i], hint, imported, finish)
126
217
  end
127
218
  end
128
219
  end
@@ -168,7 +259,8 @@ function M.collect(r, T, bm)
168
259
  for _, later in ipairs(bindings[owner] or {}) do
169
260
  if later.a == scope and later.token > first then finish = math.min(finish, later.token - 1) end
170
261
  end
171
- info = { members = M.local_members(r.nodes, scopes, owner, first, scope, finish), fields = {} }
262
+ info = { members = M.local_members(r.nodes, scopes, owner, first, scope, finish), fields = {},
263
+ base_import = metatable_import(T, bindings, bound) }
172
264
  owners[key] = info
173
265
  if bound then receiver_tables[bound] = info end
174
266
  end
@@ -214,7 +306,8 @@ function M.collect(r, T, bm)
214
306
  local best = binding_at(bindings, ref.recv or ref.name, ref.tok)
215
307
  if best and (ref.recv or best.imported) then
216
308
  local value = best.hint:match('^lua%-value:')
217
- ref.lua_binding = value and (not ref.recv and best.hint or '?') or (ref.recv and best.hint or '?')
309
+ local hint = ref.recv and conditional_hint(bindings, ref.recv, best, ref.tok) or best.hint
310
+ ref.lua_binding = value and (not ref.recv and hint or '?') or (ref.recv and hint or '?')
218
311
  end
219
312
  if ref.recv == 'self' and not best then ref.lua_binding = '?' end
220
313
  if ref.recv and (T:text(ref.tok - 3) == '.' or T:text(ref.tok - 3) == ':') then ref.lua_binding = '?' end
@@ -251,20 +344,31 @@ function M.collect(r, T, bm)
251
344
  end
252
345
  end
253
346
  end
347
+ -- Only a local declaration or parameter hides a project-wide table name.
348
+ -- `Logger = {}` is a global assignment, so its members stay global aliases.
349
+ local function local_owner(name, token)
350
+ local list = bindings[name]
351
+ if not list then return false end
352
+ for _, b in ipairs(list) do
353
+ if b.token <= token and b.a <= token and b.b >= token
354
+ and declared_local(T, params, b.token) then return true end
355
+ end
356
+ return false
357
+ end
254
358
  -- A global alias is a statement of its own: the right-hand side is a bare
255
359
  -- identifier and nothing but a separator follows it on that line. The key
256
360
  -- is dotted (`g_exports.name`) or a literal (`g_exports['name']`).
257
361
  local globals, definitions = {}, {}
258
362
  for index, n in ipairs(r.nodes) do
259
363
  local owner, key = n.qualified:match('^([%a_][%w_]*)[.:]([%a_][%w_]*)$')
260
- if owner and not bindings[owner] and not n.parent and (n.kind == 'function' or n.kind == 'method') then
364
+ if owner and not local_owner(owner, n.ti) and not n.parent and (n.kind == 'function' or n.kind == 'method') then
261
365
  definitions[n.ti] = { owner = owner, key = key, index = index }
262
366
  end
263
367
  end
264
368
  for i = 1, T.n - 4 do
265
369
  if i % 4096 == 0 then control.check() end
266
370
  local owner = T:text(i)
267
- if T.k[i] == 'id' and not bindings[owner] and T:text(i - 1) ~= '.' and T:text(i - 1) ~= ':' then
371
+ if T.k[i] == 'id' and not local_owner(owner, i) and T:text(i - 1) ~= '.' and T:text(i - 1) ~= ':' then
268
372
  local key, eq
269
373
  if T:text(i + 1) == '.' and T.k[i + 2] == 'id' then key, eq = T:text(i + 2), i + 3
270
374
  elseif T:text(i + 1) == '[' and T.m[i + 1] == i + 3 and T.k[i + 2] == 'str' then key, eq = literal(T, i + 2), i + 4 end
@@ -448,7 +552,6 @@ function M.context(G)
448
552
  return ctx
449
553
  end
450
554
  -- Project-selected aliases share the normal name index and overlay path.
451
- local EMPTY = {}
452
555
  function M.aliases(node, owners)
453
556
  if not node.lua_aliases then return EMPTY end
454
557
  local names, seen = {}, {}
@@ -468,6 +571,29 @@ function M.exported(node, name, owners)
468
571
  end
469
572
  return false
470
573
  end
574
+ -- A member alias is only nameable through its owner (`Logger.es`), so it never
575
+ -- publishes the bare key. Direct definitions already carry the dotted name.
576
+ function M.member_aliases(node)
577
+ if not node.lua_aliases then return EMPTY end
578
+ local names, seen = {}, {}
579
+ for owner, keys in pairs(node.lua_aliases) do
580
+ for _, name in ipairs(keys) do
581
+ local qualified = owner .. '.' .. name
582
+ if name ~= node.name and not seen[qualified] then
583
+ names[#names + 1], seen[qualified] = qualified, true
584
+ end
585
+ end
586
+ end
587
+ table.sort(names)
588
+ return names
589
+ end
590
+ function M.member_alias(node, qualified)
591
+ if not node.lua_aliases or not qualified then return false end
592
+ local owner, name = qualified:match('^([%a_][%w_]*)%.([%a_][%w_]*)$')
593
+ if not owner then return false end
594
+ for _, alias in ipairs(node.lua_aliases[owner] or EMPTY) do if alias == name then return true end end
595
+ return false
596
+ end
471
597
  function M.modules(G, imp, ctx)
472
598
  ctx = ctx or M.context(G)
473
599
  local key = imp.kind .. '\0' .. imp.path
@@ -517,9 +643,50 @@ function M.native_modules(G, name, ctx)
517
643
  return require('codeoutline.analysis.foreign').modules(G, 'lua', name, ctx)
518
644
  end
519
645
 
646
+ function M.has_local_receiver(T, name, offset)
647
+ for _, binding in ipairs((T.lua_bindings or EMPTY)[name] or EMPTY) do
648
+ if binding.local_declaration and T.s[binding.token] - 1 <= offset
649
+ and (binding.a == 0 or offset < T.e[binding.b]) then return true end
650
+ end
651
+ return false
652
+ end
653
+
520
654
  -- Shared query-time receiver resolution for completion and navigation. Reuse
521
655
  -- the parser's lexical bindings and loader contracts; never execute Lua.
656
+ local function module_target(G, imp, ctx)
657
+ return { modules = M.modules(G, imp, ctx), module_name = imp.path }
658
+ end
659
+
660
+ local function imported_target(G, rec, hint, ctx)
661
+ local index = tonumber(hint:match('^lua:(%d+)$'))
662
+ if index then
663
+ local imp = (rec.lua_imports or EMPTY)[index]
664
+ return imp and module_target(G, imp, ctx)
665
+ end
666
+ local branches = hint:match('^lua%-conditional:([%d,]+)$')
667
+ if not branches then return nil end
668
+ local alternatives, seen = {}, {}
669
+ for i in branches:gmatch('%d+') do
670
+ local imp = rec.lua_imports[tonumber(i)]
671
+ local key = imp.kind .. '\0' .. imp.path
672
+ if not seen[key] then
673
+ seen[key] = true
674
+ alternatives[#alternatives + 1] = module_target(G, imp, ctx)
675
+ end
676
+ end
677
+ return { alternatives = alternatives }
678
+ end
679
+
522
680
  function M.member(G, target, name)
681
+ if target and target.alternatives then
682
+ local result, seen = {}, {}
683
+ for _, alternative in ipairs(target.alternatives) do
684
+ for _, entry in ipairs(M.member(G, alternative, name)) do
685
+ if not seen[entry.node] then seen[entry.node] = true; result[#result + 1] = entry end
686
+ end
687
+ end
688
+ return result
689
+ end
523
690
  local result = {}
524
691
  for _, f in ipairs(target and target.modules or {}) do
525
692
  local rec = G.files[f]
@@ -550,12 +717,15 @@ function M.receiver(G, rec, T, name, offset, ctx, depth)
550
717
  local files = M.native_modules(G, name, ctx)
551
718
  return #files > 0 and { modules = files, module_name = name } or nil, (T.lua_bindings or {})[name] ~= nil
552
719
  end
553
- if best.self_info then return { lua_self = best.self_info }, true end
554
- local imported = tonumber(best.hint:match('^lua:(%d+)$'))
555
- if imported then
556
- local imp = rec.lua_imports[imported]
557
- return { modules = M.modules(G, imp, ctx), module_name = imp.path }, true
720
+ if best.self_info then
721
+ local imp = rec.lua_imports[best.self_info.base_import]
722
+ local target = imp and module_target(G, imp, ctx) or {}
723
+ target.lua_self = best.self_info
724
+ return target, true
558
725
  end
726
+ local hint = conditional_hint(T.lua_bindings, name, best, nil, T, offset) or best.hint
727
+ local imported = imported_target(G, rec, hint, ctx)
728
+ if imported then return imported, true end
559
729
  local i = best.token + 2
560
730
  if best.imported or T:text(best.token + 1) ~= '=' or T.k[i] ~= 'id' then return nil, true end
561
731
  local target = M.receiver(G, rec, T, T:text(i), T.s[best.token] - 1, ctx, depth + 1)
@@ -593,8 +763,18 @@ function M.resolve(G, f, name, hint, ctx, recv)
593
763
  if self_index then
594
764
  local rec = G.files[f]
595
765
  local selves = rec.lua_self or ctx and ctx.ordinary and ctx.ordinary.data(f).lua_self or {}
596
- local n = selves[self_index] and selves[self_index].members[name]
597
- return n and { G.id_of[f][n] } or {}
766
+ local info = selves[self_index]
767
+ if not info then return {} end
768
+ local n = info.members[name]
769
+ if n then return { G.id_of[f][n] } end
770
+ local imp = (rec.lua_imports or EMPTY)[info.base_import]
771
+ local result = {}
772
+ if imp and not info.fields[name] then
773
+ for _, entry in ipairs(M.member(G, module_target(G, imp, ctx), name)) do
774
+ result[#result + 1] = G.id_of[entry.file][entry.index]
775
+ end
776
+ end
777
+ return result
598
778
  end
599
779
  local local_index = tonumber(hint:match('^lua%-local:(%d+)$'))
600
780
  if local_index then
@@ -604,16 +784,17 @@ function M.resolve(G, f, name, hint, ctx, recv)
604
784
  return n and { G.id_of[f][n] } or {}
605
785
  end
606
786
  local value = tonumber(hint:match('^lua%-value:(%d+)$'))
607
- local index = value or tonumber(hint:match('^lua:(%d+)$'))
608
- local imp = index and (G.files[f].lua_imports or {})[index]
609
- if not imp then return nil end
610
- if value then name = imp.member end
787
+ local rec = G.files[f]
788
+ if value then
789
+ local imp = (rec.lua_imports or EMPTY)[value]
790
+ if not imp then return nil end
791
+ name, hint = imp.member, 'lua:' .. value
792
+ end
793
+ local target = imported_target(G, rec, hint, ctx)
794
+ if not target then return nil end
611
795
  local result = {}
612
- for _, target in ipairs(M.modules(G, imp, ctx)) do
613
- local rec = G.files[target]
614
- local exports = require('codeoutline.analysis.foreign').exports(rec, 'lua', imp.path) or rec.lua_exports
615
- local n = (exports or {})[name]
616
- if n then result[#result + 1] = G.id_of[target][n] end
796
+ for _, entry in ipairs(M.member(G, target, name)) do
797
+ result[#result + 1] = G.id_of[entry.file][entry.index]
617
798
  end
618
799
  return result
619
800
  end