From ed56385d140d57c6f8535abc8468f7b104f8ec6b Mon Sep 17 00:00:00 2001 From: Sebastian Morr Date: Fri, 25 Sep 2020 12:04:45 +0200 Subject: [PATCH] Basic completion framework --- terminal.gd | 55 ++++++++++++++++++++++++++++++++++++++++++++++++--- terminal.tscn | 33 +++++++++++++++++++++++++------ 2 files changed, 79 insertions(+), 9 deletions(-) diff --git a/terminal.gd b/terminal.gd index f5e40d1..1472b91 100644 --- a/terminal.gd +++ b/terminal.gd @@ -5,11 +5,12 @@ var thread var history = [] var history_position = 0 -onready var input = $Control/InputLine/Input -onready var output = $Control/Output +onready var input = $VBoxContainer/InputLine/Input +onready var output = $VBoxContainer/TopHalf/Output +onready var completions = $VBoxContainer/TopHalf/Completions export(NodePath) var repository_path onready var repository = get_node(repository_path) -onready var command_dropdown = $Control/InputLine/CommandDropdown +onready var command_dropdown = $VBoxContainer/InputLine/CommandDropdown onready var main = get_tree().get_root().get_node("Main") var premade_commands = [ @@ -89,3 +90,51 @@ func editor_closed(): func check_win_condition(): if repository.shell.run("bash /tmp/win 2>/dev/null >/dev/null && echo yes || echo no") == "yes\n": main.show_win_status() + +func regenerate_completions_menu(new_text): + var comp = generate_completions(new_text) + + completions.clear() + + var filtered_comp = [] + for c in comp: + if c != new_text: + filtered_comp.push_back(c) + + if filtered_comp.size() == 0: + completions.hide() + else: + completions.show() + + var _root = completions.create_item() + for c in filtered_comp: + var child = completions.create_item() + child.set_text(0, c) + +func generate_completions(command): + if command.substr(0, 4) == "git ": + var rest = command.substr(4) + var subcommands = [ + "commit", + "status", + "diff", + ] + + var results = [] + for sc in subcommands: + if sc.substr(0, rest.length()) == rest: + results.push_back("git "+sc) + + return results + return [] + +func _input_changed(new_text): + call_deferred("regenerate_completions_menu", new_text) + +func _completion_selected(): + var item = completions.get_selected() + input.text = item.get_text(0) + input.emit_signal("text_changed", input.text) + #completions.hide() + input.grab_focus() + input.caret_position = input.text.length() diff --git a/terminal.tscn b/terminal.tscn index 7010b04..978f2c3 100644 --- a/terminal.tscn +++ b/terminal.tscn @@ -27,27 +27,46 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="Control" type="VBoxContainer" parent="."] +[node name="VBoxContainer" type="VBoxContainer" parent="."] anchor_right = 1.0 anchor_bottom = 1.0 __meta__ = { "_edit_use_anchors_": false } -[node name="Output" type="RichTextLabel" parent="Control"] +[node name="TopHalf" type="Control" parent="VBoxContainer"] +margin_right = 1920.0 +margin_bottom = 1045.0 +size_flags_vertical = 3 + +[node name="Output" type="RichTextLabel" parent="VBoxContainer/TopHalf"] +margin_top = 4.0 margin_right = 1920.0 margin_bottom = 1045.0 size_flags_vertical = 3 custom_styles/normal = SubResource( 1 ) custom_fonts/normal_font = ExtResource( 4 ) scroll_following = true +__meta__ = { +"_edit_use_anchors_": false +} -[node name="InputLine" type="HBoxContainer" parent="Control"] +[node name="Completions" type="Tree" parent="VBoxContainer/TopHalf"] +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_top = -223.0 +hide_root = true +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="InputLine" type="HBoxContainer" parent="VBoxContainer"] margin_top = 1049.0 margin_right = 1920.0 margin_bottom = 1080.0 -[node name="Input" type="LineEdit" parent="Control/InputLine"] +[node name="Input" type="LineEdit" parent="VBoxContainer/InputLine"] margin_right = 1892.0 margin_bottom = 31.0 size_flags_horizontal = 3 @@ -56,7 +75,7 @@ __meta__ = { "_edit_use_anchors_": false } -[node name="CommandDropdown" type="MenuButton" parent="Control/InputLine"] +[node name="CommandDropdown" type="MenuButton" parent="VBoxContainer/InputLine"] margin_left = 1896.0 margin_right = 1920.0 margin_bottom = 31.0 @@ -86,6 +105,8 @@ mouse_filter = 1 syntax_highlighting = false [node name="TCPServer" parent="." instance=ExtResource( 3 )] -[connection signal="text_entered" from="Control/InputLine/Input" to="." method="send_command"] +[connection signal="item_selected" from="VBoxContainer/TopHalf/Completions" to="." method="_completion_selected"] +[connection signal="text_changed" from="VBoxContainer/InputLine/Input" to="." method="_input_changed"] +[connection signal="text_entered" from="VBoxContainer/InputLine/Input" to="." method="send_command"] [connection signal="pressed" from="ClearButton" to="." method="clear"] [connection signal="data_received" from="TCPServer" to="." method="receive_output"]