Skip to content

Commit

Permalink
Add script formatter, comment parsing in parser, and editor settings
Browse files Browse the repository at this point in the history
* Add verbose messages for debugging purposes
* Add disabled lines handling to parser
* Force newline at end of code
  • Loading branch information
Razoric480 committed Sep 4, 2022
1 parent e7a0a97 commit 72efc1e
Show file tree
Hide file tree
Showing 18 changed files with 5,990 additions and 94 deletions.
1 change: 1 addition & 0 deletions core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ class ScriptLanguage : public Object {

virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) { return ERR_UNAVAILABLE; }

virtual String format_code(const String &p_code) { return p_code; }
virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const = 0;
virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) = 0;
virtual void add_named_global_constant(const StringName &p_name, const Variant &p_value) {}
Expand Down
11 changes: 10 additions & 1 deletion doc/classes/EditorSettings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@
The shape of the caret to use in the script editor. [b]Line[/b] displays a vertical line to the left of the current character, whereas [b]Block[/b] displays a outline over the current character.
</member>
<member name="text_editor/appearance/guidelines/line_length_guideline_hard_column" type="int" setter="" getter="">
The column at which to display a subtle line as a line length guideline for scripts. This should generally be greater than [member text_editor/appearance/guidelines/line_length_guideline_soft_column].
The column at which to display a subtle line as a line length guideline for scripts. This should generally be greater than [member text_editor/appearance/guidelines/line_length_guideline_soft_column]. This also sets the target line length that the code formatter tries to target.
</member>
<member name="text_editor/appearance/guidelines/line_length_guideline_soft_column" type="int" setter="" getter="">
The column at which to display a [i]very[/i] subtle line as a line length guideline for scripts. This should generally be lower than [member text_editor/appearance/guidelines/line_length_guideline_hard_column].
Expand Down Expand Up @@ -721,6 +721,15 @@
<member name="text_editor/behavior/files/trim_trailing_whitespace_on_save" type="bool" setter="" getter="">
If [code]true[/code], trims trailing whitespace when saving a script. Trailing whitespace refers to tab and space characters placed at the end of lines. Since these serve no practical purpose, they can and should be removed to make version control diffs less noisy.
</member>
<member name="text_editor/behavior/formatter/format_on_save" type="bool" setter="" getter="">
If [code]true[/code], uses the code formatter before saving a script.
</member>
<member name="text_editor/behavior/formatter/indent_in_multiline_block" type="int" setter="" getter="">
The number of indents for the formatter to include when wrapping single line statements into multi-line blocks.
</member>
<member name="text_editor/behavior/formatter/lines_between_functions" type="int" setter="" getter="">
The number of extra lines between functions the formatter inserts.
</member>
<member name="text_editor/behavior/indent/auto_indent" type="bool" setter="" getter="">
If [code]true[/code], automatically indents code when pressing the [kbd]Enter[/kbd] key based on blocks above the new line.
</member>
Expand Down
5 changes: 5 additions & 0 deletions editor/editor_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,11 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
EDITOR_SETTING(Variant::INT, PROPERTY_HINT_RANGE, "text_editor/behavior/indent/size", 4, "1,64,1") // size of 0 crashes.
_initial_set("text_editor/behavior/indent/auto_indent", true);

// Behavior: Formatter
_initial_set("text_editor/behavior/formatter/indent_in_multiline_block", 2);
_initial_set("text_editor/behavior/formatter/lines_between_functions", 2);
_initial_set("text_editor/behavior/formatter/format_on_save", false);

// Behavior: Files
_initial_set("text_editor/behavior/files/trim_trailing_whitespace_on_save", false);
_initial_set("text_editor/behavior/files/autosave_interval_secs", 0);
Expand Down
94 changes: 56 additions & 38 deletions editor/plugins/script_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -912,18 +912,23 @@ void ScriptEditor::_resave_scripts(const String &p_str) {
continue; //internal script, who cares
}

if (trim_trailing_whitespace_on_save) {
se->trim_trailing_whitespace();
}
if (!format_on_save) {
if (trim_trailing_whitespace_on_save) {
se->trim_trailing_whitespace();
}

se->insert_final_newline();
se->insert_final_newline();

if (convert_indent_on_save) {
if (use_space_indentation) {
se->convert_indent_to_spaces();
} else {
se->convert_indent_to_tabs();
if (convert_indent_on_save) {
if (use_space_indentation) {
se->convert_indent_to_spaces();
} else {
se->convert_indent_to_tabs();
}
}

} else {
se->format_code();
}

Ref<TextFile> text_file = script;
Expand Down Expand Up @@ -1265,18 +1270,21 @@ void ScriptEditor::_menu_option(int p_option) {
save_current_script();
} break;
case FILE_SAVE_AS: {
if (trim_trailing_whitespace_on_save) {
current->trim_trailing_whitespace();
}

current->insert_final_newline();
if (!format_on_save) {
if (trim_trailing_whitespace_on_save) {
current->trim_trailing_whitespace();
}
current->insert_final_newline();

if (convert_indent_on_save) {
if (use_space_indentation) {
current->convert_indent_to_spaces();
} else {
current->convert_indent_to_tabs();
if (convert_indent_on_save) {
if (use_space_indentation) {
current->convert_indent_to_spaces();
} else {
current->convert_indent_to_tabs();
}
}
} else {
current->format_code();
}

Ref<Resource> resource = current->get_edited_resource();
Expand Down Expand Up @@ -2400,18 +2408,22 @@ void ScriptEditor::save_current_script() {
return;
}

if (trim_trailing_whitespace_on_save) {
current->trim_trailing_whitespace();
}
if (!format_on_save) {
if (trim_trailing_whitespace_on_save) {
current->trim_trailing_whitespace();
}

current->insert_final_newline();
current->insert_final_newline();

if (convert_indent_on_save) {
if (use_space_indentation) {
current->convert_indent_to_spaces();
} else {
current->convert_indent_to_tabs();
if (convert_indent_on_save) {
if (use_space_indentation) {
current->convert_indent_to_spaces();
} else {
current->convert_indent_to_tabs();
}
}
} else {
current->format_code();
}

Ref<Resource> resource = current->get_edited_resource();
Expand Down Expand Up @@ -2465,19 +2477,23 @@ void ScriptEditor::save_all_scripts() {
continue;
}

if (convert_indent_on_save) {
if (use_space_indentation) {
se->convert_indent_to_spaces();
} else {
se->convert_indent_to_tabs();
if (format_on_save) {
if (convert_indent_on_save) {
if (use_space_indentation) {
se->convert_indent_to_spaces();
} else {
se->convert_indent_to_tabs();
}
}
}

if (trim_trailing_whitespace_on_save) {
se->trim_trailing_whitespace();
}
if (trim_trailing_whitespace_on_save) {
se->trim_trailing_whitespace();
}

se->insert_final_newline();
se->insert_final_newline();
} else {
se->format_code();
}

if (!se->is_unsaved()) {
continue;
Expand Down Expand Up @@ -2702,6 +2718,7 @@ void ScriptEditor::_editor_settings_changed() {
trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/behavior/files/trim_trailing_whitespace_on_save");
convert_indent_on_save = EditorSettings::get_singleton()->get("text_editor/behavior/files/convert_indent_on_save");
use_space_indentation = EditorSettings::get_singleton()->get("text_editor/behavior/indent/type");
format_on_save = EditorSettings::get_singleton()->get("text_editor/behavior/formatter/format_on_save");

members_overview_enabled = EditorSettings::get_singleton()->get("text_editor/script_list/show_members_overview");
help_overview_enabled = EditorSettings::get_singleton()->get("text_editor/help/show_help_index");
Expand Down Expand Up @@ -3959,6 +3976,7 @@ ScriptEditor::ScriptEditor() {
trim_trailing_whitespace_on_save = EditorSettings::get_singleton()->get("text_editor/behavior/files/trim_trailing_whitespace_on_save");
convert_indent_on_save = EditorSettings::get_singleton()->get("text_editor/behavior/files/convert_indent_on_save");
use_space_indentation = EditorSettings::get_singleton()->get("text_editor/behavior/indent/type");
format_on_save = EditorSettings::get_singleton()->get("text_editor/behavior/formatter/format_on_save");

ScriptServer::edit_request_func = _open_script_request;

Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/script_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class ScriptEditorBase : public VBoxContainer {
virtual void set_executing_line(int p_line) = 0;
virtual void clear_executing_line() = 0;
virtual void trim_trailing_whitespace() = 0;
virtual void format_code() = 0;
virtual void insert_final_newline() = 0;
virtual void convert_indent_to_spaces() = 0;
virtual void convert_indent_to_tabs() = 0;
Expand Down Expand Up @@ -371,6 +372,7 @@ class ScriptEditor : public PanelContainer {

bool open_textfile_after_create = true;
bool trim_trailing_whitespace_on_save;
bool format_on_save;
bool use_space_indentation;
bool convert_indent_on_save;

Expand Down
38 changes: 38 additions & 0 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,40 @@ void ScriptTextEditor::trim_trailing_whitespace() {
code_editor->trim_trailing_whitespace();
}

void ScriptTextEditor::format_code() {
CodeEdit *tx = code_editor->get_text_editor();
const String original = tx->get_text();
Ref<Script> scr = script;

const String formatted_code = scr->get_language()->format_code(original);

if (formatted_code == original) {
return;
}

const Vector<String> original_lines = original.split("\n");
const Vector<String> formatted_lines = formatted_code.split("\n");

tx->begin_complex_operation();

const int end = MIN(original_lines.size(), formatted_lines.size());
for (int i = 0; i < end; ++i) {
if (original_lines[i] == formatted_lines[i]) {
continue;
}
tx->set_line(i, formatted_lines[i]);
}
if (original_lines.size() > formatted_lines.size()) {
tx->remove_text(formatted_lines.size() - 1, 0, original_lines.size() - 1, tx->get_line_width(original_lines.size() - 1));
} else if (original_lines.size() < formatted_lines.size()) {
Vector remainder{ tx->get_line(tx->get_line_count() - 1) };
remainder.append_array(formatted_lines.slice(tx->get_line_count()));
tx->set_line(tx->get_line_count() - 1, String("\n").join(remainder));
}

tx->end_complex_operation();
}

void ScriptTextEditor::insert_final_newline() {
code_editor->insert_final_newline();
}
Expand Down Expand Up @@ -1168,6 +1202,9 @@ void ScriptTextEditor::_edit_option(int p_op) {

tx->end_complex_operation();
} break;
case EDIT_FORMAT_CODE: {
format_code();
} break;
case EDIT_TRIM_TRAILING_WHITESAPCE: {
trim_trailing_whitespace();
} break;
Expand Down Expand Up @@ -1914,6 +1951,7 @@ void ScriptTextEditor::_enable_code_editor() {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
edit_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_text_editor/format_code", TTR("Format Code"), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::F), EDIT_FORMAT_CODE);
edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option));
edit_menu->get_popup()->add_separator();

Expand Down
2 changes: 2 additions & 0 deletions editor/plugins/script_text_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ class ScriptTextEditor : public ScriptEditorBase {
EDIT_SELECT_ALL,
EDIT_COMPLETE,
EDIT_AUTO_INDENT,
EDIT_FORMAT_CODE,
EDIT_TRIM_TRAILING_WHITESAPCE,
EDIT_CONVERT_INDENT_TO_SPACES,
EDIT_CONVERT_INDENT_TO_TABS,
Expand Down Expand Up @@ -217,6 +218,7 @@ class ScriptTextEditor : public ScriptEditorBase {
virtual void set_edit_state(const Variant &p_state) override;
virtual void ensure_focus() override;
virtual void trim_trailing_whitespace() override;
virtual void format_code() override;
virtual void insert_final_newline() override;
virtual void convert_indent_to_spaces() override;
virtual void convert_indent_to_tabs() override;
Expand Down
3 changes: 3 additions & 0 deletions editor/plugins/text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ void TextEditor::trim_trailing_whitespace() {
code_editor->trim_trailing_whitespace();
}

void TextEditor::format_code() {
}

void TextEditor::insert_final_newline() {
code_editor->insert_final_newline();
}
Expand Down
1 change: 1 addition & 0 deletions editor/plugins/text_editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ class TextEditor : public ScriptEditorBase {
virtual void set_executing_line(int p_line) override;
virtual void clear_executing_line() override;
virtual void trim_trailing_whitespace() override;
virtual void format_code() override;
virtual void insert_final_newline() override;
virtual void convert_indent_to_spaces() override;
virtual void convert_indent_to_tabs() override;
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ class GDScriptLanguage : public ScriptLanguage {
virtual Error lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) override;
#endif
virtual String _get_indentation() const;
virtual String format_code(const String &p_code) override;
virtual void auto_indent_code(String &p_code, int p_from_line, int p_to_line) const override;
virtual void add_global_constant(const StringName &p_variable, const Variant &p_value) override;
virtual void add_named_global_constant(const StringName &p_name, const Variant &p_value) override;
Expand Down
7 changes: 7 additions & 0 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
#include "core/io/file_access.h"
#include "gdscript_analyzer.h"
#include "gdscript_compiler.h"
#include "gdscript_format.h"
#include "gdscript_parser.h"
#include "gdscript_tokenizer.h"
#include "gdscript_utility_functions.h"
Expand Down Expand Up @@ -2976,6 +2977,12 @@ String GDScriptLanguage::_get_indentation() const {
return "\t";
}

String GDScriptLanguage::format_code(const String &p_code) {
GDScriptFormat formatter;
String output = formatter.format(p_code);
return output;
}

void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
String indent = _get_indentation();

Expand Down
Loading

0 comments on commit 72efc1e

Please sign in to comment.