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.

529 lines
17KB

  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* p, const File& f)
  22. : modDetector (f), project (p)
  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* doc)
  79. : DocumentEditorComponent (doc)
  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. CppCodeEditorComponent::~CppCodeEditorComponent()
  142. {
  143. }
  144. void CppCodeEditorComponent::handleReturnKey()
  145. {
  146. CodeEditorComponent::handleReturnKey();
  147. CodeDocument::Position pos (getCaretPos());
  148. String blockIndent, lastLineIndent;
  149. CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent);
  150. const String remainderOfBrokenLine (pos.getLineText());
  151. const int numLeadingWSChars = CodeHelpers::getLeadingWhitespace (remainderOfBrokenLine).length();
  152. if (numLeadingWSChars > 0)
  153. getDocument().deleteSection (pos, pos.movedBy (numLeadingWSChars));
  154. if (remainderOfBrokenLine.trimStart().startsWithChar ('}'))
  155. insertTextAtCaret (blockIndent);
  156. else
  157. insertTextAtCaret (lastLineIndent);
  158. const String previousLine (pos.movedByLines (-1).getLineText());
  159. const String trimmedPreviousLine (previousLine.trim());
  160. if ((trimmedPreviousLine.startsWith ("if ")
  161. || trimmedPreviousLine.startsWith ("if(")
  162. || trimmedPreviousLine.startsWith ("for ")
  163. || trimmedPreviousLine.startsWith ("for(")
  164. || trimmedPreviousLine.startsWith ("while(")
  165. || trimmedPreviousLine.startsWith ("while "))
  166. && trimmedPreviousLine.endsWithChar (')'))
  167. {
  168. insertTabAtCaret();
  169. }
  170. }
  171. void CppCodeEditorComponent::insertTextAtCaret (const String& newText)
  172. {
  173. if (getHighlightedRegion().isEmpty())
  174. {
  175. const CodeDocument::Position pos (getCaretPos());
  176. if ((newText == "{" || newText == "}")
  177. && pos.getLineNumber() > 0
  178. && pos.getLineText().trim().isEmpty())
  179. {
  180. moveCaretToStartOfLine (true);
  181. String blockIndent, lastLineIndent;
  182. if (CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent))
  183. {
  184. CodeEditorComponent::insertTextAtCaret (blockIndent);
  185. if (newText == "{")
  186. insertTabAtCaret();
  187. }
  188. }
  189. }
  190. CodeEditorComponent::insertTextAtCaret (newText);
  191. }
  192. enum { showInFinderID = 0x2fe821e3 };
  193. void CppCodeEditorComponent::addPopupMenuItems (PopupMenu& menu, const MouseEvent* e)
  194. {
  195. menu.addItem (showInFinderID,
  196. #if JUCE_MAC
  197. "Reveal " + file.getFileName() + " in Finder");
  198. #else
  199. "Reveal " + file.getFileName() + " in Explorer");
  200. #endif
  201. menu.addSeparator();
  202. CodeEditorComponent::addPopupMenuItems (menu, e);
  203. }
  204. void CppCodeEditorComponent::performPopupMenuAction (int menuItemID)
  205. {
  206. if (menuItemID == showInFinderID)
  207. file.revealToUser();
  208. else
  209. CodeEditorComponent::performPopupMenuAction (menuItemID);
  210. }
  211. void CppCodeEditorComponent::getAllCommands (Array <CommandID>& commands)
  212. {
  213. CodeEditorComponent::getAllCommands (commands);
  214. const CommandID ids[] = { CommandIDs::showFindPanel,
  215. CommandIDs::findSelection,
  216. CommandIDs::findNext,
  217. CommandIDs::findPrevious };
  218. commands.addArray (ids, numElementsInArray (ids));
  219. }
  220. void CppCodeEditorComponent::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  221. {
  222. const bool anythingSelected = isHighlightActive();
  223. switch (commandID)
  224. {
  225. case CommandIDs::showFindPanel:
  226. result.setInfo (TRANS ("Find"), TRANS ("Searches for text in the current document."), "Editing", 0);
  227. result.defaultKeypresses.add (KeyPress ('f', ModifierKeys::commandModifier, 0));
  228. break;
  229. case CommandIDs::findSelection:
  230. result.setInfo (TRANS ("Find Selection"), TRANS ("Searches for the currently selected text."), "Editing", 0);
  231. result.setActive (anythingSelected);
  232. result.defaultKeypresses.add (KeyPress ('l', ModifierKeys::commandModifier, 0));
  233. break;
  234. case CommandIDs::findNext:
  235. result.setInfo (TRANS ("Find Next"), TRANS ("Searches for the next occurrence of the current search-term."), "Editing", 0);
  236. result.defaultKeypresses.add (KeyPress ('g', ModifierKeys::commandModifier, 0));
  237. break;
  238. case CommandIDs::findPrevious:
  239. result.setInfo (TRANS ("Find Previous"), TRANS ("Searches for the previous occurrence of the current search-term."), "Editing", 0);
  240. result.defaultKeypresses.add (KeyPress ('g', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0));
  241. result.defaultKeypresses.add (KeyPress ('d', ModifierKeys::commandModifier, 0));
  242. break;
  243. default:
  244. CodeEditorComponent::getCommandInfo (commandID, result);
  245. break;
  246. }
  247. }
  248. bool CppCodeEditorComponent::perform (const InvocationInfo& info)
  249. {
  250. switch (info.commandID)
  251. {
  252. case CommandIDs::showFindPanel: showFindPanel(); return true;
  253. case CommandIDs::findSelection: findSelection(); return true;
  254. case CommandIDs::findNext: findNext (true, true); return true;
  255. case CommandIDs::findPrevious: findNext (false, false); return true;
  256. default: break;
  257. }
  258. return CodeEditorComponent::perform (info);
  259. }
  260. //==============================================================================
  261. class CppCodeEditorComponent::FindPanel : public Component,
  262. private TextEditor::Listener,
  263. private Button::Listener
  264. {
  265. public:
  266. FindPanel()
  267. : caseButton ("Case-sensitive"),
  268. findPrev ("<"),
  269. findNext (">")
  270. {
  271. editor.setColour (CaretComponent::caretColourId, Colours::black);
  272. addAndMakeVisible (&editor);
  273. label.setText ("Find:", false);
  274. label.setColour (Label::textColourId, Colours::white);
  275. label.attachToComponent (&editor, false);
  276. addAndMakeVisible (&caseButton);
  277. caseButton.setColour (ToggleButton::textColourId, Colours::white);
  278. caseButton.setToggleState (isCaseSensitiveSearch(), false);
  279. caseButton.addListener (this);
  280. findPrev.setConnectedEdges (Button::ConnectedOnRight);
  281. findNext.setConnectedEdges (Button::ConnectedOnLeft);
  282. addAndMakeVisible (&findPrev);
  283. addAndMakeVisible (&findNext);
  284. setWantsKeyboardFocus (false);
  285. setFocusContainer (true);
  286. findPrev.setWantsKeyboardFocus (false);
  287. findNext.setWantsKeyboardFocus (false);
  288. editor.setText (getSearchString());
  289. editor.addListener (this);
  290. }
  291. void setCommandManager (ApplicationCommandManager* cm)
  292. {
  293. findPrev.setCommandToTrigger (cm, CommandIDs::findPrevious, true);
  294. findNext.setCommandToTrigger (cm, CommandIDs::findNext, true);
  295. }
  296. void paint (Graphics& g)
  297. {
  298. Path outline;
  299. outline.addRoundedRectangle (1.0f, 1.0f, getWidth() - 2.0f, getHeight() - 2.0f, 8.0f);
  300. g.setColour (Colours::black.withAlpha (0.6f));
  301. g.fillPath (outline);
  302. g.setColour (Colours::white.withAlpha (0.8f));
  303. g.strokePath (outline, PathStrokeType (1.0f));
  304. }
  305. void resized()
  306. {
  307. int y = 30;
  308. editor.setBounds (10, y, getWidth() - 20, 24);
  309. y += 30;
  310. caseButton.setBounds (10, y, getWidth() / 2 - 10, 22);
  311. findNext.setBounds (getWidth() - 40, y, 30, 22);
  312. findPrev.setBounds (getWidth() - 70, y, 30, 22);
  313. }
  314. void buttonClicked (Button*)
  315. {
  316. setCaseSensitiveSearch (caseButton.getToggleState());
  317. }
  318. void textEditorTextChanged (TextEditor&)
  319. {
  320. setSearchString (editor.getText());
  321. if (CppCodeEditorComponent* ed = getOwner())
  322. ed->findNext (true, false);
  323. }
  324. void textEditorFocusLost (TextEditor&) {}
  325. void textEditorReturnKeyPressed (TextEditor&)
  326. {
  327. commandManager->invokeDirectly (CommandIDs::findNext, true);
  328. }
  329. void textEditorEscapeKeyPressed (TextEditor&)
  330. {
  331. if (CppCodeEditorComponent* ed = getOwner())
  332. ed->hideFindPanel();
  333. }
  334. CppCodeEditorComponent* getOwner() const
  335. {
  336. return findParentComponentOfClass <CppCodeEditorComponent>();
  337. }
  338. TextEditor editor;
  339. Label label;
  340. ToggleButton caseButton;
  341. TextButton findPrev, findNext;
  342. };
  343. void CppCodeEditorComponent::showFindPanel()
  344. {
  345. if (findPanel == nullptr)
  346. {
  347. findPanel = new FindPanel();
  348. findPanel->setCommandManager (commandManager);
  349. addAndMakeVisible (findPanel);
  350. resized();
  351. }
  352. findPanel->editor.grabKeyboardFocus();
  353. findPanel->editor.selectAll();
  354. }
  355. void CppCodeEditorComponent::hideFindPanel()
  356. {
  357. findPanel = nullptr;
  358. }
  359. void CppCodeEditorComponent::findSelection()
  360. {
  361. const String selected (getTextInRange (getHighlightedRegion()));
  362. if (selected.isNotEmpty())
  363. {
  364. setSearchString (selected);
  365. findNext (true, true);
  366. }
  367. }
  368. void CppCodeEditorComponent::findNext (bool forwards, bool skipCurrentSelection)
  369. {
  370. const Range<int> highlight (getHighlightedRegion());
  371. const CodeDocument::Position startPos (getDocument(), skipCurrentSelection ? highlight.getEnd()
  372. : highlight.getStart());
  373. int lineNum = startPos.getLineNumber();
  374. int linePos = startPos.getIndexInLine();
  375. const int totalLines = getDocument().getNumLines();
  376. const String searchText (getSearchString());
  377. const bool caseSensitive = isCaseSensitiveSearch();
  378. for (int linesToSearch = totalLines; --linesToSearch >= 0;)
  379. {
  380. String line (getDocument().getLine (lineNum));
  381. int index;
  382. if (forwards)
  383. {
  384. index = caseSensitive ? line.indexOf (linePos, searchText)
  385. : line.indexOfIgnoreCase (linePos, searchText);
  386. }
  387. else
  388. {
  389. if (linePos >= 0)
  390. line = line.substring (0, linePos);
  391. index = caseSensitive ? line.lastIndexOf (searchText)
  392. : line.lastIndexOfIgnoreCase (searchText);
  393. }
  394. if (index >= 0)
  395. {
  396. const CodeDocument::Position p (getDocument(), lineNum, index);
  397. selectRegion (p, p.movedBy (searchText.length()));
  398. break;
  399. }
  400. if (forwards)
  401. {
  402. linePos = 0;
  403. lineNum = (lineNum + 1) % totalLines;
  404. }
  405. else
  406. {
  407. if (--lineNum < 0)
  408. lineNum = totalLines - 1;
  409. linePos = -1;
  410. }
  411. }
  412. }
  413. void CppCodeEditorComponent::handleEscapeKey()
  414. {
  415. CodeEditorComponent::handleEscapeKey();
  416. hideFindPanel();
  417. }
  418. void CppCodeEditorComponent::resized()
  419. {
  420. CodeEditorComponent::resized();
  421. if (findPanel != nullptr)
  422. {
  423. findPanel->setSize (jmin (260, getWidth() - 32), 100);
  424. findPanel->setTopRightPosition (getWidth() - 16, 8);
  425. }
  426. }