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.

313 lines
9.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. #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. }
  32. return *codeDoc;
  33. }
  34. Component* SourceCodeDocument::createEditor()
  35. {
  36. SourceCodeEditor* e = new SourceCodeEditor (this);
  37. e->createEditor (getCodeDocument());
  38. applyLastState (*(e->editor));
  39. return e;
  40. }
  41. void SourceCodeDocument::reloadFromFile()
  42. {
  43. getCodeDocument();
  44. reloadInternal();
  45. }
  46. void SourceCodeDocument::reloadInternal()
  47. {
  48. jassert (codeDoc != nullptr);
  49. modDetector.updateHash();
  50. ScopedPointer <InputStream> in (modDetector.getFile().createInputStream());
  51. if (in != nullptr)
  52. codeDoc->loadFromStream (*in);
  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. SourceCodeDocument* doc = dynamic_cast <SourceCodeDocument*> (getDocument());
  86. if (doc != nullptr)
  87. doc->updateLastState (*editor);
  88. }
  89. void SourceCodeEditor::createEditor (CodeDocument& codeDocument)
  90. {
  91. if (document->getFile().hasFileExtension (sourceOrHeaderFileExtensions))
  92. setEditor (new CppCodeEditorComponent (codeDocument));
  93. else
  94. setEditor (new CodeEditorComponent (codeDocument, nullptr));
  95. }
  96. void SourceCodeEditor::setEditor (CodeEditorComponent* newEditor)
  97. {
  98. addAndMakeVisible (editor = newEditor);
  99. editor->setFont (AppearanceSettings::getDefaultCodeFont());
  100. editor->setTabSize (4, true);
  101. updateColourScheme();
  102. getAppSettings().appearance.settings.addListener (this);
  103. }
  104. void SourceCodeEditor::highlightLine (int lineNum, int characterIndex)
  105. {
  106. if (lineNum <= editor->getFirstLineOnScreen()
  107. || lineNum >= editor->getFirstLineOnScreen() + editor->getNumLinesOnScreen() - 1)
  108. {
  109. editor->scrollToLine (jmax (0, jmin (lineNum - editor->getNumLinesOnScreen() / 3,
  110. editor->getDocument().getNumLines() - editor->getNumLinesOnScreen())));
  111. }
  112. editor->moveCaretTo (CodeDocument::Position (editor->getDocument(), lineNum - 1, characterIndex), false);
  113. }
  114. void SourceCodeEditor::resized()
  115. {
  116. editor->setBounds (getLocalBounds());
  117. }
  118. void SourceCodeEditor::updateColourScheme() { getAppSettings().appearance.applyToCodeEditor (*editor); }
  119. void SourceCodeEditor::valueTreePropertyChanged (ValueTree&, const Identifier&) { updateColourScheme(); }
  120. void SourceCodeEditor::valueTreeChildAdded (ValueTree&, ValueTree&) { updateColourScheme(); }
  121. void SourceCodeEditor::valueTreeChildRemoved (ValueTree&, ValueTree&) { updateColourScheme(); }
  122. void SourceCodeEditor::valueTreeChildOrderChanged (ValueTree&) { updateColourScheme(); }
  123. void SourceCodeEditor::valueTreeParentChanged (ValueTree&) { updateColourScheme(); }
  124. void SourceCodeEditor::valueTreeRedirected (ValueTree&) { updateColourScheme(); }
  125. //==============================================================================
  126. namespace CppUtils
  127. {
  128. static CPlusPlusCodeTokeniser* getCppTokeniser()
  129. {
  130. static CPlusPlusCodeTokeniser cppTokeniser;
  131. return &cppTokeniser;
  132. }
  133. static String getLeadingWhitespace (String line)
  134. {
  135. line = line.removeCharacters ("\r\n");
  136. const String::CharPointerType endOfLeadingWS (line.getCharPointer().findEndOfWhitespace());
  137. return String (line.getCharPointer(), endOfLeadingWS);
  138. }
  139. static bool getIndentForCurrentBlock (CodeDocument::Position pos, String& whitespace)
  140. {
  141. int braceCount = 0;
  142. while (pos.getLineNumber() > 0)
  143. {
  144. pos = pos.movedByLines (-1);
  145. const String line (pos.getLineText());
  146. const String trimmedLine (line.trimStart());
  147. String::CharPointerType l (trimmedLine.getCharPointer());
  148. for (;;)
  149. {
  150. const juce_wchar c = l.getAndAdvance();
  151. if (c == 0)
  152. break;
  153. if (c == '}')
  154. ++braceCount;
  155. if (c == '{')
  156. {
  157. if (--braceCount < 0)
  158. {
  159. whitespace = getLeadingWhitespace (line);
  160. return true;
  161. }
  162. }
  163. if (c == '"' || c == '\'')
  164. {
  165. while (! (l.isEmpty() || l.getAndAdvance() == c))
  166. {}
  167. }
  168. if (c == '/' && *l == '/')
  169. break;
  170. }
  171. }
  172. return false;
  173. }
  174. }
  175. CppCodeEditorComponent::CppCodeEditorComponent (CodeDocument& codeDocument)
  176. : CodeEditorComponent (codeDocument, CppUtils::getCppTokeniser())
  177. {
  178. }
  179. void CppCodeEditorComponent::handleReturnKey()
  180. {
  181. CodeEditorComponent::handleReturnKey();
  182. CodeDocument::Position pos (getCaretPos());
  183. if (pos.getLineNumber() > 0 && pos.getLineText().trim().isEmpty())
  184. {
  185. const String previousLine (pos.movedByLines (-1).getLineText());
  186. const String trimmedPreviousLine (previousLine.trim());
  187. if (trimmedPreviousLine.endsWithChar ('{')
  188. || ((trimmedPreviousLine.startsWith ("if ")
  189. || trimmedPreviousLine.startsWith ("for ")
  190. || trimmedPreviousLine.startsWith ("while "))
  191. && trimmedPreviousLine.endsWithChar (')')))
  192. {
  193. const String leadingWhitespace (CppUtils::getLeadingWhitespace (previousLine));
  194. insertTextAtCaret (leadingWhitespace);
  195. insertTabAtCaret();
  196. }
  197. else
  198. {
  199. while (pos.getLineNumber() > 0)
  200. {
  201. pos = pos.movedByLines (-1);
  202. const String leadingWhitespace (CppUtils::getLeadingWhitespace (pos.getLineText()));
  203. if (leadingWhitespace.isNotEmpty())
  204. {
  205. insertTextAtCaret (leadingWhitespace);
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. void CppCodeEditorComponent::insertTextAtCaret (const String& newText)
  213. {
  214. if (getHighlightedRegion().isEmpty())
  215. {
  216. const CodeDocument::Position pos (getCaretPos());
  217. if ((newText == "{" || newText == "}")
  218. && pos.getLineNumber() > 0
  219. && pos.getLineText().trim().isEmpty())
  220. {
  221. moveCaretToStartOfLine (true);
  222. String whitespace;
  223. if (CppUtils::getIndentForCurrentBlock (pos, whitespace))
  224. {
  225. CodeEditorComponent::insertTextAtCaret (whitespace);
  226. if (newText == "{")
  227. insertTabAtCaret();
  228. }
  229. }
  230. else if (newText == getDocument().getNewLineCharacters()
  231. && pos.getLineNumber() > 0)
  232. {
  233. const String remainderOfLine (pos.getLineText().substring (pos.getIndexInLine()));
  234. if (remainderOfLine.startsWithChar ('{') || remainderOfLine.startsWithChar ('}'))
  235. {
  236. String whitespace;
  237. if (CppUtils::getIndentForCurrentBlock (pos, whitespace))
  238. {
  239. CodeEditorComponent::insertTextAtCaret (newText + whitespace);
  240. if (remainderOfLine.startsWithChar ('{'))
  241. insertTabAtCaret();
  242. return;
  243. }
  244. }
  245. }
  246. }
  247. CodeEditorComponent::insertTextAtCaret (newText);
  248. }