How can I use CodeHighlighter Resource?

Godot Version

v4.4.1.stable.mono.official [49a5bc7b6]

Question

That’s all. The layout doesn’t look intuitive and I can’t seem to find any tutorial online. Thank you in advance for your help!

You’ll need to create a script that extends CodeHighlighter and implements the different colors you want to have and use it when needed.

Example:

extends Node

@onready var code_edit: CodeEdit = $CodeEdit


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


class Highlighter extends CodeHighlighter:

	func _init() -> void:
		function_color = Color.RED
		number_color = Color.DARK_KHAKI
		symbol_color = Color.YELLOW
		member_variable_color = Color.GREEN

		# Color for the different keywords
		add_keyword_color("var", Color.CORAL)
		add_keyword_color("for", Color.CORAL)
		add_keyword_color("in", Color.CORAL)

		# Just making print() an special keyword and not a function
		add_keyword_color("print", Color.DARK_MAGENTA)

		# Color for string literals
		add_color_region('"', '"', Color.AQUAMARINE, false)
		# Color for comments
		add_color_region("//", "", Color.GRAY, true)

Result:

This uses an inner class so it’s easier to showcase. You can use a new script and give it a class_name so you can use it in the editor directly.

2 Likes

https://docs.godotengine.org/en/4.4/classes/class_codehighlighter.html

Maybe as a subnode of TextEdit?