Class CodeEdit
- Namespace
- Godot
- Assembly
- GodotSharp.dll
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.
public class CodeEdit : TextEdit, IDisposable
- Inheritance
-
CodeEdit
- Implements
- Inherited Members
Constructors
CodeEdit()
public CodeEdit()
Properties
AutoBraceCompletionEnabled
If true, uses AutoBraceCompletionPairs 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.
public bool AutoBraceCompletionEnabled { get; set; }
Property Value
AutoBraceCompletionHighlightMatching
If true, highlights brace pairs when the caret is on either one, using AutoBraceCompletionPairs. If matching, the pairs will be underlined. If a brace is unmatched, it is colored with brace_mismatch_color
.
public bool AutoBraceCompletionHighlightMatching { get; set; }
Property Value
AutoBraceCompletionPairs
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 AutoBraceCompletionEnabled and AutoBraceCompletionHighlightMatching.
public Dictionary AutoBraceCompletionPairs { get; set; }
Property Value
CodeCompletionEnabled
If true, the ProjectSettings.input/ui_text_completion_query
action requests code completion. To handle it, see _RequestCodeCompletion(bool) or CodeCompletionRequested.
public bool CodeCompletionEnabled { get; set; }
Property Value
CodeCompletionPrefixes
Sets prefixes that will trigger code completion.
public Array<string> CodeCompletionPrefixes { get; set; }
Property Value
DelimiterComments
Sets the comment delimiters. All existing comment delimiters will be removed.
public Array<string> DelimiterComments { get; set; }
Property Value
DelimiterStrings
Sets the string delimiters. All existing string delimiters will be removed.
public Array<string> DelimiterStrings { get; set; }
Property Value
GuttersDrawBookmarks
If true, bookmarks are drawn in the gutter. This gutter is shared with breakpoints and executing lines. See SetLineAsBookmarked(int, bool).
public bool GuttersDrawBookmarks { get; set; }
Property Value
GuttersDrawBreakpointsGutter
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 SetLineAsBreakpoint(int, bool).
public bool GuttersDrawBreakpointsGutter { get; set; }
Property Value
GuttersDrawExecutingLines
If true, executing lines are marked in the gutter. This gutter is shared with breakpoints and bookmarks. See SetLineAsExecuting(int, bool).
public bool GuttersDrawExecutingLines { get; set; }
Property Value
GuttersDrawFoldGutter
If true, the fold gutter is drawn. In this gutter, the can_fold_code_region
icon is drawn for each foldable line (see CanFoldLine(int)) and the folded_code_region
icon is drawn for each folded line (see IsLineFolded(int)). These icons can be clicked to toggle the fold state, see ToggleFoldableLine(int). LineFolding must be true to show icons.
public bool GuttersDrawFoldGutter { get; set; }
Property Value
GuttersDrawLineNumbers
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.
public bool GuttersDrawLineNumbers { get; set; }
Property Value
GuttersZeroPadLineNumbers
If true, line numbers drawn in the gutter are zero padded based on the total line count. Requires GuttersDrawLineNumbers to be set to true.
public bool GuttersZeroPadLineNumbers { get; set; }
Property Value
IndentAutomatic
If true, an extra indent is automatically inserted when a new line is added and a prefix in IndentAutomaticPrefixes is found. If a brace pair opening key is found, the matching closing brace will be moved to another new line (see AutoBraceCompletionPairs).
public bool IndentAutomatic { get; set; }
Property Value
IndentAutomaticPrefixes
Prefixes to trigger an automatic indent. Used when IndentAutomatic is set to true.
public Array<string> IndentAutomaticPrefixes { get; set; }
Property Value
IndentSize
Size of the tabulation indent (one Tab press) in characters. If IndentUseSpaces is enabled the number of spaces to use.
public int IndentSize { get; set; }
Property Value
IndentUseSpaces
Use spaces instead of tabs for indentation.
public bool IndentUseSpaces { get; set; }
Property Value
LineFolding
If true, lines can be folded. Otherwise, line folding methods like FoldLine(int) will not work and CanFoldLine(int) will always return false. See GuttersDrawFoldGutter.
public bool LineFolding { get; set; }
Property Value
LineLengthGuidelines
Draws vertical lines at the provided columns. The first entry is considered a main hard guideline and is draw more prominently.
public Array<int> LineLengthGuidelines { get; set; }
Property Value
SymbolLookupOnClick
Set when a validated word from SymbolValidate is clicked, the SymbolLookup should be emitted.
public bool SymbolLookupOnClick { get; set; }
Property Value
SymbolTooltipOnHover
Set when a word is hovered, the SymbolHovered should be emitted.
public bool SymbolTooltipOnHover { get; set; }
Property Value
Methods
AddAutoBraceCompletionPair(string, string)
Adds a brace pair.
Both the start and end keys must be symbols. Only the start key has to be unique.
public void AddAutoBraceCompletionPair(string startKey, string endKey)
Parameters
AddCodeCompletionOption(CodeCompletionKind, string, string, Color?, Resource, Variant, int)
Submits an item to the queue of potential candidates for the autocomplete menu. Call UpdateCodeCompletionOptions(bool) to update the list.
location
indicates location of the option relative to the location of the code completion query. See CodeEdit.CodeCompletionLocation for how to set this value.
Note: This list will replace all current candidates.
public void AddCodeCompletionOption(CodeEdit.CodeCompletionKind type, string displayText, string insertText, Color? textColor = null, Resource icon = null, Variant value = default, int location = 1024)
Parameters
type
CodeEdit.CodeCompletionKinddisplayText
stringinsertText
stringtextColor
Color?If the parameter is null, then the default value is
new Color(1.0f, 1.0f, 1.0f, 1.0f)
.icon
Resourcevalue
Variantlocation
int
AddCommentDelimiter(string, string, bool)
Adds a comment delimiter from startKey
to endKey
. Both keys should be symbols, and startKey
must not be shared with other delimiters.
If lineOnly
is true or endKey
is an empty string, the region does not carry over to the next line.
public void AddCommentDelimiter(string startKey, string endKey, bool lineOnly = false)
Parameters
AddStringDelimiter(string, string, bool)
Defines a string delimiter from startKey
to endKey
. Both keys should be symbols, and startKey
must not be shared with other delimiters.
If lineOnly
is true or endKey
is an empty string, the region does not carry over to the next line.
public void AddStringDelimiter(string startKey, string endKey, bool lineOnly = false)
Parameters
CanFoldLine(int)
Returns true if the given line is foldable. A line is foldable if it is the start of a valid code region (see GetCodeRegionStartTag()), if it is the start of a comment or string block, or if the next non-empty line is more indented (see GetIndentLevel(int)).
public bool CanFoldLine(int line)
Parameters
line
int
Returns
CancelCodeCompletion()
Cancels the autocomplete menu.
public void CancelCodeCompletion()
ClearBookmarkedLines()
Clears all bookmarked lines.
public void ClearBookmarkedLines()
ClearBreakpointedLines()
Clears all breakpointed lines.
public void ClearBreakpointedLines()
ClearCommentDelimiters()
Removes all comment delimiters.
public void ClearCommentDelimiters()
ClearExecutingLines()
Clears all executed lines.
public void ClearExecutingLines()
ClearStringDelimiters()
Removes all string delimiters.
public void ClearStringDelimiters()
ConfirmCodeCompletion(bool)
Inserts the selected entry into the text. If replace
is true, any existing text is replaced rather than merged.
public void ConfirmCodeCompletion(bool replace = false)
Parameters
replace
bool
ConvertIndent(int, int)
Converts the indents of lines between fromLine
and toLine
to tabs or spaces as set by IndentUseSpaces.
Values of -1
convert the entire text.
public void ConvertIndent(int fromLine = -1, int toLine = -1)
Parameters
CreateCodeRegion()
Creates a new code region with the selection. At least one single line comment delimiter have to be defined (see AddCommentDelimiter(string, string, bool)).
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 SetCodeRegionTags(string, string)).
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
)
public void CreateCodeRegion()
DeleteLines()
Deletes all lines that are selected or have a caret on them.
public void DeleteLines()
DoIndent()
If there is no selection, indentation is inserted at the caret. Otherwise, the selected lines are indented like IndentLines(). Equivalent to the ProjectSettings.input/ui_text_indent
action. The indentation characters used depend on IndentUseSpaces and IndentSize.
public void DoIndent()
DuplicateLines()
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.
public void DuplicateLines()
DuplicateSelection()
Duplicates all selected text and duplicates all lines with a caret on them.
public void DuplicateSelection()
EmitSignalBreakpointToggled(long)
protected void EmitSignalBreakpointToggled(long line)
Parameters
line
long
EmitSignalCodeCompletionRequested()
protected void EmitSignalCodeCompletionRequested()
EmitSignalSymbolHovered(string, long, long)
protected void EmitSignalSymbolHovered(string symbol, long line, long column)
Parameters
EmitSignalSymbolLookup(string, long, long)
protected void EmitSignalSymbolLookup(string symbol, long line, long column)
Parameters
EmitSignalSymbolValidate(string)
protected void EmitSignalSymbolValidate(string symbol)
Parameters
symbol
string
FoldAllLines()
Folds all lines that are possible to be folded (see CanFoldLine(int)).
public void FoldAllLines()
FoldLine(int)
Folds the given line, if possible (see CanFoldLine(int)).
public void FoldLine(int line)
Parameters
line
int
GetAutoBraceCompletionCloseKey(string)
Gets the matching auto brace close key for openKey
.
public string GetAutoBraceCompletionCloseKey(string openKey)
Parameters
openKey
string
Returns
GetBookmarkedLines()
Gets all bookmarked lines.
public int[] GetBookmarkedLines()
Returns
- int[]
GetBreakpointedLines()
Gets all breakpointed lines.
public int[] GetBreakpointedLines()
Returns
- int[]
GetCodeCompletionOption(int)
Gets the completion option at index
. The return Dictionary has the following key-values:
kind
: CodeEdit.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.
public Dictionary GetCodeCompletionOption(int index)
Parameters
index
int
Returns
GetCodeCompletionOptions()
Gets all completion options, see GetCodeCompletionOption(int) for return content.
public Array<Dictionary> GetCodeCompletionOptions()
Returns
GetCodeCompletionSelectedIndex()
Gets the index of the current selected completion option.
public int GetCodeCompletionSelectedIndex()
Returns
GetCodeRegionEndTag()
Returns the code region end tag (without comment delimiter).
public string GetCodeRegionEndTag()
Returns
GetCodeRegionStartTag()
Returns the code region start tag (without comment delimiter).
public string GetCodeRegionStartTag()
Returns
GetDelimiterEndKey(int)
Gets the end key for a string or comment region index.
public string GetDelimiterEndKey(int delimiterIndex)
Parameters
delimiterIndex
int
Returns
GetDelimiterEndPosition(int, int)
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
.
public Vector2 GetDelimiterEndPosition(int line, int column)
Parameters
Returns
GetDelimiterStartKey(int)
Gets the start key for a string or comment region index.
public string GetDelimiterStartKey(int delimiterIndex)
Parameters
delimiterIndex
int
Returns
GetDelimiterStartPosition(int, int)
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
.
public Vector2 GetDelimiterStartPosition(int line, int column)
Parameters
Returns
GetExecutingLines()
Gets all executing lines.
public int[] GetExecutingLines()
Returns
- int[]
GetFoldedLines()
Returns all lines that are currently folded.
public Array<int> GetFoldedLines()
Returns
GetTextForCodeCompletion()
Returns the full text with char 0xFFFF
at the caret location.
public string GetTextForCodeCompletion()
Returns
GetTextForSymbolLookup()
Returns the full text with char 0xFFFF
at the cursor location.
public string GetTextForSymbolLookup()
Returns
GetTextWithCursorChar(int, int)
Returns the full text with char 0xFFFF
at the specified location.
public string GetTextWithCursorChar(int line, int column)
Parameters
Returns
HasAutoBraceCompletionCloseKey(string)
Returns true if close key closeKey
exists.
public bool HasAutoBraceCompletionCloseKey(string closeKey)
Parameters
closeKey
string
Returns
HasAutoBraceCompletionOpenKey(string)
Returns true if open key openKey
exists.
public bool HasAutoBraceCompletionOpenKey(string openKey)
Parameters
openKey
string
Returns
HasCommentDelimiter(string)
Returns true if comment startKey
exists.
public bool HasCommentDelimiter(string startKey)
Parameters
startKey
string
Returns
HasGodotClassMethod(in godot_string_name)
Check if the type contains a method with the given name. This method is used by Godot to check if a method exists before invoking it. Do not call or override this method.
protected override bool HasGodotClassMethod(in godot_string_name method)
Parameters
method
godot_string_nameName of the method to check for.
Returns
HasGodotClassSignal(in godot_string_name)
Check if the type contains a signal with the given name. This method is used by Godot to check if a signal exists before raising it. Do not call or override this method.
protected override bool HasGodotClassSignal(in godot_string_name signal)
Parameters
signal
godot_string_nameName of the signal to check for.
Returns
HasStringDelimiter(string)
Returns true if string startKey
exists.
public bool HasStringDelimiter(string startKey)
Parameters
startKey
string
Returns
IndentLines()
Indents all lines that are selected or have a caret on them. Uses spaces or a tab depending on IndentUseSpaces. See UnindentLines().
public void IndentLines()
InvokeGodotClassMethod(in godot_string_name, NativeVariantPtrArgs, out godot_variant)
Invokes the method with the given name, using the given arguments. This method is used by Godot to invoke methods from the engine side. Do not call or override this method.
protected override bool InvokeGodotClassMethod(in godot_string_name method, NativeVariantPtrArgs args, out godot_variant ret)
Parameters
method
godot_string_nameName of the method to invoke.
args
NativeVariantPtrArgsArguments to use with the invoked method.
ret
godot_variantValue returned by the invoked method.
Returns
IsInComment(int, int)
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
.
public int IsInComment(int line, int column = -1)
Parameters
Returns
IsInString(int, int)
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
.
public int IsInString(int line, int column = -1)
Parameters
Returns
IsLineBookmarked(int)
Returns true if the given line is bookmarked. See SetLineAsBookmarked(int, bool).
public bool IsLineBookmarked(int line)
Parameters
line
int
Returns
IsLineBreakpointed(int)
Returns true if the given line is breakpointed. See SetLineAsBreakpoint(int, bool).
public bool IsLineBreakpointed(int line)
Parameters
line
int
Returns
IsLineCodeRegionEnd(int)
Returns true if the given line is a code region end. See SetCodeRegionTags(string, string).
public bool IsLineCodeRegionEnd(int line)
Parameters
line
int
Returns
IsLineCodeRegionStart(int)
Returns true if the given line is a code region start. See SetCodeRegionTags(string, string).
public bool IsLineCodeRegionStart(int line)
Parameters
line
int
Returns
IsLineExecuting(int)
Returns true if the given line is marked as executing. See SetLineAsExecuting(int, bool).
public bool IsLineExecuting(int line)
Parameters
line
int
Returns
IsLineFolded(int)
Returns true if the given line is folded. See FoldLine(int).
public bool IsLineFolded(int line)
Parameters
line
int
Returns
MoveLinesDown()
Moves all lines down that are selected or have a caret on them.
public void MoveLinesDown()
MoveLinesUp()
Moves all lines up that are selected or have a caret on them.
public void MoveLinesUp()
RemoveCommentDelimiter(string)
Removes the comment delimiter with startKey
.
public void RemoveCommentDelimiter(string startKey)
Parameters
startKey
string
RemoveStringDelimiter(string)
Removes the string delimiter with startKey
.
public void RemoveStringDelimiter(string startKey)
Parameters
startKey
string
RequestCodeCompletion(bool)
Emits CodeCompletionRequested, 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.
public void RequestCodeCompletion(bool force = false)
Parameters
force
bool
SetCodeCompletionSelectedIndex(int)
Sets the current selected completion option.
public void SetCodeCompletionSelectedIndex(int index)
Parameters
index
int
SetCodeHint(string)
Sets the code hint text. Pass an empty string to clear.
public void SetCodeHint(string codeHint)
Parameters
codeHint
string
SetCodeHintDrawBelow(bool)
If true, the code hint will draw below the main caret. If false, the code hint will draw above the main caret. See SetCodeHint(string).
public void SetCodeHintDrawBelow(bool drawBelow)
Parameters
drawBelow
bool
SetCodeRegionTags(string, string)
Sets the code region start and end tags (without comment delimiter).
public void SetCodeRegionTags(string start = "region", string end = "endregion")
Parameters
SetLineAsBookmarked(int, bool)
Sets the given line as bookmarked. If true and GuttersDrawBookmarks is true, draws the bookmark
icon in the gutter for this line. See GetBookmarkedLines() and IsLineBookmarked(int).
public void SetLineAsBookmarked(int line, bool bookmarked)
Parameters
SetLineAsBreakpoint(int, bool)
Sets the given line as a breakpoint. If true and GuttersDrawBreakpointsGutter is true, draws the breakpoint
icon in the gutter for this line. See GetBreakpointedLines() and IsLineBreakpointed(int).
public void SetLineAsBreakpoint(int line, bool breakpointed)
Parameters
SetLineAsExecuting(int, bool)
Sets the given line as executing. If true and GuttersDrawExecutingLines is true, draws the executing_line
icon in the gutter for this line. See GetExecutingLines() and IsLineExecuting(int).
public void SetLineAsExecuting(int line, bool executing)
Parameters
SetSymbolLookupWordAsValid(bool)
Sets the symbol emitted by SymbolValidate as a valid lookup.
public void SetSymbolLookupWordAsValid(bool valid)
Parameters
valid
bool
ToggleFoldableLine(int)
Toggle the folding of the code block at the given line.
public void ToggleFoldableLine(int line)
Parameters
line
int
ToggleFoldableLinesAtCarets()
Toggle the folding of the code block on all lines with a caret on them.
public void ToggleFoldableLinesAtCarets()
UnfoldAllLines()
Unfolds all lines that are folded.
public void UnfoldAllLines()
UnfoldLine(int)
Unfolds the given line if it is folded or if it is hidden under a folded line.
public void UnfoldLine(int line)
Parameters
line
int
UnindentLines()
Unindents all lines that are selected or have a caret on them. Uses spaces or a tab depending on IndentUseSpaces. Equivalent to the ProjectSettings.input/ui_text_dedent
action. See IndentLines().
public void UnindentLines()
UpdateCodeCompletionOptions(bool)
Submits all completion options added with AddCodeCompletionOption(CodeCompletionKind, string, string, Color?, Resource, Variant, int). Will try to force the autocomplete menu to popup, if force
is true.
Note: This will replace all current candidates.
public void UpdateCodeCompletionOptions(bool force)
Parameters
force
bool
_ConfirmCodeCompletion(bool)
Override this method to define how the selected entry should be inserted. If replace
is true, any existing text should be replaced.
public virtual void _ConfirmCodeCompletion(bool replace)
Parameters
replace
bool
_FilterCodeCompletionCandidates(Array<Dictionary>)
Override this method to define what items in candidates
should be displayed.
Both candidates
and the return is a Array of Dictionary, see GetCodeCompletionOption(int) for Dictionary content.
public virtual Array<Dictionary> _FilterCodeCompletionCandidates(Array<Dictionary> candidates)
Parameters
candidates
Array<Dictionary>
Returns
_RequestCodeCompletion(bool)
Override this method to define what happens when the user requests code completion. If force
is true, any checks should be bypassed.
public virtual void _RequestCodeCompletion(bool force)
Parameters
force
bool
Events
BreakpointToggled
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.
public event CodeEdit.BreakpointToggledEventHandler BreakpointToggled
Event Type
CodeCompletionRequested
Emitted when the user requests code completion. This signal will not be sent if _RequestCodeCompletion(bool) is overridden or CodeCompletionEnabled is false.
public event Action CodeCompletionRequested
Event Type
SymbolHovered
Emitted when the user hovers over a symbol. Unlike MouseEntered, this signal is not emitted immediately, but when the cursor is over the symbol for ProjectSettings.gui/timers/tooltip_delay_sec
seconds.
Note: SymbolTooltipOnHover must be true for this signal to be emitted.
public event CodeEdit.SymbolHoveredEventHandler SymbolHovered
Event Type
SymbolLookup
Emitted when the user has clicked on a valid symbol.
public event CodeEdit.SymbolLookupEventHandler SymbolLookup
Event Type
SymbolValidate
Emitted when the user hovers over a symbol. The symbol should be validated and responded to, by calling SetSymbolLookupWordAsValid(bool).
Note: SymbolLookupOnClick must be true for this signal to be emitted.
public event CodeEdit.SymbolValidateEventHandler SymbolValidate