Skip to content

Commit

Permalink
Add initial GDScript formatter along with tests and front-ends
Browse files Browse the repository at this point in the history
This change lays the groundwork for adding GDScript formatting capability to the engine.
In particular, this change introduces:
 - a very minimal GDScript formatter with a few hard-coded rules
 - unit tests covering hard-coded rules
 - ability to format the code from the editor
 - ability to check file formatting via commandline
  • Loading branch information
Scony committed Sep 23, 2024
1 parent e4e024a commit 207a77b
Show file tree
Hide file tree
Showing 12 changed files with 241 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,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
3 changes: 3 additions & 0 deletions editor/plugins/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ Import("env")

env.add_source_files(env.editor_sources, "*.cpp")

if env["tests"]:
env.Append(CPPDEFINES=["TESTS_ENABLED"])

SConscript("gizmos/SCsub")
SConscript("tiles/SCsub")
1 change: 1 addition & 0 deletions editor/plugins/script_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ class ScriptEditorBase : public VBoxContainer {
virtual void clear_executing_line() = 0;
virtual void trim_trailing_whitespace() = 0;
virtual void trim_final_newlines() = 0;
virtual void format_code() = 0;
virtual void insert_final_newline() = 0;
virtual void convert_indent() = 0;
virtual void ensure_focus() = 0;
Expand Down
44 changes: 44 additions & 0 deletions editor/plugins/script_text_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,42 @@ void ScriptTextEditor::trim_trailing_whitespace() {
code_editor->trim_trailing_whitespace();
}

void ScriptTextEditor::format_code() {
#ifdef TESTS_ENABLED
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();
#endif
}

void ScriptTextEditor::trim_final_newlines() {
code_editor->trim_final_newlines();
}
Expand Down Expand Up @@ -1440,6 +1476,11 @@ void ScriptTextEditor::_edit_option(int p_op) {
case EDIT_TRIM_FINAL_NEWLINES: {
trim_final_newlines();
} break;
#ifdef TESTS_ENABLED
case EDIT_FORMAT_CODE: {
format_code();
} break;
#endif
case EDIT_CONVERT_INDENT_TO_SPACES: {
code_editor->set_indent_using_spaces(true);
convert_indent();
Expand Down Expand Up @@ -2316,6 +2357,9 @@ void ScriptTextEditor::_enable_code_editor() {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES);
#ifdef TESTS_ENABLED
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);
#endif
{
PopupMenu *sub_menu = memnew(PopupMenu);
sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
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 @@ -119,6 +119,7 @@ class ScriptTextEditor : public ScriptEditorBase {
EDIT_AUTO_INDENT,
EDIT_TRIM_TRAILING_WHITESAPCE,
EDIT_TRIM_FINAL_NEWLINES,
EDIT_FORMAT_CODE,
EDIT_CONVERT_INDENT_TO_SPACES,
EDIT_CONVERT_INDENT_TO_TABS,
EDIT_TOGGLE_COMMENT,
Expand Down Expand Up @@ -230,6 +231,7 @@ class ScriptTextEditor : public ScriptEditorBase {
virtual void ensure_focus() override;
virtual void trim_trailing_whitespace() override;
virtual void trim_final_newlines() override;
virtual void format_code() override;
virtual void insert_final_newline() override;
virtual void convert_indent() override;
virtual void tag_saved_version() override;
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 @@ -135,6 +135,7 @@ class TextEditor : public ScriptEditorBase {
virtual void clear_executing_line() override;
virtual void trim_trailing_whitespace() override;
virtual void trim_final_newlines() override;
virtual void format_code() override {}
virtual void insert_final_newline() override;
virtual void convert_indent() override;
virtual void ensure_focus() override;
Expand Down
29 changes: 29 additions & 0 deletions main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
#endif // _3D_DISABLED

#ifdef TESTS_ENABLED
#include "modules/gdscript/gdscript_formatter.h"
#include "tests/test_main.h"
#endif

Expand Down Expand Up @@ -634,6 +635,9 @@ void Main::print_help(const char *p_binary) {
print_help_option("-s, --script <script>", "Run a script.\n");
print_help_option("--main-loop <main_loop_name>", "Run a MainLoop specified by its global class name.\n");
print_help_option("--check-only", "Only parse for errors and quit (use with --script).\n");
#ifdef TESTS_ENABLED
print_help_option("--format", "Format the file in-place or (if used with --check-only) check if file is formatted correctly. This option must be used with --script.\n");
#endif
#ifdef TOOLS_ENABLED
print_help_option("--import", "Starts the editor, waits for any resources to be imported, and then quits.\n", CLI_OPTION_AVAILABILITY_EDITOR);
print_help_option("--export-release <preset> <path>", "Export the project in release mode using the given preset and output path. The preset name should match one defined in \"export_presets.cfg\".\n", CLI_OPTION_AVAILABILITY_EDITOR);
Expand Down Expand Up @@ -3465,6 +3469,9 @@ int Main::start() {
String script;
String main_loop_type;
bool check_only = false;
#ifdef TESTS_ENABLED
bool format = false;
#endif

#ifdef TOOLS_ENABLED
String doc_tool_path;
Expand Down Expand Up @@ -3493,6 +3500,10 @@ int Main::start() {
// Designed to override and pass arguments to the unit test handler.
if (E->get() == "--check-only") {
check_only = true;
#ifdef TESTS_ENABLED
} else if (E->get() == "--format") {
format = true;
#endif
#ifdef TOOLS_ENABLED
} else if (E->get() == "--no-docbase") {
gen_flags.set_flag(DocTools::GENERATE_FLAG_SKIP_BASIC_TYPES);
Expand Down Expand Up @@ -3751,6 +3762,24 @@ int Main::start() {
Ref<Script> script_res = ResourceLoader::load(script);
ERR_FAIL_COND_V_MSG(script_res.is_null(), EXIT_FAILURE, "Can't load script: " + script);

#ifdef TESTS_ENABLED
if (format) {
if (check_only) {
GDScriptFormatter formatter;
String formattedCode;
if (formatter.format(script_res->get_source_code(), formattedCode) != OK)
return EXIT_FAILURE;
if (script_res->get_source_code() != formattedCode) {
print_line("Would reformat the script."); // TODO: Print diff.
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
} else {
ERR_FAIL_V_MSG(EXIT_FAILURE, "Formatting in-place is not supported yet");
}
}
#endif

if (check_only) {
return script_res->is_valid() ? EXIT_SUCCESS : EXIT_FAILURE;
}
Expand Down
1 change: 1 addition & 0 deletions modules/gdscript/gdscript.h
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,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
13 changes: 13 additions & 0 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "gdscript_analyzer.h"
#include "gdscript_compiler.h"
#include "gdscript_formatter.h"
#include "gdscript_parser.h"
#include "gdscript_tokenizer.h"
#include "gdscript_utility_functions.h"
Expand Down Expand Up @@ -3554,6 +3555,18 @@ String GDScriptLanguage::_get_indentation() const {
return "\t";
}

String GDScriptLanguage::format_code(const String &p_code) {
#ifdef TESTS_ENABLED
GDScriptFormatter formatter;
String formatted_code;
Error err = formatter.format(p_code, formatted_code);
if (err == OK) {
return formatted_code;
}
#endif
return p_code;
}

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

Expand Down
46 changes: 46 additions & 0 deletions modules/gdscript/gdscript_formatter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**************************************************************************/
/* gdscript_formatter.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifdef TESTS_ENABLED

#include "gdscript_formatter.h"

Error GDScriptFormatter::format(const String &p_code, String &r_formatted_code) {
if (p_code == "")
return OK;
if (p_code == "pass" || p_code == "pass\n") {
r_formatted_code = "pass\n";
return OK;
}
ERR_PRINT("Formatting failed.");
return FAILED;
}

#endif // #ifdef TESTS_ENABLED
42 changes: 42 additions & 0 deletions modules/gdscript/gdscript_formatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**************************************************************************/
/* gdscript_formatter.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef GDSCRIPT_FORMATTER_H
#define GDSCRIPT_FORMATTER_H

#include "core/error/error_list.h"
#include "core/string/string_name.h"

class GDScriptFormatter {
public:
Error format(const String &p_code, String &r_formatted_code);
};

#endif // GDSCRIPT_FORMATTER_H
58 changes: 58 additions & 0 deletions modules/gdscript/tests/test_formatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**************************************************************************/
/* test_formatter.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifndef TEST_FORMATTER_H
#define TEST_FORMATTER_H

#include "../gdscript_formatter.h"

#include "tests/test_macros.h"

namespace GDScriptTests {

TEST_SUITE("[Modules][GDScript][Formatter][Basic]") {
TEST_CASE("Formatting empty file should succeed") {
GDScriptFormatter formatter;
String code;
String output;
CHECK_EQ(formatter.format(code, output), OK);
}

TEST_CASE("Formatting dummy 'pass' should succeed and yield correct outcome") {
GDScriptFormatter formatter;
String code = "pass";
String output;
CHECK_EQ(formatter.format(code, output), OK);
CHECK_EQ(output, "pass\n");
}
}
} //namespace GDScriptTests

#endif // TEST_FORMATTER_H

0 comments on commit 207a77b

Please sign in to comment.