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.

243 lines
8.9KB

  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 SourceCodeDocument : public OpenDocumentManager::Document
  24. {
  25. public:
  26. //==============================================================================
  27. SourceCodeDocument (Project*, const File&);
  28. bool loadedOk() const { return true; }
  29. bool isForFile (const File& file) const { return getFile() == file; }
  30. bool isForNode (const ValueTree& node) const { return false; }
  31. bool refersToProject (Project& p) const { return project == &p; }
  32. Project* getProject() const { return project; }
  33. String getName() const { return getFile().getFileName(); }
  34. String getType() const { return getFile().getFileExtension() + " file"; }
  35. File getFile() const { return modDetector.getFile(); }
  36. bool needsSaving() const { return codeDoc != nullptr && codeDoc->hasChangedSinceSavePoint(); }
  37. bool hasFileBeenModifiedExternally() { return modDetector.hasBeenModified(); }
  38. void fileHasBeenRenamed (const File& newFile) { modDetector.fileHasBeenRenamed (newFile); }
  39. String getState() const { return lastState != nullptr ? lastState->toString() : String::empty; }
  40. void restoreState (const String& state) { lastState = new CodeEditorComponent::State (state); }
  41. void reloadFromFile();
  42. bool save();
  43. Component* createEditor();
  44. Component* createViewer() { return createEditor(); }
  45. void updateLastState (CodeEditorComponent& editor);
  46. void applyLastState (CodeEditorComponent& editor) const;
  47. CodeDocument& getCodeDocument();
  48. //==============================================================================
  49. struct Type : public OpenDocumentManager::DocumentType
  50. {
  51. bool canOpenFile (const File& file) { return file.hasFileExtension ("cpp;h;hpp;mm;m;c;cc;cxx;txt;inc;tcc;xml;plist;rtf;html;htm;php;py;rb;cs"); }
  52. Document* openFile (Project* project, const File& file) { return new SourceCodeDocument (project, file); }
  53. };
  54. protected:
  55. FileModificationDetector modDetector;
  56. ScopedPointer<CodeDocument> codeDoc;
  57. Project* project;
  58. ScopedPointer<CodeEditorComponent::State> lastState;
  59. void reloadInternal();
  60. };
  61. //==============================================================================
  62. class SourceCodeEditor : public DocumentEditorComponent,
  63. private ValueTree::Listener
  64. {
  65. public:
  66. SourceCodeEditor (OpenDocumentManager::Document* document);
  67. ~SourceCodeEditor();
  68. void createEditor (CodeDocument& codeDocument);
  69. void setEditor (CodeEditorComponent*);
  70. void highlightLine (int lineNum, int characterIndex);
  71. ScopedPointer<CodeEditorComponent> editor;
  72. private:
  73. void resized();
  74. void valueTreePropertyChanged (ValueTree&, const Identifier&);
  75. void valueTreeChildAdded (ValueTree&, ValueTree&);
  76. void valueTreeChildRemoved (ValueTree&, ValueTree&);
  77. void valueTreeChildOrderChanged (ValueTree&);
  78. void valueTreeParentChanged (ValueTree&);
  79. void valueTreeRedirected (ValueTree&);
  80. void updateColourScheme();
  81. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (SourceCodeEditor);
  82. };
  83. //==============================================================================
  84. class CppCodeEditorComponent : public CodeEditorComponent
  85. {
  86. public:
  87. CppCodeEditorComponent (CodeDocument& codeDocument)
  88. : CodeEditorComponent (codeDocument, getCppTokeniser())
  89. {
  90. }
  91. void handleReturnKey()
  92. {
  93. CodeEditorComponent::handleReturnKey();
  94. const CodeDocument::Position pos (getCaretPos());
  95. if (pos.getLineNumber() > 0 && pos.getLineText().trim().isEmpty())
  96. {
  97. String indent;
  98. getIndentForCurrentBlock (pos, indent);
  99. const String previousLine (pos.movedByLines (-1).getLineText());
  100. const String trimmedPreviousLine (previousLine.trim());
  101. const String leadingWhitespace (getLeadingWhitespace (previousLine));
  102. insertTextAtCaret (leadingWhitespace);
  103. if (trimmedPreviousLine.endsWithChar ('{')
  104. || ((trimmedPreviousLine.startsWith ("if ")
  105. || trimmedPreviousLine.startsWith ("for ")
  106. || trimmedPreviousLine.startsWith ("while "))
  107. && trimmedPreviousLine.endsWithChar (')')))
  108. insertTabAtCaret();
  109. }
  110. }
  111. void insertTextAtCaret (const String& newText)
  112. {
  113. if (getHighlightedRegion().isEmpty())
  114. {
  115. const CodeDocument::Position pos (getCaretPos());
  116. if ((newText == "{" || newText == "}")
  117. && pos.getLineNumber() > 0
  118. && pos.getLineText().trim().isEmpty())
  119. {
  120. moveCaretToStartOfLine (true);
  121. String whitespace;
  122. if (getIndentForCurrentBlock (pos, whitespace))
  123. {
  124. CodeEditorComponent::insertTextAtCaret (whitespace);
  125. if (newText == "{")
  126. insertTabAtCaret();
  127. }
  128. }
  129. else if (newText == getDocument().getNewLineCharacters()
  130. && pos.getLineNumber() > 0)
  131. {
  132. const String remainderOfLine (pos.getLineText().substring (pos.getIndexInLine()));
  133. if (remainderOfLine.startsWithChar ('{') || remainderOfLine.startsWithChar ('}'))
  134. {
  135. String whitespace;
  136. if (getIndentForCurrentBlock (pos, whitespace))
  137. {
  138. CodeEditorComponent::insertTextAtCaret (newText + whitespace);
  139. if (remainderOfLine.startsWithChar ('{'))
  140. insertTabAtCaret();
  141. return;
  142. }
  143. }
  144. }
  145. }
  146. CodeEditorComponent::insertTextAtCaret (newText);
  147. }
  148. private:
  149. static CPlusPlusCodeTokeniser* getCppTokeniser()
  150. {
  151. static CPlusPlusCodeTokeniser cppTokeniser;
  152. return &cppTokeniser;
  153. }
  154. static String getLeadingWhitespace (String line)
  155. {
  156. line = line.removeCharacters ("\r\n");
  157. const String::CharPointerType endOfLeadingWS (line.getCharPointer().findEndOfWhitespace());
  158. return String (line.getCharPointer(), endOfLeadingWS);
  159. }
  160. static bool getIndentForCurrentBlock (CodeDocument::Position pos, String& whitespace)
  161. {
  162. int braceCount = 0;
  163. while (pos.getLineNumber() > 0)
  164. {
  165. pos = pos.movedByLines (-1);
  166. const String line (pos.getLineText());
  167. const String trimmedLine (line.trimStart());
  168. StringArray tokens;
  169. tokens.addTokens (trimmedLine, true);
  170. for (int i = tokens.size(); --i >= 0;)
  171. {
  172. if (tokens[i] == "}")
  173. ++braceCount;
  174. if (tokens[i] == "{")
  175. {
  176. if (--braceCount < 0)
  177. {
  178. whitespace = getLeadingWhitespace (line);
  179. return true;
  180. }
  181. }
  182. }
  183. }
  184. return false;
  185. }
  186. };
  187. #endif // __JUCER_SOURCECODEEDITOR_JUCEHEADER__