0.2.0-dev Download ZIP
123 lines ·4.5 KB Raw
| 1 | <?xml version="1.0" encoding="UTF-8" ?> |
| 2 | <class name="SyntaxHighlighter" inherits="Resource" api_type="core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd"> |
| 3 | <brief_description> |
| 4 | Base class for syntax highlighters. Provides syntax highlighting data to a [TextEdit]. |
| 5 | </brief_description> |
| 6 | <description> |
| 7 | Base class for syntax highlighters. Provides syntax highlighting data to a [TextEdit]. The associated [TextEdit] will call into the [SyntaxHighlighter] on an as-needed basis. |
| 8 | [b]Note:[/b] A [SyntaxHighlighter] instance should not be used across multiple [TextEdit] nodes. |
| 9 | </description> |
| 10 | <tutorials> |
| 11 | </tutorials> |
| 12 | <methods> |
| 13 | <method name="_clear_highlighting_cache" qualifiers="virtual"> |
| 14 | <return type="void" /> |
| 15 | <description> |
| 16 | Virtual method which can be overridden to clear any local caches. |
| 17 | </description> |
| 18 | </method> |
| 19 | <method name="_get_line_syntax_highlighting" qualifiers="virtual const"> |
| 20 | <return type="Dictionary" /> |
| 21 | <param index="0" name="line" type="int" /> |
| 22 | <description> |
| 23 | Virtual method which can be overridden to return syntax highlighting data. |
| 24 | See [method get_line_syntax_highlighting] for more details. |
| 25 | </description> |
| 26 | </method> |
| 27 | <method name="_update_cache" qualifiers="virtual"> |
| 28 | <return type="void" /> |
| 29 | <description> |
| 30 | Virtual method which can be overridden to update any local caches. |
| 31 | </description> |
| 32 | </method> |
| 33 | <method name="clear_highlighting_cache"> |
| 34 | <return type="void" /> |
| 35 | <description> |
| 36 | Clears all cached syntax highlighting data. |
| 37 | Then calls overridable method [method _clear_highlighting_cache]. |
| 38 | </description> |
| 39 | </method> |
| 40 | <method name="get_line_syntax_highlighting"> |
| 41 | <return type="Dictionary" /> |
| 42 | <param index="0" name="line" type="int" /> |
| 43 | <description> |
| 44 | Returns the syntax highlighting data for the line at index [param line]. If the line is not cached, calls [method _get_line_syntax_highlighting] first to calculate the data. |
| 45 | Each entry is a column number containing a nested [Dictionary]. The column number denotes the start of a region, the region will end if another region is found, or at the end of the line. The nested [Dictionary] contains the data for that region. Currently only the key [code]"color"[/code] is supported. |
| 46 | [b]Example:[/b] Possible return value. This means columns [code]0[/code] to [code]4[/code] should be red, and columns [code]5[/code] to the end of the line should be green: |
| 47 | [codeblock] |
| 48 | { |
| 49 | 0: { |
| 50 | "color": Color(1, 0, 0) |
| 51 | }, |
| 52 | 5: { |
| 53 | "color": Color(0, 1, 0) |
| 54 | } |
| 55 | } |
| 56 | [/codeblock] |
| 57 | [b]Example:[/b] Sample implementation. Colors the word [code]Godot[/code] blue. |
| 58 | [codeblocks] |
| 59 | [gdscript] |
| 60 | func _get_line_syntax_highlighting(line: int) -> Dictionary: |
| 61 | var pattern_text = "Godot" |
| 62 | var result = Dictionary() |
| 63 | var text_edit = get_text_edit() |
| 64 | var line_text = text_edit.get_line(line) |
| 65 | |
| 66 | var pos = line_text.find(pattern_text) |
| 67 | while pos != -1: |
| 68 | result[pos] = { "color": Color(0.0, 0.0, 1.0) } |
| 69 | var end_pos = pos + pattern_text.length() |
| 70 | pos = line_text.find(pattern_text, end_pos) |
| 71 | if pos != end_pos: |
| 72 | result[end_pos] = { "color": Color(1.0, 1.0, 1.0) } |
| 73 | |
| 74 | return result |
| 75 | |
| 76 | [/gdscript] |
| 77 | [csharp] |
| 78 | public override Dictionary _GetLineSyntaxHighlighting(int line) |
| 79 | { |
| 80 | string patternText = "Godot"; |
| 81 | Dictionary result = new Dictionary(); |
| 82 | TextEdit textEdit = GetTextEdit(); |
| 83 | string lineText = textEdit.GetLine(line); |
| 84 | |
| 85 | var pos = lineText.Find(patternText); |
| 86 | while (pos != -1) |
| 87 | { |
| 88 | result[pos] = new Dictionary() |
| 89 | { |
| 90 | { "color", new Color(0.0f, 0.0f, 1.0f) } |
| 91 | }; |
| 92 | var endPos = pos + patternText.Length; |
| 93 | pos = lineText.Find(patternText, endPos); |
| 94 | if (pos != endPos) |
| 95 | { |
| 96 | result[endPos] = new Dictionary() |
| 97 | { |
| 98 | { "color", new Color(1.0f, 1.0f, 1.0f) } |
| 99 | }; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | return result; |
| 104 | } |
| 105 | [/csharp] |
| 106 | [/codeblocks] |
| 107 | </description> |
| 108 | </method> |
| 109 | <method name="get_text_edit" qualifiers="const"> |
| 110 | <return type="TextEdit" /> |
| 111 | <description> |
| 112 | Returns the associated [TextEdit] node. |
| 113 | </description> |
| 114 | </method> |
| 115 | <method name="update_cache"> |
| 116 | <return type="void" /> |
| 117 | <description> |
| 118 | Clears then updates the [SyntaxHighlighter] caches. Override [method _update_cache] for a callback. |
| 119 | [b]Note:[/b] This is called automatically when the associated [TextEdit] node, updates its own cache. |
| 120 | </description> |
| 121 | </method> |
| 122 | </methods> |
| 123 | </class> |