The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCER_SOURCECODEEDITOR_JUCEHEADER__
  19. #define __JUCER_SOURCECODEEDITOR_JUCEHEADER__
  20. #include "../Project/jucer_Project.h"
  21. #include "../Application/jucer_DocumentEditorComponent.h"
  22. //==============================================================================
  23. class SourceCodeEditor : public DocumentEditorComponent,
  24. private ValueTree::Listener
  25. {
  26. public:
  27. SourceCodeEditor (OpenDocumentManager::Document* document, CodeDocument& codeDocument);
  28. SourceCodeEditor (OpenDocumentManager::Document* document);
  29. ~SourceCodeEditor();
  30. void createEditor (CodeDocument& codeDocument);
  31. void setEditor (CodeEditorComponent*);
  32. private:
  33. ScopedPointer<CodeEditorComponent> editor;
  34. void resized();
  35. void valueTreePropertyChanged (ValueTree&, const Identifier&);
  36. void valueTreeChildAdded (ValueTree&, ValueTree&);
  37. void valueTreeChildRemoved (ValueTree&, ValueTree&);
  38. void valueTreeChildOrderChanged (ValueTree&);
  39. void valueTreeParentChanged (ValueTree&);
  40. void valueTreeRedirected (ValueTree&);
  41. void updateColourScheme();
  42. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor);
  43. };
  44. //==============================================================================
  45. class CppCodeEditorComponent : public CodeEditorComponent
  46. {
  47. public:
  48. CppCodeEditorComponent (CodeDocument& codeDocument)
  49. : CodeEditorComponent (codeDocument, getCppTokeniser())
  50. {
  51. }
  52. void handleReturnKey()
  53. {
  54. CodeEditorComponent::handleReturnKey();
  55. const CodeDocument::Position pos (getCaretPos());
  56. if (pos.getLineNumber() > 0 && pos.getLineText().trim().isEmpty())
  57. {
  58. String indent (getIndentForCurrentBlock (pos));
  59. const String previousLine (pos.movedByLines (-1).getLineText());
  60. const String trimmedPreviousLine (previousLine.trim());
  61. const String leadingWhitespace (getLeadingWhitespace (previousLine));
  62. insertTextAtCaret (leadingWhitespace);
  63. if (trimmedPreviousLine.endsWithChar ('{')
  64. || ((trimmedPreviousLine.startsWith ("if ")
  65. || trimmedPreviousLine.startsWith ("for ")
  66. || trimmedPreviousLine.startsWith ("while "))
  67. && trimmedPreviousLine.endsWithChar (')')))
  68. insertTabAtCaret();
  69. }
  70. }
  71. void insertTextAtCaret (const String& newText)
  72. {
  73. if (getHighlightedRegion().isEmpty())
  74. {
  75. const CodeDocument::Position pos (getCaretPos());
  76. if ((newText == "{" || newText == "}")
  77. && pos.getLineNumber() > 0
  78. && pos.getLineText().trim().isEmpty())
  79. {
  80. moveCaretToStartOfLine (true);
  81. CodeEditorComponent::insertTextAtCaret (getIndentForCurrentBlock (pos));
  82. if (newText == "{")
  83. insertTabAtCaret();
  84. }
  85. }
  86. CodeEditorComponent::insertTextAtCaret (newText);
  87. }
  88. private:
  89. static CPlusPlusCodeTokeniser* getCppTokeniser()
  90. {
  91. static CPlusPlusCodeTokeniser cppTokeniser;
  92. return &cppTokeniser;
  93. }
  94. static String getLeadingWhitespace (String line)
  95. {
  96. line = line.removeCharacters ("\r\n");
  97. const String::CharPointerType endOfLeadingWS (line.getCharPointer().findEndOfWhitespace());
  98. return String (line.getCharPointer(), endOfLeadingWS);
  99. }
  100. static String getIndentForCurrentBlock (CodeDocument::Position pos)
  101. {
  102. int braceCount = 0;
  103. while (pos.getLineNumber() > 0)
  104. {
  105. pos = pos.movedByLines (-1);
  106. const String line (pos.getLineText());
  107. const String trimmedLine (line.trimStart());
  108. StringArray tokens;
  109. tokens.addTokens (trimmedLine, true);
  110. for (int i = tokens.size(); --i >= 0;)
  111. {
  112. if (tokens[i] == "}")
  113. ++braceCount;
  114. if (tokens[i] == "{")
  115. if (--braceCount < 0)
  116. return getLeadingWhitespace (line);
  117. }
  118. }
  119. return String::empty;
  120. }
  121. };
  122. #endif // __JUCER_SOURCECODEEDITOR_JUCEHEADER__