face-hh / griddycode

A code editor made with Godot. Code has never been more lit!
Apache License 2.0
1.58k stars 101 forks source link

Syntax highlighting does not allow for non-alphanumeric charactes in function names #133

Open Shannarra opened 1 month ago

Shannarra commented 1 month ago

Describe the bug As part of adding Ruby support I came across multiple issues when it comes to syntax highlighting.

In some languages, functions can have non-alphanumeric characters as part of their names.

One of which is Ruby. In Ruby, your function names can end with an exclamation mark (!) or a question mark (?). This has a significance especially when it comes to semantics, as the naming of the function is part of the standard of writing Ruby code.

For example, the difference between:

arr = [1, 2, 3, 4]
arr.map(&:to_s)

and

arr = [1, 2, 3, 4]
arr.map!(&:to_s)

is that in the first example, the original array arr will not be modified, instead a modified copy of it will be returned, whereas in the second snippet, the base array will be modified and all it's items will be turned into strings.

Same for the ruby Array#empty? method, generally, method names ending with a question mark (?) should always return a boolean.

This issue holds true even though the piece of code that handles functions actually handles functions ending with ! and ?, the syntax highlighting does not work properly. And yes, the ! or ? is part of the function's name.

This issue holds true also for languages that support kebab-case function names (such as CSS (yes, the current css mode doesn't handle kebab-case), or any LISP-based programming language), or other weird function names.

Another example is Haskell and the way it handles function names, as it supports adding a ' at the end of a function name, for example:

fact :: Int -> Int 
fact 0 = 1 
fact n = n * fact ( n - 1 ) 

and re-writing the same function using guards, we can call it "fact prime", or in Haskell terms:

fact' :: Integer -> Integer 
fact' n | n == 0 = 1 
        | n /= 0 = n * fact' (n-1) 

The compilers will do just fine, but missing syntax highlighting can and will lead to inevitable confusion.

As a potential fix, I'd suggest maybe adding a way to extend or modify the regex Godot uses to detect function names, so that said functions can be detected on a language basis.