-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
383 lines (329 loc) · 14.8 KB
/
main.lua
File metadata and controls
383 lines (329 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
#!/usr/bin/env luajit
-- obsidian-code: Codebase Function Graph Visualizer
-- Usage:
-- love . (GUI mode)
-- luajit main.lua --cli <path> (CLI mode, JSON output)
-- Set up LÖVE2D's internal file loader to find our lua/ modules inside the .love archive
if love and love.filesystem then
love.filesystem.setRequirePath("?.lua;?/init.lua;lua/?.lua;lua/ui/?.lua")
end
-- Set up standard Lua module paths for filesystem access
local sourceDir = love and love.filesystem.getSource() or (arg[0]:match("(.*/)") or "./")
-- Get the directory containing the .exe (sourceDir may be the .exe path on Windows fused builds)
local sourceParent = sourceDir:match("^(.+)[/\\][^/\\]+$") or sourceDir
package.path = sourceDir .. "/lua/?.lua;" .. sourceDir .. "/lua/ui/?.lua;" .. package.path
-- Add native C module search paths (.so on Linux, .dll on Windows)
local home = os.getenv("HOME") or ""
local isWindows = love and love.system and love.system.getOS() == "Windows"
if isWindows then
-- On Windows fused builds, DLLs are next to the .exe
package.cpath = sourceParent .. "/?.dll;"
.. sourceParent .. "/?51.dll;"
.. sourceDir .. "/?.dll;"
.. ".\\?.dll;"
.. package.cpath
else
package.cpath = home .. "/.luarocks/lib/lua/5.1/?.so;"
.. "/usr/local/lib/lua/5.1/?.so;"
.. "/usr/lib/lua/5.1/?.so;"
.. package.cpath
end
-- CLI mode: analyze and print JSON, no GUI
if arg and arg[1] == "--cli" then
local cjson = require("cjson")
local analyzer = require("analyzer")
local path = arg[2]
if not path then
io.stderr:write("Usage: luajit main.lua --cli <project-path>\n")
os.exit(1)
end
local ok, result = pcall(analyzer.analyze, path)
if not ok then
io.stderr:write("Error: " .. tostring(result) .. "\n")
os.exit(1)
end
print(cjson.encode(result))
os.exit(0)
end
-- GUI mode (LÖVE2D)
local colors = require("ui.colors")
local toolbar = require("ui.toolbar")
local sidebar = require("ui.sidebar")
local graphView = require("ui.graph_view")
local flowView = require("ui.flow_view")
local graphData = nil
local defaultFont = nil
-- On Windows, write to a log file on Desktop (SFX extracts to temp dir)
local logFile = nil
local function log(msg)
io.stderr:write("[obsidian-code] " .. msg .. "\n")
if not logFile then
-- Try Desktop, then USERPROFILE, then next to exe
local desktop = os.getenv("USERPROFILE")
if desktop then
logFile = io.open(desktop .. "\\Desktop\\obsidian-code.log", "w")
if not logFile then
logFile = io.open(desktop .. "\\obsidian-code.log", "w")
end
end
if not logFile then
logFile = io.open(sourceParent .. "/obsidian-code.log", "w")
end
end
if logFile then
logFile:write("[obsidian-code] " .. msg .. "\n")
logFile:flush()
end
end
local function analyzeProject(path)
-- Wrap everything in pcall so errors never crash the app
local success, errMsg = pcall(function()
log("Analyzing: " .. path)
log("sourceDir: " .. sourceDir)
log("sourceParent: " .. sourceParent)
log("package.cpath: " .. package.cpath)
-- Load native C modules one at a time with logging
log("Step 1: loading cjson...")
local ok_cjson, cjson = pcall(require, "cjson")
if not ok_cjson then
toolbar.setError("cjson not found: " .. tostring(cjson))
log("FAIL cjson: " .. tostring(cjson))
return
end
log("Step 1: cjson OK")
log("Step 2: loading lfs...")
local ok_lfs, lfs = pcall(require, "lfs")
if not ok_lfs then
toolbar.setError("lfs not found: " .. tostring(lfs))
log("FAIL lfs: " .. tostring(lfs))
return
end
log("Step 2: lfs OK")
log("Step 3: loading analyzer...")
local ok_analyzer, analyzer = pcall(require, "analyzer")
if not ok_analyzer then
toolbar.setError("analyzer load failed: " .. tostring(analyzer))
log("FAIL analyzer: " .. tostring(analyzer))
return
end
log("Step 3: analyzer OK")
-- Tell analyzer where to find tree-sitter .so/.dll files
analyzer.base_dir = sourceParent
analyzer.log = log -- share file-based logging
log("Step 4: base_dir=" .. analyzer.base_dir)
log("Step 5: calling analyzer.analyze(" .. path .. ")...")
local ok, result = pcall(analyzer.analyze, path)
if not ok then
toolbar.setError(tostring(result))
log("FAIL analyze: " .. tostring(result))
return
end
log("Step 6: Done: " .. #result.nodes .. " nodes, " .. #result.edges .. " edges")
graphData = result
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
local sidebarW = sidebar.getWidth()
local toolbarH = toolbar.getHeight()
graphView.setGraph(result, w - sidebarW, h - toolbarH)
sidebar.setGraph(result)
flowView.setGraph(result)
flowView.reset()
toolbar.setViewMode("network")
end)
if not success then
toolbar.setError("Crash: " .. tostring(errMsg))
log("CRASH in analyzeProject: " .. tostring(errMsg))
end
end
function love.load()
log("=== obsidian-code starting ===")
log("OS: " .. (love.system.getOS and love.system.getOS() or "unknown"))
log("sourceDir: " .. sourceDir)
log("sourceParent: " .. sourceParent)
log("package.cpath: " .. package.cpath)
love.window.setTitle("obsidian-code")
love.graphics.setBackgroundColor(colors.base)
defaultFont = love.graphics.newFont(13)
love.graphics.setFont(defaultFont)
graphView.init()
-- Wire up callbacks
toolbar.setCallbacks(analyzeProject, function(mode)
-- View mode changed
end)
sidebar.setCallbacks(
function(nodeId) -- select node
toolbar.setViewMode("flow")
flowView.selectNode(nodeId)
end,
nil -- filter file (not implemented yet)
)
flowView.setCallbacks(function(nodeId)
-- Navigated to a node in flow view
end)
-- Load demo data for quick testing
loadDemo()
end
function loadDemo()
graphData = {
root_dir = "/demo",
files = { "src/auth.ts", "src/api.ts", "src/db.ts", "src/utils.ts" },
nodes = {
{ id = "src/auth.ts:login:5", name = "login", file_path = "src/auth.ts", line = 5, kind = "function", params = { "username", "password" }, is_exported = true, is_async = true },
{ id = "src/auth.ts:validateToken:20", name = "validateToken", file_path = "src/auth.ts", line = 20, kind = "function", params = { "token" }, is_exported = true, is_async = false },
{ id = "src/auth.ts:hashPassword:35", name = "hashPassword", file_path = "src/auth.ts", line = 35, kind = "arrow", params = { "pw" }, is_exported = false, is_async = false },
{ id = "src/api.ts:fetchUser:3", name = "fetchUser", file_path = "src/api.ts", line = 3, kind = "function", params = { "id" }, is_exported = true, is_async = true },
{ id = "src/api.ts:createUser:15", name = "createUser", file_path = "src/api.ts", line = 15, kind = "function", params = { "data" }, is_exported = true, is_async = true },
{ id = "src/api.ts:deleteUser:30", name = "deleteUser", file_path = "src/api.ts", line = 30, kind = "function", params = { "id" }, is_exported = true, is_async = true },
{ id = "src/db.ts:query:2", name = "query", file_path = "src/db.ts", line = 2, kind = "function", params = { "sql", "params" }, is_exported = true, is_async = true },
{ id = "src/db.ts:connect:20", name = "connect", file_path = "src/db.ts", line = 20, kind = "function", params = {}, is_exported = true, is_async = true },
{ id = "src/db.ts:disconnect:35", name = "disconnect", file_path = "src/db.ts", line = 35, kind = "function", params = {}, is_exported = true, is_async = true },
{ id = "src/utils.ts:formatDate:1", name = "formatDate", file_path = "src/utils.ts", line = 1, kind = "arrow", params = { "date" }, is_exported = true, is_async = false },
{ id = "src/utils.ts:sanitize:10", name = "sanitize", file_path = "src/utils.ts", line = 10, kind = "arrow", params = { "input" }, is_exported = true, is_async = false },
{ id = "src/utils.ts:log:18", name = "log", file_path = "src/utils.ts", line = 18, kind = "arrow", params = { "msg" }, is_exported = false, is_async = false },
},
edges = {
{ source = "src/auth.ts:login:5", target = "src/api.ts:fetchUser:3", line = 8, is_conditional = false, is_in_loop = false, order = 1 },
{ source = "src/auth.ts:login:5", target = "src/auth.ts:hashPassword:35", line = 7, is_conditional = false, is_in_loop = false, order = 0 },
{ source = "src/auth.ts:login:5", target = "src/utils.ts:log:18", line = 12, is_conditional = false, is_in_loop = false, order = 2 },
{ source = "src/auth.ts:validateToken:20", target = "src/db.ts:query:2", line = 22, is_conditional = false, is_in_loop = false, order = 0 },
{ source = "src/api.ts:fetchUser:3", target = "src/db.ts:query:2", line = 6, is_conditional = false, is_in_loop = false, order = 0 },
{ source = "src/api.ts:fetchUser:3", target = "src/utils.ts:sanitize:10", line = 9, is_conditional = true, is_in_loop = false, order = 1 },
{ source = "src/api.ts:createUser:15", target = "src/db.ts:query:2", line = 18, is_conditional = false, is_in_loop = false, order = 0 },
{ source = "src/api.ts:createUser:15", target = "src/auth.ts:hashPassword:35", line = 17, is_conditional = false, is_in_loop = false, order = 1 },
{ source = "src/api.ts:deleteUser:30", target = "src/db.ts:query:2", line = 32, is_conditional = false, is_in_loop = false, order = 0 },
{ source = "src/api.ts:deleteUser:30", target = "src/utils.ts:log:18", line = 34, is_conditional = false, is_in_loop = false, order = 1 },
{ source = "src/db.ts:query:2", target = "src/db.ts:connect:20", line = 5, is_conditional = true, is_in_loop = false, order = 0 },
},
}
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
local sidebarW = sidebar.getWidth()
local toolbarH = toolbar.getHeight()
graphView.setGraph(graphData, w - sidebarW, h - toolbarH)
sidebar.setGraph(graphData)
flowView.setGraph(graphData)
end
function love.update(dt)
toolbar.update(dt)
graphView.update(dt)
-- Update cursor
local cursor = graphView.getCursor()
if cursor then
love.mouse.setCursor(cursor)
else
love.mouse.setCursor()
end
end
function love.draw()
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
local toolbarH = toolbar.getHeight()
local sidebarW = sidebar.getWidth()
-- Toolbar (full width, top)
love.graphics.setFont(defaultFont)
toolbar.draw(w)
-- Sidebar (left, below toolbar)
sidebar.draw(0, toolbarH, sidebarW, h - toolbarH)
-- Main content area (right of sidebar, below toolbar)
local contentX = sidebarW
local contentY = toolbarH
local contentW = w - sidebarW
local contentH = h - toolbarH
-- Background for content
love.graphics.setColor(colors.mantle)
love.graphics.rectangle("fill", contentX, contentY, contentW, contentH)
love.graphics.setFont(defaultFont)
if toolbar.getViewMode() == "network" then
graphView.draw(contentX, contentY, contentW, contentH)
else
flowView.draw(contentX, contentY, contentW, contentH)
end
-- Welcome screen if no data
if not graphData then
love.graphics.setColor(colors.overlay0)
love.graphics.printf("Enter a project path and click Analyze", contentX, contentY + contentH / 2 - 10, contentW, "center")
end
-- Error bar on top of everything
love.graphics.setFont(defaultFont)
toolbar.drawError(w)
end
function love.mousepressed(x, y, button)
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
local toolbarH = toolbar.getHeight()
local sidebarW = sidebar.getWidth()
-- Toolbar gets first priority
if toolbar.mousepressed(x, y, button, w) then return end
-- Sidebar
if sidebar.mousepressed(x, y, button, 0, toolbarH, sidebarW, h - toolbarH) then return end
-- Content area
local contentX = sidebarW
local contentY = toolbarH
local contentW = w - sidebarW
local contentH = h - toolbarH
if toolbar.getViewMode() == "network" then
graphView.mousepressed(x, y, button, contentX, contentY, contentW, contentH)
else
flowView.mousepressed(x, y, button, contentX, contentY, contentW, contentH)
end
end
function love.mousereleased(x, y, button)
if sidebar.mousereleased(x, y, button) then return end
local clickedId = graphView.mousereleased(x, y, button)
if clickedId then
-- Node was clicked — switch to flow view
toolbar.setViewMode("flow")
flowView.selectNode(clickedId)
end
end
function love.mousemoved(x, y, dx, dy)
if sidebar.mousemoved(x, y) then return end
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
local toolbarH = toolbar.getHeight()
local sidebarW = sidebar.getWidth()
local contentX = sidebarW
local contentY = toolbarH
local contentW = w - sidebarW
local contentH = h - toolbarH
graphView.mousemoved(x, y, contentX, contentY, contentW, contentH)
end
function love.wheelmoved(wx, wy)
local mx, my = love.mouse.getPosition()
local w = love.graphics.getWidth()
local h = love.graphics.getHeight()
local toolbarH = toolbar.getHeight()
local sidebarW = sidebar.getWidth()
-- Sidebar scroll
if sidebar.wheelmoved(wx, wy, mx, my, 0, toolbarH, sidebarW, h - toolbarH) then return end
-- Flow view scroll
if toolbar.getViewMode() == "flow" then
local contentX = sidebarW
local contentY = toolbarH
local contentW = w - sidebarW
local contentH = h - toolbarH
if flowView.wheelmoved(wx, wy, mx, my, contentX, contentY, contentW, contentH) then return end
end
-- Graph zoom
if toolbar.getViewMode() == "network" then
graphView.wheelmoved(wx, wy, mx, my, sidebarW, toolbarH)
end
end
function love.textinput(t)
toolbar.textinput(t)
end
function love.keypressed(key)
if toolbar.keypressed(key) then return end
-- Global shortcuts
if key == "escape" then
love.event.quit()
end
end
function love.resize(w, h)
-- Re-settle graph for new dimensions if we have data
if graphData then
local sidebarW = sidebar.getWidth()
local toolbarH = toolbar.getHeight()
graphView.setGraph(graphData, w - sidebarW, h - toolbarH)
end
end