Skip to content

Commit 5d880bf

Browse files
authored
Merge pull request #40613 from HaSa1002/multi-lang-docs
Add multiple programming language support to class reference
2 parents 6d4d834 + 5b51ae1 commit 5d880bf

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
@@ -602,6 +602,43 @@ def escape_rst(text, until_pos=-1): # type: (str) -> str
602602
return text
603603

604604

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

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

659670
# Handle normal text
660671
else:
@@ -699,7 +710,7 @@ def rstize_text(text, state): # type: (str, State) -> str
699710
else: # command
700711
cmd = tag_text
701712
space_pos = tag_text.find(" ")
702-
if cmd == "/codeblock":
713+
if cmd == "/codeblock" or cmd == "/gdscript" or cmd == "/csharp":
703714
tag_text = ""
704715
tag_depth -= 1
705716
inside_code = False
@@ -815,6 +826,20 @@ def rstize_text(text, state): # type: (str, State) -> str
815826
tag_depth += 1
816827
tag_text = "\n::\n"
817828
inside_code = True
829+
elif cmd == "gdscript":
830+
tag_depth += 1
831+
tag_text = "\n .. code-tab:: gdscript GDScript\n"
832+
inside_code = True
833+
elif cmd == "csharp":
834+
tag_depth += 1
835+
tag_text = "\n .. code-tab:: csharp\n"
836+
inside_code = True
837+
elif cmd == "codeblocks":
838+
tag_depth += 1
839+
tag_text = "\n.. tabs::"
840+
elif cmd == "/codeblocks":
841+
tag_depth -= 1
842+
tag_text = ""
818843
elif cmd == "br":
819844
# Make a new paragraph instead of a linebreak, rst is not so linebreak friendly
820845
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)