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.

263 lines
8.7KB

  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. #include "jucer_SourceCodeEditor.h"
  19. #include "../Application/jucer_OpenDocumentManager.h"
  20. //==============================================================================
  21. SourceCodeDocument::SourceCodeDocument (Project* project_, const File& file_)
  22. : modDetector (file_), project (project_)
  23. {
  24. }
  25. CodeDocument& SourceCodeDocument::getCodeDocument()
  26. {
  27. if (codeDoc == nullptr)
  28. {
  29. codeDoc = new CodeDocument();
  30. reloadInternal();
  31. codeDoc->clearUndoHistory();
  32. }
  33. return *codeDoc;
  34. }
  35. Component* SourceCodeDocument::createEditor()
  36. {
  37. SourceCodeEditor* e = new SourceCodeEditor (this);
  38. e->createEditor (getCodeDocument());
  39. applyLastState (*(e->editor));
  40. return e;
  41. }
  42. void SourceCodeDocument::reloadFromFile()
  43. {
  44. getCodeDocument();
  45. reloadInternal();
  46. }
  47. void SourceCodeDocument::reloadInternal()
  48. {
  49. jassert (codeDoc != nullptr);
  50. modDetector.updateHash();
  51. codeDoc->applyChanges (modDetector.getFile().loadFileAsString());
  52. codeDoc->setSavePoint();
  53. }
  54. bool SourceCodeDocument::save()
  55. {
  56. TemporaryFile temp (modDetector.getFile());
  57. {
  58. FileOutputStream fo (temp.getFile());
  59. if (! (fo.openedOk() && getCodeDocument().writeToStream (fo)))
  60. return false;
  61. }
  62. if (! temp.overwriteTargetFileWithTemporary())
  63. return false;
  64. getCodeDocument().setSavePoint();
  65. modDetector.updateHash();
  66. return true;
  67. }
  68. void SourceCodeDocument::updateLastState (CodeEditorComponent& editor)
  69. {
  70. lastState = new CodeEditorComponent::State (editor);
  71. }
  72. void SourceCodeDocument::applyLastState (CodeEditorComponent& editor) const
  73. {
  74. if (lastState != nullptr)
  75. lastState->restoreState (editor);
  76. }
  77. //==============================================================================
  78. SourceCodeEditor::SourceCodeEditor (OpenDocumentManager::Document* document_)
  79. : DocumentEditorComponent (document_)
  80. {
  81. }
  82. SourceCodeEditor::~SourceCodeEditor()
  83. {
  84. getAppSettings().appearance.settings.removeListener (this);
  85. if (SourceCodeDocument* doc = dynamic_cast <SourceCodeDocument*> (getDocument()))
  86. doc->updateLastState (*editor);
  87. }
  88. void SourceCodeEditor::createEditor (CodeDocument& codeDocument)
  89. {
  90. if (document->getFile().hasFileExtension (sourceOrHeaderFileExtensions))
  91. setEditor (new CppCodeEditorComponent (document->getFile(), codeDocument));
  92. else
  93. setEditor (new CodeEditorComponent (codeDocument, nullptr));
  94. }
  95. void SourceCodeEditor::setEditor (CodeEditorComponent* newEditor)
  96. {
  97. addAndMakeVisible (editor = newEditor);
  98. editor->setFont (AppearanceSettings::getDefaultCodeFont());
  99. editor->setTabSize (4, true);
  100. updateColourScheme();
  101. getAppSettings().appearance.settings.addListener (this);
  102. }
  103. void SourceCodeEditor::scrollToKeepRangeOnScreen (const Range<int>& range)
  104. {
  105. const int space = jmin (10, editor->getNumLinesOnScreen() / 3);
  106. const CodeDocument::Position start (editor->getDocument(), range.getStart());
  107. const CodeDocument::Position end (editor->getDocument(), range.getEnd());
  108. editor->scrollToKeepLinesOnScreen (Range<int> (start.getLineNumber() - space, end.getLineNumber() + space));
  109. }
  110. void SourceCodeEditor::highlight (const Range<int>& range, bool cursorAtStart)
  111. {
  112. scrollToKeepRangeOnScreen (range);
  113. if (cursorAtStart)
  114. {
  115. editor->moveCaretTo (CodeDocument::Position (editor->getDocument(), range.getEnd()), false);
  116. editor->moveCaretTo (CodeDocument::Position (editor->getDocument(), range.getStart()), true);
  117. }
  118. else
  119. {
  120. editor->setHighlightedRegion (range);
  121. }
  122. }
  123. void SourceCodeEditor::resized()
  124. {
  125. editor->setBounds (getLocalBounds());
  126. }
  127. void SourceCodeEditor::updateColourScheme() { getAppSettings().appearance.applyToCodeEditor (*editor); }
  128. void SourceCodeEditor::valueTreePropertyChanged (ValueTree&, const Identifier&) { updateColourScheme(); }
  129. void SourceCodeEditor::valueTreeChildAdded (ValueTree&, ValueTree&) { updateColourScheme(); }
  130. void SourceCodeEditor::valueTreeChildRemoved (ValueTree&, ValueTree&) { updateColourScheme(); }
  131. void SourceCodeEditor::valueTreeChildOrderChanged (ValueTree&) { updateColourScheme(); }
  132. void SourceCodeEditor::valueTreeParentChanged (ValueTree&) { updateColourScheme(); }
  133. void SourceCodeEditor::valueTreeRedirected (ValueTree&) { updateColourScheme(); }
  134. //==============================================================================
  135. static CPlusPlusCodeTokeniser cppTokeniser;
  136. CppCodeEditorComponent::CppCodeEditorComponent (const File& f, CodeDocument& codeDocument)
  137. : CodeEditorComponent (codeDocument, &cppTokeniser), file (f)
  138. {
  139. setCommandManager (commandManager);
  140. }
  141. void CppCodeEditorComponent::handleReturnKey()
  142. {
  143. CodeEditorComponent::handleReturnKey();
  144. CodeDocument::Position pos (getCaretPos());
  145. String blockIndent, lastLineIndent;
  146. CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent);
  147. const String remainderOfBrokenLine (pos.getLineText());
  148. const int numLeadingWSChars = CodeHelpers::getLeadingWhitespace (remainderOfBrokenLine).length();
  149. if (numLeadingWSChars > 0)
  150. getDocument().deleteSection (pos, pos.movedBy (numLeadingWSChars));
  151. if (remainderOfBrokenLine.trimStart().startsWithChar ('}'))
  152. insertTextAtCaret (blockIndent);
  153. else
  154. insertTextAtCaret (lastLineIndent);
  155. const String previousLine (pos.movedByLines (-1).getLineText());
  156. const String trimmedPreviousLine (previousLine.trim());
  157. if ((trimmedPreviousLine.startsWith ("if ")
  158. || trimmedPreviousLine.startsWith ("if(")
  159. || trimmedPreviousLine.startsWith ("for ")
  160. || trimmedPreviousLine.startsWith ("for(")
  161. || trimmedPreviousLine.startsWith ("while(")
  162. || trimmedPreviousLine.startsWith ("while "))
  163. && trimmedPreviousLine.endsWithChar (')'))
  164. {
  165. insertTabAtCaret();
  166. }
  167. }
  168. void CppCodeEditorComponent::insertTextAtCaret (const String& newText)
  169. {
  170. if (getHighlightedRegion().isEmpty())
  171. {
  172. const CodeDocument::Position pos (getCaretPos());
  173. if ((newText == "{" || newText == "}")
  174. && pos.getLineNumber() > 0
  175. && pos.getLineText().trim().isEmpty())
  176. {
  177. moveCaretToStartOfLine (true);
  178. String blockIndent, lastLineIndent;
  179. if (CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent))
  180. {
  181. CodeEditorComponent::insertTextAtCaret (blockIndent);
  182. if (newText == "{")
  183. insertTabAtCaret();
  184. }
  185. }
  186. }
  187. CodeEditorComponent::insertTextAtCaret (newText);
  188. }
  189. enum { showInFinderID = 0x2fe821e3 };
  190. void CppCodeEditorComponent::addPopupMenuItems (PopupMenu& menu, const MouseEvent* e)
  191. {
  192. menu.addItem (showInFinderID,
  193. #if JUCE_MAC
  194. "Reveal " + file.getFileName() + " in Finder");
  195. #else
  196. "Reveal " + file.getFileName() + " in Explorer");
  197. #endif
  198. menu.addSeparator();
  199. CodeEditorComponent::addPopupMenuItems (menu, e);
  200. }
  201. void CppCodeEditorComponent::performPopupMenuAction (int menuItemID)
  202. {
  203. if (menuItemID == showInFinderID)
  204. file.revealToUser();
  205. else
  206. CodeEditorComponent::performPopupMenuAction (menuItemID);
  207. }