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

[FEATURE] GDScript Code Highlighting Support #3222

Closed
DarienMC opened this issue Jan 31, 2025 · 10 comments
Closed

[FEATURE] GDScript Code Highlighting Support #3222

DarienMC opened this issue Jan 31, 2025 · 10 comments
Labels

Comments

@DarienMC
Copy link
Contributor

Is your feature request related to a problem? Please describe.

When I use write my notes from game development tutorials, I notice that there is no support for the Godot Engine, which uses GDScript.

Describe the solution you'd like

I'd like to type gdscript at the start of a fenced code block to get syntax highlighting for GDScript like we get here on GitHub below.

# Everything after "#" is a comment.
# A file is a class!

# (optional) icon to show in the editor dialogs:
@icon("res://path/to/optional/icon.svg")

# (optional) class definition:
class_name MyClass

# Inheritance:
extends BaseClass

@export var my_scene : PackedScene = preload("res://scenes/my_scene.tscn")

# Member variables.
var a = 5
var s = "Hello"
var arr = [1, 2, 3]
var dict = {"key": "value", 2: 3}
var other_dict = {key = "value", other_key = 2}
var typed_var: int
var inferred_type := "String"

@onready var my_sprite_2d : Sprite2D = $Sprite2D
@onready var my_sub_viewport : SubViewport = %HandsViewport

# Constants.
const ANSWER = 42
const THE_NAME = "Charly"

# Enums.
enum {UNIT_NEUTRAL, UNIT_ENEMY, UNIT_ALLY}
enum Named {THING_1, THING_2, ANOTHER_THING = -1}

# Built-in vector types.
var v2 = Vector2(1, 2)
var v3 = Vector3(1, 2, 3)


# Functions.
func some_function(param1, param2, param3):
	const local_const = 5

	if param1 < local_const:
		print(param1)
	elif param2 > 5:
		print(param2)
	else:
		print("Fail!")

	for i in range(20):
		print(i)

	while param2 != 0:
		param2 -= 1

	match param3:
		3:
			print("param3 is 3!")
		_:
			print("param3 is not 3!")

	var local_var = param1 + 3
	return local_var


# Functions override functions with the same name on the base/super class.
# If you still want to call them, use "super":
func something(p1, p2):
	super(p1, p2)


# It's also possible to call another function in the super class:
func other_something(p1, p2):
	super.something(p1, p2)


# Inner class
class Something:
	var a = 10


# Constructor
func _init():
	print("Constructed!")
	var lv = Something.new()
	print(lv.a)

Describe alternatives you've considered

Using another note app or using screenshots from within the engine to paste inside my notes, but this adds friction to my workflow.

Additional context

I've actually already pulled the repository and attempted to implement this feature myself. Here is a screenshot below:

Image

With permission, I can create then push a branch called feature/gdscript_highlighting for review.

Output from the debug section in the settings dialog

Expand
@pbek
Copy link
Owner

pbek commented Jan 31, 2025

Sure, go on! Create a pull request! 👍️

@DarienMC
Copy link
Contributor Author

Awesome, I will do so after work, within 12 hours from now.

pbek added a commit to pbek/qmarkdowntextedit that referenced this issue Feb 1, 2025
…script_highlighting

Add GDScript Language Data for Markdown Highlighting.
pbek added a commit that referenced this issue Feb 1, 2025
Signed-off-by: Patrizio Bekerle <[email protected]>
@pbek
Copy link
Owner

pbek commented Feb 1, 2025

25.2.0

  • Added GDScript support to the note edit code block syntax highlighter
    (for #3222, thank you, @DarienMC)

@pbek
Copy link
Owner

pbek commented Feb 1, 2025

There now is a new release, could you please test it and report if it works for you?

@DarienMC
Copy link
Contributor Author

DarienMC commented Feb 1, 2025

Sure thing!

Pulling from the release branch to get version 25.2.0, then building and running it through Qt Creator. Everything seems to work as intended.

Image

Some nitpicks, but not dealbreakers.

How it is supposed to be highlighted below through the Godot VSCode extension:

Image

How the current implementation highlights these same lines:

Image

These are nitpicks though, and not even the GitHub markdown support does this. But, I'm sure this is an easy fix for anyone who wants to adjust some things.

I use the FlatPak version of QOwnNotes (through FlatHub), and I'll be making use of this feature somewhat frequently.

@pbek
Copy link
Owner

pbek commented Feb 2, 2025

Great, thank you for testing!

How the current implementation highlights these same lines:

What was the difference that was relevant for you?

@pbek pbek closed this as completed Feb 2, 2025
@DarienMC
Copy link
Contributor Author

DarienMC commented Feb 2, 2025

Thank you,

The difference that is relevant for me is the following:

Both $NodePath constructs and %UniqueNode are supposed to be highlighted as literals.

In the Godot game engine, everything within a scene is organized into a tree of "Nodes" that you can then reference in GDScript using these constructs.

A common example: say we have a scene with three nodes:

  1. a root node named CharacterBody2D of type CharacterBody2D
  2. a child node HurtBox of type Area2D
  3. its child node is a node named CollisonShape2D of type CollisionShape2D

If we were to attach a script to the root node, then wanted to reference its child node HurtBox anywhere in that script, we can use the construct $HurtBox. It would be all highlighted as a literal.

If we were to then try to reference the CollisionShape2D node, we can use the forward slash '/' in a similar way to how file systems work. Therefore, we can use the construct $HurtBox/CollisionShape2D. It would be all highlighted as a literal, including the forward slash '/'.

Say the NodePath above is deemed to long. Some scenes will have modules nested deep within their node tree that we want the root node to reference. We can then use a %UniqueNode construct to reference any node in a scene tree a that was given a unique identifier in the scene tree editor.

If we were to assign the CollisionShape2D node a unique name "ThisIsAUniqueName", we can then reference this node in our script as %ThisIsAUniqueName, highlighted as a literal.

@pbek
Copy link
Owner

pbek commented Feb 2, 2025

Is there anything you can change in the highlighting?

@DarienMC
Copy link
Contributor Author

DarienMC commented Feb 3, 2025

I should be able to work on it by next weekend, as I've just been busy lately (currently at work, away from home). I never coded in C++, let alone used Qt Creator before so I've used this as a learning experience over the weekend. Everything I did in my contribution was just from recognizing patterns in the existing code and going from there.

I know this problem should be easy as it's a matter of iterating over characters and checking whether or not a space, newline, or period is found.

@pbek
Copy link
Owner

pbek commented Feb 3, 2025

Everything I did in my contribution was just from recognizing patterns in the existing code and going from there.

That's how we can learn! 😁👍️
Have fun! 😉

pbek added a commit that referenced this issue Feb 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants