lua-gdextension icon indicating copy to clipboard operation
lua-gdextension copied to clipboard

Syntax highlighting for CodeEdit

Open OvercookedBeef opened this issue 9 months ago • 3 comments

Godot has a CodeEdit widget that I'm using for my game. It would be nice to have syntax/code highlighting for editing.

CodeEdit has the following:

Image

I tried to quick load but it looks like there's not anything included for this. Could there be a built-in syntax highlighter for Lua in this library?

OvercookedBeef avatar Mar 28 '25 16:03 OvercookedBeef

Hey @OvercookedBeef, thanks for the request.

It would be nice to have syntax/code highlighting for editing

I totally agree! There's nothing ready for this in this addon, but we could certainly have something.

Could there be a built-in syntax highlighter for Lua in this library?

Sure. A CodeHighlighter with the correct script that adds the keywords to it should be enough at least to have keywords .

Maybe we should add a scripted CodeEdit as well to handle other things about the Lua language that the highlighter doesn't support, such as setting the correct comment and string delimiters.

Since both of them can be done with scripts, I think we don't need to make subclasses of them in C++, we can just use GDScript directly.

gilzoide avatar Apr 06 '25 12:04 gilzoide

The default SyntaxHighlighter supports Lua comments/strings just fine.

	highlighter.color_regions = {
		"--[[ ]]": COLORS.COMMENT,
		"--  ": COLORS.COMMENT,
		"\" \"": COLORS.STRING,
		"' '": COLORS.STRING
	}

I have ran into issues with setting up auto tab/auto brace completion. The pairs must be 1 character, and Lua uses keywords such as do/then.

Here is my current CodeEdit script with syntax highlighting. It doesn't cover everything, but it should be a good starting point.

class_name LuaCodeEdit extends CodeEdit

var COLORS = {
	"TEXT": Color("#d9d9d9"),
	"FUNCTION": Color("#ffea3f"),
	"NUMBER": Color("#ffc049"),
	"KEYWORD": Color("#ff6671"),
	"STRING": Color("#ff8502"),
	"COMMENT": Color("#85878a")
}

const LUA_KEYWORDS = [
	"and", "break", "do", "else", "elseif", 
	"end", "false", "for", "function", "if",
	"in", "local", "nil", "not", "or", "repeat", 
	"return", "then", "true", "until", "while"
]

func _ready() -> void:
	syntax_highlighter = _create_lua_highlighter()
	
func _create_lua_highlighter():
	var h = CodeHighlighter.new()
	
	h.function_color = COLORS.FUNCTION
	h.number_color = COLORS.NUMBER
	h.symbol_color = COLORS.TEXT
	
	h.keyword_colors = {}
	for keyword in LUA_KEYWORDS:
		h.keyword_colors[keyword] = COLORS.KEYWORD

	h.color_regions = {
		"--[[ ]]": COLORS.COMMENT,
		"--  ": COLORS.COMMENT,
		"\" \"": COLORS.STRING,
		"' '": COLORS.STRING
	}

	return h

xetrics avatar Apr 17 '25 17:04 xetrics

Hey @xetrics, thanks for the example script.

I have ran into issues with setting up auto tab/auto brace completion. The pairs must be 1 character, and Lua uses keywords such as do/then.

Yeah, if this is a limitation of the CodeEdit itself, I don't think we'll be able to solve it easily. We could add support for that using code completion, but then people would have to accept the completion instead of it being automatic. Though I guess it would be quite annoying to try to write "document" or something like that and when you type "do" the editor adds an "end" automatically when you actually don't want that.

Just a side note, reading Godot's code for CodeEdit::add_auto_brace_completion_pair, the problem seems to be about using letters rather than because of the open/closing brace length > 1 character. Not that it matters, I was just curious.

gilzoide avatar Apr 18 '25 16:04 gilzoide

Hey folks! I just added LuaCodeEdit and LuaSyntaxHighlight to the project, so that people can better use Lua code editors in their projects.

Here's a glimpse of LuaSyntaxHighlight: changing lua_keyword_color sets the color for all Lua keywords, the same occurs for lua_member_keyword_color, string_color and comment_color. There's also the "Fill with Editor colors" tool button for copying the colors from the current script editor theme! Image

LuaCodeEdit doesn't do much yet, but when we add code completion to LuaLanguageExtension we'll hook the same code completion functionality to it, so I suggest you start using it already! Although not released yet, this is already in the main branch, you can download the latest build to try it out.

I hope they work for your use case, feel free to reopen this Issue or create new Discussions or Issues if you feel this functionality needs more features or fine tuning.

Cheers \o/

gilzoide avatar May 18 '25 19:05 gilzoide