LUAEditorSyntaxHighlighter.hxx 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #if !defined(Q_MOC_RUN)
  9. #include <AzCore/base.h>
  10. #include <AzCore/Memory/SystemAllocator.h>
  11. #include <AzCore/std/containers/unordered_set.h>
  12. #include <AzCore/std/containers/vector.h>
  13. #include <AzCore/std/function/function_template.h>
  14. #include <AzCore/std/string/string.h>
  15. #include <QColor>
  16. #include <QRegularExpression>
  17. #include <QSyntaxHighlighter>
  18. #include <QTextEdit>
  19. #endif
  20. #pragma once
  21. namespace LUAEditor
  22. {
  23. class LUASyntaxHighlighter
  24. : public QSyntaxHighlighter
  25. {
  26. Q_OBJECT
  27. public:
  28. class StateMachine;
  29. AZ_CLASS_ALLOCATOR(LUASyntaxHighlighter, AZ::SystemAllocator);
  30. LUASyntaxHighlighter(QWidget* parent);
  31. LUASyntaxHighlighter(QTextDocument* parent);
  32. ~LUASyntaxHighlighter();
  33. void highlightBlock(const QString& text) override;
  34. //set to -1 to disable bracket highlighting
  35. void SetBracketHighlighting(int openBracketPos, int closeBracketPos);
  36. //this is done using QT extraSelections not regular highlighting
  37. QList<QTextEdit::ExtraSelection> HighlightMatchingNames(const QTextCursor& cursor, const QString& text) const;
  38. //returns empty string if cursor is not currently on a lua name i.e. in a string literal or comment.
  39. QString GetLUAName(const QTextCursor& cursor);
  40. signals:
  41. void LUANamesInScopeChanged(const QStringList& luaNames) const;
  42. private:
  43. void BuildRegExes();
  44. void AddBlockKeywords();
  45. StateMachine* m_machine;
  46. AZStd::unordered_set<AZStd::string> m_LUAStartBlockKeywords;
  47. AZStd::unordered_set<AZStd::string> m_LUAEndBlockKeywords;
  48. struct HighlightingRule
  49. {
  50. bool stopProcessingMoreRulesAfterThis = false;
  51. QRegularExpression pattern;
  52. AZStd::function<QColor()> colorCB;
  53. };
  54. AZStd::vector<HighlightingRule> m_highlightingRules;
  55. int m_openBracketPos{-1};
  56. int m_closeBracketPos{-1};
  57. mutable int m_currentScopeBlock{-1};
  58. };
  59. }