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

Make code completion helper and syntax highlighter global #1697

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
offset_right = 428.0
offset_bottom = 128.0
size_flags_horizontal = 3
size_flags_vertical = 3
script = ExtResource("1_76vf2")

[node name="Label3" type="Label" parent="."]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extends DialogicCharacterEditorMainSection

func _ready() -> void:
# Connecting all necessary signals
%ColorPickerButton.custom_minimum_size.x = DialogicUtil.get_editor_scale()*30
%ColorPickerButton.color_changed.connect(character_editor.something_changed)
%DisplayNameLineEdit.text_changed.connect(character_editor.something_changed)
%NicknameLineEdit.text_changed.connect(character_editor.something_changed)
Expand Down
15 changes: 8 additions & 7 deletions addons/dialogic/Editor/Events/Fields/MultilineText.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ extends CodeEdit
var property_name : String
signal value_changed

@onready var code_completion_helper :Node= find_parent('EditorsManager').get_node('CodeCompletionHelper')

func _ready() -> void:
text_changed.connect(_on_text_changed)
syntax_highlighter = load('res://addons/dialogic/Editor/TimelineEditor/TextEditor/syntax_highlighter.gd').new()
syntax_highlighter.mode = syntax_highlighter.Modes.TEXT_EVENT_ONLY
syntax_highlighter = code_completion_helper.syntax_highlighter


func _on_text_changed(value := "") -> void:
Expand All @@ -31,19 +32,19 @@ func take_autofocus() -> void:

# Called if something was typed
func _request_code_completion(force:bool):
$CodeCompletionHelper.request_code_completion(force, self)
code_completion_helper.request_code_completion(force, self, 0)


# Filters the list of all possible options, depending on what was typed
# Purpose of the different Kinds is explained in [_request_code_completion]
func _filter_code_completion_candidates(candidates:Array) -> Array:
return $CodeCompletionHelper.filter_code_completion_candidates(candidates, self)
return code_completion_helper.filter_code_completion_candidates(candidates, self)


# Called when code completion was activated
# Inserts the selected item
func _confirm_code_completion(replace:bool) -> void:
$CodeCompletionHelper.confirm_code_completion(replace, self)
code_completion_helper.confirm_code_completion(replace, self)


################################################################################
Expand All @@ -52,9 +53,9 @@ func _confirm_code_completion(replace:bool) -> void:

# Performs an action (like opening a link) when a valid symbol was clicked
func _on_symbol_lookup(symbol, line, column):
$CodeCompletionHelper.symbol_lookup(symbol, line, column)
code_completion_helper.symbol_lookup(symbol, line, column)


# Called to test if a symbol can be clicked
func _on_symbol_validate(symbol:String) -> void:
$CodeCompletionHelper.symbol_validate(symbol, self)
code_completion_helper.symbol_validate(symbol, self)
13 changes: 4 additions & 9 deletions addons/dialogic/Editor/Events/Fields/MultilineText.tscn
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
[gd_scene load_steps=6 format=3 uid="uid://dyp7m2nvab1aj"]
[gd_scene load_steps=5 format=3 uid="uid://dyp7m2nvab1aj"]

[ext_resource type="Script" path="res://addons/dialogic/Editor/Events/Fields/MultilineText.gd" id="1"]
[ext_resource type="StyleBox" path="res://addons/dialogic/Editor/Events/styles/TextBackground.tres" id="1_dkxlh"]
[ext_resource type="StyleBox" uid="uid://cu8otiwksn8ma" path="res://addons/dialogic/Editor/Events/styles/TextBackground.tres" id="1_dkxlh"]
[ext_resource type="Script" path="res://addons/dialogic/Editor/TimelineEditor/TextEditor/syntax_highlighter.gd" id="1_wj4ha"]
[ext_resource type="Script" path="res://addons/dialogic/Editor/TimelineEditor/TextEditor/CodeCompletionHelper.gd" id="3_mmga0"]

[sub_resource type="SyntaxHighlighter" id="SyntaxHighlighter_42qr1"]
[sub_resource type="SyntaxHighlighter" id="SyntaxHighlighter_2q5dk"]
script = ExtResource("1_wj4ha")

[node name="MultilineText" type="CodeEdit"]
Expand All @@ -15,7 +14,7 @@ size_flags_horizontal = 3
size_flags_vertical = 3
theme_override_styles/normal = ExtResource("1_dkxlh")
wrap_mode = 1
syntax_highlighter = SubResource("SyntaxHighlighter_42qr1")
syntax_highlighter = SubResource("SyntaxHighlighter_2q5dk")
scroll_fit_content_height = true
symbol_lookup_on_click = true
delimiter_strings = Array[String]([])
Expand All @@ -28,7 +27,3 @@ auto_brace_completion_pairs = {
"{": "}"
}
script = ExtResource("1")

[node name="CodeCompletionHelper" type="Node" parent="."]
script = ExtResource("3_mmga0")
mode = 0
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
extends Node

enum Modes {TEXT_EVENT_ONLY, FULL_HIGHLIGHTING}
@export var mode := Modes.FULL_HIGHLIGHTING

var syntax_highlighter :SyntaxHighlighter = load("res://addons/dialogic/Editor/TimelineEditor/TextEditor/syntax_highlighter.gd").new()

# These RegEx's are used to deduce information from the current line for auto-completion

Expand Down Expand Up @@ -53,14 +54,14 @@ func get_line_untill_caret(line:String) -> String:
# Adds all kinds of options depending on the
# content of the current line, the last word and the symbol that came before
# Triggers opening of the popup
func request_code_completion(force:bool, text:CodeEdit) -> void:
func request_code_completion(force:bool, text:CodeEdit, mode:=Modes.FULL_HIGHLIGHTING) -> void:
## TODO remove this once https://github.com/godotengine/godot/issues/38560 is fixed
if mode != Modes.FULL_HIGHLIGHTING:
return

# make sure shortcode event references are loaded
if mode == Modes.FULL_HIGHLIGHTING:
var hidden_events :Array= DialogicUtil.get_editor_setting('hidden_event_buttons')
var hidden_events :Array= DialogicUtil.get_editor_setting('hidden_event_buttons', [])
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this and everything you do! It's a big help!

I was getting a frequent error when typing, so I'm glad to see the fix is in main as well!

Screenshot from 2023-09-12 17-03-25

if shortcode_events.is_empty():
for event in text.timeline_editor.editors_manager.resource_helper.event_script_cache:
if event.get_shortcode() != 'default_shortcode':
Expand Down Expand Up @@ -107,9 +108,9 @@ func request_code_completion(force:bool, text:CodeEdit) -> void:
if shortcode_events[shortcode].get_meta('hidden', false):
continue
if shortcode_events[shortcode].get_shortcode_parameters().is_empty():
text.add_code_completion_option(CodeEdit.KIND_MEMBER, shortcode, shortcode, shortcode_events[shortcode].event_color.lerp(text.syntax_highlighter.normal_color, 0.3), shortcode_events[shortcode]._get_icon())
text.add_code_completion_option(CodeEdit.KIND_MEMBER, shortcode, shortcode, shortcode_events[shortcode].event_color.lerp(syntax_highlighter.normal_color, 0.3), shortcode_events[shortcode]._get_icon())
else:
text.add_code_completion_option(CodeEdit.KIND_MEMBER, shortcode, shortcode+" ", shortcode_events[shortcode].event_color.lerp(text.syntax_highlighter.normal_color, 0.3), shortcode_events[shortcode]._get_icon())
text.add_code_completion_option(CodeEdit.KIND_MEMBER, shortcode, shortcode+" ", shortcode_events[shortcode].event_color.lerp(syntax_highlighter.normal_color, 0.3), shortcode_events[shortcode]._get_icon())
else:
var current_shortcode := completion_shortcode_getter_regex.search(line)
if !current_shortcode:
Expand All @@ -126,7 +127,7 @@ func request_code_completion(force:bool, text:CodeEdit) -> void:
var parameters :Array = shortcode_events[code].get_shortcode_parameters().keys()
for param in parameters:
if !param+'=' in line:
text.add_code_completion_option(CodeEdit.KIND_MEMBER, param, param+'="' , shortcode_events[code].event_color.lerp(text.syntax_highlighter.normal_color, 0.3), text.get_theme_icon("MemberProperty", "EditorIcons"))
text.add_code_completion_option(CodeEdit.KIND_MEMBER, param, param+'="' , shortcode_events[code].event_color.lerp(syntax_highlighter.normal_color, 0.3), text.get_theme_icon("MemberProperty", "EditorIcons"))

# suggest values
elif symbol == '=' or symbol == '"' or get_code_completion_prev_symbol(text) == '"':
Expand All @@ -141,18 +142,19 @@ func request_code_completion(force:bool, text:CodeEdit) -> void:
return
if !shortcode_events[code].get_shortcode_parameters()[current_parameter].has('suggestions'):
if typeof(shortcode_events[code].get_shortcode_parameters()[current_parameter].default) == TYPE_BOOL:
suggest_bool(text, shortcode_events[code].event_color.lerp(text.syntax_highlighter.normal_color, 0.3))
suggest_bool(text, shortcode_events[code].event_color.lerp(syntax_highlighter.normal_color, 0.3))
elif len(word) > 0:
text.add_code_completion_option(CodeEdit.KIND_MEMBER, word, word, shortcode_events[code].event_color.lerp(text.syntax_highlighter.normal_color, 0.3), text.get_theme_icon("GuiScrollArrowRight", "EditorIcons"), '" ')
text.add_code_completion_option(CodeEdit.KIND_MEMBER, word, word, shortcode_events[code].event_color.lerp(syntax_highlighter.normal_color, 0.3), text.get_theme_icon("GuiScrollArrowRight", "EditorIcons"), '" ')
text.update_code_completion_options(true)
return

var suggestions : Dictionary= shortcode_events[code].get_shortcode_parameters()[current_parameter]['suggestions'].call()
for key in suggestions.keys():
if suggestions[key].has('text_alt'):
text.add_code_completion_option(CodeEdit.KIND_MEMBER, key, suggestions[key].text_alt[0], shortcode_events[code].event_color.lerp(text.syntax_highlighter.normal_color, 0.3), suggestions[key].get('icon', null), '" ')
text.add_code_completion_option(CodeEdit.KIND_MEMBER, key, suggestions[key].text_alt[0], shortcode_events[code].event_color.lerp(syntax_highlighter.normal_color, 0.3), suggestions[key].get('icon', null), '" ')
else:
text.add_code_completion_option(CodeEdit.KIND_MEMBER, key, str(suggestions[key].value), shortcode_events[code].event_color.lerp(text.syntax_highlighter.normal_color, 0.3), suggestions[key].get('icon', null), '" ')
text.add_code_completion_option(CodeEdit.KIND_MEMBER, key, str(suggestions[key].value), shortcode_events[code].event_color.lerp(syntax_highlighter.normal_color, 0.3), suggestions[key].get('icon', null), '" ')


# Force update and showing of the popup
text.update_code_completion_options(true)
Expand All @@ -179,9 +181,9 @@ func request_code_completion(force:bool, text:CodeEdit) -> void:
func suggest_characters(text:CodeEdit, type := CodeEdit.KIND_MEMBER, text_event_start:=false) -> void:
for character in text.timeline_editor.editors_manager.resource_helper.character_directory:
if text_event_start and text.timeline_editor.editors_manager.resource_helper.character_directory[character].resource.portraits.is_empty():
text.add_code_completion_option(type, character, character+': ', text.syntax_highlighter.character_name_color, load("res://addons/dialogic/Editor/Images/Resources/character.svg"))
text.add_code_completion_option(type, character, character+': ', syntax_highlighter.character_name_color, load("res://addons/dialogic/Editor/Images/Resources/character.svg"))
else:
text.add_code_completion_option(type, character, character, text.syntax_highlighter.character_name_color, load("res://addons/dialogic/Editor/Images/Resources/character.svg"))
text.add_code_completion_option(type, character, character, syntax_highlighter.character_name_color, load("res://addons/dialogic/Editor/Images/Resources/character.svg"))

# Helper that adds all timelines as options
func suggest_timelines(text:CodeEdit, type := CodeEdit.KIND_MEMBER, color:=Color()) -> void:
Expand All @@ -201,15 +203,15 @@ func suggest_portraits(text:CodeEdit, character_name:String, end_check:=')') ->
return
var character_resource :DialogicCharacter= text.timeline_editor.editors_manager.resource_helper.character_directory[character_name]['resource']
for portrait in character_resource.portraits:
text.add_code_completion_option(CodeEdit.KIND_MEMBER, portrait, portrait, text.syntax_highlighter.character_portrait_color, load("res://addons/dialogic/Editor/Images/Resources/character.svg"), end_check)
text.add_code_completion_option(CodeEdit.KIND_MEMBER, portrait, portrait, syntax_highlighter.character_portrait_color, load("res://addons/dialogic/Editor/Images/Resources/character.svg"), end_check)
if character_resource.portraits.is_empty():
text.add_code_completion_option(CodeEdit.KIND_MEMBER, 'Has no portraits!', '', text.syntax_highlighter.character_portrait_color, load("res://addons/dialogic/Editor/Images/Pieces/warning.svg"))
text.add_code_completion_option(CodeEdit.KIND_MEMBER, 'Has no portraits!', '', syntax_highlighter.character_portrait_color, load("res://addons/dialogic/Editor/Images/Pieces/warning.svg"))


# Helper that adds all variable paths as options
func suggest_variables(text:CodeEdit):
for variable in DialogicUtil.list_variables(ProjectSettings.get_setting('dialogic/variables')):
text.add_code_completion_option(CodeEdit.KIND_MEMBER, variable, variable, text.syntax_highlighter.variable_color, text.get_theme_icon("MemberProperty", "EditorIcons"), '}')
text.add_code_completion_option(CodeEdit.KIND_MEMBER, variable, variable, syntax_highlighter.variable_color, text.get_theme_icon("MemberProperty", "EditorIcons"), '}')


# Helper that adds true and false as options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var shortcode_events := {}
var custom_syntax_events := []
var text_event :DialogicTextEvent = null


func _init():
# Load colors from editor settings
if DialogicUtil.get_dialogic_plugin():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ extends CodeEdit
## Sub-Editor that allows editing timelines in a text format.

@onready var timeline_editor := get_parent().get_parent()
@onready var code_completion_helper :Node= find_parent('EditorsManager').get_node('CodeCompletionHelper')

var label_regex := RegEx.create_from_string('label +(?<name>[^\n]+)')

func _ready():
syntax_highlighter = load("res://addons/dialogic/Editor/TimelineEditor/TextEditor/syntax_highlighter.gd").new()

await find_parent('EditorView').ready
syntax_highlighter = code_completion_helper.syntax_highlighter
timeline_editor.editors_manager.sidebar.content_item_activated.connect(_on_content_item_clicked)

func _on_text_editor_text_changed():
Expand Down Expand Up @@ -173,7 +173,6 @@ func _drop_data(at_position:Vector2, data:Variant) -> void:
insert_text_at_caret('"'+data.files[0]+'"')



func _on_update_timer_timeout():
update_content_list()

Expand Down Expand Up @@ -206,19 +205,19 @@ func _on_content_item_clicked(label:String) -> void:

# Called if something was typed
func _request_code_completion(force:bool):
$CodeCompletionHelper.request_code_completion(force, self)
code_completion_helper.request_code_completion(force, self)


# Filters the list of all possible options, depending on what was typed
# Purpose of the different Kinds is explained in [_request_code_completion]
func _filter_code_completion_candidates(candidates:Array) -> Array:
return $CodeCompletionHelper.filter_code_completion_candidates(candidates, self)
return code_completion_helper.filter_code_completion_candidates(candidates, self)


# Called when code completion was activated
# Inserts the selected item
func _confirm_code_completion(replace:bool) -> void:
$CodeCompletionHelper.confirm_code_completion(replace, self)
code_completion_helper.confirm_code_completion(replace, self)


################################################################################
Expand All @@ -227,9 +226,9 @@ func _confirm_code_completion(replace:bool) -> void:

# Performs an action (like opening a link) when a valid symbol was clicked
func _on_symbol_lookup(symbol, line, column):
$CodeCompletionHelper.symbol_lookup(symbol, line, column)
code_completion_helper.symbol_lookup(symbol, line, column)


# Called to test if a symbol can be clicked
func _on_symbol_validate(symbol:String) -> void:
$CodeCompletionHelper.symbol_validate(symbol, self)
code_completion_helper.symbol_validate(symbol, self)
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
[gd_scene load_steps=5 format=3 uid="uid://defdeav8rli6o"]
[gd_scene load_steps=2 format=3 uid="uid://defdeav8rli6o"]

[ext_resource type="Script" path="res://addons/dialogic/Editor/TimelineEditor/TextEditor/timeline_editor_text.gd" id="1_1kbx2"]
[ext_resource type="Script" path="res://addons/dialogic/Editor/TimelineEditor/TextEditor/syntax_highlighter.gd" id="1_5qfro"]
[ext_resource type="Script" path="res://addons/dialogic/Editor/TimelineEditor/TextEditor/CodeCompletionHelper.gd" id="3_3bgmj"]

[sub_resource type="SyntaxHighlighter" id="SyntaxHighlighter_yb1h4"]
script = ExtResource("1_5qfro")

[node name="TimelineTextEditor" type="CodeEdit"]
offset_top = 592.0
Expand All @@ -14,7 +9,6 @@ offset_bottom = 600.0
theme_override_constants/line_spacing = 10
highlight_current_line = true
draw_tabs = true
syntax_highlighter = SubResource("SyntaxHighlighter_yb1h4")
minimap_draw = true
caret_blink = true
line_folding = true
Expand All @@ -30,9 +24,6 @@ script = ExtResource("1_1kbx2")
[node name="UpdateTimer" type="Timer" parent="."]
one_shot = true

[node name="CodeCompletionHelper" type="Node" parent="."]
script = ExtResource("3_3bgmj")

[connection signal="code_completion_requested" from="." to="." method="_on_code_completion_requested"]
[connection signal="symbol_lookup" from="." to="." method="_on_symbol_lookup"]
[connection signal="symbol_validate" from="." to="." method="_on_symbol_validate"]
Expand Down
4 changes: 3 additions & 1 deletion addons/dialogic/Editor/TimelineEditor/test_timeline_scene.gd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ func _ready() -> void:
scene.position = get_viewport_rect().size/2.0

randomize()
var current_timeline: String = DialogicUtil.get_editor_setting('current_timeline_path')
var current_timeline: String = DialogicUtil.get_editor_setting('current_timeline_path', null)
if !current_timeline:
get_tree().quit()
Dialogic.start(current_timeline)
Dialogic.timeline_ended.connect(get_tree().quit)
Dialogic.signal_event.connect(recieve_event_signal)
Expand Down
Loading