diff options
author | Danil Alexeev <danil@alexeev.xyz> | 2024-03-08 22:16:55 +0300 |
---|---|---|
committer | Danil Alexeev <danil@alexeev.xyz> | 2024-03-09 00:02:31 +0300 |
commit | 87718d2a6e3dd7f625b257ff7e5d11ce24b28ff6 (patch) | |
tree | 9fd165e19abaf5548399d889b6d3e4208b2e013e /modules | |
parent | ffc41fb76df5922321cdd98cce12715a039629b0 (diff) | |
download | redot-engine-87718d2a6e3dd7f625b257ff7e5d11ce24b28ff6.tar.gz |
Editor Help: Add syntax highlighting for code blocks
Diffstat (limited to 'modules')
-rw-r--r-- | modules/gdscript/doc_classes/@GDScript.xml | 12 | ||||
-rw-r--r-- | modules/gdscript/gdscript_parser.cpp | 24 |
2 files changed, 25 insertions, 11 deletions
diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index a68d65e8d3..d98580b771 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -95,7 +95,7 @@ print(get_stack()) [/codeblock] Starting from [code]_ready()[/code], [code]bar()[/code] would print: - [codeblock] + [codeblock lang=text] [{function:bar, line:12, source:res://script.gd}, {function:foo, line:9, source:res://script.gd}, {function:_ready, line:6, source:res://script.gd}] [/codeblock] [b]Note:[/b] This function only works if the running instance is connected to a debugging server (i.e. an editor instance). [method get_stack] will not work in projects exported in release mode, or in projects exported in debug mode if not connected to a debugging server. @@ -116,7 +116,7 @@ print(d.values()) [/codeblock] Prints out: - [codeblock] + [codeblock lang=text] [@subpath, @path, foo] [, res://test.gd, bar] [/codeblock] @@ -190,7 +190,7 @@ <description> Like [method @GlobalScope.print], but includes the current stack frame when running with the debugger turned on. The output in the console may look like the following: - [codeblock] + [codeblock lang=text] Test print At: res://test.gd:15:_process() [/codeblock] @@ -202,7 +202,7 @@ <description> Prints a stack trace at the current code location. See also [method get_stack]. The output in the console may look like the following: - [codeblock] + [codeblock lang=text] Frame 0 - res://test.gd:16 in function '_process' [/codeblock] [b]Note:[/b] This function only works if the running instance is connected to a debugging server (i.e. an editor instance). [method print_stack] will not work in projects exported in release mode, or in projects exported in debug mode if not connected to a debugging server. @@ -232,7 +232,7 @@ print(array[i]) [/codeblock] Output: - [codeblock] + [codeblock lang=text] 9 6 3 @@ -243,7 +243,7 @@ print(i / 10.0) [/codeblock] Output: - [codeblock] + [codeblock lang=text] 0.3 0.2 0.1 diff --git a/modules/gdscript/gdscript_parser.cpp b/modules/gdscript/gdscript_parser.cpp index aae45274e0..49341cb670 100644 --- a/modules/gdscript/gdscript_parser.cpp +++ b/modules/gdscript/gdscript_parser.cpp @@ -3460,6 +3460,7 @@ enum DocLineState { DOC_LINE_NORMAL, DOC_LINE_IN_CODE, DOC_LINE_IN_CODEBLOCK, + DOC_LINE_IN_KBD, }; static String _process_doc_line(const String &p_line, const String &p_text, const String &p_space_prefix, DocLineState &r_state) { @@ -3505,21 +3506,23 @@ static String _process_doc_line(const String &p_line, const String &p_text, cons from = rb_pos + 1; String tag = line.substr(lb_pos + 1, rb_pos - lb_pos - 1); - if (tag == "code") { + if (tag == "code" || tag.begins_with("code ")) { r_state = DOC_LINE_IN_CODE; - } else if (tag == "codeblock") { + } else if (tag == "codeblock" || tag.begins_with("codeblock ")) { if (lb_pos == 0) { line_join = "\n"; } else { result += line.substr(buffer_start, lb_pos - buffer_start) + '\n'; } - result += "[codeblock]"; + result += "[" + tag + "]"; if (from < len) { result += '\n'; } r_state = DOC_LINE_IN_CODEBLOCK; buffer_start = from; + } else if (tag == "kbd") { + r_state = DOC_LINE_IN_KBD; } } break; case DOC_LINE_IN_CODE: { @@ -3529,7 +3532,7 @@ static String _process_doc_line(const String &p_line, const String &p_text, cons break; } - from = pos + 7; + from = pos + 7; // `len("[/code]")`. r_state = DOC_LINE_NORMAL; } break; @@ -3540,7 +3543,7 @@ static String _process_doc_line(const String &p_line, const String &p_text, cons break; } - from = pos + 12; + from = pos + 12; // `len("[/codeblock]")`. if (pos == 0) { line_join = "\n"; @@ -3555,6 +3558,17 @@ static String _process_doc_line(const String &p_line, const String &p_text, cons r_state = DOC_LINE_NORMAL; buffer_start = from; } break; + case DOC_LINE_IN_KBD: { + int pos = line.find("[/kbd]", from); + if (pos < 0) { + process = false; + break; + } + + from = pos + 6; // `len("[/kbd]")`. + + r_state = DOC_LINE_NORMAL; + } break; } } |