LUAEditorSyntaxHighlighter.hxx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 <QSyntaxHighlighter>
  11. #include <AzCore/Memory/SystemAllocator.h>
  12. #include <AzCore/std/string/string.h>
  13. #include <AzCore/std/containers/unordered_set.h>
  14. #include <QTextEdit>
  15. #endif
  16. #pragma once
  17. namespace LUAEditor
  18. {
  19. class LUASyntaxHighlighter
  20. : public QSyntaxHighlighter
  21. {
  22. Q_OBJECT
  23. public:
  24. class StateMachine;
  25. AZ_CLASS_ALLOCATOR(LUASyntaxHighlighter, AZ::SystemAllocator);
  26. LUASyntaxHighlighter(QWidget* parent);
  27. LUASyntaxHighlighter(QTextDocument* parent);
  28. ~LUASyntaxHighlighter();
  29. void highlightBlock(const QString& text) override;
  30. //set to -1 to disable bracket highlighting
  31. void SetBracketHighlighting(int openBracketPos, int closeBracketPos);
  32. void SetFont(QFont font) { m_font = font; }
  33. //this is done using QT extraSelections not regular highlighting
  34. QList<QTextEdit::ExtraSelection> HighlightMatchingNames(const QTextCursor& cursor, const QString& text) const;
  35. //returns empty string if cursor is not currently on a lua name i.e. in a string literal or comment.
  36. QString GetLUAName(const QTextCursor& cursor);
  37. signals:
  38. void LUANamesInScopeChanged(const QStringList& luaNames) const;
  39. private:
  40. void AddBlockKeywords();
  41. StateMachine* m_machine;
  42. AZStd::unordered_set<AZStd::string> m_LUAStartBlockKeywords;
  43. AZStd::unordered_set<AZStd::string> m_LUAEndBlockKeywords;
  44. int m_openBracketPos{-1};
  45. int m_closeBracketPos{-1};
  46. QFont m_font{"OpenSans", 10};
  47. mutable int m_currentScopeBlock{-1};
  48. };
  49. }