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.

552 lines
18KB

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