Skip to content

Commit 5b51ae1

Browse files
committed
Add multiple programming language support to class reference
1 parent 0cd98ec commit 5b51ae1

File tree

3 files changed

+113
-37
lines changed

3 files changed

+113
-37
lines changed

doc/tools/makerst.py

+62-37
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,43 @@ def escape_rst(text, until_pos=-1): # type: (str) -> str
600600
return text
601601

602602

603+
def format_codeblock(code_type, post_text, indent_level, state): # types: str, str, int, state
604+
end_pos = post_text.find("[/" + code_type + "]")
605+
if end_pos == -1:
606+
print_error("[" + code_type + "] without a closing tag, file: {}".format(state.current_class), state)
607+
return None
608+
609+
code_text = post_text[len("[" + code_type + "]") : end_pos]
610+
post_text = post_text[end_pos:]
611+
612+
# Remove extraneous tabs
613+
code_pos = 0
614+
while True:
615+
code_pos = code_text.find("\n", code_pos)
616+
if code_pos == -1:
617+
break
618+
619+
to_skip = 0
620+
while code_pos + to_skip + 1 < len(code_text) and code_text[code_pos + to_skip + 1] == "\t":
621+
to_skip += 1
622+
623+
if to_skip > indent_level:
624+
print_error(
625+
"Four spaces should be used for indentation within ["
626+
+ code_type
627+
+ "], file: {}".format(state.current_class),
628+
state,
629+
)
630+
631+
if len(code_text[code_pos + to_skip + 1 :]) == 0:
632+
code_text = code_text[:code_pos] + "\n"
633+
code_pos += 1
634+
else:
635+
code_text = code_text[:code_pos] + "\n " + code_text[code_pos + to_skip + 1 :]
636+
code_pos += 5 - to_skip
637+
return ["\n[" + code_type + "]" + code_text + post_text, len("\n[" + code_type + "]" + code_text)]
638+
639+
603640
def rstize_text(text, state): # type: (str, State) -> str
604641
# Linebreak + tabs in the XML should become two line breaks unless in a "codeblock"
605642
pos = 0
@@ -616,43 +653,17 @@ def rstize_text(text, state): # type: (str, State) -> str
616653
post_text = text[pos + 1 :]
617654

618655
# Handle codeblocks
619-
if post_text.startswith("[codeblock]"):
620-
end_pos = post_text.find("[/codeblock]")
621-
if end_pos == -1:
622-
print_error("[codeblock] without a closing tag, file: {}".format(state.current_class), state)
656+
if (
657+
post_text.startswith("[codeblock]")
658+
or post_text.startswith("[gdscript]")
659+
or post_text.startswith("[csharp]")
660+
):
661+
block_type = post_text[1:].split("]")[0]
662+
result = format_codeblock(block_type, post_text, indent_level, state)
663+
if result is None:
623664
return ""
624-
625-
code_text = post_text[len("[codeblock]") : end_pos]
626-
post_text = post_text[end_pos:]
627-
628-
# Remove extraneous tabs
629-
code_pos = 0
630-
while True:
631-
code_pos = code_text.find("\n", code_pos)
632-
if code_pos == -1:
633-
break
634-
635-
to_skip = 0
636-
while code_pos + to_skip + 1 < len(code_text) and code_text[code_pos + to_skip + 1] == "\t":
637-
to_skip += 1
638-
639-
if to_skip > indent_level:
640-
print_error(
641-
"Four spaces should be used for indentation within [codeblock], file: {}".format(
642-
state.current_class
643-
),
644-
state,
645-
)
646-
647-
if len(code_text[code_pos + to_skip + 1 :]) == 0:
648-
code_text = code_text[:code_pos] + "\n"
649-
code_pos += 1
650-
else:
651-
code_text = code_text[:code_pos] + "\n " + code_text[code_pos + to_skip + 1 :]
652-
code_pos += 5 - to_skip
653-
654-
text = pre_text + "\n[codeblock]" + code_text + post_text
655-
pos += len("\n[codeblock]" + code_text)
665+
text = pre_text + result[0]
666+
pos += result[1]
656667

657668
# Handle normal text
658669
else:
@@ -697,7 +708,7 @@ def rstize_text(text, state): # type: (str, State) -> str
697708
else: # command
698709
cmd = tag_text
699710
space_pos = tag_text.find(" ")
700-
if cmd == "/codeblock":
711+
if cmd == "/codeblock" or cmd == "/gdscript" or cmd == "/csharp":
701712
tag_text = ""
702713
tag_depth -= 1
703714
inside_code = False
@@ -813,6 +824,20 @@ def rstize_text(text, state): # type: (str, State) -> str
813824
tag_depth += 1
814825
tag_text = "\n::\n"
815826
inside_code = True
827+
elif cmd == "gdscript":
828+
tag_depth += 1
829+
tag_text = "\n .. code-tab:: gdscript GDScript\n"
830+
inside_code = True
831+
elif cmd == "csharp":
832+
tag_depth += 1
833+
tag_text = "\n .. code-tab:: csharp\n"
834+
inside_code = True
835+
elif cmd == "codeblocks":
836+
tag_depth += 1
837+
tag_text = "\n.. tabs::"
838+
elif cmd == "/codeblocks":
839+
tag_depth -= 1
840+
tag_text = ""
816841
elif cmd == "br":
817842
# Make a new paragraph instead of a linebreak, rst is not so linebreak friendly
818843
tag_text = "\n\n"

editor/editor_help.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -1252,6 +1252,55 @@ static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt) {
12521252

12531253
String bbcode = p_bbcode.dedent().replace("\t", "").replace("\r", "").strip_edges();
12541254

1255+
// Select the correct code examples
1256+
switch ((int)EDITOR_GET("text_editor/help/class_reference_examples")) {
1257+
case 0: // GDScript
1258+
bbcode = bbcode.replace("[gdscript]", "[codeblock]");
1259+
bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
1260+
1261+
for (int pos = bbcode.find("[csharp]"); pos != -1; pos = bbcode.find("[csharp]")) {
1262+
if (bbcode.find("[/csharp]") == -1) {
1263+
WARN_PRINT("Unclosed [csharp] block or parse fail in code (search for tag errors)");
1264+
break;
1265+
}
1266+
1267+
bbcode.erase(pos, bbcode.find("[/csharp]") + 9 - pos);
1268+
while (bbcode[pos] == '\n') {
1269+
bbcode.erase(pos, 1);
1270+
}
1271+
}
1272+
break;
1273+
case 1: // C#
1274+
bbcode = bbcode.replace("[csharp]", "[codeblock]");
1275+
bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
1276+
1277+
for (int pos = bbcode.find("[gdscript]"); pos != -1; pos = bbcode.find("[gdscript]")) {
1278+
if (bbcode.find("[/gdscript]") == -1) {
1279+
WARN_PRINT("Unclosed [gdscript] block or parse fail in code (search for tag errors)");
1280+
break;
1281+
}
1282+
1283+
bbcode.erase(pos, bbcode.find("[/gdscript]") + 11 - pos);
1284+
while (bbcode[pos] == '\n') {
1285+
bbcode.erase(pos, 1);
1286+
}
1287+
}
1288+
break;
1289+
case 2: // GDScript and C#
1290+
bbcode = bbcode.replace("[csharp]", "[b]C#:[/b]\n[codeblock]");
1291+
bbcode = bbcode.replace("[gdscript]", "[b]GDScript:[/b]\n[codeblock]");
1292+
1293+
bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
1294+
bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
1295+
break;
1296+
}
1297+
1298+
// Remove codeblocks (they would be printed otherwise)
1299+
bbcode = bbcode.replace("[codeblocks]\n", "");
1300+
bbcode = bbcode.replace("\n[/codeblocks]", "");
1301+
bbcode = bbcode.replace("[codeblocks]", "");
1302+
bbcode = bbcode.replace("[/codeblocks]", "");
1303+
12551304
// remove extra new lines around code blocks
12561305
bbcode = bbcode.replace("[codeblock]\n", "[codeblock]");
12571306
bbcode = bbcode.replace("\n[/codeblock]", "[/codeblock]");

editor/editor_settings.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,8 @@ void EditorSettings::_load_defaults(Ref<ConfigFile> p_extra_config) {
495495
hints["text_editor/help/help_source_font_size"] = PropertyInfo(Variant::INT, "text_editor/help/help_source_font_size", PROPERTY_HINT_RANGE, "8,48,1");
496496
_initial_set("text_editor/help/help_title_font_size", 23);
497497
hints["text_editor/help/help_title_font_size"] = PropertyInfo(Variant::INT, "text_editor/help/help_title_font_size", PROPERTY_HINT_RANGE, "8,48,1");
498+
_initial_set("text_editor/help/class_reference_examples", 0);
499+
hints["text_editor/help/class_reference_examples"] = PropertyInfo(Variant::INT, "text_editor/help/class_reference_examples", PROPERTY_HINT_ENUM, "GDScript,C#,GDScript and C#");
498500

499501
/* Editors */
500502

0 commit comments

Comments
 (0)