Class CodeEdit
A multiline text editor designed for editing code.
- Inheritance
-
CodeEdit
Remarks
CodeEdit is a specialized TextEdit designed for editing plain text code files. It has many features commonly found in code editors such as line numbers, line folding, code completion, indent management, and string/comment management.
Note: Regardless of locale, CodeEdit will by default always use left-to-right text direction to correctly display source code.
Properties
auto_brace_completion_enabled
If true
, uses auto_brace_completion_pairs to automatically insert the closing brace when the opening brace is inserted by typing or autocompletion. Also automatically removes the closing brace when using backspace on the opening brace.
var auto_brace_completion_enabled : bool = false
Property Value
Remarks
auto_brace_completion_highlight_matching
If true
, highlights brace pairs when the caret is on either one, using auto_brace_completion_pairs. If matching, the pairs will be underlined. If a brace is unmatched, it is colored with brace_mismatch_color.
var auto_brace_completion_highlight_matching : bool = false
Property Value
Remarks
auto_brace_completion_pairs
Sets the brace pairs to be autocompleted. For each entry in the dictionary, the key is the opening brace and the value is the closing brace that matches it. A brace is a String made of symbols. See auto_brace_completion_enabled and auto_brace_completion_highlight_matching.
var auto_brace_completion_pairs : Dictionary = { "\"": "\"", "'": "'", "(": ")", "[": "]", "{": "}" }
Property Value
Remarks
- void set_auto_brace_completion_pairs(Dictionary value)
- Dictionary get_auto_brace_completion_pairs
code_completion_enabled
If true
, the input/ui_text_completion_query action requests code completion. To handle it, see CodeEdit._request_code_completion or code_completion_requested.
var code_completion_enabled : bool = false
Property Value
Remarks
code_completion_prefixes
Sets prefixes that will trigger code completion.
var code_completion_prefixes : String[] = []
Property Value
- String[]
Remarks
delimiter_comments
Sets the comment delimiters. All existing comment delimiters will be removed.
var delimiter_comments : String[] = []
Property Value
- String[]
Remarks
delimiter_strings
Sets the string delimiters. All existing string delimiters will be removed.
var delimiter_strings : String[] = ["' '", "\" \""]
Property Value
- String[]
Remarks
gutters_draw_bookmarks
If true
, bookmarks are drawn in the gutter. This gutter is shared with breakpoints and executing lines. See CodeEdit.set_line_as_bookmarked.
var gutters_draw_bookmarks : bool = false
Property Value
Remarks
gutters_draw_breakpoints_gutter
If true
, breakpoints are drawn in the gutter. This gutter is shared with bookmarks and executing lines. Clicking the gutter will toggle the breakpoint for the line, see CodeEdit.set_line_as_breakpoint.
var gutters_draw_breakpoints_gutter : bool = false
Property Value
Remarks
gutters_draw_executing_lines
If true
, executing lines are marked in the gutter. This gutter is shared with breakpoints and bookmarks. See CodeEdit.set_line_as_executing.
var gutters_draw_executing_lines : bool = false
Property Value
Remarks
gutters_draw_fold_gutter
If true
, the fold gutter is drawn. In this gutter, the can_fold_code_region icon is drawn for each foldable line (see CodeEdit.can_fold_line) and the folded_code_region icon is drawn for each folded line (see CodeEdit.is_line_folded). These icons can be clicked to toggle the fold state, see CodeEdit.toggle_foldable_line. line_folding must be true
to show icons.
var gutters_draw_fold_gutter : bool = false
Property Value
Remarks
gutters_draw_line_numbers
If true
, the line number gutter is drawn. Line numbers start at 1
and are incremented for each line of text. Clicking and dragging in the line number gutter will select entire lines of text.
var gutters_draw_line_numbers : bool = false
Property Value
Remarks
gutters_zero_pad_line_numbers
If true
, line numbers drawn in the gutter are zero padded based on the total line count. Requires gutters_draw_line_numbers to be set to true
.
var gutters_zero_pad_line_numbers : bool = false
Property Value
Remarks
indent_automatic
If true
, an extra indent is automatically inserted when a new line is added and a prefix in indent_automatic_prefixes is found. If a brace pair opening key is found, the matching closing brace will be moved to another new line (see auto_brace_completion_pairs).
var indent_automatic : bool = false
Property Value
Remarks
indent_automatic_prefixes
Prefixes to trigger an automatic indent. Used when indent_automatic is set to true
.
var indent_automatic_prefixes : String[] = [":", "{", "[", "("]
Property Value
- String[]
Remarks
indent_size
Size of the tabulation indent (one Tab
press) in characters. If indent_use_spaces is enabled the number of spaces to use.
var indent_size : int = 4
Property Value
Remarks
indent_use_spaces
Use spaces instead of tabs for indentation.
var indent_use_spaces : bool = false
Property Value
Remarks
line_folding
If true
, lines can be folded. Otherwise, line folding methods like CodeEdit.fold_line will not work and CodeEdit.can_fold_line will always return false
. See gutters_draw_fold_gutter.
var line_folding : bool = false
Property Value
Remarks
line_length_guidelines
Draws vertical lines at the provided columns. The first entry is considered a main hard guideline and is draw more prominently.
var line_length_guidelines : int[] = []
Property Value
- int[]
Remarks
symbol_lookup_on_click
Set when a validated word from CodeEdit.symbol_validate is clicked, the CodeEdit.symbol_lookup should be emitted.
var symbol_lookup_on_click : bool = false
Property Value
Remarks
symbol_tooltip_on_hover
Set when a word is hovered, the CodeEdit.symbol_hovered should be emitted.
var symbol_tooltip_on_hover : bool = false
Property Value
Remarks
bookmark_color
Theme Property
Color of the bookmark icon for bookmarked lines.
= ``Color(0.5, 0.64, 1, 0.8)``
Property Value
brace_mismatch_color
Theme Property
Color of the text to highlight mismatched braces.
= ``Color(1, 0.2, 0.2, 1)``
Property Value
breakpoint_color
Theme Property
Color of the breakpoint icon for bookmarked lines.
= ``Color(0.9, 0.29, 0.3, 1)``
Property Value
code_folding_color
Theme Property
Color for all icons related to line folding.
= ``Color(0.8, 0.8, 0.8, 0.8)``
Property Value
completion_background_color
Theme Property
Sets the background Color for the code completion popup.
= ``Color(0.17, 0.16, 0.2, 1)``
Property Value
completion_existing_color
Theme Property
Background highlight Color for matching text in code completion options.
= ``Color(0.87, 0.87, 0.87, 0.13)``
Property Value
completion_scroll_color
Theme Property
Color of the scrollbar in the code completion popup.
= ``Color(1, 1, 1, 0.29)``
Property Value
completion_scroll_hovered_color
Theme Property
Color of the scrollbar in the code completion popup when hovered.
= ``Color(1, 1, 1, 0.4)``
Property Value
completion_selected_color
Theme Property
Background highlight Color for the current selected option item in the code completion popup.
= ``Color(0.26, 0.26, 0.27, 1)``
Property Value
executing_line_color
Theme Property
Color of the executing icon for executing lines.
= ``Color(0.98, 0.89, 0.27, 1)``
Property Value
folded_code_region_color
Theme Property
Color of background line highlight for folded code region.
= ``Color(0.68, 0.46, 0.77, 0.2)``
Property Value
line_length_guideline_color
Theme Property
Color of the main line length guideline, secondary guidelines will have 50% alpha applied.
= ``Color(0.3, 0.5, 0.8, 0.1)``
Property Value
line_number_color
Theme Property
Sets the Color of line numbers.
= ``Color(0.67, 0.67, 0.67, 0.4)``
Property Value
completion_lines
Theme Property
Max number of options to display in the code completion popup at any one time.
= ``7``
Property Value
completion_max_width
Theme Property
Max width of options in the code completion popup. Options longer than this will be cut off.
= ``50``
Property Value
completion_scroll_width
Theme Property
Width of the scrollbar in the code completion popup.
= ``6``
Property Value
bookmark
Theme Property
Sets a custom Texture2D to draw in the bookmark gutter for bookmarked lines.
Texture2D bookmark
Property Value
breakpoint
Theme Property
Sets a custom Texture2D to draw in the breakpoint gutter for breakpointed lines.
Texture2D breakpoint
Property Value
can_fold
Theme Property
Sets a custom Texture2D to draw in the line folding gutter when a line can be folded.
Texture2D can_fold
Property Value
can_fold_code_region
Theme Property
Sets a custom Texture2D to draw in the line folding gutter when a code region can be folded.
Texture2D can_fold_code_region
Property Value
completion_color_bg
Theme Property
Background panel for the color preview box in autocompletion (visible when the color is translucent).
Texture2D completion_color_bg
Property Value
executing_line
Theme Property
Icon to draw in the executing gutter for executing lines.
Texture2D executing_line
Property Value
folded
Theme Property
Sets a custom Texture2D to draw in the line folding gutter when a line is folded and can be unfolded.
Texture2D folded
Property Value
folded_code_region
Theme Property
Sets a custom Texture2D to draw in the line folding gutter when a code region is folded and can be unfolded.
Texture2D folded_code_region
Property Value
folded_eol_icon
Theme Property
Sets a custom Texture2D to draw at the end of a folded line.
Texture2D folded_eol_icon
Property Value
completion
Theme Property
StyleBox for the code completion popup.
StyleBox completion
Property Value
Methods
_confirm_code_completion(bool)
Qualifiers: virtual
Override this method to define how the selected entry should be inserted. If replace
is true
, any existing text should be replaced.
void _confirm_code_completion(bool replace)
Parameters
replace
bool
_filter_code_completion_candidates(Dictionary[])
Qualifiers: virtualconst
Override this method to define what items in candidates
should be displayed.
Both candidates
and the return is a Array of Dictionary, see CodeEdit.get_code_completion_option for Dictionary content.
Dictionary[] _filter_code_completion_candidates(Dictionary[] candidates)
Parameters
candidates
Dictionary[]
_request_code_completion(bool)
Qualifiers: virtual
Override this method to define what happens when the user requests code completion. If force
is true
, any checks should be bypassed.
void _request_code_completion(bool force)
Parameters
force
bool
add_auto_brace_completion_pair(String, String)
Adds a brace pair.
Both the start and end keys must be symbols. Only the start key has to be unique.
void add_auto_brace_completion_pair(String start_key, String end_key)
Parameters
add_code_completion_option(int, String, String, Color, Resource, Variant, int)
Submits an item to the queue of potential candidates for the autocomplete menu. Call CodeEdit.update_code_completion_options to update the list.
location
indicates location of the option relative to the location of the code completion query. See CodeCompletionLocation for how to set this value.
Note: This list will replace all current candidates.
void add_code_completion_option(int type, String display_text, String insert_text, Color text_color, Resource icon, Variant value, int location)
Parameters
type
intdisplay_text
Stringinsert_text
Stringtext_color
Coloricon
Resourcevalue
Variantlocation
int
add_comment_delimiter(String, String, bool)
Adds a comment delimiter from start_key
to end_key
. Both keys should be symbols, and start_key
must not be shared with other delimiters.
If line_only
is true
or end_key
is an empty String, the region does not carry over to the next line.
void add_comment_delimiter(String start_key, String end_key, bool line_only)
Parameters
add_string_delimiter(String, String, bool)
Defines a string delimiter from start_key
to end_key
. Both keys should be symbols, and start_key
must not be shared with other delimiters.
If line_only
is true
or end_key
is an empty String, the region does not carry over to the next line.
void add_string_delimiter(String start_key, String end_key, bool line_only)
Parameters
can_fold_line(int)
Qualifiers: const
Returns true
if the given line is foldable. A line is foldable if it is the start of a valid code region (see get_code_region_start_tag), if it is the start of a comment or string block, or if the next non-empty line is more indented (see TextEdit.get_indent_level).
bool can_fold_line(int line)
Parameters
line
int
cancel_code_completion
Cancels the autocomplete menu.
void cancel_code_completion
clear_bookmarked_lines
Clears all bookmarked lines.
void clear_bookmarked_lines
clear_breakpointed_lines
Clears all breakpointed lines.
void clear_breakpointed_lines
clear_comment_delimiters
Removes all comment delimiters.
void clear_comment_delimiters
clear_executing_lines
Clears all executed lines.
void clear_executing_lines
clear_string_delimiters
Removes all string delimiters.
void clear_string_delimiters
confirm_code_completion(bool)
Inserts the selected entry into the text. If replace
is true
, any existing text is replaced rather than merged.
void confirm_code_completion(bool replace)
Parameters
replace
bool
convert_indent(int, int)
Converts the indents of lines between from_line
and to_line
to tabs or spaces as set by indent_use_spaces.
Values of -1
convert the entire text.
void convert_indent(int from_line, int to_line)
Parameters
create_code_region
Creates a new code region with the selection. At least one single line comment delimiter have to be defined (see CodeEdit.add_comment_delimiter).
A code region is a part of code that is highlighted when folded and can help organize your script.
Code region start and end tags can be customized (see CodeEdit.set_code_region_tags).
Code regions are delimited using start and end tags (respectively region
and endregion
by default) preceded by one line comment delimiter. (eg. #region
and #endregion
)
void create_code_region
delete_lines
Deletes all lines that are selected or have a caret on them.
void delete_lines
do_indent
If there is no selection, indentation is inserted at the caret. Otherwise, the selected lines are indented like indent_lines. Equivalent to the input/ui_text_indent action. The indentation characters used depend on indent_use_spaces and indent_size.
void do_indent
duplicate_lines
Duplicates all lines currently selected with any caret. Duplicates the entire line beneath the current one no matter where the caret is within the line.
void duplicate_lines
duplicate_selection
Duplicates all selected text and duplicates all lines with a caret on them.
void duplicate_selection
fold_all_lines
Folds all lines that are possible to be folded (see CodeEdit.can_fold_line).
void fold_all_lines
fold_line(int)
Folds the given line, if possible (see CodeEdit.can_fold_line).
void fold_line(int line)
Parameters
line
int
get_auto_brace_completion_close_key(String)
Qualifiers: const
Gets the matching auto brace close key for open_key
.
String get_auto_brace_completion_close_key(String open_key)
Parameters
open_key
String
get_bookmarked_lines
Qualifiers: const
Gets all bookmarked lines.
PackedInt32Array get_bookmarked_lines
get_breakpointed_lines
Qualifiers: const
Gets all breakpointed lines.
PackedInt32Array get_breakpointed_lines
get_code_completion_option(int)
Qualifiers: const
Gets the completion option at index
. The return Dictionary has the following key-values:
kind
: CodeCompletionKind
display_text
: Text that is shown on the autocomplete menu.
insert_text
: Text that is to be inserted when this item is selected.
font_color
: Color of the text on the autocomplete menu.
icon
: Icon to draw on the autocomplete menu.
default_value
: Value of the symbol.
Dictionary get_code_completion_option(int index)
Parameters
index
int
get_code_completion_options
Qualifiers: const
Gets all completion options, see CodeEdit.get_code_completion_option for return content.
Dictionary[] get_code_completion_options
get_code_completion_selected_index
Qualifiers: const
Gets the index of the current selected completion option.
int get_code_completion_selected_index
get_code_region_end_tag
Qualifiers: const
Returns the code region end tag (without comment delimiter).
String get_code_region_end_tag
get_code_region_start_tag
Qualifiers: const
Returns the code region start tag (without comment delimiter).
String get_code_region_start_tag
get_delimiter_end_key(int)
Qualifiers: const
Gets the end key for a string or comment region index.
String get_delimiter_end_key(int delimiter_index)
Parameters
delimiter_index
int
get_delimiter_end_position(int, int)
Qualifiers: const
If line
column
is in a string or comment, returns the end position of the region. If not or no end could be found, both Vector2 values will be -1
.
Vector2 get_delimiter_end_position(int line, int column)
Parameters
get_delimiter_start_key(int)
Qualifiers: const
Gets the start key for a string or comment region index.
String get_delimiter_start_key(int delimiter_index)
Parameters
delimiter_index
int
get_delimiter_start_position(int, int)
Qualifiers: const
If line
column
is in a string or comment, returns the start position of the region. If not or no start could be found, both Vector2 values will be -1
.
Vector2 get_delimiter_start_position(int line, int column)
Parameters
get_executing_lines
Qualifiers: const
Gets all executing lines.
PackedInt32Array get_executing_lines
get_folded_lines
Qualifiers: const
Returns all lines that are currently folded.
int[] get_folded_lines
get_text_for_code_completion
Qualifiers: const
Returns the full text with char 0xFFFF
at the caret location.
String get_text_for_code_completion
get_text_for_symbol_lookup
Qualifiers: const
Returns the full text with char 0xFFFF
at the cursor location.
String get_text_for_symbol_lookup
get_text_with_cursor_char(int, int)
Qualifiers: const
Returns the full text with char 0xFFFF
at the specified location.
String get_text_with_cursor_char(int line, int column)
Parameters
has_auto_brace_completion_close_key(String)
Qualifiers: const
Returns true
if close key close_key
exists.
bool has_auto_brace_completion_close_key(String close_key)
Parameters
close_key
String
has_auto_brace_completion_open_key(String)
Qualifiers: const
Returns true
if open key open_key
exists.
bool has_auto_brace_completion_open_key(String open_key)
Parameters
open_key
String
has_comment_delimiter(String)
Qualifiers: const
Returns true
if comment start_key
exists.
bool has_comment_delimiter(String start_key)
Parameters
start_key
String
has_string_delimiter(String)
Qualifiers: const
Returns true
if string start_key
exists.
bool has_string_delimiter(String start_key)
Parameters
start_key
String
indent_lines
Indents all lines that are selected or have a caret on them. Uses spaces or a tab depending on indent_use_spaces. See unindent_lines.
void indent_lines
is_in_comment(int, int)
Qualifiers: const
Returns delimiter index if line
column
is in a comment. If column
is not provided, will return delimiter index if the entire line
is a comment. Otherwise -1
.
int is_in_comment(int line, int column)
Parameters
is_in_string(int, int)
Qualifiers: const
Returns the delimiter index if line
column
is in a string. If column
is not provided, will return the delimiter index if the entire line
is a string. Otherwise -1
.
int is_in_string(int line, int column)
Parameters
is_line_bookmarked(int)
Qualifiers: const
Returns true
if the given line is bookmarked. See CodeEdit.set_line_as_bookmarked.
bool is_line_bookmarked(int line)
Parameters
line
int
is_line_breakpointed(int)
Qualifiers: const
Returns true
if the given line is breakpointed. See CodeEdit.set_line_as_breakpoint.
bool is_line_breakpointed(int line)
Parameters
line
int
is_line_code_region_end(int)
Qualifiers: const
Returns true
if the given line is a code region end. See CodeEdit.set_code_region_tags.
bool is_line_code_region_end(int line)
Parameters
line
int
is_line_code_region_start(int)
Qualifiers: const
Returns true
if the given line is a code region start. See CodeEdit.set_code_region_tags.
bool is_line_code_region_start(int line)
Parameters
line
int
is_line_executing(int)
Qualifiers: const
Returns true
if the given line is marked as executing. See CodeEdit.set_line_as_executing.
bool is_line_executing(int line)
Parameters
line
int
is_line_folded(int)
Qualifiers: const
Returns true
if the given line is folded. See CodeEdit.fold_line.
bool is_line_folded(int line)
Parameters
line
int
move_lines_down
Moves all lines down that are selected or have a caret on them.
void move_lines_down
move_lines_up
Moves all lines up that are selected or have a caret on them.
void move_lines_up
remove_comment_delimiter(String)
Removes the comment delimiter with start_key
.
void remove_comment_delimiter(String start_key)
Parameters
start_key
String
remove_string_delimiter(String)
Removes the string delimiter with start_key
.
void remove_string_delimiter(String start_key)
Parameters
start_key
String
request_code_completion(bool)
Emits code_completion_requested, if force
is true
will bypass all checks. Otherwise will check that the caret is in a word or in front of a prefix. Will ignore the request if all current options are of type file path, node path, or signal.
void request_code_completion(bool force)
Parameters
force
bool
set_code_completion_selected_index(int)
Sets the current selected completion option.
void set_code_completion_selected_index(int index)
Parameters
index
int
set_code_hint(String)
Sets the code hint text. Pass an empty string to clear.
void set_code_hint(String code_hint)
Parameters
code_hint
String
set_code_hint_draw_below(bool)
If true
, the code hint will draw below the main caret. If false
, the code hint will draw above the main caret. See CodeEdit.set_code_hint.
void set_code_hint_draw_below(bool draw_below)
Parameters
draw_below
bool
set_code_region_tags(String, String)
Sets the code region start and end tags (without comment delimiter).
void set_code_region_tags(String start, String end)
Parameters
set_line_as_bookmarked(int, bool)
Sets the given line as bookmarked. If true
and gutters_draw_bookmarks is true
, draws the bookmark icon in the gutter for this line. See get_bookmarked_lines and CodeEdit.is_line_bookmarked.
void set_line_as_bookmarked(int line, bool bookmarked)
Parameters
set_line_as_breakpoint(int, bool)
Sets the given line as a breakpoint. If true
and gutters_draw_breakpoints_gutter is true
, draws the breakpoint icon in the gutter for this line. See get_breakpointed_lines and CodeEdit.is_line_breakpointed.
void set_line_as_breakpoint(int line, bool breakpointed)
Parameters
set_line_as_executing(int, bool)
Sets the given line as executing. If true
and gutters_draw_executing_lines is true
, draws the executing_line icon in the gutter for this line. See get_executing_lines and CodeEdit.is_line_executing.
void set_line_as_executing(int line, bool executing)
Parameters
set_symbol_lookup_word_as_valid(bool)
Sets the symbol emitted by CodeEdit.symbol_validate as a valid lookup.
void set_symbol_lookup_word_as_valid(bool valid)
Parameters
valid
bool
toggle_foldable_line(int)
Toggle the folding of the code block at the given line.
void toggle_foldable_line(int line)
Parameters
line
int
toggle_foldable_lines_at_carets
Toggle the folding of the code block on all lines with a caret on them.
void toggle_foldable_lines_at_carets
unfold_all_lines
Unfolds all lines that are folded.
void unfold_all_lines
unfold_line(int)
Unfolds the given line if it is folded or if it is hidden under a folded line.
void unfold_line(int line)
Parameters
line
int
unindent_lines
Unindents all lines that are selected or have a caret on them. Uses spaces or a tab depending on indent_use_spaces. Equivalent to the input/ui_text_dedent action. See indent_lines.
void unindent_lines
update_code_completion_options(bool)
Submits all completion options added with CodeEdit.add_code_completion_option. Will try to force the autocomplete menu to popup, if force
is true
.
Note: This will replace all current candidates.
void update_code_completion_options(bool force)
Parameters
force
bool
Events
breakpoint_toggled(int)
Emitted when a breakpoint is added or removed from a line. If the line is moved via backspace a removed is emitted at the old line.
signal breakpoint_toggled(int line)
Parameters
line
int
code_completion_requested
Emitted when the user requests code completion. This signal will not be sent if CodeEdit._request_code_completion is overridden or code_completion_enabled is false
.
signal code_completion_requested
symbol_hovered(String, int, int)
Emitted when the user hovers over a symbol. Unlike mouse_entered, this signal is not emitted immediately, but when the cursor is over the symbol for gui/timers/tooltip_delay_sec seconds.
Note: symbol_tooltip_on_hover must be true
for this signal to be emitted.
signal symbol_hovered(String symbol, int line, int column)
Parameters
symbol_lookup(String, int, int)
Emitted when the user has clicked on a valid symbol.
signal symbol_lookup(String symbol, int line, int column)
Parameters
symbol_validate(String)
Emitted when the user hovers over a symbol. The symbol should be validated and responded to, by calling CodeEdit.set_symbol_lookup_word_as_valid.
Note: symbol_lookup_on_click must be true
for this signal to be emitted.
signal symbol_validate(String symbol)
Parameters
symbol
String