Path: blob/master/editor/script/syntax_highlighters.h
20860 views
/**************************************************************************/1/* syntax_highlighters.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "scene/resources/syntax_highlighter.h"3334class EditorSyntaxHighlighter : public SyntaxHighlighter {35GDCLASS(EditorSyntaxHighlighter, SyntaxHighlighter)3637private:38Ref<RefCounted> edited_resource;3940protected:41static void _bind_methods();4243GDVIRTUAL0RC(String, _get_name)44GDVIRTUAL0RC(PackedStringArray, _get_supported_languages)45GDVIRTUAL0RC(Ref<EditorSyntaxHighlighter>, _create)4647public:48virtual String _get_name() const;49virtual PackedStringArray _get_supported_languages() const;5051void _set_edited_resource(const Ref<Resource> &p_res) { edited_resource = p_res; }52Ref<RefCounted> _get_edited_resource() { return edited_resource; }5354virtual Ref<EditorSyntaxHighlighter> _create() const;55};5657class EditorStandardSyntaxHighlighter : public EditorSyntaxHighlighter {58GDCLASS(EditorStandardSyntaxHighlighter, EditorSyntaxHighlighter)5960private:61Ref<CodeHighlighter> highlighter;62ScriptLanguage *script_language = nullptr; // See GH-89610.6364public:65virtual void _update_cache() override;66virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }6768virtual String _get_name() const override { return TTR("Standard"); }6970virtual Ref<EditorSyntaxHighlighter> _create() const override;7172void _set_script_language(ScriptLanguage *p_script_language) { script_language = p_script_language; }7374EditorStandardSyntaxHighlighter() { highlighter.instantiate(); }75};7677class EditorPlainTextSyntaxHighlighter : public EditorSyntaxHighlighter {78GDCLASS(EditorPlainTextSyntaxHighlighter, EditorSyntaxHighlighter)7980public:81virtual String _get_name() const override { return TTR("Plain Text"); }8283virtual Ref<EditorSyntaxHighlighter> _create() const override;84};8586class EditorJSONSyntaxHighlighter : public EditorSyntaxHighlighter {87GDCLASS(EditorJSONSyntaxHighlighter, EditorSyntaxHighlighter)8889private:90Ref<CodeHighlighter> highlighter;9192public:93virtual void _update_cache() override;94virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }9596virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "json" }; }97virtual String _get_name() const override { return TTR("JSON"); }9899virtual Ref<EditorSyntaxHighlighter> _create() const override;100101EditorJSONSyntaxHighlighter() { highlighter.instantiate(); }102};103104class EditorMarkdownSyntaxHighlighter : public EditorSyntaxHighlighter {105GDCLASS(EditorMarkdownSyntaxHighlighter, EditorSyntaxHighlighter)106107private:108Ref<CodeHighlighter> highlighter;109110public:111virtual void _update_cache() override;112virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }113114virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "md", "markdown" }; }115virtual String _get_name() const override { return TTR("Markdown"); }116117virtual Ref<EditorSyntaxHighlighter> _create() const override;118119EditorMarkdownSyntaxHighlighter() { highlighter.instantiate(); }120};121122class EditorConfigFileSyntaxHighlighter : public EditorSyntaxHighlighter {123GDCLASS(EditorConfigFileSyntaxHighlighter, EditorSyntaxHighlighter)124125private:126Ref<CodeHighlighter> highlighter;127128public:129virtual void _update_cache() override;130virtual Dictionary _get_line_syntax_highlighting_impl(int p_line) override { return highlighter->get_line_syntax_highlighting(p_line); }131132// While not explicitly designed for those formats, this highlighter happens133// to handle TSCN, TRES, `project.godot` well. We can expose it in case the134// user opens one of these using the script editor (which can be done using135// the All Files filter).136virtual PackedStringArray _get_supported_languages() const override { return PackedStringArray{ "ini", "cfg", "tscn", "tres", "godot" }; }137virtual String _get_name() const override { return TTR("ConfigFile"); }138139virtual Ref<EditorSyntaxHighlighter> _create() const override;140141EditorConfigFileSyntaxHighlighter() { highlighter.instantiate(); }142};143144145