Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/script/text_editor.cpp
20904 views
1
/**************************************************************************/
2
/* text_editor.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "text_editor.h"
32
33
#include "core/io/json.h"
34
#include "editor/script/script_editor_plugin.h"
35
#include "editor/settings/editor_settings.h"
36
37
void TextEditor::_validate_script() {
38
TextEditorBase::_validate_script();
39
40
Ref<JSON> json_file = edited_res;
41
if (json_file.is_valid()) {
42
CodeEdit *te = code_editor->get_text_editor();
43
44
te->set_line_background_color(code_editor->get_error_pos().x, Color(0, 0, 0, 0));
45
code_editor->set_error("");
46
47
if (json_file->parse(te->get_text(), true) != OK) {
48
code_editor->set_error(json_file->get_error_message().replace("[", "[lb]"));
49
code_editor->set_error_pos(json_file->get_error_line(), 0);
50
te->set_line_background_color(code_editor->get_error_pos().x, EDITOR_GET("text_editor/theme/highlighting/mark_color"));
51
}
52
}
53
}
54
55
void TextEditor::apply_code() {
56
Ref<TextFile> text_file = edited_res;
57
if (text_file.is_valid()) {
58
text_file->set_text(code_editor->get_text_editor()->get_text());
59
}
60
61
Ref<JSON> json_file = edited_res;
62
if (json_file.is_valid()) {
63
json_file->parse(code_editor->get_text_editor()->get_text(), true);
64
}
65
code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
66
}
67
68
ScriptEditorBase *TextEditor::create_editor(const Ref<Resource> &p_resource) {
69
if (Object::cast_to<TextFile>(*p_resource) || Object::cast_to<JSON>(*p_resource)) {
70
return memnew(TextEditor);
71
}
72
return nullptr;
73
}
74
75
Control *TextEditor::get_edit_menu() {
76
if (!edit_menus) {
77
edit_menus = memnew(EditMenus);
78
}
79
return edit_menus;
80
}
81
82
void TextEditor::register_editor() {
83
ScriptEditor::register_create_script_editor_function(create_editor);
84
}
85
86
TextEditor::TextEditor() {
87
add_child(code_editor);
88
}
89
90