Skip to content

Commit 3eb8b0a

Browse files
committed
Add Markdown syntax highlighting to the script editor
1 parent 9425535 commit 3eb8b0a

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
@@ -259,6 +259,52 @@ Ref<EditorSyntaxHighlighter> EditorJSONSyntaxHighlighter::_create() const {
259259
return syntax_highlighter;
260260
}
261261

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

264310
/*** SCRIPT EDITOR ****/
@@ -4331,6 +4377,10 @@ ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
43314377
json_syntax_highlighter.instantiate();
43324378
register_syntax_highlighter(json_syntax_highlighter);
43334379

4380+
Ref<EditorMarkdownSyntaxHighlighter> markdown_syntax_highlighter;
4381+
markdown_syntax_highlighter.instantiate();
4382+
register_syntax_highlighter(markdown_syntax_highlighter);
4383+
43344384
_update_online_doc();
43354385
}
43364386

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)