lite-xl / console

A console plugin for the lite text editor
MIT License
23 stars 9 forks source link

No all compilers return line and col number, but a range! #16

Open SmileYzn opened 1 year ago

SmileYzn commented 1 year ago

Pawn language return a range where warning / error was like this

/home/SmileY/antiflood.sma(22 -- 24) : warning 215: expression has no effect

I suggest to filter column and line using string.match inside tonumber:

function ConsoleView:on_mouse_pressed(...)
  local caught = ConsoleView.super.on_mouse_pressed(self, ...)
  if caught then
    return
  end
  local item = output[self.hovered_idx]
  if item then
    local file, line, col = item.text:match(item.file_pattern)
    local resolved_file = resolve_file(item.file_prefix, file)
    if not resolved_file then
      -- fixes meson output which adds ../ for build sub directories
      resolved_file = resolve_file(
        item.file_prefix,
        file:gsub("%.%./", ""):gsub("^%./", "")
      )
    end
    if not resolved_file then
      core.error("Couldn't resolve file \"%s\"", file)
      return
    end
    core.try(function()
      core.root_view:open_doc(core.open_doc(resolved_file))
      line = tonumber(string.match(line, "%d+")) or 1
      col = tonumber(string.match(col, "%d+")) or 1
      core.add_thread(function()
        core.active_view.doc:set_selection(line, col)
      end)
    end)
  end
end

My console run command was

          -- Run console command
          console.run
          {
            command         = compileCommand,
            file_pattern    = "(.*)%((.*)%) : (.*)",
            file_prefix     = ".",
            cwd             = ".",
            error_pattern   = "error",
            warning_pattern = "warning",
            on_complete     = function()            
              core.log("AMX Mod X Compiler: Compilation Complete")
            end
          }
Guldoman commented 1 year ago

I think the system could be refactored a bit to allow the user to configure a callback function that takes the text, line, and column and returns the file, its line and column.

Still, in this case, a pattern to match the first line in the range should be simple enough to write.