Attention: Here be dragons
This is the latest
(unstable) version of this documentation, which may document features
not available in or compatible with released stable versions of Godot.
Checking the stable version of the documentation...
CodeEdit
继承: TextEdit < Control < CanvasItem < Node < Object
多行文本编辑器,针对编辑代码而设计。
描述
CodeEdit 是一种专门用于编辑纯文本代码文件的 TextEdit。它包含了许多代码编辑器中的常见功能,如行号、折行、代码补全、缩进管理以及字符串/注释管理。
注意:无论使用什么区域设置,CodeEdit 默认总是使用从左至右的文本方向来正确显示源代码。
属性
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
layout_direction |
|
|
|
||
|
||
|
||
|
||
text_direction |
|
方法
主题属性
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
信号
breakpoint_toggled(line: int) 🔗
在行中添加或移除断点时触发。如果该行通过退格键移动,则在旧行处触发一个移除的信号。
code_completion_requested() 🔗
在用户请求代码补全时发出。如果 _request_code_completion() 被覆盖或者 code_completion_enabled 为 false
,则不会发送该信号。
symbol_hovered(symbol: String, line: int, column: int) 🔗
当用户在符号上悬停时发出。该信号与 Control.mouse_entered 不同,不会立即发出,而是需要光标在符号上停留 ProjectSettings.gui/timers/tooltip_delay_sec 秒。
注意:symbol_tooltip_on_hover 为 true
时才会发出该信号。
symbol_lookup(symbol: String, line: int, column: int) 🔗
用户点击有效符号时发出。
symbol_validate(symbol: String) 🔗
用户将鼠标悬停在符号上时发出。应该通过调用 set_symbol_lookup_word_as_valid() 对该符号进行验证和响应。
注意:symbol_lookup_on_click 必须为 true
才会发出该信号。
枚举
enum CodeCompletionKind: 🔗
CodeCompletionKind KIND_CLASS = 0
将该选项标记为类。
CodeCompletionKind KIND_FUNCTION = 1
将该选项标记为函数。
CodeCompletionKind KIND_SIGNAL = 2
将该选项标记为 Godot 信号。
CodeCompletionKind KIND_VARIABLE = 3
将该选项标记为变量。
CodeCompletionKind KIND_MEMBER = 4
将该选项标记为成员。
CodeCompletionKind KIND_ENUM = 5
将该选项标记为枚举条目。
CodeCompletionKind KIND_CONSTANT = 6
将该选项标记为常量。
CodeCompletionKind KIND_NODE_PATH = 7
将该选项标记为 Godot 节点路径。
CodeCompletionKind KIND_FILE_PATH = 8
将该选项标记为文件路径。
CodeCompletionKind KIND_PLAIN_TEXT = 9
将该选项标记为未分类或纯文本。
enum CodeCompletionLocation: 🔗
CodeCompletionLocation LOCATION_LOCAL = 0
该选项是相对于代码补全查询位置的 - 例如局部变量。位置的后续值表示选项来自外部类,确切的值表示它们的距离(就内部类而言)。
CodeCompletionLocation LOCATION_PARENT_MASK = 256
该选项来自于所在的类或父类,相对于代码补全查询的位置。请使用类的深度进行按位 OR(或)运算(例如 0
表示当前类,1
表示父类,2
表示父类的父类等),从而在当前类或父类中存储选项的深度。
CodeCompletionLocation LOCATION_OTHER_USER_CODE = 512
该选项来自用户代码,不是局部,也不是派生类(例如自动加载单例)。
CodeCompletionLocation LOCATION_OTHER = 1024
该选项来自其他引擎代码,未被其他枚举常量覆盖 - 例如内置类。
属性说明
bool auto_brace_completion_enabled = false
🔗
如果为 true
,则当左括号被键入或被自动补全插入时,使用 auto_brace_completion_pairs 自动插入右括号。对左括号使用退格键时,也会自动移除右括号。
bool auto_brace_completion_highlight_matching = false
🔗
如果为 true
,则当光标在一对括号中任何一个的位置时,使用 auto_brace_completion_pairs 将其高亮。配对的括号将添加下划线。如果无法配对,则会使用 brace_mismatch_color 着色。
Dictionary auto_brace_completion_pairs = { "\"": "\"", "'": "'", "(": ")", "[": "]", "{": "}" }
🔗
void set_auto_brace_completion_pairs(value: Dictionary)
Dictionary get_auto_brace_completion_pairs()
将括号对设置为自动补全。字典元素中,键是左括号、值是对应的右括号。括号是由符号构成的 String。见 auto_brace_completion_enabled 和 auto_brace_completion_highlight_matching。
bool code_completion_enabled = false
🔗
如果为 true
,则 ProjectSettings.input/ui_text_completion_query 动作会请求进行代码补全。处理请求见 _request_code_completion() 或 code_completion_requested。
Array[String] code_completion_prefixes = []
🔗
设置将触发代码补全的前缀。
Array[String] delimiter_comments = []
🔗
设置注释分隔符。将删除所有的现有注释分隔符。
Array[String] delimiter_strings = ["' '", "\" \""]
🔗
设置字符串分隔符。将删除所有的现有字符串分隔符。
bool gutters_draw_bookmarks = false
🔗
如果为 true
,则在边栏中绘制书签。该边栏与断点和执行行共享。见 set_line_as_bookmarked().
bool gutters_draw_breakpoints_gutter = false
🔗
如果为 true
,则在边栏中绘制断点。该边栏与书签和执行行共享。点击边栏会开关对应行的断点,见 set_line_as_breakpoint().
bool gutters_draw_executing_lines = false
🔗
如果为 true
,则在边栏中绘制执行行。该边栏与断点和书签共享。见 set_line_as_executing().
bool gutters_draw_fold_gutter = false
🔗
如果为 true
,则会绘制折叠栏。该边栏会为每个能够折叠的行绘制 can_fold_code_region 图标(见 can_fold_line()),已折叠的行则绘制 folded_code_region 图标(见 is_line_folded())。点击这些图标可以开关折叠状态,见 toggle_foldable_line()。line_folding 为 true
才会显示图标。
bool gutters_draw_line_numbers = false
🔗
如果为 true
,则会绘制行号栏。行号从 1
开始,逐行递增。在行号栏中点击并拖动会选中整行文本。
bool gutters_zero_pad_line_numbers = false
🔗
如果为 true
,在边栏中绘制的行号会根据总行数用零填充。gutters_draw_line_numbers 须为 true
。
bool indent_automatic = false
🔗
如果为 true
,添加新行并且存在 indent_automatic_prefixes 中的前缀时会自动插入一级额外的缩进。如果存在括号对中的左括号,就会将对应的右括号移动到新的一行(见 auto_brace_completion_pairs)。
Array[String] indent_automatic_prefixes = [":", "{", "[", "("]
🔗
触发自动缩进的前缀。indent_automatic 为 true
时使用。
制表缩进的大小(按一次 Tab),以字符为单位。如果启用 indent_use_spaces,则代表使用的空格数。
bool indent_use_spaces = false
🔗
使用空格代替制表符进行缩进。
如果为 true
,则可以折叠行。否则 fold_line() 等与行折叠相关的方法将无法工作,并且 can_fold_line() 将始终返回 false
。见 gutters_draw_fold_gutter。
Array[int] line_length_guidelines = []
🔗
在提供的列上绘制垂直线。第一个条目被认为是主要的硬参考线,并且被绘制得更显眼。
bool symbol_lookup_on_click = false
🔗
设置当来自 symbol_validate 的验证词被点击时,应发出 symbol_lookup。
bool symbol_tooltip_on_hover = false
🔗
设置当悬停在单词上时,应发出 symbol_hovered。
方法说明
void _confirm_code_completion(replace: bool) virtual 🔗
覆盖此方法以定义所选条目应如何插入。如果 replace
为 true
,任何现有的文本都应该被替换。
Array[Dictionary] _filter_code_completion_candidates(candidates: Array[Dictionary]) virtual const 🔗
覆盖此方法以确定应该显示 candidates
中的哪些项。
参数 candidates
和返回值都是一个 Array 的 Dictionary,而 Dictionary 的键值,详见 get_code_completion_option()。
void _request_code_completion(force: bool) virtual 🔗
覆盖此方法以定义当用户请求代码完成时发生的情况。如果 force
为 true
,会绕过任何检查。
void add_auto_brace_completion_pair(start_key: String, end_key: String) 🔗
添加一对括号。
开始和结束键都必须是符号。只有开始键必须是唯一的。
void add_code_completion_option(type: CodeCompletionKind, display_text: String, insert_text: String, text_color: Color = Color(1, 1, 1, 1), icon: Resource = null, value: Variant = null, location: int = 1024) 🔗
向自动补全菜单的潜在候选队列提交条目。请调用 update_code_completion_options() 来更新列表。
location
指示的是该选项相对于代码补全请求位置的位置。这个值如何设置见 CodeCompletionLocation。
注意:这个列表将替换所有当前候选。
void add_comment_delimiter(start_key: String, end_key: String, line_only: bool = false) 🔗
添加从 start_key
到 end_key
的注释分隔符。两个键都应该是符号,并且 start_key
不得与其他分隔符共享。
如果 line_only
为 true
或 end_key
为空 String,则该区块不会延续到下一行。
void add_string_delimiter(start_key: String, end_key: String, line_only: bool = false) 🔗
定义从 start_key
到 end_key
的字符串分隔符。两个键都应该是符号,并且 start_key
不得与其他分隔符共享。
如果 line_only
为 true
或 end_key
为空 String,则该区块不会延续到下一行。
bool can_fold_line(line: int) const 🔗
如果给定的行可折叠则返回 true
。可折叠的情况有:这一行是有效代码区块的开头(见 get_code_region_start_tag()),这一行是注释或字符串块的开头,以及后续非空行的缩进程度更高(见 TextEdit.get_indent_level())。
void cancel_code_completion() 🔗
取消自动补全菜单。
void clear_bookmarked_lines() 🔗
清除所有书签行。
void clear_breakpointed_lines() 🔗
清除所有断点行。
void clear_comment_delimiters() 🔗
移除所有注释分隔符。
void clear_executing_lines() 🔗
清除所有已执行的行。
void clear_string_delimiters() 🔗
移除所有字符串分隔符。
void confirm_code_completion(replace: bool = false) 🔗
将选定的条目插入到文本中。如果 replace
为 true
,任何现有的文本都会被替换,而不是被合并。
void convert_indent(from_line: int = -1, to_line: int = -1) 🔗
将 from_line
和 to_line
之间的行缩进,转换为 indent_use_spaces 设置的制表符或空格。
值均为 -1
将转换整个文本。
void create_code_region() 🔗
使用选区创建一个新的代码区块。必须至少定义一个单行注释分隔符(见 add_comment_delimiter())。
代码区块是代码的一部分,它在折叠时会高亮显示,且可以帮助组织脚本。
代码区块的开始和结束标记可以被自定义(见 set_code_region_tags())。
代码区块使用开始和结束标记(默认情况下分别为 region
和 endregion
)进行分隔,前面有一个行注释分隔符。(例如 #region
和 #endregion
)
void delete_lines() 🔗
删除所选行以及有光标停留的行。
void do_indent() 🔗
如果没有选中内容,则在光标处插入缩进。否则会将所选行缩进一级,与 indent_lines() 类似。等价于 ProjectSettings.input/ui_text_indent 动作。所使用的缩进字符取决于 indent_use_spaces 和 indent_size。
void duplicate_lines() 🔗
复制当前使用任何文本光标选择的所有行。无论文本光标位于行内的哪个位置,复制当前行所在的整行。
void duplicate_selection() 🔗
制作所有选中文本的副本并制作所有光标所在行的副本。
void fold_all_lines() 🔗
折叠所有可能被折叠的行(参见 can_fold_line())。
如果可能,折叠给定的行(参见 can_fold_line())。
String get_auto_brace_completion_close_key(open_key: String) const 🔗
获取 open_key
相匹配的括号自动闭合键。
PackedInt32Array get_bookmarked_lines() const 🔗
获取所有书签行。
PackedInt32Array get_breakpointed_lines() const 🔗
获取所有断点行。
Dictionary get_code_completion_option(index: int) const 🔗
获取在 index
处的补全选项。返回的 Dictionary 有以下键值。
kind
:CodeCompletionKind
display_text
:在自动补全菜单上显示的文本。
insert_text
:当选中这个选项时要插入的文本。
font_color
:自动补全菜单上文本的颜色。
icon
:在自动补全菜单上绘制的图标。
default_value
:符号的值。
Array[Dictionary] get_code_completion_options() const 🔗
获取所有补全选项,返回值见 get_code_completion_option()。
int get_code_completion_selected_index() const 🔗
获取当前已选定补全项的索引。
String get_code_region_end_tag() const 🔗
返回代码区块结束标签(不带注释分隔符)。
String get_code_region_start_tag() const 🔗
返回代码区块开始标签(不带注释分隔符)。
String get_delimiter_end_key(delimiter_index: int) const 🔗
获取字符串或注释块索引的结束键。
Vector2 get_delimiter_end_position(line: int, column: int) const 🔗
如果 line
column
是在一个字符串或注释中,则返回该区域的结束位置。如果不在或未找到结束位置,则 Vector2 的两个值都将是 -1
。
String get_delimiter_start_key(delimiter_index: int) const 🔗
获取字符串或注释块索引的开始键。
Vector2 get_delimiter_start_position(line: int, column: int) const 🔗
如果 line
column
是在一个字符串或注释中,则返回该区域的起始位置。如果不在或未找到开始位置,则 Vector2 的两个值都将是 -1
。
PackedInt32Array get_executing_lines() const 🔗
获取所有正在执行的行。
Array[int] get_folded_lines() const 🔗
返回当前所有折叠的行。
String get_text_for_code_completion() const 🔗
返回在文本光标位置带有 0xFFFF
字符的全文。
String get_text_for_symbol_lookup() const 🔗
返回在鼠标光标处带有 0xFFFF
字符的全文。
String get_text_with_cursor_char(line: int, column: int) const 🔗
返回在指定位置带有 0xFFFF
字符的全文。
bool has_auto_brace_completion_close_key(close_key: String) const 🔗
如果关闭键 close_key
存在,则返回 true
。
bool has_auto_brace_completion_open_key(open_key: String) const 🔗
如果打开键 open_key
存在,则返回 true
。
bool has_comment_delimiter(start_key: String) const 🔗
如果注释的 start_key
存在,返回 true
。
bool has_string_delimiter(start_key: String) const 🔗
如果字符串的 start_key
存在,返回 true
。
void indent_lines() 🔗
缩进所有选中的行或光标所在的行。使用空格还是制表符取决于 indent_use_spaces。见 unindent_lines()。
int is_in_comment(line: int, column: int = -1) const 🔗
如果 line
column
在一条注释中,则返回分隔符索引。如果没有提供 column
,且整个 line
是一条注释,则将返回分隔符索引。否则返回 -1
。
int is_in_string(line: int, column: int = -1) const 🔗
如果 line
column
在一条字符串中,则返回分隔符索引。如果没有提供 column
,且整个 line
是一个字符串,则将返回分隔符索引。否则返回 -1
。
bool is_line_bookmarked(line: int) const 🔗
如果给定的行设置了书签,则返回 true
。见 set_line_as_bookmarked()。
bool is_line_breakpointed(line: int) const 🔗
如果给定的行设置了断点,则返回 true
。见 set_line_as_breakpoint()。
bool is_line_code_region_end(line: int) const 🔗
如果给定的行是代码区块的结尾,则返回 true
。见 set_code_region_tags()。
bool is_line_code_region_start(line: int) const 🔗
如果给定的行是代码区块的开头,则返回 true
。见 set_code_region_tags()。
bool is_line_executing(line: int) const 🔗
如果给定的行被标记为正在执行,则返回 true
。见 set_line_as_executing()。
bool is_line_folded(line: int) const 🔗
如果给定的行已折叠,则返回 true
。见 fold_line()。
void move_lines_down() 🔗
将所选行以及有光标停留的行向下移动。
void move_lines_up() 🔗
将所选行以及有光标停留的行向上移动。
void remove_comment_delimiter(start_key: String) 🔗
移除带有 start_key
的注释分隔符。
void remove_string_delimiter(start_key: String) 🔗
移除带有 start_key
的字符串分隔符。
void request_code_completion(force: bool = false) 🔗
发出 code_completion_requested,如果 force
为 true
则会绕过所有检查。否则,将检查光标是否在一个词中或在一个前缀的前面。如果当前所有选项都是文件路径、节点路径或信号类型,将忽略该请求。
void set_code_completion_selected_index(index: int) 🔗
设置当前选定的补全选项。
void set_code_hint(code_hint: String) 🔗
设置代码提示文本。传递一个空字符串来清除。
void set_code_hint_draw_below(draw_below: bool) 🔗
如果为 true
,则会在主光标下方绘制代码提示。如果为 false
,则会在主光标上方绘制代码提示。见 set_code_hint()。
void set_code_region_tags(start: String = "region", end: String = "endregion") 🔗
设置代码区块的开始和结束标签(不含注释分隔符)。
void set_line_as_bookmarked(line: int, bookmarked: bool) 🔗
为给定的行设置书签。如果为 true
并且 gutters_draw_bookmarks 为 true
,则会在边栏中为该行绘制 bookmark 图标。见 get_bookmarked_lines() 和 is_line_bookmarked()。
void set_line_as_breakpoint(line: int, breakpointed: bool) 🔗
为给定的行设置断点。如果为 true
并且 gutters_draw_breakpoints_gutter 为 true
,则会在边栏中为该行绘制 breakpoint 图标。见 get_breakpointed_lines() 和 is_line_breakpointed()。
void set_line_as_executing(line: int, executing: bool) 🔗
将给定的行设置为正在执行。如果为 true
并且 gutters_draw_executing_lines 为 true
,则会在边栏中为该行绘制 executing_line 图标。见 get_executing_lines() 和 is_line_executing()。
void set_symbol_lookup_word_as_valid(valid: bool) 🔗
将 symbol_validate 发出的符号设置为有效查找。
void toggle_foldable_line(line: int) 🔗
在给定行切换代码块的折叠。
void toggle_foldable_lines_at_carets() 🔗
在所有带有光标的行上切换代码块的折叠。
void unfold_all_lines() 🔗
展开所有已折叠的行。
如果给定的行处于折叠状态,或被隐藏在折叠行下,则展开该行。
void unindent_lines() 🔗
将所选行或光标所在行取消一级缩进。会根据 indent_use_spaces 决定使用空格还是制表符。等价于 ProjectSettings.input/ui_text_dedent 动作。见 indent_lines()。
void update_code_completion_options(force: bool) 🔗
提交所有用 add_code_completion_option() 添加的补全选项。如果 force
是 true
,将尝试强制弹出自动补全菜单 。
注意:这将取代所有当前的候补选项。
主题属性说明
Color bookmark_color = Color(0.5, 0.64, 1, 0.8)
🔗
书签图标的 Color,用于标记了书签的行。
Color brace_mismatch_color = Color(1, 0.2, 0.2, 1)
🔗
用于高亮不匹配括号文本的 Color。
Color breakpoint_color = Color(0.9, 0.29, 0.3, 1)
🔗
书签行的断点图标的 Color。
Color code_folding_color = Color(0.8, 0.8, 0.8, 0.8)
🔗
所有与折叠行相关的图标的 Color。
Color completion_background_color = Color(0.17, 0.16, 0.2, 1)
🔗
设置代码补全弹出窗口的背景色 Color。
Color completion_existing_color = Color(0.87, 0.87, 0.87, 0.13)
🔗
用于匹配代码补全选项中的文本的背景高亮的 Color。
Color completion_scroll_color = Color(1, 1, 1, 0.29)
🔗
代码补全弹出窗口中滚动条的 Color。
Color completion_scroll_hovered_color = Color(1, 1, 1, 0.4)
🔗
代码补全弹出窗口中滚动条在悬停状态的 Color。
Color completion_selected_color = Color(0.26, 0.26, 0.27, 1)
🔗
代码补全弹出窗口中,当前选定选项的背景高亮的 Color。
Color executing_line_color = Color(0.98, 0.89, 0.27, 1)
🔗
执行行执行图标的 Color。
Color folded_code_region_color = Color(0.68, 0.46, 0.77, 0.2)
🔗
折叠代码区块的背景行高亮 Color。
Color line_length_guideline_color = Color(0.3, 0.5, 0.8, 0.1)
🔗
主行长度参考线的 Color,次要参考线将应用 50% 的 Alpha。
Color line_number_color = Color(0.67, 0.67, 0.67, 0.4)
🔗
设置行号的颜色 Color。
同时在代码补全弹出窗口中显示的最大选项数。
int completion_max_width = 50
🔗
代码补全弹出窗口中选项的最大宽度。超过此长度的选项将被截断。
int completion_scroll_width = 6
🔗
代码补全弹出窗口中滚动条的宽度。
设置书签行的自定义 Texture2D,会在书签栏中绘制。
设置断点行的自定义 Texture2D,会在断点栏中绘制。
设置可折叠行的自定义 Texture2D,会在折叠行栏中绘制。
Texture2D can_fold_code_region 🔗
设置一个自定义 Texture2D,以在代码区块可以折叠时在行折叠栏中绘制。
Texture2D completion_color_bg 🔗
自动补全时颜色预览框的背景面板(颜色半透明时可见)。
执行行的图标,会在执行栏中绘制。
设置一个自定义 Texture2D,以在行被折叠且可以展开时,在行折叠边栏中绘制。
Texture2D folded_code_region 🔗
设置一个自定义 Texture2D,以在代码区块被折叠且可以展开时,在行折叠边栏中绘制。
设置要绘制在折叠行末尾的一个自定义 Texture2D。
用于代码补全弹窗的 StyleBox。