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

-- Store message details by location
local messageDetails = {}
local shortMessages = {}
local allMessages = {}

function clearGutterMessages(bp)
  -- Clear only our messages (by owner) from the buffer
  bp.Buf:ClearMessages("json-analyzer")

  -- Also clear our internal storage
  messageDetails = {}
  shortMessages = {}
  allMessages = {}
end

function loadTestMessages(bp, args)
  -- Clear existing messages from gutter
  clearGutterMessages(bp)

  -- Manually create test messages (hardcoded for now)
  -- Note: micro uses 0-based line indexing internally
  allMessages = {
    {
      loc = { startLine = 0, endLine = 0, startCol = 0, endCol = 4 },
      type = "info",
      shortMsg = "prefer-const: This variable can be const",
      details = "📋 ESLint Rule: prefer-const\n\n🔍 Issue Location: Line 1, columns 1-5\n📝 Problem: Variable is never reassigned after initialization\n💡 Suggestion: Use 'const' instead of 'let' for variables that are never reassigned\n🎯 Impact: Improves code clarity and prevents accidental reassignment\n\n🛠️ Auto-fix available: Replace 'let' with 'const'"
    },
    {
      loc = { startLine = 1, endLine = 1, startCol = 5, endCol = 10 },
      type = "warning",
      shortMsg = "no-undef: 'hello' is not defined",
      details = "📋 ESLint Rule: no-undef\n\n🔍 Issue Location: Line 2, columns 6-11\n⚠️  Problem: Variable 'hello' is used but never declared\n💡 Suggestion: Declare the variable before use or import it if from another module\n🎯 Impact: ReferenceError at runtime\n\n📝 Possible fixes:\n- Add: const hello = 'world'\n- Add: import { hello } from './module'\n- Add: /* global hello */ comment"
    },
    {
      loc = { startLine = 3, endLine = 3, startCol = 10, endCol = 15 },
      type = "error",
      shortMsg = "parse-error: Expected ';' after expression",
      details = "📋 Parser Error\n\n🔍 Issue Location: Line 4, columns 11-16\n❌ Problem: Unexpected token. Expected ';' after expression statement\n💡 Suggestion: Add missing semicolon or check for syntax errors\n🎯 Impact: Code will not compile or run\n\n📝 Common causes:\n- Missing semicolon\n- Unclosed parentheses or brackets\n- Invalid character in identifier\n\n🛠️ Quick fix: Add semicolon at end of statement"
    },
    {
      loc = { startLine = 4, endLine = 4, startCol = 0, endCol = 8 },
      type = "warning",
      shortMsg = "no-unused-vars: 'testVar' is assigned but never used",
      details = "📋 ESLint Rule: no-unused-vars\n\n🔍 Issue Location: Line 5, columns 1-9\n⚠️  Problem: Variable 'testVar' is declared but its value is never read\n💡 Suggestion: Remove unused variable or use it in the code\n🎯 Impact: Dead code, potential memory waste\n\n📝 Options:\n- Remove the variable declaration\n- Use the variable in your code\n- Prefix with underscore: _testVar (if intentionally unused)\n- Add ESLint disable comment"
    }
  }

  -- Process each message
  for _, msg in ipairs(allMessages) do
    local loc = msg.loc
    local key = string.format("%d:%d-%d:%d-%d", loc.startLine, loc.startCol, loc.endCol, loc.endLine, loc.endCol)
    messageDetails[key] = msg.details or msg.shortMsg
    shortMessages[key] = msg.shortMsg
  end

  micro.InfoBar():Message("Loaded " .. #allMessages .. " test messages")
  displayMessagesFromJSON(bp, {})
end

function displayMessagesFromJSON(bp, args)
  -- Process each message from JSON and add to buffer
  for _, msg in ipairs(allMessages) do
    local loc = msg.loc
    local startLoc = buffer.Loc(loc.startCol, loc.startLine)
    local endLoc = buffer.Loc(loc.endCol, loc.endLine)

    -- Determine message type
    local msgType = buffer.MTInfo
    if msg.type == "warning" then
      msgType = buffer.MTWarning
    elseif msg.type == "error" then
      msgType = buffer.MTError
    end

    -- Create and add message
    local bufMsg = buffer.NewMessage("json-analyzer", msg.shortMsg, startLoc, endLoc, msgType)
    bp.Buf:AddMessage(bufMsg)

    -- Store detailed message for inspection
    local key = string.format("%d:%d-%d:%d-%d", loc.startLine, loc.startCol, loc.endCol, loc.endLine, loc.endCol)
    messageDetails[key] = msg.details or msg.shortMsg
    shortMessages[key] = msg.shortMsg
  end

  micro.InfoBar():Message("Displayed " .. #allMessages .. " messages in buffer")
end

function showMessageAtCursor(bp, args)
  local cursorLoc = bp.Cursor.Loc

  -- Check if cursor is on a message location
  for key, detailText in pairs(messageDetails) do
    local startLine, startCol, endCol, endLine, endCol2 = key:match("(%d+):(%d+)-(%d+):(%d+)-(%d+)")
    startLine, startCol, endCol, endLine, endCol2 = tonumber(startLine), tonumber(startCol), tonumber(endCol), tonumber(endLine), tonumber(endCol2)

    local startLoc = buffer.Loc(startCol, startLine)
    local endLoc = buffer.Loc(endCol2, endLine)

    -- Check if cursor is within the message range using Loc comparison
    if cursorLoc.GreaterEqual(startLoc) and cursorLoc.LessEqual(endLoc) then
      -- Get short message for InfoBar
      local shortMsg = shortMessages[key] or "Message found"

      -- Create buffer with detailed message
      local detailBuf = buffer.NewBuffer(detailText, "")

      -- Open in vertical split
      bp:VSplitBuf(detailBuf)

      -- Show short message in InfoBar
      micro.InfoBar():Message("📍 " .. shortMsg)
      return
    end
  end

  micro.InfoBar():Message("No detailed message at cursor position")
end