pkulchenko / ZeroBraneStudio

Lightweight Lua-based IDE for Lua with code completion, syntax highlighting, live coding, remote debugger, and code analyzer; supports Lua 5.1, 5.2, 5.3, 5.4, LuaJIT and other Lua interpreters on Windows, macOS, and Linux
http://studio.zerobrane.com/
Other
2.59k stars 518 forks source link

[Request] Teal Support #1152

Open qEagleStrikerp opened 1 year ago

qEagleStrikerp commented 1 year ago

Teal is still relatively new, but it looks very promising. Currently there is no IDE for it with debugging support, and seeing as ZeroBraneStudio is most people's favorite Lua IDE, it would be nice to have Teal support for it (via Plugin maybe?).

pkulchenko commented 1 year ago

Looking at the Moonscript plugin may be a good first step. Assuming that lua modules (like Mobdebug and luasocket) can be loaded, then someone can start debugging from a Teal script to test this quickly (without any additional coding, just by following the instructions for remote debugging) and if Teal integration with Lua provides a replacement for load/loadstring and some support for source maps, then other debugging features can be supported as well (similar to how it's done for moonscript).

qEagleStrikerp commented 1 year ago

Thanks for your answer. I did indeed get it to work by altering the Moonscript plugin to create a Teal interpreter. But all syntax highlighting is lost for .tl files, sadly. Is there any way to load a .tl file and still have syntax highlighting? Edit: I got that to work by adding editor.specmap.tl = 'lua' to user.lua. So it is actually possible to set up with minimal work. Thank you! The only thing that currently doesn't work is compiling .tl files to .lua files from within ZeroBrane Studio, without having to use cmd. But that's not too important for now.

Should I create a pull request for the teal.lua plugin?

pkulchenko commented 1 year ago

I got that to work by adding editor.specmap.tl = 'lua' to user.lua. So it is actually possible to set up with minimal work. Thank you!

Yes, that should work, but it's also possible to add a more elaborate lexer for the actual teal code. You can start with lualibs/lexers/lua.lua and turn it into tl.lua lexer (and then register it from teal.lua plugin using ide:AddLexer("lexlpeg.teal", [[text of teal lexer]]) command). You'll also need to add it to a spec and register this spec with ide:AddSpec():

local spec = {
  exts = {"tl"},
  lexer = "lexlpeg.teal", -- same lexer name as used in AddLexer
  apitype = "lua",
  linecomment = "--",
  sep = ".:",
  marksymbols = ide.spec.lua.marksymbols,
}
ide:AddSpec("teal", spec)
pkulchenko commented 1 year ago

Should I create a pull request for the teal.lua plugin?

Yes, you can submit it to https://github.com/pkulchenko/ZeroBranePackage repository.

qEagleStrikerp commented 1 year ago

What's the syntax of ide:AddLexer(name, lexer)? Specifically, what does it expect for "lexer", a path to a file? Edit: Managed to figure that one out. But now on startup Zerobrane Studio outputs "Error while processing file: 'src/editor/package.lua:1206: attempt to call global 'ReloadAPIs' (a nil value)'." It seems that the function "ReloadAPIs" doesn't exist currently. The line that throws the error is "ide:AddSpec("teal", spec)".

pkulchenko commented 1 year ago

What's the syntax of ide:AddLexer(name, lexer)? Specifically, what does it expect for "lexer", a path to a file?

(for others who may be looking at this) It's a string with the lexer code to be loaded from a plugin.

It seems that the function "ReloadAPIs" doesn't exist currently. The line that throws the error is "ide:AddSpec("teal", spec)".

Let me take a quick look.

pkulchenko commented 1 year ago

"Error while processing file: 'src/editor/package.lua:1206: attempt to call global 'ReloadAPIs' (a nil value)'."

@qEagleStrikerp, the function does exist for sure (it's in src/editor/autocomplete.lua file), but you're probably making AddSpec call too early, before the setup is completed. You need to call it from onRegister plugin callback. See this section in the documentation for an example.

qEagleStrikerp commented 1 year ago

@pkulchenko This was indeed the case. Calling it within onRegister works. However, now I have another issue: ide:AddLexer() doesn't actually do anything, I get the same error message "Can't load LexLPeg 'lexlpeg.teal' lexer: no file 'D:\Portable\Coding\ZeroBraneStudio-1.90\lualibs\lexers\teal.lua'" whether it is in the code or not. From what I could find out by looking at src/editor/editor.lua, it seems that this should create a new file called "lexlpeg.teal" on first calling, but this creation process never happens. Did I understand something wrong? I stored the entire lexer file in a string and passed it as the second argument to ide:AddLexer(). Edit: running ide:GetLexer("lexlpeg.teal") int the local console correctly returns the lexer though, so the function definitely gets called Edit 2: Digging a little deeper, I may have found the issue: in editor.lua, function setLexLPegLexer() sets lex.LEXERPATH to look in the temporary directory, but lexer.lua doesn't have this variable and doesn't consider it. Thus, the call ok, err = pcall(lex.load, lexer) returns false. Edit 3: It was due to me trying it on the outdated 1.90 release. On the current version and the Lua 5.4 branch, it works fine! Gotta make some adjustments, then create a pull request. What should I put in as value for "dependencies"? Since it requires the current version (>1.90) which doesn't have an official release yet. Edit 4: I set up the lexer so that it correctly colors types. They got the same colors as libraries, but I guess that's because nobody set up colors for types in the first place? That's not too much a problem for now, but something that really destroys the optics is the fact that types get treated like variables. This results in types being underlined if they've already been used or them being crossed out if they are "reassigned" (e.g. writing local enum foo, local enum bar). Also, the "global" keyword is underlined as well which it shouldn't be. Is there any way to change this behavior?

qEagleStrikerp commented 1 year ago

@pkulchenko Could you take a look at my previous Edit 4?

pkulchenko commented 1 year ago

Thank you for the update; good to see you're making progress! Colors are a bit problematic, as they are remapped using lexlpegmap in editor.lua, which is not configurable at this moment. I have it on my todo list, but it's not a simple change, so requires a bit of work. You can update all the styles in your own spec, so it should be fixable, but I'd prefer to have a more convenient way to map all those styles and their colors.

qEagleStrikerp commented 1 year ago

@pkulchenko Thanks for your answer! Is changing colors and changing the underlining / crossing out all related to the same problem? Because changing colors is not nearly as important for the visuals as changing the way the grammar is processed. Especially the following problem:

local enum foo end
local enum bar end

becomes

local enum foo end local enum bar end

which really destroys the optics, unfortunately.

pkulchenko commented 1 year ago

This is controlled by marksymbols element in the spec; you're using the Lua one with the obvious issues. You can remove it completely, which will disable it or provide your own Teal-specific function.

qEagleStrikerp commented 1 year ago

@pkulchenko Thanks, works like a charm, looks really beautiful. I created a pull request for the plugin :).

qEagleStrikerp commented 1 year ago

@pkulchenko I noticed there was a little feature missing, namely the ability to save both .lua and .tl files. I found a workaround, but I think the way I solved it is not the intended way (every other thing I tried messed up either the lua or the teal lexer though). Would be nice if you could take a look at it some time: https://github.com/pkulchenko/ZeroBranePackage/pull/95/commits/1263d000bcb091cd7cb5e6af52da7acd159abfb2

qEagleStrikerp commented 1 year ago

@pkulchenko Any chance you could take a look at the aforementioned pull request? Then I could link to it from the Teal Repo.