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
local micro = import("micro")
local buffer = import("micro/buffer")
local config = import("micro/config")
local filepath = import("filepath")

function goToFile(bp, args)
  local cursor = bp.Cursor
  local line = bp.Buf:Line(cursor.Y)

  -- Extract word at cursor position
  local startPos = findWordStart(line, cursor.X + 1) -- Lua is 1-indexed
  local endPos = findWordEnd(line, cursor.X + 1)
  local word = line:sub(startPos, endPos)

  if word == "" then
    micro.InfoBar():Message("No word under cursor - running tv files...")
    runTvFiles(bp)
    return
  end

  -- Try different path resolutions
  local candidates = {}

  -- If absolute path, try as-is
  if word:sub(1, 1) == "/" then
    table.insert(candidates, word)
  else
    -- Try relative to current buffer's directory
    if bp.Buf.Path and bp.Buf.Path ~= "" then
      local currentDir = filepath.Dir(bp.Buf.Path)
      table.insert(candidates, filepath.Join(currentDir, word))
    end

    -- Try relative to PWD
    table.insert(candidates, word)
  end

  -- Try to open first existing file
  for _, path in ipairs(candidates) do
    if fileExists(path) then
      local tab = OpenTabFromFile(bp, path)
      if tab then
        micro.InfoBar():Message("Opened: " .. path)
      else
        micro.InfoBar():Error("Failed to open: " .. path)
      end
      return
    end
  end

  -- File not found, fall back to running tv files
  micro.InfoBar():Message("File not found: " .. word .. " - running tv files...")
  runTvFiles(bp)
end