From c382671951c2621cd4aa7822c8e05670acc8b3b9 Mon Sep 17 00:00:00 2001 From: Relintai Date: Fri, 15 Jul 2022 12:30:34 +0200 Subject: [PATCH] Type information part1. --- addons/file-editor/scripts/FileEditor.gd | 56 ++++----- addons/file-editor/scripts/LastOpenedFiles.gd | 2 +- addons/file-editor/scripts/Preview.gd | 110 ++++++++++-------- addons/file-editor/scripts/VanillaEditor.gd | 43 +++---- 4 files changed, 118 insertions(+), 93 deletions(-) diff --git a/addons/file-editor/scripts/FileEditor.gd b/addons/file-editor/scripts/FileEditor.gd index 27ef90f..655c8bb 100644 --- a/addons/file-editor/scripts/FileEditor.gd +++ b/addons/file-editor/scripts/FileEditor.gd @@ -67,11 +67,11 @@ var LastOpenedFiles = null var Preview = preload("res://addons/file-editor/scripts/Preview.gd") var VanillaEditor = preload("res://addons/file-editor/scripts/VanillaEditor.gd") -var directories = [] -var files = [] -var current_file_index = -1 -var current_file_path = "" -var save_as = false +var directories : Array = [] +var files : Array = [] +var current_file_index : int = -1 +var current_file_path : String = "" +var save_as : bool = false var current_editor : Control var current_font : DynamicFont @@ -413,8 +413,8 @@ func _on_font_selected(font_path : String): func _on_fileitem_pressed(index : int): current_file_index = index - var selected_item_metadata = open_file_list.get_item_metadata(current_file_index) - var extension = selected_item_metadata[0].current_path.get_file().get_extension() + var selected_item_metadata : Array = open_file_list.get_item_metadata(current_file_index) + var extension : String = selected_item_metadata[0].current_path.get_file().get_extension() if open_file_list.get_item_text(current_file_index).begins_with("(*)"): editing_file = true @@ -445,7 +445,7 @@ func open_file(path : String, font : String = "null"): if current_file_path != path: current_file_path = path - var vanilla_editor = open_in_vanillaeditor(path) + var vanilla_editor : Control = open_in_vanillaeditor(path) if font != "null" and vanilla_editor.get("custom_fonts/font")!=null: vanilla_editor.set_font(font) @@ -466,7 +466,7 @@ func generate_file_item(path : String , veditor : Control): open_file_list.select(open_file_list.get_item_count()-1) func open_in_vanillaeditor(path : String) -> Control: - var editor = VanillaEditor.new() + var editor : Control = VanillaEditor.new() editor.LastOpenedFiles = LastOpenedFiles editor.file_list = file_list split_editor_container.add_child(editor,true) @@ -481,7 +481,7 @@ func open_in_vanillaeditor(path : String) -> Control: var current_file : File = File.new() current_file.open(path,File.READ) - var current_content = "" + var current_content : String = "" current_content = current_file.get_as_text() var last_modified = OS.get_datetime_from_unix_time(current_file.get_modified_time(path)) @@ -515,8 +515,8 @@ func _on_update_file(): var current_file : File = File.new() current_file.open(current_file_path,File.READ) - var current_content = current_file.get_as_text() - var last_modified = OS.get_datetime_from_unix_time(current_file.get_modified_time(current_file_path)) + var current_content : String = current_file.get_as_text() + var last_modified : Dictionary = OS.get_datetime_from_unix_time(current_file.get_modified_time(current_file_path)) current_file.close() @@ -539,9 +539,10 @@ func open_file_list(): file_list.set_position(OS.get_screen_size()/2 - file_list.get_size()/2) func create_new_file(given_path : String): - var current_file = File.new() + var current_file : File = File.new() current_file.open(given_path,File.WRITE) - if save_as : + + if save_as: current_file.store_line(current_editor.text_editor.get_text()) current_file.close() @@ -549,10 +550,10 @@ func create_new_file(given_path : String): func save_file(current_path : String): print("Saving file: ",current_path) - var current_file = File.new() + var current_file : File = File.new() current_file.open(current_path,File.WRITE) - var current_content = "" - var lines = current_editor.text_editor.get_line_count() + var current_content : String = "" + var lines : int = current_editor.text_editor.get_line_count() for line in range(0,lines): if current_editor.text_editor.get_line(line) == "": @@ -565,7 +566,7 @@ func save_file(current_path : String): current_file_path = current_path - var last_modified = OS.get_datetime_from_unix_time(current_file.get_modified_time(current_file_path)) + var last_modified : Dictionary = OS.get_datetime_from_unix_time(current_file.get_modified_time(current_file_path)) current_editor.update_lastmodified(last_modified,"save") @@ -579,7 +580,10 @@ func save_file(current_path : String): update_list() func clean_editor() -> void : - for vanillaeditor in get_tree().get_nodes_in_group("vanilla_editor"): + var nodes : Array = get_tree().get_nodes_in_group("vanilla_editor") + + for i in range(nodes.size()): + var vanillaeditor : Node = nodes[i] vanillaeditor.queue_free() open_file_name.clear() @@ -587,34 +591,34 @@ func clean_editor() -> void : func csv_preview(): - var preview = Preview.new() + var preview : Control = Preview.new() get_parent().get_parent().get_parent().add_child(preview) preview.popup() preview.window_title += " ("+current_file_path.get_file()+")" - var lines = current_editor.text_editor.get_line_count() - var rows = [] + var lines : int = current_editor.text_editor.get_line_count() + var rows : Array = Array() - for i in range(0,lines-1): + for i in range(0, lines-1): rows.append(current_editor.text_editor.get_line(i).rsplit(",",false)) preview.print_csv(rows) func bbcode_preview(): - var preview = Preview.new() + var preview : Control = Preview.new() get_parent().get_parent().get_parent().add_child(preview) preview.popup() preview.window_title += " ("+current_file_path.get_file()+")" preview.print_bb(current_editor.text_editor.get_text()) func markdown_preview(): - var preview = Preview.new() + var preview : Control = Preview.new() get_parent().get_parent().get_parent().add_child(preview) preview.popup() preview.window_title += " ("+current_file_path.get_file()+")" preview.print_markdown(current_editor.text_editor.get_text()) func html_preview(): - var preview = Preview.new() + var preview : Control = Preview.new() get_parent().get_parent().get_parent().add_child(preview) preview.popup() preview.window_title += " ("+current_file_path.get_file()+")" diff --git a/addons/file-editor/scripts/LastOpenedFiles.gd b/addons/file-editor/scripts/LastOpenedFiles.gd index a8386fb..cec32d0 100644 --- a/addons/file-editor/scripts/LastOpenedFiles.gd +++ b/addons/file-editor/scripts/LastOpenedFiles.gd @@ -66,6 +66,6 @@ func store_editor_fonts(file_name : String, font_path : String): editor_settings.set_project_metadata("file_editor", "file_fonts", fonts_dict) -func get_editor_font(): +func get_editor_font() -> String: #var editor_plugin : EditorPlugin = EditorPlugin.new() return editor_plugin.get_editor_interface().get_editor_settings().get_setting("interface/editor/code_font") diff --git a/addons/file-editor/scripts/Preview.gd b/addons/file-editor/scripts/Preview.gd index 1706616..6b83b14 100644 --- a/addons/file-editor/scripts/Preview.gd +++ b/addons/file-editor/scripts/Preview.gd @@ -47,90 +47,109 @@ func print_bb(content : String): text_preview.show() func print_markdown(content : String): - var result = "" - var bolded = [] - var italics = [] - var striked = [] - var coded = [] - var linknames = [] - var images = [] - var links = [] - var lists = [] - var underlined = [] + var result : Array = Array() + var bolded : Array = Array() + var italics : Array = Array() + var striked : Array = Array() + var coded : Array = Array() + var linknames : Array = Array() + var images : Array = Array() + var links : Array = Array() + var lists : Array = Array() + var underlined : Array = Array() - var regex = RegEx.new() + var regex : RegEx = RegEx.new() regex.compile('\\*\\*(?.*)\\*\\*') result = regex.search_all(content) - if result: - for res in result: - bolded.append(res.get_string("boldtext")) + for i in range(result.size()): + var res : RegExMatch = result[i] + bolded.append(res.get_string("boldtext")) regex.compile('\\_\\_(?.*)\\_\\_') result = regex.search_all(content) - if result: - for res in result: - underlined.append(res.get_string("underlinetext")) + for i in range(result.size()): + var res : RegExMatch = result[i] + underlined.append(res.get_string("underlinetext")) regex.compile("\\*(?.*)\\*") result = regex.search_all(content) - if result: - for res in result: - italics.append(res.get_string("italictext")) + for i in range(result.size()): + var res : RegExMatch = result[i] + italics.append(res.get_string("italictext")) regex.compile("~~(?.*)~~") result = regex.search_all(content) - if result: - for res in result: - striked.append(res.get_string("strikedtext")) + for i in range(result.size()): + var res : RegExMatch = result[i] + striked.append(res.get_string("strikedtext")) regex.compile("`(?.*)`") result = regex.search_all(content) - if result: - for res in result: - coded.append(res.get_string("coded")) + for i in range(result.size()): + var res : RegExMatch = result[i] + coded.append(res.get_string("coded")) regex.compile("[+-*](?\\s.*)") result = regex.search_all(content) - if result: - for res in result: - lists.append(res.get_string("element")) + for i in range(result.size()): + var res : RegExMatch = result[i] + lists.append(res.get_string("element")) regex.compile("(?!\\[.*?\\))") result = regex.search_all(content) - if result: - for res in result: - images.append(res.get_string("img")) + for i in range(result.size()): + var res : RegExMatch = result[i] + images.append(res.get_string("img")) regex.compile("\\[(?.*?)\\]|\\((?[h\\.]\\S*?)\\)") result = regex.search_all(content) - if result: - for res in result: - if res.get_string("link")!="": - links.append(res.get_string("link")) - if res.get_string("linkname")!="": - linknames.append(res.get_string("linkname")) + for i in range(result.size()): + var res : RegExMatch = result[i] + + if res.get_string("link")!="": + links.append(res.get_string("link")) + + if res.get_string("linkname")!="": + linknames.append(res.get_string("linkname")) - for bold in bolded: + for i in range(bolded.size()): + var bold : String = bolded[i] content = content.replace("**"+bold+"**","[b]"+bold+"[/b]") - for italic in italics: + + for i in range(italics.size()): + var italic : String = italics[i] content = content.replace("*"+italic+"*","[i]"+italic+"[/i]") - for strik in striked: + + for i in range(striked.size()): + var strik : String = striked[i] content = content.replace("~~"+strik+"~~","[s]"+strik+"[/s]") - for underline in underlined: + + for i in range(underlined.size()): + var underline : String = underlined[i] content = content.replace("__"+underline+"__","[u]"+underline+"[/u]") - for code in coded: + + for i in range(coded.size()): + var code : String = coded[i] content = content.replace("`"+code+"`","[code]"+code+"[/code]") - for image in images: + + for i in range(images.size()): + var image : String = images[i] var substr = image.split("(") var imglink = substr[1].rstrip(")") content = content.replace(image,"[img]"+imglink+"[/img]") + for i in links.size(): content = content.replace("["+linknames[i]+"]("+links[i]+")","[url="+links[i]+"]"+linknames[i]+"[/url]") - for element in lists: + + for i in range(lists.size()): + var element : String = lists[i] + if content.find("- "+element): content = content.replace("-"+element,"[indent]-"+element+"[/indent]") + if content.find("+ "+element): content = content.replace("+"+element,"[indent]-"+element+"[/indent]") + if content.find("* "+element): content = content.replace("+"+element,"[indent]-"+element+"[/indent]") @@ -175,7 +194,6 @@ func print_csv(rows : Array): label.set_valign(1) table_preview.add_child(label) - table_preview.show() func _on_Preview_popup_hide(): diff --git a/addons/file-editor/scripts/VanillaEditor.gd b/addons/file-editor/scripts/VanillaEditor.gd index 68e8d0c..981cbdf 100644 --- a/addons/file-editor/scripts/VanillaEditor.gd +++ b/addons/file-editor/scripts/VanillaEditor.gd @@ -1,12 +1,11 @@ tool extends VBoxContainer -var LastOpenedFiles = null +var LastOpenedFiles : Reference = null var text_editor : TextEdit = null var file_list : FileDialog = null -var ClosingFile var search_box : HBoxContainer = null var search_box_line_edit : LineEdit = null @@ -25,10 +24,10 @@ var file_info_last_modified : Label = null var file_info_c_counter : Label = null var file_info_read_only : CheckBox = null -var current_path = "" -var current_filename = "" +var current_path : String = "" +var current_filename : String = "" -var search_flag = 0 +var search_flag : int = 0 signal text_changed() @@ -191,8 +190,9 @@ func set_font(font_path : String) -> void: text_editor.set("custom_fonts/font",dynamic_font) func load_default_font() -> void: - var default_font = LastOpenedFiles.get_editor_font() - if default_font: + var default_font : String = LastOpenedFiles.get_editor_font() + + if default_font != "": set_font(default_font) func set_wrap_enabled(enabled:bool): @@ -295,19 +295,22 @@ func _on_text_editor_text_changed(): func count_characters(): var counted : int = 0 - for line in text_editor.get_line_count(): + + for line in range(text_editor.get_line_count()): counted += text_editor.get_line(line).length() file_info_c_counter.set_text(str(counted)) -func _on_LineEdit_text_changed(new_text): - var linecount = text_editor.get_line_count() +func _on_LineEdit_text_changed(new_text : String): + var linecount : int = text_editor.get_line_count() if new_text != "": - var found - var find = false - for line in range(0,linecount): - for column in range(0,text_editor.get_line(line).length()): + var found : PoolIntArray + var find : bool = false + + for line in range(linecount): + for column in range(text_editor.get_line(line).length()): found = text_editor.search( new_text, search_flag, line , column ) + if found.size(): if found[1] == line: # if not find: @@ -318,7 +321,7 @@ func _on_LineEdit_text_changed(new_text): else: text_editor.select(0,0,0,0) -func _on_matchcase_toggled(button_pressed): +func _on_matchcase_toggled(button_pressed : bool): if button_pressed: search_flag = 1 else: @@ -329,7 +332,7 @@ func _on_matchcase_toggled(button_pressed): _on_LineEdit_text_changed(search_box_line_edit.get_text()) -func _on_wholewords_toggled(button_pressed): +func _on_wholewords_toggled(button_pressed : bool): if button_pressed: search_flag = 2 else: @@ -351,10 +354,10 @@ func open_search_box(): search_box.get_node("LineEdit").grab_focus() func _on_Button_pressed(): - var linecount = text_editor.get_line_count()-1 - var old_text = replace_box_replace_le.get_text() - var new_text = replace_box_with.get_text() - var text = text_editor.get_text() + var linecount : int = text_editor.get_line_count()-1 + var old_text : String = replace_box_replace_le.get_text() + var new_text : String = replace_box_with.get_text() + var text : String = text_editor.get_text() text_editor.set_text(text.replace(old_text,new_text)) func open_replace_box():