How To Set Member Function Colour Inside CodeHighlighter?

Godot Version

4.4

Question

I have been trying to make a CodeHighlighter resource to match the Godot code highlighter as close as I can, and have nearly gotten there except for two things, one of which is that member functions and normal functions have the same colour:

(Godot’s highlighter in the editor, then my recreation.)


My recreation
(By ‘member functions’ I mean the functions like ‘node_signal.disconnect()’ opposed to function names like ‘disconnect_all_connections’)

Is there a way to do this like how the Godot highlighter does? The CodeHighlighter has a ‘Member Variable’ colour setting, but doesn’t seem to have the equivalent for functions.
So am I missing something or is there a way to achieve the same result without having to keyword every function that we call to set the colour?

Thanks.

Doesn’t CodeHighlighter.function_color work?

GDScript has 2 colors for functions. The lighter blue when defining them (like func _ready():) and the darker one when they aren’t.

If you want the same behavior I think you’ll need to implement it in SyntaxHighlighter._get_line_syntax_highlighting() for the one that define them.

Since I want it just for the ones that as you said are darker when they are not defining them, since that is blanket for all the functions.

Is there documentation for how the default data is acquired?

Since I would want to keep nearly all of the default data (Since if I can do this I could change the default function colour to the darker one and then change the highlighting only for the defining line), but I can’t see if there is a way to modify the returned data opposed to fully overriding it.

Thanks.

I don’t think that’s possible. You should probably open a proposal here with the changes you need.

There’s a cursed workaround. Using another TextEdit to get the highlight of the line and modify that…:

extends Node

@onready var code_edit: CodeEdit = $CodeEdit

func _ready() -> void:
	var highlighter = Highlighter.new()
	code_edit.syntax_highlighter = highlighter


class Highlighter extends CodeHighlighter:

	var fb_text_edit:TextEdit

	func _init() -> void:
		fb_text_edit = TextEdit.new()
		var other = CodeHighlighter.new()
		fb_text_edit.syntax_highlighter = other
		other.function_color = Color.RED
		other.number_color = Color.BLUE
		other.symbol_color = Color.GREEN

		other.add_keyword_color("func", Color.YELLOW)
		other.add_keyword_color("true", Color.CYAN)
		other.add_color_region('"', '"', Color.BLUE_VIOLET)



	func _get_line_syntax_highlighting(line: int) -> Dictionary:
		var text_edit = get_text_edit()
		fb_text_edit.text = text_edit.text
		var text = text_edit.get_line(line)
		var highlight = fb_text_edit.syntax_highlighter.get_line_syntax_highlighting(line)
		if text.begins_with("func"):
			for idx in highlight.keys():
				var value = highlight.get(idx)
				if value.color == Color.RED:
					value.color = Color.DARK_GOLDENROD
					
		return highlight

I mean… it works? I guess? :person_shrugging:

1 Like

It works great! The only annoying thing is that for this I had to re-create the CodeHighlighter I was using as a saved resource in the _init since when I tried using load() it was defaulting all the values, but after that it worked wonders!
Thank you so much!

Thanks, I will go do that now as it is weird this isn’t available already.

(Edit: Here is the issue I created)