runtime_analysis 1.0.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 (267) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +115 -0
  3. data/LICENSE.txt +96 -0
  4. data/README.md +164 -0
  5. data/editor/nvim/README.md +278 -0
  6. data/editor/nvim/lua/runtime_analysis/connected.lua +565 -0
  7. data/editor/nvim/lua/runtime_analysis/go_to_class.lua +63 -0
  8. data/editor/nvim/lua/runtime_analysis/inline.lua +138 -0
  9. data/editor/nvim/lua/runtime_analysis/lens.lua +50 -0
  10. data/editor/nvim/lua/runtime_analysis/lsp.lua +92 -0
  11. data/editor/nvim/lua/runtime_analysis/menu.lua +98 -0
  12. data/editor/nvim/lua/runtime_analysis/preview.lua +70 -0
  13. data/editor/nvim/lua/runtime_analysis/project.lua +28 -0
  14. data/editor/nvim/lua/runtime_analysis/rerun.lua +39 -0
  15. data/editor/nvim/lua/runtime_analysis/text.lua +23 -0
  16. data/editor/nvim/lua/runtime_analysis/value_popup.lua +45 -0
  17. data/editor/nvim/plugin/runtime_analysis.lua +71 -0
  18. data/examples/trackers/PROMPT.md +86 -0
  19. data/examples/trackers/honeybadger.rb +19 -0
  20. data/examples/trackers/sentry.rb +16 -0
  21. data/ext/runtime_analysis/extconf.rb +68 -0
  22. data/ext/runtime_analysis/fast.c +775 -0
  23. data/ext/runtime_analysis/fast.h +59 -0
  24. data/ext/runtime_analysis/hold.c +560 -0
  25. data/ext/runtime_analysis/init.c +36 -0
  26. data/ext/runtime_analysis/names.c +78 -0
  27. data/ext/runtime_analysis/names.h +28 -0
  28. data/ext/runtime_analysis/novelty.c +139 -0
  29. data/ext/runtime_analysis/novelty.h +22 -0
  30. data/ext/runtime_analysis/os.c +73 -0
  31. data/ext/runtime_analysis/os.h +32 -0
  32. data/ext/runtime_analysis/ring.c +445 -0
  33. data/ext/runtime_analysis/rows.c +304 -0
  34. data/ext/runtime_analysis/sink.c +485 -0
  35. data/ext/runtime_analysis/sink.h +240 -0
  36. data/ext/runtime_analysis/state.c +254 -0
  37. data/ext/runtime_analysis/state.h +57 -0
  38. data/ext/runtime_analysis/wrapper.c +278 -0
  39. data/ext/runtime_analysis/wrapper.h +24 -0
  40. data/lib/runtime_analysis/errors.rb +34 -0
  41. data/lib/runtime_analysis/failsafe.rb +123 -0
  42. data/lib/runtime_analysis/host/controller_entry_point.rb +18 -0
  43. data/lib/runtime_analysis/host/declared.rb +152 -0
  44. data/lib/runtime_analysis/host/inject.rb +90 -0
  45. data/lib/runtime_analysis/host/job_runners.rb +27 -0
  46. data/lib/runtime_analysis/host/middleware.rb +23 -0
  47. data/lib/runtime_analysis/host/minitest_integration.rb +22 -0
  48. data/lib/runtime_analysis/host/railtie.rb +42 -0
  49. data/lib/runtime_analysis/host/reports.rb +54 -0
  50. data/lib/runtime_analysis/host/rspec_integration.rb +17 -0
  51. data/lib/runtime_analysis/host/web.rb +22 -0
  52. data/lib/runtime_analysis/host.rb +34 -0
  53. data/lib/runtime_analysis/inject.rb +5 -0
  54. data/lib/runtime_analysis/minitest.rb +9 -0
  55. data/lib/runtime_analysis/native/build.rb +17 -0
  56. data/lib/runtime_analysis/native/byte_size.rb +27 -0
  57. data/lib/runtime_analysis/native/call_site_logger/definitions.rb +51 -0
  58. data/lib/runtime_analysis/native/call_site_logger/held.rb +35 -0
  59. data/lib/runtime_analysis/native/call_site_logger/rows.rb +110 -0
  60. data/lib/runtime_analysis/native/call_site_logger/segments.rb +100 -0
  61. data/lib/runtime_analysis/native/call_site_logger/streams.rb +110 -0
  62. data/lib/runtime_analysis/native/call_site_logger.rb +80 -0
  63. data/lib/runtime_analysis/native/rotation_policy.rb +28 -0
  64. data/lib/runtime_analysis/native/sanitizer_runtime.rb +50 -0
  65. data/lib/runtime_analysis/native/sink.rb +12 -0
  66. data/lib/runtime_analysis/native.rb +74 -0
  67. data/lib/runtime_analysis/platform.rb +30 -0
  68. data/lib/runtime_analysis/query/index/anonymise.rb +98 -0
  69. data/lib/runtime_analysis/query/index/graph.rb +107 -0
  70. data/lib/runtime_analysis/query/index/graph_cache.rb +32 -0
  71. data/lib/runtime_analysis/query/index/reading/declared.rb +42 -0
  72. data/lib/runtime_analysis/query/index/reading/dict.rb +43 -0
  73. data/lib/runtime_analysis/query/index/reading/frame.rb +27 -0
  74. data/lib/runtime_analysis/query/index/reading/node.rb +45 -0
  75. data/lib/runtime_analysis/query/index/reading/path.rb +21 -0
  76. data/lib/runtime_analysis/query/index/reading/reruns.rb +30 -0
  77. data/lib/runtime_analysis/query/index/reading/rows.rb +128 -0
  78. data/lib/runtime_analysis/query/index/reading/stream.rb +82 -0
  79. data/lib/runtime_analysis/query/index/reading/tree.rb +66 -0
  80. data/lib/runtime_analysis/query/index/reading/tree_builder.rb +135 -0
  81. data/lib/runtime_analysis/query/index/reading/values.rb +49 -0
  82. data/lib/runtime_analysis/query/index/reading.rb +80 -0
  83. data/lib/runtime_analysis/query/index/shapes.rb +142 -0
  84. data/lib/runtime_analysis/query/index/signatures/active_support.rb +31 -0
  85. data/lib/runtime_analysis/query/index/signatures/core.json +1 -0
  86. data/lib/runtime_analysis/query/index/signatures/layers.rb +29 -0
  87. data/lib/runtime_analysis/query/index/signatures/lineage.rb +57 -0
  88. data/lib/runtime_analysis/query/index/signatures/overloads.rb +32 -0
  89. data/lib/runtime_analysis/query/index/signatures/rbs/entry.rb +95 -0
  90. data/lib/runtime_analysis/query/index/signatures/rbs/export.rb +76 -0
  91. data/lib/runtime_analysis/query/index/signatures/rbs/library.rb +43 -0
  92. data/lib/runtime_analysis/query/index/signatures/rbs/overload.rb +66 -0
  93. data/lib/runtime_analysis/query/index/signatures/rbs/project.rb +69 -0
  94. data/lib/runtime_analysis/query/index/signatures/rbs/types.rb +79 -0
  95. data/lib/runtime_analysis/query/index/signatures/rbs.rb +57 -0
  96. data/lib/runtime_analysis/query/index/signatures/sorbet/sig.rb +48 -0
  97. data/lib/runtime_analysis/query/index/signatures/sorbet/signed.rb +69 -0
  98. data/lib/runtime_analysis/query/index/signatures/sorbet/table.rb +43 -0
  99. data/lib/runtime_analysis/query/index/signatures/sorbet/types.rb +96 -0
  100. data/lib/runtime_analysis/query/index/signatures/sorbet.rb +150 -0
  101. data/lib/runtime_analysis/query/index/signatures/sources.rb +80 -0
  102. data/lib/runtime_analysis/query/index/signatures/written.rb +45 -0
  103. data/lib/runtime_analysis/query/index/signatures.rb +93 -0
  104. data/lib/runtime_analysis/query/index/source_named_graph.rb +84 -0
  105. data/lib/runtime_analysis/query/index/static/change_set.rb +20 -0
  106. data/lib/runtime_analysis/query/index/static/changes.rb +80 -0
  107. data/lib/runtime_analysis/query/index/static/code_visitor.rb +144 -0
  108. data/lib/runtime_analysis/query/index/static/klass.rb +13 -0
  109. data/lib/runtime_analysis/query/index/static/method.rb +19 -0
  110. data/lib/runtime_analysis/query/index/static/params.rb +52 -0
  111. data/lib/runtime_analysis/query/index/static/source.rb +69 -0
  112. data/lib/runtime_analysis/query/index/static/stub.rb +14 -0
  113. data/lib/runtime_analysis/query/index/static/stub_visitor.rb +133 -0
  114. data/lib/runtime_analysis/query/index/static/value.rb +17 -0
  115. data/lib/runtime_analysis/query/index/static/values.rb +71 -0
  116. data/lib/runtime_analysis/query/index/static/visibility.rb +40 -0
  117. data/lib/runtime_analysis/query/index/static.rb +85 -0
  118. data/lib/runtime_analysis/query/index.rb +32 -0
  119. data/lib/runtime_analysis/query/nodes.rb +60 -0
  120. data/lib/runtime_analysis/query/project/config.rb +108 -0
  121. data/lib/runtime_analysis/query/project.rb +40 -0
  122. data/lib/runtime_analysis/query/report/document.rb +84 -0
  123. data/lib/runtime_analysis/query/report/finding.rb +15 -0
  124. data/lib/runtime_analysis/query/report/json_renderer.rb +51 -0
  125. data/lib/runtime_analysis/query/report/list.rb +15 -0
  126. data/lib/runtime_analysis/query/report/location.rb +14 -0
  127. data/lib/runtime_analysis/query/report/one_row.rb +14 -0
  128. data/lib/runtime_analysis/query/report/outline.rb +15 -0
  129. data/lib/runtime_analysis/query/report/paint.rb +34 -0
  130. data/lib/runtime_analysis/query/report/related.rb +11 -0
  131. data/lib/runtime_analysis/query/report/table.rb +15 -0
  132. data/lib/runtime_analysis/query/report/text.rb +13 -0
  133. data/lib/runtime_analysis/query/report/text_renderer.rb +102 -0
  134. data/lib/runtime_analysis/query/report.rb +35 -0
  135. data/lib/runtime_analysis/query/surfaces/class_value.rb +17 -0
  136. data/lib/runtime_analysis/query/surfaces/cli/answer.rb +32 -0
  137. data/lib/runtime_analysis/query/surfaces/cli/command_line.rb +77 -0
  138. data/lib/runtime_analysis/query/surfaces/cli/usage.rb +32 -0
  139. data/lib/runtime_analysis/query/surfaces/cli.rb +101 -0
  140. data/lib/runtime_analysis/query/surfaces/crystallise/lexical.rb +44 -0
  141. data/lib/runtime_analysis/query/surfaces/crystallise/notation.rb +33 -0
  142. data/lib/runtime_analysis/query/surfaces/crystallise/observed.rb +102 -0
  143. data/lib/runtime_analysis/query/surfaces/crystallise/outline.rb +111 -0
  144. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_file.rb +100 -0
  145. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_member.rb +50 -0
  146. data/lib/runtime_analysis/query/surfaces/crystallise/rbs_notation.rb +44 -0
  147. data/lib/runtime_analysis/query/surfaces/crystallise/seen.rb +74 -0
  148. data/lib/runtime_analysis/query/surfaces/crystallise/sig_text.rb +49 -0
  149. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_edit.rb +106 -0
  150. data/lib/runtime_analysis/query/surfaces/crystallise/sorbet_notation.rb +48 -0
  151. data/lib/runtime_analysis/query/surfaces/crystallise/subject.rb +55 -0
  152. data/lib/runtime_analysis/query/surfaces/crystallise.rb +94 -0
  153. data/lib/runtime_analysis/query/surfaces/mcp.rb +89 -0
  154. data/lib/runtime_analysis/query/surfaces/record/commands.rb +45 -0
  155. data/lib/runtime_analysis/query/surfaces/record/environment.rb +48 -0
  156. data/lib/runtime_analysis/query/surfaces/record/ignore.rb +62 -0
  157. data/lib/runtime_analysis/query/surfaces/record/question.rb +55 -0
  158. data/lib/runtime_analysis/query/surfaces/record/run.rb +30 -0
  159. data/lib/runtime_analysis/query/surfaces/record.rb +122 -0
  160. data/lib/runtime_analysis/query/surfaces/rerun.rb +87 -0
  161. data/lib/runtime_analysis/query/surfaces/setup/agents.rb +76 -0
  162. data/lib/runtime_analysis/query/surfaces/setup/changes.rb +107 -0
  163. data/lib/runtime_analysis/query/surfaces/setup/project.rb +80 -0
  164. data/lib/runtime_analysis/query/surfaces/setup/steps.md +205 -0
  165. data/lib/runtime_analysis/query/surfaces/setup.rb +98 -0
  166. data/lib/runtime_analysis/query/surfaces.rb +14 -0
  167. data/lib/runtime_analysis/query/tools/base.rb +86 -0
  168. data/lib/runtime_analysis/query/tools/method_lookup.rb +37 -0
  169. data/lib/runtime_analysis/query/tools/namespaces.rb +36 -0
  170. data/lib/runtime_analysis/query/tools/origins.rb +47 -0
  171. data/lib/runtime_analysis/query/tools/stubs.rb +53 -0
  172. data/lib/runtime_analysis/query/tools/unknown_method.rb +11 -0
  173. data/lib/runtime_analysis/query/tools/verify_mock.rb +145 -0
  174. data/lib/runtime_analysis/query/tools/what_calls.rb +67 -0
  175. data/lib/runtime_analysis/query/tools/what_it_receives.rb +142 -0
  176. data/lib/runtime_analysis/query/tools/where_nil_comes_from.rb +105 -0
  177. data/lib/runtime_analysis/query/tools/who_actually_answered.rb +82 -0
  178. data/lib/runtime_analysis/query/tools.rb +34 -0
  179. data/lib/runtime_analysis/query.rb +33 -0
  180. data/lib/runtime_analysis/recording/blob.rb +13 -0
  181. data/lib/runtime_analysis/recording/format.rb +32 -0
  182. data/lib/runtime_analysis/recording/opcodes.rb +30 -0
  183. data/lib/runtime_analysis/recording/raise_ids.rb +15 -0
  184. data/lib/runtime_analysis/recording/ruby_sink.rb +176 -0
  185. data/lib/runtime_analysis/recording/run_manifest.rb +110 -0
  186. data/lib/runtime_analysis/recording/seen/nothing.rb +15 -0
  187. data/lib/runtime_analysis/recording/seen/tally.rb +48 -0
  188. data/lib/runtime_analysis/recording/seen.rb +63 -0
  189. data/lib/runtime_analysis/recording/sink.rb +19 -0
  190. data/lib/runtime_analysis/recording/source_blob.rb +20 -0
  191. data/lib/runtime_analysis/recording.rb +52 -0
  192. data/lib/runtime_analysis/rspec.rb +8 -0
  193. data/lib/runtime_analysis/ruby_parsing/packages.rb +17 -0
  194. data/lib/runtime_analysis/ruby_parsing.rb +98 -0
  195. data/lib/runtime_analysis/trace/configuration/capturing.rb +32 -0
  196. data/lib/runtime_analysis/trace/configuration/per_run.rb +31 -0
  197. data/lib/runtime_analysis/trace/configuration/tier.rb +27 -0
  198. data/lib/runtime_analysis/trace/configuration.rb +115 -0
  199. data/lib/runtime_analysis/trace/entries.rb +47 -0
  200. data/lib/runtime_analysis/trace/hold/raises.rb +28 -0
  201. data/lib/runtime_analysis/trace/hold/requests.rb +47 -0
  202. data/lib/runtime_analysis/trace/hold/telling.rb +45 -0
  203. data/lib/runtime_analysis/trace/hold/unwound.rb +30 -0
  204. data/lib/runtime_analysis/trace/hold.rb +142 -0
  205. data/lib/runtime_analysis/trace/inclusion.rb +46 -0
  206. data/lib/runtime_analysis/trace/installer/alias_repair.rb +68 -0
  207. data/lib/runtime_analysis/trace/installer/answers.rb +24 -0
  208. data/lib/runtime_analysis/trace/installer/body_sync.rb +25 -0
  209. data/lib/runtime_analysis/trace/installer/built_classes.rb +43 -0
  210. data/lib/runtime_analysis/trace/installer/called_names.rb +33 -0
  211. data/lib/runtime_analysis/trace/installer/class_builders.rb +50 -0
  212. data/lib/runtime_analysis/trace/installer/deferral.rb +42 -0
  213. data/lib/runtime_analysis/trace/installer/hook.rb +170 -0
  214. data/lib/runtime_analysis/trace/installer/included_gems.rb +53 -0
  215. data/lib/runtime_analysis/trace/installer/probe.rb +28 -0
  216. data/lib/runtime_analysis/trace/installer/reflection.rb +50 -0
  217. data/lib/runtime_analysis/trace/installer/registry.rb +78 -0
  218. data/lib/runtime_analysis/trace/installer/sorbet.rb +29 -0
  219. data/lib/runtime_analysis/trace/installer/wrapping/ivar_writes.rb +62 -0
  220. data/lib/runtime_analysis/trace/installer/wrapping.rb +66 -0
  221. data/lib/runtime_analysis/trace/installer.rb +149 -0
  222. data/lib/runtime_analysis/trace/mechanism/block_entries.rb +29 -0
  223. data/lib/runtime_analysis/trace/mechanism/frame_readers.rb +61 -0
  224. data/lib/runtime_analysis/trace/mechanism/native_asks.rb +36 -0
  225. data/lib/runtime_analysis/trace/mechanism/native_sync.rb +28 -0
  226. data/lib/runtime_analysis/trace/mechanism/pure_wrapper.rb +41 -0
  227. data/lib/runtime_analysis/trace/mechanism/ruby_wrapper.rb +141 -0
  228. data/lib/runtime_analysis/trace/mechanism/wrapper.rb +79 -0
  229. data/lib/runtime_analysis/trace/mechanism/wrapper_shape.rb +40 -0
  230. data/lib/runtime_analysis/trace/mechanism.rb +28 -0
  231. data/lib/runtime_analysis/trace/nil_origin/answers.rb +24 -0
  232. data/lib/runtime_analysis/trace/nil_origin/beyond.rb +182 -0
  233. data/lib/runtime_analysis/trace/nil_origin/index.rb +164 -0
  234. data/lib/runtime_analysis/trace/nil_origin/lifetime.rb +29 -0
  235. data/lib/runtime_analysis/trace/nil_origin/named.rb +48 -0
  236. data/lib/runtime_analysis/trace/nil_origin/objects.rb +78 -0
  237. data/lib/runtime_analysis/trace/nil_origin/provenance.rb +136 -0
  238. data/lib/runtime_analysis/trace/nil_origin/receiver.rb +121 -0
  239. data/lib/runtime_analysis/trace/nil_origin/rows.rb +43 -0
  240. data/lib/runtime_analysis/trace/nil_origin/source.rb +315 -0
  241. data/lib/runtime_analysis/trace/nil_origin/stored.rb +159 -0
  242. data/lib/runtime_analysis/trace/nil_origin/told.rb +152 -0
  243. data/lib/runtime_analysis/trace/nil_origin/values.rb +47 -0
  244. data/lib/runtime_analysis/trace/nil_origin/weighed.rb +70 -0
  245. data/lib/runtime_analysis/trace/nil_origin/writers.rb +79 -0
  246. data/lib/runtime_analysis/trace/nil_origin.rb +278 -0
  247. data/lib/runtime_analysis/trace/novelty.rb +17 -0
  248. data/lib/runtime_analysis/trace/pure/novelty.rb +43 -0
  249. data/lib/runtime_analysis/trace/pure/state.rb +94 -0
  250. data/lib/runtime_analysis/trace/pure.rb +15 -0
  251. data/lib/runtime_analysis/trace/recorder/class_name.rb +63 -0
  252. data/lib/runtime_analysis/trace/recorder/fingerprint.rb +39 -0
  253. data/lib/runtime_analysis/trace/recorder/frame_key.rb +27 -0
  254. data/lib/runtime_analysis/trace/recorder/paths.rb +89 -0
  255. data/lib/runtime_analysis/trace/recorder/raise_site.rb +36 -0
  256. data/lib/runtime_analysis/trace/recorder/reachability.rb +46 -0
  257. data/lib/runtime_analysis/trace/recorder/row_emission.rb +137 -0
  258. data/lib/runtime_analysis/trace/recorder/safe_steps.rb +84 -0
  259. data/lib/runtime_analysis/trace/recorder/tree_steps.rb +42 -0
  260. data/lib/runtime_analysis/trace/recorder/unwinding.rb +23 -0
  261. data/lib/runtime_analysis/trace/recorder.rb +149 -0
  262. data/lib/runtime_analysis/trace/state.rb +16 -0
  263. data/lib/runtime_analysis/trace.rb +142 -0
  264. data/lib/runtime_analysis/version.rb +6 -0
  265. data/lib/runtime_analysis/web.rb +6 -0
  266. data/lib/runtime_analysis.rb +125 -0
  267. metadata +330 -0
@@ -0,0 +1,565 @@
1
+ -- A sidebar listing what is connected to the code under the cursor by recorded calls: the methods
2
+ -- that reach it and the methods it reaches, nearest first, each heading with how many files that is. With the cursor inside a method it shows
3
+ -- that method alone; outside any method it shows the whole file. It follows the buffer and the
4
+ -- cursor, asking `ra whats_connected` and redrawing.
5
+
6
+ local project = require("runtime_analysis.project")
7
+ local preview = require("runtime_analysis.preview")
8
+
9
+ local M = {}
10
+
11
+ local NAMESPACE = vim.api.nvim_create_namespace("runtime_analysis_connected")
12
+ local GROUP = "runtime_analysis_connected"
13
+
14
+ -- Change any of these with require("runtime_analysis.connected").setup({ ... }).
15
+ local config = {
16
+ width = 64, -- columns
17
+ depth = "all", -- how many files away to follow: a number, or "all"
18
+ debounce = 150, -- milliseconds the cursor must rest before asking
19
+ theme = "vivid", -- "vivid": the sidebar's own palette. "scheme": follow the colour scheme's groups.
20
+ heat = { warm = 100, hot = 1000 }, -- call counts at which the number at the right changes colour
21
+ highlights = {}, -- group name -> highlight definition, applied over the theme
22
+ }
23
+
24
+ -- Every colour is a highlight group. A theme gives each group a definition, and anything in
25
+ -- setup({ highlights = ... }) is applied over it.
26
+ --
27
+ -- "vivid" is the palette of the code samples on the website (VS Code's Dark+ and Light+), chosen by
28
+ -- 'background'. It spends colour where it carries meaning: namespace against class, directory against
29
+ -- file, callers against callees, and a call count that warms as it grows.
30
+ local VIVID = {
31
+ dark = {
32
+ RuntimeAnalysisCallers = { fg = "#569CD6", bold = true },
33
+ RuntimeAnalysisCallees = { fg = "#C586C0", bold = true },
34
+ RuntimeAnalysisTally = { fg = "#6A9955" },
35
+ RuntimeAnalysisNamespace = { fg = "#2E8B7A" },
36
+ RuntimeAnalysisClass = { fg = "#4EC9B0" },
37
+ RuntimeAnalysisMethod = { fg = "#DCDCAA" },
38
+ RuntimeAnalysisBangMethod = { fg = "#FF7B72" },
39
+ RuntimeAnalysisPredicateMethod = { fg = "#9CDCFE" },
40
+ RuntimeAnalysisClassMethod = { fg = "#D2A8FF" },
41
+ RuntimeAnalysisClassBangMethod = { fg = "#FF7B72", bold = true },
42
+ RuntimeAnalysisClassPredicateMethod = { fg = "#9CDCFE", bold = true },
43
+ RuntimeAnalysisDirectory = { fg = "#6E7681" },
44
+ RuntimeAnalysisFile = { fg = "#CE9178" },
45
+ RuntimeAnalysisLine = { fg = "#B5CEA8" },
46
+ RuntimeAnalysisCalls = { fg = "#6E7681" },
47
+ RuntimeAnalysisCallsWarm = { fg = "#DCDCAA" },
48
+ RuntimeAnalysisCallsHot = { fg = "#F0883E", bold = true },
49
+ RuntimeAnalysisMessage = { fg = "#6A9955", italic = true },
50
+ },
51
+ light = {
52
+ RuntimeAnalysisCallers = { fg = "#0000FF", bold = true },
53
+ RuntimeAnalysisCallees = { fg = "#AF00DB", bold = true },
54
+ RuntimeAnalysisTally = { fg = "#008000" },
55
+ RuntimeAnalysisNamespace = { fg = "#5B9FB0" },
56
+ RuntimeAnalysisClass = { fg = "#267F99" },
57
+ RuntimeAnalysisMethod = { fg = "#795E26" },
58
+ RuntimeAnalysisBangMethod = { fg = "#CD3131" },
59
+ RuntimeAnalysisPredicateMethod = { fg = "#0070C1" },
60
+ RuntimeAnalysisClassMethod = { fg = "#AF00DB" },
61
+ RuntimeAnalysisClassBangMethod = { fg = "#CD3131", bold = true },
62
+ RuntimeAnalysisClassPredicateMethod = { fg = "#0070C1", bold = true },
63
+ RuntimeAnalysisDirectory = { fg = "#8E8E8E" },
64
+ RuntimeAnalysisFile = { fg = "#A31515" },
65
+ RuntimeAnalysisLine = { fg = "#098658" },
66
+ RuntimeAnalysisCalls = { fg = "#8E8E8E" },
67
+ RuntimeAnalysisCallsWarm = { fg = "#795E26" },
68
+ RuntimeAnalysisCallsHot = { fg = "#CD3131", bold = true },
69
+ RuntimeAnalysisMessage = { fg = "#008000", italic = true },
70
+ },
71
+ }
72
+
73
+ -- "scheme" links each group to a standard one, so the sidebar looks like the rest of the editor. A
74
+ -- table borrows another group's colour and adds to it.
75
+ local SCHEME = {
76
+ RuntimeAnalysisCallers = "Title",
77
+ RuntimeAnalysisCallees = "Title",
78
+ RuntimeAnalysisTally = "Comment",
79
+ RuntimeAnalysisNamespace = "Type",
80
+ RuntimeAnalysisClass = "Type",
81
+ RuntimeAnalysisMethod = "Function",
82
+ RuntimeAnalysisBangMethod = "DiagnosticWarn",
83
+ RuntimeAnalysisPredicateMethod = "Boolean",
84
+ RuntimeAnalysisClassMethod = "Special",
85
+ RuntimeAnalysisClassBangMethod = { like = "DiagnosticWarn", bold = true },
86
+ RuntimeAnalysisClassPredicateMethod = { like = "Boolean", bold = true },
87
+ RuntimeAnalysisDirectory = "Comment",
88
+ RuntimeAnalysisFile = "Comment",
89
+ RuntimeAnalysisLine = "LineNr",
90
+ RuntimeAnalysisCalls = "Number",
91
+ RuntimeAnalysisCallsWarm = "Number",
92
+ RuntimeAnalysisCallsHot = { like = "Number", bold = true },
93
+ RuntimeAnalysisMessage = "Comment",
94
+ }
95
+
96
+ -- The group for a method, from how it is called (# or .) and how its name ends.
97
+ local METHOD_GROUPS = {
98
+ ["#"] = { [""] = "RuntimeAnalysisMethod", ["!"] = "RuntimeAnalysisBangMethod", ["?"] = "RuntimeAnalysisPredicateMethod" },
99
+ ["."] = { [""] = "RuntimeAnalysisClassMethod", ["!"] = "RuntimeAnalysisClassBangMethod",
100
+ ["?"] = "RuntimeAnalysisClassPredicateMethod" },
101
+ }
102
+
103
+ local function method_group(separator, name)
104
+ local ending = name:match("[!?]$") or ""
105
+ return (METHOD_GROUPS[separator] or METHOD_GROUPS["#"])[ending]
106
+ end
107
+
108
+ local function borrowed(definition)
109
+ local colours = vim.api.nvim_get_hl(0, { name = definition.like, link = false })
110
+ local extra = vim.tbl_extend("force", {}, definition)
111
+ extra.like = nil
112
+ return vim.tbl_extend("force", { fg = colours.fg, ctermfg = colours.ctermfg }, extra)
113
+ end
114
+
115
+ local function theme_definitions()
116
+ if config.theme ~= "scheme" then
117
+ return VIVID[vim.o.background == "light" and "light" or "dark"]
118
+ end
119
+ local definitions = {}
120
+ for group, target in pairs(SCHEME) do
121
+ definitions[group] = type(target) == "string" and { link = target } or borrowed(target)
122
+ end
123
+ return definitions
124
+ end
125
+
126
+ local function apply_highlights()
127
+ for group, definition in pairs(theme_definitions()) do
128
+ vim.api.nvim_set_hl(0, group, definition)
129
+ end
130
+ for group, definition in pairs(config.highlights) do
131
+ vim.api.nvim_set_hl(0, group, definition)
132
+ end
133
+ end
134
+
135
+ -- `stack` is set while the sidebar shows one call stack to step through instead of the connections.
136
+ local state = { buf = nil, win = nil, target = nil, file = nil, root = nil,
137
+ places = {}, cache = {}, shown = nil, request = 0, timer = nil, help = false, stack = nil }
138
+
139
+ local HELP = {
140
+ connections = {
141
+ "<CR>: open the method",
142
+ "o: step through the call stack to it",
143
+ "1 2 3: follow that many files away",
144
+ "a: follow all the way",
145
+ "q: close",
146
+ },
147
+ stack = {
148
+ "j k: move, showing each frame",
149
+ "<CR>: open the frame and stay there",
150
+ "<Esc>: back to where you were",
151
+ "q: close",
152
+ },
153
+ }
154
+
155
+ -- The first lines, the way NERDTree does it: a pointer to the help, or the help itself.
156
+ local function help_lines(lines, marks)
157
+ local text = { '" Press ? for help' }
158
+ if state.help then
159
+ text = { '" Runtime Analysis', '" ================' }
160
+ for _, key in ipairs(HELP[state.stack and "stack" or "connections"]) do table.insert(text, '" ' .. key) end
161
+ table.insert(text, '" ?: close help')
162
+ end
163
+ for _, line in ipairs(text) do
164
+ table.insert(lines, line)
165
+ table.insert(marks, { line = #lines - 1, col = 0, opts = { end_col = #line, hl_group = "RuntimeAnalysisMessage" } })
166
+ end
167
+ table.insert(lines, "")
168
+ end
169
+
170
+ local function sidebar_open()
171
+ return state.win and vim.api.nvim_win_is_valid(state.win)
172
+ end
173
+
174
+ -- The answer's blocks: an optional "lines 12 to 30" naming the method in scope, then a heading and
175
+ -- a table for each direction.
176
+ local function parse(document)
177
+ local sections, first, last = {}, nil, nil
178
+ for _, block in ipairs(document.blocks or {}) do
179
+ local text = block.type == "text" and block.lines and block.lines[1]
180
+ if text and text:match("^lines %d+ to %d+$") then
181
+ first, last = text:match("^lines (%d+) to (%d+)$")
182
+ elseif text then
183
+ table.insert(sections, { heading = text, rows = {} })
184
+ elseif block.type == "table" and sections[#sections] then
185
+ sections[#sections].rows = block.rows or {}
186
+ end
187
+ end
188
+ return { title = document.title, summary = document.summary or "", sections = sections,
189
+ first = tonumber(first), last = tonumber(last) }
190
+ end
191
+
192
+ local function draw(lines, marks, places)
193
+ if not (state.buf and vim.api.nvim_buf_is_valid(state.buf)) then return end
194
+ vim.bo[state.buf].modifiable = true
195
+ vim.api.nvim_buf_set_lines(state.buf, 0, -1, false, lines)
196
+ vim.bo[state.buf].modifiable = false
197
+ vim.api.nvim_buf_clear_namespace(state.buf, NAMESPACE, 0, -1)
198
+ for _, mark in ipairs(marks) do
199
+ vim.api.nvim_buf_set_extmark(state.buf, NAMESPACE, mark.line, mark.col or 0, mark.opts)
200
+ end
201
+ state.places = places
202
+ end
203
+
204
+ -- The sidebar does not wrap, and it is as narrow as the screen makes it, so a message is broken to
205
+ -- the width it has now.
206
+ local function message(text)
207
+ text = vim.split(text, "\n", { plain = true })[1] or ""
208
+ local width = state.win and vim.api.nvim_win_is_valid(state.win) and vim.api.nvim_win_get_width(state.win)
209
+ local lines = require("runtime_analysis.text").wrap(text, width or config.width)
210
+ local marks = {}
211
+ for index, line in ipairs(lines) do
212
+ table.insert(marks, { line = index - 1, opts = { end_col = #line, hl_group = "RuntimeAnalysisMessage" } })
213
+ end
214
+ state.shown = nil
215
+ draw(#lines > 0 and lines or { "" }, marks, {})
216
+ end
217
+
218
+ local function mark(marks, line, from, to, group)
219
+ table.insert(marks, { line = line, col = from, opts = { end_col = to, hl_group = group } })
220
+ end
221
+
222
+ local function calls_group(calls)
223
+ local count = tonumber(calls) or 0
224
+ if count >= config.heat.hot then return "RuntimeAnalysisCallsHot" end
225
+ return count >= config.heat.warm and "RuntimeAnalysisCallsWarm" or "RuntimeAnalysisCalls"
226
+ end
227
+
228
+ -- One row: indented a step per file crossed, Namespace::Class#method, then directory/file:line, with
229
+ -- the call count as virtual text at the end of the line. The row remembers where the method is
230
+ -- defined, for Enter.
231
+ local function row_line(row, lines, marks, places, section)
232
+ local identity, path, line_number, hops, calls = row[1], row[2], row[3], tonumber(row[4]) or 1, row[5] or ""
233
+ local indent = string.rep(" ", hops + 1)
234
+ local class, separator, name = identity:match("^(.-)([#.])(.+)$")
235
+ class, separator, name = class or "", separator or "#", name or identity
236
+ local namespace = class:match("^(.*::)") or ""
237
+ local file = path
238
+ path = path:match(".*/gems/(.+)$") or path -- a gem's file is shown from the gem's own directory
239
+ local directory = path:match("^(.*/)") or ""
240
+ local text = indent .. identity .. " " .. path .. (line_number and (":" .. line_number) or "")
241
+ table.insert(lines, text)
242
+ local line, at, path_at = #lines - 1, #indent, #indent + #identity + 2
243
+ places[#lines] = { file = file, line = tonumber(line_number), identity = identity, section = section }
244
+ mark(marks, line, at, at + #namespace, "RuntimeAnalysisNamespace")
245
+ mark(marks, line, at + #namespace, at + #class, "RuntimeAnalysisClass")
246
+ mark(marks, line, at + #class, at + #identity, method_group(separator, name))
247
+ mark(marks, line, path_at, path_at + #directory, "RuntimeAnalysisDirectory")
248
+ mark(marks, line, path_at + #directory, path_at + #path, "RuntimeAnalysisFile")
249
+ mark(marks, line, path_at + #path, #text, "RuntimeAnalysisLine")
250
+ table.insert(marks, { line = line, opts = { virt_text = { { tostring(calls), calls_group(calls) } }, virt_text_pos = "right_align" } })
251
+ end
252
+
253
+ local function file_count(rows)
254
+ local seen, count = {}, 0
255
+ for _, row in ipairs(rows) do
256
+ if not seen[row[2]] then seen[row[2]], count = true, count + 1 end
257
+ end
258
+ return count == 1 and "1 file" or (count .. " files")
259
+ end
260
+
261
+ -- "reaches unlock_next (3 files)" above the callers, "unlock_next reaches (5 files)" above the callees.
262
+ local function heading_line(section, subject, lines, marks)
263
+ local words = section.heading == "reaches it" and ("▲ reaches " .. subject) or ("▼ " .. subject .. " reaches")
264
+ local tally = " (" .. file_count(section.rows) .. ")"
265
+ table.insert(lines, words .. tally)
266
+ mark(marks, #lines - 1, 0, #words, section.heading == "reaches it" and "RuntimeAnalysisCallers" or "RuntimeAnalysisCallees")
267
+ mark(marks, #lines - 1, #words, #words + #tally, "RuntimeAnalysisTally")
268
+ end
269
+
270
+ local function show(answer)
271
+ if state.shown == answer then return end
272
+ local subject = answer.first and (answer.title:match("[#.]([^#.]+)$") or answer.title) or "this file"
273
+ local lines, marks, places = {}, {}, {}
274
+ help_lines(lines, marks)
275
+ for _, section in ipairs(answer.sections) do
276
+ heading_line(section, subject, lines, marks)
277
+ for _, row in ipairs(section.rows) do
278
+ row_line(row, lines, marks, places, section.heading)
279
+ end
280
+ table.insert(lines, "")
281
+ end
282
+ draw(lines, marks, places)
283
+ state.shown = answer
284
+ end
285
+
286
+ local function cache_key(file, line)
287
+ return table.concat({ file, line, config.depth }, ":")
288
+ end
289
+
290
+ -- An answer for a method holds for every line of that method; one for the whole file, only for the
291
+ -- line that was asked about.
292
+ local function remember(file, line, answer)
293
+ for covered = answer.first or line, answer.last or line do
294
+ state.cache[cache_key(file, covered)] = answer
295
+ end
296
+ end
297
+
298
+ local function query(root, file, line)
299
+ state.request = state.request + 1
300
+ local request = state.request
301
+ local asked = { "whats_connected", file, "--line", tostring(line), "--json", "--top", "all" }
302
+ local command = vim.list_extend(project.ra(root), asked)
303
+ if config.depth ~= "all" then
304
+ vim.list_extend(command, { "--depth", tostring(config.depth) })
305
+ end
306
+ vim.system(command, { cwd = root, text = true }, vim.schedule_wrap(function(result)
307
+ if request ~= state.request or state.stack then return end -- the cursor moved on while ra was answering
308
+ if result.code ~= 0 then
309
+ return message(vim.trim((result.stderr or ""):gsub("^ra: ", "")))
310
+ end
311
+ local ok, document = pcall(vim.json.decode, result.stdout)
312
+ if not ok then return message("ra returned something that is not JSON") end
313
+ local answer = parse(document)
314
+ remember(file, line, answer)
315
+ show(answer)
316
+ end))
317
+ end
318
+
319
+ -- Show what is connected to the cursor's line in the window being followed.
320
+ function M.refresh()
321
+ if state.stack then return end
322
+ if not (sidebar_open() and state.file and state.target and vim.api.nvim_win_is_valid(state.target)) then return end
323
+ local line = vim.api.nvim_win_get_cursor(state.target)[1]
324
+ local cached = state.cache[cache_key(state.file, line)]
325
+ if cached then
326
+ state.request = state.request + 1 -- drop any answer still on its way
327
+ return show(cached)
328
+ end
329
+ query(state.root, state.file, line)
330
+ end
331
+
332
+ local function refresh_soon()
333
+ if state.timer then state.timer:stop() end
334
+ state.timer = vim.defer_fn(M.refresh, config.debounce)
335
+ end
336
+
337
+ local function followable(bufnr)
338
+ return sidebar_open() and bufnr ~= state.buf and vim.bo[bufnr].buftype == "" and vim.api.nvim_buf_get_name(bufnr) ~= ""
339
+ end
340
+
341
+ -- Called on every buffer change. Ignores the sidebar itself, unnamed buffers and special buffers.
342
+ function M.follow(bufnr, window)
343
+ if state.stack or not followable(bufnr) then return end
344
+ local name = vim.api.nvim_buf_get_name(bufnr)
345
+ state.target = window or vim.api.nvim_get_current_win()
346
+ local root = project.root(bufnr)
347
+ local file = vim.startswith(name, root .. "/") and name:sub(#root + 2) or name
348
+ if file ~= state.file or root ~= state.root then
349
+ state.file, state.root = file, root
350
+ message("asking ra…")
351
+ end
352
+ M.refresh()
353
+ end
354
+
355
+ -- Open the method under the cursor, at its definition, in the window the sidebar is following.
356
+ local function jump()
357
+ local place = state.places[vim.api.nvim_win_get_cursor(0)[1]]
358
+ if not place then return end
359
+ local target = state.target and vim.api.nvim_win_is_valid(state.target) and state.target
360
+ if target then vim.api.nvim_set_current_win(target) else vim.cmd("wincmd l") end
361
+ local path = vim.startswith(place.file, "/") and place.file or (state.root .. "/" .. place.file)
362
+ vim.cmd.edit(vim.fn.fnameescape(path))
363
+ if place.line then
364
+ pcall(vim.api.nvim_win_set_cursor, 0, { place.line, 0 })
365
+ vim.cmd("normal! zz")
366
+ end
367
+ end
368
+
369
+ local function absolute(file)
370
+ return vim.startswith(file, "/") and file or (state.root .. "/" .. file)
371
+ end
372
+
373
+ local function cursor_place()
374
+ return state.places[vim.api.nvim_win_get_cursor(state.win)[1]]
375
+ end
376
+
377
+ -- The stack, one frame a line. The window being followed shows the frame under the cursor.
378
+ local function draw_stack()
379
+ local stack = state.stack
380
+ local lines, marks, places = {}, {}, {}
381
+ help_lines(lines, marks)
382
+ local heading = "◆ stack to " .. (stack.title:match("[#.]([^#.]+)$") or stack.title)
383
+ table.insert(lines, heading .. " (" .. stack.summary:gsub(" through %S+", "") .. ")")
384
+ mark(marks, #lines - 1, 0, #heading, "RuntimeAnalysisCallees")
385
+ mark(marks, #lines - 1, #heading, #lines[#lines], "RuntimeAnalysisTally")
386
+ for _, frame in ipairs(stack.frames) do
387
+ row_line({ frame[1], frame[2], frame[3], 0 }, lines, marks, places)
388
+ end
389
+ draw(lines, marks, places)
390
+ end
391
+
392
+ local function show_frame()
393
+ local place = state.stack and cursor_place()
394
+ if place then preview.show(absolute(place.file), place.line) end
395
+ end
396
+
397
+ -- Start on the frame for the row the stack was asked from, or the last frame when it is not in it.
398
+ local function enter_stack(document, asked)
399
+ local frames = {}
400
+ for _, block in ipairs(document.blocks or {}) do
401
+ if block.type == "table" then frames = block.rows or {} end
402
+ end
403
+ if #frames == 0 then return message("no recorded call stack for this row") end
404
+ state.stack = { title = document.title, summary = document.summary or "", frames = frames,
405
+ back = vim.api.nvim_win_get_cursor(state.win) }
406
+ preview.start(state.target)
407
+ draw_stack()
408
+ local at = #state.places
409
+ for line, place in pairs(state.places) do
410
+ if place.identity == asked.identity then at = line end
411
+ end
412
+ vim.api.nvim_win_set_cursor(state.win, { at, 0 })
413
+ show_frame()
414
+ end
415
+
416
+ local function leave_stack()
417
+ local back = state.stack.back
418
+ state.stack, state.shown = nil, nil
419
+ return back
420
+ end
421
+
422
+ -- The stack leads to the deeper of the two methods and passes through the other: a row above the
423
+ -- subject reaches it, a row beneath is reached from it. With the whole file in view there is no subject.
424
+ local function stack_ends(place)
425
+ local subject = state.shown and state.shown.first and state.shown.title
426
+ if not subject then return place.identity, nil end
427
+ if place.section == "reaches it" then return subject, place.identity end
428
+ return place.identity, subject
429
+ end
430
+
431
+ local function ask_stack(place, method, through)
432
+ local command = vim.list_extend(project.ra(state.root), { "call_stack", method, "--json", "--top", "all" })
433
+ if through then vim.list_extend(command, { "--through", through }) end
434
+ vim.system(command, { cwd = state.root, text = true }, vim.schedule_wrap(function(result)
435
+ if not sidebar_open() or state.stack then return end
436
+ local ok, document = pcall(vim.json.decode, result.stdout or "")
437
+ if result.code == 0 and ok then return enter_stack(document, place) end
438
+ -- The subject may be an example rather than a method, which no stack can pass through.
439
+ if through then return ask_stack(place, place.identity, nil) end
440
+ message(vim.trim((result.stderr or ""):gsub("^ra: ", "")))
441
+ end))
442
+ end
443
+
444
+ local function open_stack()
445
+ local place = not state.stack and cursor_place()
446
+ if not (place and place.identity) or vim.startswith(place.identity, "(") then return end
447
+ if not (state.target and vim.api.nvim_win_is_valid(state.target)) then return end
448
+ ask_stack(place, stack_ends(place))
449
+ end
450
+
451
+ -- Back to the connections, with the followed window as it was before the stack was opened.
452
+ local function close_stack()
453
+ if not state.stack then return end
454
+ preview.cancel()
455
+ local back = leave_stack()
456
+ M.refresh()
457
+ pcall(vim.api.nvim_win_set_cursor, state.win, back)
458
+ end
459
+
460
+ -- Stay on the frame being shown, with the cursor in it.
461
+ local function accept_frame()
462
+ local window = preview.accept()
463
+ leave_stack()
464
+ if window and vim.api.nvim_win_is_valid(window) then vim.api.nvim_set_current_win(window) end
465
+ end
466
+
467
+ local function toggle_help()
468
+ state.help = not state.help
469
+ if state.stack then return draw_stack() end
470
+ local answer = state.shown
471
+ state.shown = nil
472
+ if answer then show(answer) end
473
+ end
474
+
475
+ local function create_buffer()
476
+ local buf = vim.api.nvim_create_buf(false, true)
477
+ vim.bo[buf].buftype, vim.bo[buf].bufhidden, vim.bo[buf].swapfile = "nofile", "wipe", false
478
+ vim.bo[buf].filetype = "runtime_analysis"
479
+ vim.api.nvim_buf_set_name(buf, "ra://connected")
480
+ vim.keymap.set("n", "<CR>", function()
481
+ if state.stack then accept_frame() else jump() end
482
+ end, { buffer = buf, desc = "Open this method or frame" })
483
+ vim.keymap.set("n", "o", open_stack, { buffer = buf, desc = "Step through the call stack to this method" })
484
+ vim.keymap.set("n", "<Esc>", close_stack, { buffer = buf, desc = "Leave the call stack" })
485
+ vim.keymap.set("n", "?", toggle_help, { buffer = buf, desc = "Show or hide the keys" })
486
+ vim.api.nvim_create_autocmd("CursorMoved", { buffer = buf, callback = show_frame })
487
+ vim.keymap.set("n", "q", M.close, { buffer = buf, desc = "Close the sidebar" })
488
+ for _, depth in ipairs({ "1", "2", "3" }) do
489
+ vim.keymap.set("n", depth, function() M.set_depth(depth) end, { buffer = buf, desc = "Follow " .. depth .. " files away" })
490
+ end
491
+ vim.keymap.set("n", "a", function() M.set_depth("all") end, { buffer = buf, desc = "Follow all the way" })
492
+ return buf
493
+ end
494
+
495
+ local function watch()
496
+ local group = vim.api.nvim_create_augroup(GROUP, { clear = true })
497
+ vim.api.nvim_create_autocmd("BufEnter", { group = group, callback = function(event) M.follow(event.buf) end })
498
+ vim.api.nvim_create_autocmd("ColorScheme", { group = group, callback = apply_highlights })
499
+ vim.api.nvim_create_autocmd("CursorMoved", { group = group, callback = function(event)
500
+ if followable(event.buf) and vim.api.nvim_get_current_win() == state.target then refresh_soon() end
501
+ end })
502
+ -- Saving can move every method, so answers remembered by line are dropped.
503
+ vim.api.nvim_create_autocmd("BufWritePost", { group = group, callback = function(event)
504
+ if followable(event.buf) then
505
+ state.cache = {}
506
+ refresh_soon()
507
+ end
508
+ end })
509
+ end
510
+
511
+ -- Opened from a file tree or another special window, there is no file under the cursor to follow,
512
+ -- so take the first window on screen that holds one.
513
+ local function follow_something(origin)
514
+ M.follow(vim.api.nvim_win_get_buf(origin), origin)
515
+ for _, window in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
516
+ if state.file then return end
517
+ M.follow(vim.api.nvim_win_get_buf(window), window)
518
+ end
519
+ if not state.file then message("move to a file to see what it is connected to") end
520
+ end
521
+
522
+ function M.setup(options)
523
+ config = vim.tbl_deep_extend("force", config, options or {})
524
+ apply_highlights()
525
+ end
526
+
527
+ function M.open()
528
+ if sidebar_open() then return end
529
+ apply_highlights()
530
+ local origin = vim.api.nvim_get_current_win()
531
+ state.buf = create_buffer()
532
+ vim.cmd("topleft vertical " .. config.width .. "split")
533
+ state.win = vim.api.nvim_get_current_win()
534
+ vim.api.nvim_win_set_buf(state.win, state.buf)
535
+ for option, value in pairs({ number = false, relativenumber = false, wrap = false, winfixwidth = true,
536
+ signcolumn = "no", cursorline = true, foldcolumn = "0" }) do
537
+ vim.wo[state.win][option] = value
538
+ end
539
+ vim.api.nvim_set_current_win(origin)
540
+ watch()
541
+ state.file, state.shown = nil, nil
542
+ follow_something(origin)
543
+ end
544
+
545
+ function M.close()
546
+ if state.stack then
547
+ preview.cancel()
548
+ state.stack = nil
549
+ end
550
+ if sidebar_open() then vim.api.nvim_win_close(state.win, true) end
551
+ pcall(vim.api.nvim_del_augroup_by_name, GROUP)
552
+ state.win, state.buf, state.file, state.shown = nil, nil, nil, nil
553
+ end
554
+
555
+ function M.toggle()
556
+ if sidebar_open() then M.close() else M.open() end
557
+ end
558
+
559
+ function M.set_depth(depth)
560
+ config.depth = depth == "all" and "all" or (tonumber(depth) or "all")
561
+ state.shown = nil
562
+ M.refresh()
563
+ end
564
+
565
+ return M
@@ -0,0 +1,63 @@
1
+ -- A `gf` that goes to a class. On a constant it opens where the class is declared. On a variable, a
2
+ -- parameter or a call it opens the class the recording saw there, and for an Array the class it
3
+ -- held. Where the language server knows no class, the key does what it did before.
4
+
5
+ local M = {}
6
+
7
+ local TAKEN = "runtime_analysis_class_key"
8
+
9
+ local function client_for(bufnr)
10
+ return vim.lsp.get_clients({ bufnr = bufnr, name = "runtime_analysis" })[1]
11
+ end
12
+
13
+ -- The first place is opened, with the jump list and the tag stack able to come back. When the
14
+ -- recording saw several classes, they are all in the location list.
15
+ local function open(client, places)
16
+ vim.lsp.util.show_document(places[1], client.offset_encoding, { focus = true })
17
+ if #places == 1 then return end
18
+ local items = vim.lsp.util.locations_to_items(places, client.offset_encoding)
19
+ vim.fn.setloclist(0, {}, " ", { title = "Classes seen here", items = items })
20
+ vim.notify(("ra: %d classes were seen here. :lopen lists them"):format(#places))
21
+ end
22
+
23
+ local function feed(keys, remap)
24
+ vim.api.nvim_feedkeys(vim.api.nvim_replace_termcodes(keys, true, true, true), remap and "m" or "n", false)
25
+ end
26
+
27
+ -- A mapping another plugin gave the key is run as it was written: its Lua function, or its keys
28
+ -- with <SID> pointed back at the script that defined it.
29
+ local function replay(prior)
30
+ local expr = prior.expr == 1
31
+ if prior.callback and not expr then return prior.callback() end
32
+ local keys = prior.callback and prior.callback() or prior.rhs:gsub("<[Ss][Ii][Dd]>", "<SNR>" .. (prior.sid or 0) .. "_")
33
+ if expr and not prior.callback then keys = vim.api.nvim_eval(keys) end
34
+ feed(keys, prior.noremap ~= 1)
35
+ end
36
+
37
+ local function as_before(key, prior)
38
+ if prior.callback or (prior.rhs and prior.rhs ~= "") then return replay(prior) end
39
+ local ok, problem = pcall(vim.cmd, "normal! " .. key)
40
+ if not ok then vim.notify(tostring(problem):gsub("^.-(E%d+:)", "%1"), vim.log.levels.ERROR) end
41
+ end
42
+
43
+ local function go(key, prior)
44
+ local bufnr = vim.api.nvim_get_current_buf()
45
+ local client = client_for(bufnr)
46
+ if not client then return as_before(key, prior) end
47
+ local params = vim.lsp.util.make_position_params(0, client.offset_encoding)
48
+ client:request("textDocument/typeDefinition", params, function(_, places)
49
+ if type(places) == "table" and #places > 0 then open(client, places) else as_before(key, prior) end
50
+ end, bufnr)
51
+ end
52
+
53
+ -- Takes `key` in a buffer, once. The language server can attach again after a restart, and the
54
+ -- mapping that is remembered must stay the one from before this plugin.
55
+ function M.enable(bufnr, key)
56
+ bufnr = bufnr == 0 and vim.api.nvim_get_current_buf() or bufnr
57
+ if vim.b[bufnr][TAKEN] then return end
58
+ vim.b[bufnr][TAKEN] = key
59
+ local prior = vim.api.nvim_buf_call(bufnr, function() return vim.fn.maparg(key, "n", false, true) end)
60
+ vim.keymap.set("n", key, function() go(key, prior) end, { buffer = bufnr, desc = "Go to the class under the cursor" })
61
+ end
62
+
63
+ return M