Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add_keyword_color in textedit does not works using symbols ([ < > ] ....) #30586

Open
newold3 opened this issue Jul 14, 2019 · 6 comments
Open

Comments

@newold3
Copy link

newold3 commented Jul 14, 2019

Godot version:
3.1

OS/device including version:
Windows 10 64 Bits

Issue description:
using command add_keyword_color from textEdit does not works if you use symbols

Steps to reproduce:

Minimal reproduction project:

var textEdit = $textEdit # node text edit
textEdit.add_keyword_color("[", Color(1,1,0,1)) # Does not works
textEdit.add_keyword_color("<", Color(0,1,1,1)) # Does not works
textEdit.add_keyword_color("A", Color(1,1,0,1)) # This works
textEdit.add_keyword_color("8", Color(1,1,0,1)) # This works
@tom-jk
Copy link

tom-jk commented Aug 1, 2020

Can reproduce in 3.2.3.rc3.

More comprehensive test (attach as script to a textedit):

extends TextEdit


func _ready():
	var chars = ""
	for c in range(32, 256):
		chars += char(c)
	
	for c in chars:
		add_keyword_color(c, Color(0.3,0.6,0.9,1))
		#prints(ord(c), c, has_keyword_color(c), get_keyword_color(c))
	
	for c in chars:
		text += c + " "
	
	yield(get_tree(), "idle_frame")
	update()

@BenStigsen
Copy link

I'm having the same problem with version 3.2 on Solus OS.

@abstractqqq
Copy link

please fix this bug...

@Calinou
Copy link
Member

Calinou commented Jul 29, 2021

This likely occurs because symbol color takes precedence over keyword color. I guess we should change syntax highlighting to highlight keywords after symbols.

please fix this bug...

Bugs are fixed on a best-effort basis (in other words, when a contributor knows how to resolve them).

@Calinou Calinou modified the milestone: 3.4 Jul 29, 2021
@abstractqqq
Copy link

This likely occurs because symbol color takes precedence over keyword color. I guess we should change syntax highlighting to highlight keywords after symbols.

please fix this bug...

Bugs are fixed on a best-effort basis (in other words, when a contributor knows how to resolve them).

Thank you for the response!

@S0yKaf
Copy link
Contributor

S0yKaf commented Dec 9, 2022

If people are looking for a solution to this problem. there is one very hacky way to do it that works. It involves disabling syntax highlighting and using draw_string() inside the TextEdit node.

Here's a simple implementation of this hack. which can be scaled to handle any amount of additional highlights you want:

# This is dependent on the font you're using it's not too long to figure out the values
var first_char = Vector2(48,29)
var line_height = 33
var character_length = 14
var indexed_text = []
var highlights = []

# I'm writing everything in process for clarity sake, do this with the text_changed() signal instead.
func _process(delta):
    var highlights = []
    var indexed_text = text.split("\n") # this lets us keep track of which line the text is on.
    var regex = RegEx.new()
    regex.compile("(\\$\\w*)") # match any word that starts with $
    var line_number = 0
    # for every line look for the matching text and add it to an array
    for line in indexed_text:
        var results = regex.search_all(line)
        for r in results:
            # keep track of where the text is. this is for the drawing step
            highlight_cache.append({
                    "word": r.get_string(0),
                    "color": Color.green,
                    "character": r.get_start(0),
                    "line": line_number
                })
        line_number += 1

    update()
    
func _draw():
    var font: Font = get_font("font")
    # for every keyword/region we found using regex,
    # draw a new string over that word using the desired color.
    for word in highlight_cache:
        var line = word["line"]
        # this is only needed if you show line numbers, as the gutter expands
        # it throws off the drawing allignement.
        var character = word["character"] + (str(indexed_text.size()).length() - 2)
        # we need to be aware of the scrolling position as to not throw 
        # off the alignement again
        # one thing to note, vertical aligned is stored in lines
        # Horizontal is in pixels.
        var draw_pos = first_char + \
            Vector2(character_length * (character) - scroll_horizontal,
            line_height * (line-scroll_vertical))
        #draw the colored text over the not colored text position.
        draw_string(font,draw_pos, word['word'], word['color'])

and here's how it should look like. note I've got more conditions in there but as you can see, it works perfectly.
2022-12-09-151651_732x355_scrot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants