zyedidia / micro

A modern and intuitive terminal-based text editor
https://micro-editor.github.io
MIT License
24.39k stars 1.16k forks source link

IsWhitespace from util package not working #3297

Closed dei-layborer closed 1 month ago

dei-layborer commented 1 month ago

Description of the problem or steps to reproduce

I'm working on a plugin, and tried to use IsWhitespace(rune), defined in the util package. The relevant code follows:

local mutil = import("micro/util")

-- snip

function preRune(bp, r)

   -- snip

   -- NOTE: prevChar is defined earlier
   if mutil.IsWhitespace(prevChar) then
      -- snip
   end
end

When preRune is triggered, micro crashes with an error pointing to the if statement and the error message attempt to call a non-function object.

From what little I know of Lua, it seems like this means IsWhitespace isn't accessible?

Specifications

Commit hash: 225927b OS: Linux (openSUSE tumbleweed, specifically) Terminal: Konsole

Andriamanitra commented 1 month ago

The "micro/util" you can import in Lua is not the same thing as the internal util package. The only functions it exposes are: https://github.com/zyedidia/micro/blob/917650826a9da1b8d7223d2e3b9b1ad5ac845aa2/cmd/micro/initlua.go#L148-L165

(also documented in help plugins)

dei-layborer commented 1 month ago

I see. That being the case, it seems like this part of plugins.md is misleading:

This may seem like a small list of available functions but some of the objects returned by the functions have many methods. The Lua plugin may access any public methods of an object returned by any of the functions above. Unfortunately it is not possible to list all the available functions on this page. Please go to the internal documentation at https://pkg.go.dev/github.com/zyedidia/micro/v2/internal to see the full list of available methods. Note that only methods of types that are available to plugins via the functions above can be called from a plugin.

Andriamanitra commented 1 month ago

The documentation is correct although I can see how it could be confusing if you don't read it carefully: import is not on the list of functions the documentation is referring to, and it just returns a regular Lua table with some functions in it. Those functions (import("micro/util").HttpRequest, import("micro").CurPane, etc.) return the (Go) objects the documentation is referring to. The (Go) objects are userdata on the Lua side, and you can indeed access any of their public methods.

dei-layborer commented 1 month ago

I more meant that it says to go look at the internal documentation at that link "to see the full list of available methods," but not all of those methods are actually available.

dmaluka commented 1 month ago

IsWhitespace() is not a method.

The documentation explicitly warns: "Note that only methods of types that are available to plugins via the functions above can be called from a plugin."

For example, one of those "functions above" is CurPane(), it returns a BufPane object, and all the methods of this object indeed can be invoked from Lua.

local bp = micro.CurPane()
bp:AddTab()

etc.

dei-layborer commented 1 month ago

Again, this is not clear from the documentation for the reasons I stated above. But I won't belabor it at this point.