Skip to content

Commit

Permalink
Fixed reopening files from the previous session in the editor's text …
Browse files Browse the repository at this point in the history
…editor module. Also small cleanups.
  • Loading branch information
Relintai committed Jan 12, 2024
1 parent 7747a4e commit b9a0b82
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
28 changes: 21 additions & 7 deletions editor_modules/text_editor/text_editor_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "text_editor_vanilla_editor.h"

void TextEditorSettings::store_opened_files(ItemList *filecontainer) {
ERR_FAIL_COND(!filecontainer);

Array arr;

for (int child = 0; child < filecontainer->get_item_count(); ++child) {
Expand Down Expand Up @@ -71,40 +73,52 @@ void TextEditorSettings::remove_opened_file(const int index, ItemList *fileconta
for (int i = 0; i < arr.size(); ++i) {
Array a = arr[i];

if (a.size() < 2) {
continue;
}

if (a[0] == f) {
arr.remove(i);
break;
}
}

EditorSettings::get_singleton()->set_project_metadata("file_editor", "files", arr);
Dictionary fonts_dict = EditorSettings::get_singleton()->get_project_metadata("file_editor", "file_fonts", Dictionary());

/*
Dictionary fonts_dict = EditorSettings::get_singleton()->get_project_metadata("file_editor", "file_fonts", Dictionary());
if (fonts_dict.has(f)) {
fonts_dict.erase(f);
EditorSettings::get_singleton()->set_project_metadata("file_editor", "file_fonts", fonts_dict);
}
*/
}

Array TextEditorSettings::load_opened_files() {
Array arr = EditorSettings::get_singleton()->get_project_metadata("file_editor", "files", Array());
Dictionary fonts_dict = EditorSettings::get_singleton()->get_project_metadata("file_editor", "file_fonts", Dictionary());
Array keys;

for (int i = 0; i < arr.size(); ++i) { //i in range(arr.size())
for (int i = 0; i < arr.size(); ++i) {
Array a = arr[i];

if (a.size() < 2) {
continue;
}

// creating and returning an Array with this format [1:file name, 2:file path, 3:file font];
Array k;
k.push_back(a[0]);
k.push_back(a[1]);

if (fonts_dict.has(a[0])) {
k.push_back(fonts_dict[a[0]]);
}

else {
/*
if (fonts_dict.has(a[2])) {
k.push_back(fonts_dict[a[2]]);
} else {
k.push_back("null");
}
*/

keys.append(k);
}
Expand Down
20 changes: 13 additions & 7 deletions editor_modules/text_editor/text_file_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void TextFileEditor::_on_font_selected(const String &font_path) {
current_editor->set_font(font_path);
}

last_opened_files->store_editor_fonts(current_file_path.get_file(), font_path);
_text_editor_settings->store_editor_fonts(current_file_path.get_file(), font_path);
}

void TextFileEditor::_on_fileitem_pressed(const int index) {
Expand Down Expand Up @@ -250,15 +250,18 @@ void TextFileEditor::open_file(const String &path, const String &font) {
current_file_path = path;
TextEditorVanillaEditor *vanilla_editor = open_in_vanillaeditor(path);

/*
Ref<Font> edf = vanilla_editor->get("custom_fonts/font");
//TODO this logic seems wrong
if (font != "null" && edf.is_valid()) {
vanilla_editor->set_font(font);
}
*/

generate_file_item(path, vanilla_editor);
last_opened_files->store_opened_files(_open_file_list);

_text_editor_settings->store_opened_files(_open_file_list);

current_editor->show();
}
Expand Down Expand Up @@ -324,7 +327,7 @@ void TextFileEditor::close_file(const int index) {
void TextFileEditor::confirm_close(const int index) {
ERR_FAIL_INDEX(index, _open_file_list->get_item_count());

last_opened_files->remove_opened_file(index, _open_file_list);
_text_editor_settings->remove_opened_file(index, _open_file_list);
_open_file_list->remove_item(index);
open_file_name->clear();
current_editor->queue_delete();
Expand Down Expand Up @@ -435,7 +438,6 @@ void TextFileEditor::clean_editor() {
current_file_path = "";
open_file_name->clear();
_open_file_list->clear();
last_opened_files->store_opened_files(_open_file_list);
}

void TextFileEditor::csv_preview() {
Expand Down Expand Up @@ -548,7 +550,7 @@ void TextFileEditor::_on_ConfirmationDialog_confirmed() {
}

TextFileEditor::TextFileEditor() {
last_opened_files.instance();
_text_editor_settings.instance();

DIRECTORY = "res://";
EXCEPTIONS = "addons";
Expand Down Expand Up @@ -776,12 +778,16 @@ void TextFileEditor::_notification(int p_what) {
clean_editor();
connect_signals();

Array opened_files = last_opened_files->load_opened_files();
Array opened_files = _text_editor_settings->load_opened_files();

for (int i = 0; i < opened_files.size(); ++i) {
Array opened_file = opened_files[i];

open_file(opened_file[1], opened_file[2]);
if (opened_file.size() < 2) {
continue;
}

open_file(opened_file[0], opened_file[1]);
}

file_list->set_filters(EXTENSIONS);
Expand Down
2 changes: 1 addition & 1 deletion editor_modules/text_editor/text_file_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class TextFileEditor : public Control {
ConfirmationDialog *confirmation_close;
FileDialog *select_font_dialog;

Ref<TextEditorSettings> last_opened_files;
Ref<TextEditorSettings> _text_editor_settings;

Array directories;
Array files;
Expand Down

0 comments on commit b9a0b82

Please sign in to comment.