Skip to content

Commit fd75707

Browse files
committed
Merge pull request godotengine#78312 from Calinou/editor-add-markdown-highlighting
Add Markdown syntax highlighting to the script editor
2 parents 7444da7 + 3eb8b0a commit fd75707

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

editor/plugins/script_editor_plugin.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,52 @@ Ref<EditorSyntaxHighlighter> EditorJSONSyntaxHighlighter::_create() const {
261261
return syntax_highlighter;
262262
}
263263

264+
////
265+
266+
void EditorMarkdownSyntaxHighlighter::_update_cache() {
267+
highlighter->set_text_edit(text_edit);
268+
highlighter->clear_keyword_colors();
269+
highlighter->clear_member_keyword_colors();
270+
highlighter->clear_color_regions();
271+
272+
// Disable automatic symbolic highlights, as these don't make sense for prose.
273+
highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
274+
highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
275+
highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
276+
highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/text_color"));
277+
278+
// Headings (any level).
279+
const Color function_color = EDITOR_GET("text_editor/theme/highlighting/function_color");
280+
highlighter->add_color_region("#", "", function_color);
281+
282+
// Bold.
283+
highlighter->add_color_region("**", "**", function_color);
284+
// `__bold__` syntax is not supported as color regions must begin with a symbol,
285+
// not a character that is valid in an identifier.
286+
287+
// Code (both inline code and triple-backticks code blocks).
288+
const Color code_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
289+
highlighter->add_color_region("`", "`", code_color);
290+
291+
// Link (both references and inline links with URLs). The URL is not highlighted.
292+
const Color link_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
293+
highlighter->add_color_region("[", "]", link_color);
294+
295+
// Quote.
296+
const Color quote_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
297+
highlighter->add_color_region(">", "", quote_color, true);
298+
299+
// HTML comment, which is also supported in Markdown.
300+
const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
301+
highlighter->add_color_region("<!--", "-->", comment_color);
302+
}
303+
304+
Ref<EditorSyntaxHighlighter> EditorMarkdownSyntaxHighlighter::_create() const {
305+
Ref<EditorMarkdownSyntaxHighlighter> syntax_highlighter;
306+
syntax_highlighter.instantiate();
307+
return syntax_highlighter;
308+
}
309+
264310
////////////////////////////////////////////////////////////////////////////////
265311

266312
/*** SCRIPT EDITOR ****/
@@ -4414,6 +4460,10 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
44144460
json_syntax_highlighter.instantiate();
44154461
register_syntax_highlighter(json_syntax_highlighter);
44164462

4463+
Ref<EditorMarkdownSyntaxHighlighter> markdown_syntax_highlighter;
4464+
markdown_syntax_highlighter.instantiate();
4465+
register_syntax_highlighter(markdown_syntax_highlighter);
4466+
44174467
_update_online_doc();
44184468
}
44194469

editor/plugins/script_editor_plugin.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,24 @@ class EditorJSONSyntaxHighlighter : public EditorSyntaxHighlighter {
120120
EditorJSONSyntaxHighlighter() { highlighter.instantiate(); }
121121
};
122122

123+
class EditorMarkdownSyntaxHighlighter : public EditorSyntaxHighlighter {
124+
GDCLASS(EditorMarkdownSyntaxHighlighter, EditorSyntaxHighlighter)
125+
126+
private:
127+
Ref<CodeHighlighter> highlighter;
128+
129+
public:
130+
virtual void _update_cache() override;
131+
virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }
132+
133+
virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "md", "markdown" }; }
134+
virtual String _get_name() const override { return TTR("Markdown"); }
135+
136+
virtual Ref<EditorSyntaxHighlighter> _create() const override;
137+
138+
EditorMarkdownSyntaxHighlighter() { highlighter.instantiate(); }
139+
};
140+
123141
///////////////////////////////////////////////////////////////////////////////
124142

125143
class ScriptEditorQuickOpen : public ConfirmationDialog {

0 commit comments

Comments
 (0)