Issues getting an EditorSyntaxHighlighter to work

Godot Version

4.2.1 stable

Question

I’m currently trying to create an editor syntax highlighter in a plugin so that txt files I create are highlighted but I’m not able to get it to work

When I add prints to the three functions in TextHighlighter I can see that _get_name and _get_supported_languages are called when I open a new txt file but _get_line_syntax_highlighting never ends up being called

@tool
extends EditorPlugin

var text_highlighter: TextHighlighter

func _enter_tree():
	text_highlighter = TextHighlighter.new()
	EditorInterface.get_script_editor().register_syntax_highlighter(text_highlighter)


func _exit_tree() -> void:
	if is_instance_valid(text_highlighter):
		EditorInterface.get_script_editor().unregister_syntax_highlighter(text_highlighter)
		text_highlighter = null


class TextHighlighter extends EditorSyntaxHighlighter:
	func _get_name() -> String:
		return "Text"


	func _get_supported_languages() -> PackedStringArray:
		return ["TextFile"]


	func _get_line_syntax_highlighting(line) -> Dictionary:
		return {
			0: {
				"color": Color.BLUE
			}
		}

Did you select the Syntax Highlighter in the script editor under Edit -> Syntax Highlighter? It won’t be selected by default.

1 Like