Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

CodeEdit

继承: TextEdit < Control < CanvasItem < Node < Object

多行文本编辑器,针对编辑代码而设计。

描述

CodeEdit 是一种专门用于编辑纯文本代码文件的 TextEdit。它包含了许多代码编辑器中的常见功能,如行号、折行、代码补全、缩进管理以及字符串/注释管理。

注意:无论使用什么区域设置,CodeEdit 默认总是使用从左至右的文本方向来正确显示源代码。

属性

bool

auto_brace_completion_enabled

false

bool

auto_brace_completion_highlight_matching

false

Dictionary

auto_brace_completion_pairs

{ "\"": "\"", "'": "'", "(": ")", "[": "]", "{": "}" }

bool

code_completion_enabled

false

String[]

code_completion_prefixes

[]

String[]

delimiter_comments

[]

String[]

delimiter_strings

["' '", "\" \""]

bool

gutters_draw_bookmarks

false

bool

gutters_draw_breakpoints_gutter

false

bool

gutters_draw_executing_lines

false

bool

gutters_draw_fold_gutter

false

bool

gutters_draw_line_numbers

false

bool

gutters_zero_pad_line_numbers

false

bool

indent_automatic

false

String[]

indent_automatic_prefixes

[":", "{", "[", "("]

int

indent_size

4

bool

indent_use_spaces

false

LayoutDirection

layout_direction

2 (overrides Control)

bool

line_folding

false

int[]

line_length_guidelines

[]

bool

symbol_lookup_on_click

false

TextDirection

text_direction

1 (overrides TextEdit)

方法

void

_confirm_code_completion ( bool replace ) virtual

Dictionary[]

_filter_code_completion_candidates ( Dictionary[] candidates ) virtual const

void

_request_code_completion ( bool force ) virtual

void

add_auto_brace_completion_pair ( String start_key, String end_key )

void

add_code_completion_option ( CodeCompletionKind type, String display_text, String insert_text, Color text_color=Color(1, 1, 1, 1), Resource icon=null, Variant value=0, int location=1024 )

void

add_comment_delimiter ( String start_key, String end_key, bool line_only=false )

void

add_string_delimiter ( String start_key, String end_key, bool line_only=false )

bool

can_fold_line ( int line ) const

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 ( bool replace=false )

void

convert_indent ( int from_line=-1, int to_line=-1 )

void

create_code_region ( )

void

do_indent ( )

void

duplicate_lines ( )

void

fold_all_lines ( )

void

fold_line ( int line )

String

get_auto_brace_completion_close_key ( String open_key ) const

PackedInt32Array

get_bookmarked_lines ( ) const

PackedInt32Array

get_breakpointed_lines ( ) const

Dictionary

get_code_completion_option ( int index ) const

Dictionary[]

get_code_completion_options ( ) const

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 ( int delimiter_index ) const

Vector2

get_delimiter_end_position ( int line, int column ) const

String

get_delimiter_start_key ( int delimiter_index ) const

Vector2

get_delimiter_start_position ( int line, int column ) const

PackedInt32Array

get_executing_lines ( ) const

int[]

get_folded_lines ( ) const

String

get_text_for_code_completion ( ) const

String

get_text_for_symbol_lookup ( ) const

String

get_text_with_cursor_char ( int line, int column ) const

bool

has_auto_brace_completion_close_key ( String close_key ) const

bool

has_auto_brace_completion_open_key ( String open_key ) const

bool

has_comment_delimiter ( String start_key ) const

bool

has_string_delimiter ( String start_key ) const

void

indent_lines ( )

int

is_in_comment ( int line, int column=-1 ) const

int

is_in_string ( int line, int column=-1 ) const

bool

is_line_bookmarked ( int line ) const

bool

is_line_breakpointed ( int line ) const

bool

is_line_code_region_end ( int line ) const

bool

is_line_code_region_start ( int line ) const

bool

is_line_executing ( int line ) const

bool

is_line_folded ( int line ) const

void

remove_comment_delimiter ( String start_key )

void

remove_string_delimiter ( String start_key )

void

request_code_completion ( bool force=false )

void

set_code_completion_selected_index ( int index )

void

set_code_hint ( String code_hint )

void

set_code_hint_draw_below ( bool draw_below )

void

set_code_region_tags ( String start="region", String end="endregion" )

void

set_line_as_bookmarked ( int line, bool bookmarked )

void

set_line_as_breakpoint ( int line, bool breakpointed )

void

set_line_as_executing ( int line, bool executing )

void

set_symbol_lookup_word_as_valid ( bool valid )

void

toggle_foldable_line ( int line )

void

unfold_all_lines ( )

void

unfold_line ( int line )

void

unindent_lines ( )

void

update_code_completion_options ( bool force )

主题属性

Color

bookmark_color

Color(0.5, 0.64, 1, 0.8)

Color

brace_mismatch_color

Color(1, 0.2, 0.2, 1)

Color

breakpoint_color

Color(0.9, 0.29, 0.3, 1)

Color

code_folding_color

Color(0.8, 0.8, 0.8, 0.8)

Color

completion_background_color

Color(0.17, 0.16, 0.2, 1)

Color

completion_existing_color

Color(0.87, 0.87, 0.87, 0.13)

Color

completion_scroll_color

Color(1, 1, 1, 0.29)

Color

completion_scroll_hovered_color

Color(1, 1, 1, 0.4)

Color

completion_selected_color

Color(0.26, 0.26, 0.27, 1)

Color

executing_line_color

Color(0.98, 0.89, 0.27, 1)

Color

folded_code_region_color

Color(0.68, 0.46, 0.77, 0.2)

Color

line_length_guideline_color

Color(0.3, 0.5, 0.8, 0.1)

Color

line_number_color

Color(0.67, 0.67, 0.67, 0.4)

int

completion_lines

7

int

completion_max_width

50

int

completion_scroll_width

6

Texture2D

bookmark

Texture2D

breakpoint

Texture2D

can_fold

Texture2D

can_fold_code_region

Texture2D

executing_line

Texture2D

folded

Texture2D

folded_code_region

Texture2D

folded_eol_icon

StyleBox

completion


信号

breakpoint_toggled ( int line )

在行中添加或移除断点时触发。如果该行通过退格键移动,则在旧行处触发一个移除的信号。


code_completion_requested ( )

当用户请求代码补全时触发。


symbol_lookup ( String symbol, int line, int column )

用户点击有效符号时发出。


symbol_validate ( String symbol )

用户将鼠标悬停在符号上时发出。应该通过调用 set_symbol_lookup_word_as_valid 对该符号进行验证和响应。


枚举

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

  • void set_auto_brace_completion_enabled ( bool value )

  • bool is_auto_brace_completion_enabled ( )

设置括号对是否应自动补全。


bool auto_brace_completion_highlight_matching = false

  • void set_highlight_matching_braces_enabled ( bool value )

  • bool is_highlight_matching_braces_enabled ( )

高亮不匹配的括号对。


Dictionary auto_brace_completion_pairs = { "\"": "\"", "'": "'", "(": ")", "[": "]", "{": "}" }

  • void set_auto_brace_completion_pairs ( Dictionary value )

  • Dictionary get_auto_brace_completion_pairs ( )

将括号对设置为自动补全。


bool code_completion_enabled = false

  • void set_code_completion_enabled ( bool value )

  • bool is_code_completion_enabled ( )

设置是否允许代码补全。


String[] code_completion_prefixes = []

  • void set_code_completion_prefixes ( String[] value )

  • String[] get_code_completion_prefixes ( )

设置将触发代码补全的前缀。


String[] delimiter_comments = []

  • void set_comment_delimiters ( String[] value )

  • String[] get_comment_delimiters ( )

设置注释分隔符。将删除所有的现有注释分隔符。


String[] delimiter_strings = ["' '", "\" \""]

  • void set_string_delimiters ( String[] value )

  • String[] get_string_delimiters ( )

设置字符串分隔符。将删除所有的现有字符串分隔符。


bool gutters_draw_bookmarks = false

  • void set_draw_bookmarks_gutter ( bool value )

  • bool is_drawing_bookmarks_gutter ( )

设置是否应在边栏中绘制书签。该边栏与断点和执行行共享。


bool gutters_draw_breakpoints_gutter = false

  • void set_draw_breakpoints_gutter ( bool value )

  • bool is_drawing_breakpoints_gutter ( )

设置是否应在边栏中绘制断点。该边栏与书签和执行行共享。


bool gutters_draw_executing_lines = false

  • void set_draw_executing_lines_gutter ( bool value )

  • bool is_drawing_executing_lines_gutter ( )

设置是否应在边栏中绘制执行行。该边栏与断点和书签共享。


bool gutters_draw_fold_gutter = false

  • void set_draw_fold_gutter ( bool value )

  • bool is_drawing_fold_gutter ( )

设置是否应在装订线中绘制可折叠行图标。


bool gutters_draw_line_numbers = false

  • void set_draw_line_numbers ( bool value )

  • bool is_draw_line_numbers_enabled ( )

设置是否应在装订线中绘制行号。


bool gutters_zero_pad_line_numbers = false

  • void set_line_numbers_zero_padded ( bool value )

  • bool is_line_numbers_zero_padded ( )

设置在装订线中绘制的行号是否填充零。


bool indent_automatic = false

  • void set_auto_indent_enabled ( bool value )

  • bool is_auto_indent_enabled ( )

设置是否启用自动缩进,如果找到前缀或括号,这将添加额外的缩进。


String[] indent_automatic_prefixes = [":", "{", "[", "("]

  • void set_auto_indent_prefixes ( String[] value )

  • String[] get_auto_indent_prefixes ( )

触发自动缩进的前缀。


int indent_size = 4

  • void set_indent_size ( int value )

  • int get_indent_size ( )

制表缩进的大小(按一次 Tab),以字符为单位。如果启用 indent_use_spaces,则代表使用的空格数。


bool indent_use_spaces = false

  • void set_indent_using_spaces ( bool value )

  • bool is_indent_using_spaces ( )

使用空格代替制表符进行缩进。


bool line_folding = false

  • void set_line_folding_enabled ( bool value )

  • bool is_line_folding_enabled ( )

设置是否允许折叠行。


int[] line_length_guidelines = []

  • void set_line_length_guidelines ( int[] value )

  • int[] get_line_length_guidelines ( )

在提供的列上绘制垂直线。第一个条目被认为是主要的硬参考线,并且被绘制得更显眼。


bool symbol_lookup_on_click = false

  • void set_symbol_lookup_on_click_enabled ( bool value )

  • bool is_symbol_lookup_on_click_enabled ( )

设置当来自 symbol_validate 的验证词被点击时,应发出 symbol_lookup


方法说明

void _confirm_code_completion ( bool replace ) virtual

覆盖此方法以定义所选条目应如何插入。如果 replace 为真,任何现有的文本都应该被替换。


Dictionary[] _filter_code_completion_candidates ( Dictionary[] candidates ) virtual const

覆盖此方法以确定应该显示 candidates 中的哪些项。

参数 candidates 和返回值都是一个 ArrayDictionary,而 Dictionary 的键值,详见 get_code_completion_option


void _request_code_completion ( bool force ) virtual

覆盖此方法以定义当用户请求代码完成时发生的情况。如果 force 为真,会绕过任何检查。


void add_auto_brace_completion_pair ( String start_key, String end_key )

添加一对括号。

开始和结束键都必须是符号。只有开始键必须是唯一的。


void add_code_completion_option ( CodeCompletionKind type, String display_text, String insert_text, Color text_color=Color(1, 1, 1, 1), Resource icon=null, Variant value=0, int location=1024 )

向自动补全菜单的潜在候选队列提交条目。请调用 update_code_completion_options 来更新列表。

location 指示的是该选项相对于代码补全请求位置的位置。这个值如何设置见 CodeCompletionLocation

注意:这个列表将替换所有当前候选。


void add_comment_delimiter ( String start_key, String end_key, bool line_only=false )

添加注释分隔符。

开始键和结束键都必须是符号。只有开始键必须是唯一的。

line_only 表示该区域应该持续到该行的末尾,还是延续到下一行。如果结束键为空,则自动设置为true


void add_string_delimiter ( String start_key, String end_key, bool line_only=false )

添加字符串分隔符。

开始键和结束键都必须是符号。只有开始键必须是唯一的。

line_only 表示该区域应该持续到该行的末尾,还是延续到下一行。如果结束键为空,则自动设置为true


bool can_fold_line ( int line ) const

返回给定的行是否可折叠,也就是说,它的正下方有缩进的行或注释 / 字符串块。


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 ( bool replace=false )

将选定的条目插入到文本中。如果 replace 为真,任何现有的文本都会被替换,而不是被合并。


void convert_indent ( int from_line=-1, int to_line=-1 )

from_lineto_line 之间的行缩进,转换为 indent_use_spaces 设置的制表符或空格。

值均为 -1 将转换整个文本。


void create_code_region ( )

使用选区创建一个新的代码区块。必须至少定义一个单行注释分隔符(见 add_comment_delimiter)。

代码区块是代码的一部分,它在折叠时会高亮显示,且可以帮助组织脚本。

代码区块的开始和结束标记可以被自定义(见 set_code_region_tags)。

代码区块使用开始和结束标记(默认情况下分别为 regionendregion)进行分隔,前面有一个行注释分隔符。(例如 #region#endregion


void do_indent ( )

执行一个缩进,就像用户触发了“ui_text_indent”动作一样。


void duplicate_lines ( )

复制当前使用任何文本光标选择的所有行。无论文本光标位于行内的哪个位置,复制当前行所在的整行。


void fold_all_lines ( )

折叠所有可能被折叠的行(参见 can_fold_line)。


void fold_line ( int line )

如果可能,折叠给定的行(参见 can_fold_line)。


String get_auto_brace_completion_close_key ( String open_key ) const

获取 open_key 相匹配的括号自动闭合键。


PackedInt32Array get_bookmarked_lines ( ) const

获取所有书签行。


PackedInt32Array get_breakpointed_lines ( ) const

获取所有断点行。


Dictionary get_code_completion_option ( int index ) const

获取在 index 处的补全选项。返回的 Dictionary 有以下键值。

kindCodeCompletionKind

display_text :在自动补全菜单上显示的文本。

insert_text :当选中这个选项时要插入的文本。

font_color:自动补全菜单上文本的颜色。

icon :在自动补全菜单上绘制的图标。

default_value:符号的值。


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 ( int delimiter_index ) const

获取字符串或注释块索引的结束键。


Vector2 get_delimiter_end_position ( int line, int column ) const

如果 line column 是在一个字符串或注释中,则返回该区域的结束位置。如果不在或未找到结束位置,则 Vector2 的两个值都将是 -1


String get_delimiter_start_key ( int delimiter_index ) const

获取字符串或注释块索引的开始键。


Vector2 get_delimiter_start_position ( int line, int column ) const

如果 line column 是在一个字符串或注释中,则返回该区域的起始位置。如果不在或未找到开始位置,则 Vector2 的两个值都将是 -1


PackedInt32Array get_executing_lines ( ) const

获取所有正在执行的行。


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 ( int line, int column ) const

返回在指定位置带有 0xFFFF 字符的全文。


bool has_auto_brace_completion_close_key ( String close_key ) const

如果关闭键 close_key 存在,则返回 true


bool has_auto_brace_completion_open_key ( String open_key ) const

如果打开键 open_key 存在,则返回 true


bool has_comment_delimiter ( String start_key ) const

如果注释的 start_key 存在,返回 true


bool has_string_delimiter ( String start_key ) const

如果字符串的 start_key 存在,返回 true


void indent_lines ( )

缩进选定的行,或者在没有选择的情况下,将光标行缩进一个。


int is_in_comment ( int line, int column=-1 ) const

如果 line column 在一条注释中,则返回分隔符索引。如果没有提供 column,且整个 line 是一条注释,则将返回分隔符索引。否则返回 -1


int is_in_string ( int line, int column=-1 ) const

如果 line column 在一条字符串中,则返回分隔符索引。如果没有提供 column,且整个 line 是一个字符串,则将返回分隔符索引。否则返回 -1


bool is_line_bookmarked ( int line ) const

返回指定索引处的行是否添加了书签。


bool is_line_breakpointed ( int line ) const

返回指定索引处的行是否有断点。


bool is_line_code_region_end ( int line ) const

返回指定索引处的行是否是代码区块末尾。


bool is_line_code_region_start ( int line ) const

返回指定索引处的行是否是代码区块开始。


bool is_line_executing ( int line ) const

返回指定索引处的行是否标记为正在执行。


bool is_line_folded ( int line ) const

返回指定索引处的行是否折叠。


void remove_comment_delimiter ( String start_key )

移除带有 start_key 的注释分隔符。


void remove_string_delimiter ( String start_key )

移除带有 start_key 的字符串分隔符。


void request_code_completion ( bool force=false )

发出 code_completion_requested,如果 force 为真将绕过所有检查。否则,将检查光标是否在一个词中或在一个前缀的前面。如果当前所有选项都是文件路径、节点路径或信号类型,将忽略该请求。


void set_code_completion_selected_index ( int index )

设置当前选定的补全选项。


void set_code_hint ( String code_hint )

设置代码提示文本。传递一个空字符串来清除。


void set_code_hint_draw_below ( bool draw_below )

设置代码提示是否应绘制在文本下方。


void set_code_region_tags ( String start="region", String end="endregion" )

设置代码区块的开始和结束标签(不含注释分隔符)。


void set_line_as_bookmarked ( int line, bool bookmarked )

将该行设置为书签。


void set_line_as_breakpoint ( int line, bool breakpointed )

将该行设置为断点。


void set_line_as_executing ( int line, bool executing )

将该行设置为正在执行。


void set_symbol_lookup_word_as_valid ( bool valid )

symbol_validate 发出的符号设置为有效查找。


void toggle_foldable_line ( int line )

在给定行切换代码块的折叠。


void unfold_all_lines ( )

展开所有行,无论是否被折叠。


void unfold_line ( int line )

展开之前被折叠的所有行。


void unindent_lines ( )

解除所选行的缩进,或者在没有选择的情况下,将光标行缩进一个。与触发“ui_text_unindent ”动作相同。


void update_code_completion_options ( bool force )

提交所有用 add_code_completion_option 添加的补全选项。如果 forcetrue,将尝试强制弹出自动补全菜单 。

注意:这将取代所有当前的候补选项。


主题属性说明

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_lines = 7

同时在代码补全弹出窗口中显示的最大选项数。


int completion_max_width = 50

代码补全弹出窗口中选项的最大宽度。超过此长度的选项将被截断。


int completion_scroll_width = 6

代码补全弹出窗口中滚动条的宽度。


Texture2D bookmark

设置书签行的自定义 Texture2D,会在书签栏中绘制。


Texture2D breakpoint

设置断点行的自定义 Texture2D,会在断点栏中绘制。


Texture2D can_fold

设置可折叠行的自定义 Texture2D,会在折叠行栏中绘制。


Texture2D can_fold_code_region

设置一个自定义 Texture2D,以在代码区块可以折叠时在行折叠栏中绘制。


Texture2D executing_line

执行行的图标,会在执行栏中绘制。


Texture2D folded

设置一个自定义 Texture2D,以在行被折叠且可以展开时,在行折叠边栏中绘制。


Texture2D folded_code_region

设置一个自定义 Texture2D,以在代码区块被折叠且可以展开时,在行折叠边栏中绘制。


Texture2D folded_eol_icon

设置要绘制在折叠行末尾的一个自定义 Texture2D


StyleBox completion

用于代码补全弹窗的 StyleBox