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.

659 lines
21KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. #include "jucer_SourceCodeEditor.h"
  18. #include "../Application/jucer_Application.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, 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. codeDoc->applyChanges (getFile().loadFileAsString());
  51. codeDoc->setSavePoint();
  52. }
  53. static bool writeCodeDocToFile (const File& file, CodeDocument& doc)
  54. {
  55. TemporaryFile temp (file);
  56. {
  57. FileOutputStream fo (temp.getFile());
  58. if (! (fo.openedOk() && doc.writeToStream (fo)))
  59. return false;
  60. }
  61. return temp.overwriteTargetFileWithTemporary();
  62. }
  63. bool SourceCodeDocument::save()
  64. {
  65. if (writeCodeDocToFile (getFile(), getCodeDocument()))
  66. {
  67. getCodeDocument().setSavePoint();
  68. modDetector.updateHash();
  69. return true;
  70. }
  71. return false;
  72. }
  73. bool SourceCodeDocument::saveAs()
  74. {
  75. FileChooser fc (TRANS("Save As..."), getFile(), "*");
  76. if (! fc.browseForFileToSave (true))
  77. return true;
  78. return writeCodeDocToFile (fc.getResult(), getCodeDocument());
  79. }
  80. void SourceCodeDocument::updateLastState (CodeEditorComponent& editor)
  81. {
  82. lastState = new CodeEditorComponent::State (editor);
  83. }
  84. void SourceCodeDocument::applyLastState (CodeEditorComponent& editor) const
  85. {
  86. if (lastState != nullptr)
  87. lastState->restoreState (editor);
  88. }
  89. //==============================================================================
  90. SourceCodeEditor::SourceCodeEditor (OpenDocumentManager::Document* doc, CodeDocument& codeDocument)
  91. : DocumentEditorComponent (doc)
  92. {
  93. GenericCodeEditorComponent* ed = nullptr;
  94. const File file (document->getFile());
  95. if (fileNeedsCppSyntaxHighlighting (file))
  96. {
  97. ed = new CppCodeEditorComponent (file, codeDocument);
  98. }
  99. else
  100. {
  101. CodeTokeniser* tokeniser = nullptr;
  102. if (file.hasFileExtension ("xml;svg"))
  103. {
  104. static XmlTokeniser xmlTokeniser;
  105. tokeniser = &xmlTokeniser;
  106. }
  107. if (file.hasFileExtension ("lua"))
  108. {
  109. static LuaTokeniser luaTokeniser;
  110. tokeniser = &luaTokeniser;
  111. }
  112. ed = new GenericCodeEditorComponent (file, codeDocument, tokeniser);
  113. }
  114. setEditor (ed);
  115. }
  116. SourceCodeEditor::SourceCodeEditor (OpenDocumentManager::Document* doc, GenericCodeEditorComponent* ed)
  117. : DocumentEditorComponent (doc)
  118. {
  119. setEditor (ed);
  120. }
  121. SourceCodeEditor::~SourceCodeEditor()
  122. {
  123. if (editor != nullptr)
  124. editor->getDocument().removeListener (this);
  125. getAppSettings().appearance.settings.removeListener (this);
  126. if (SourceCodeDocument* doc = dynamic_cast <SourceCodeDocument*> (getDocument()))
  127. doc->updateLastState (*editor);
  128. }
  129. void SourceCodeEditor::setEditor (GenericCodeEditorComponent* newEditor)
  130. {
  131. if (editor != nullptr)
  132. editor->getDocument().removeListener (this);
  133. addAndMakeVisible (editor = newEditor);
  134. editor->setFont (AppearanceSettings::getDefaultCodeFont());
  135. editor->setTabSize (4, true);
  136. updateColourScheme();
  137. getAppSettings().appearance.settings.addListener (this);
  138. editor->getDocument().addListener (this);
  139. }
  140. void SourceCodeEditor::scrollToKeepRangeOnScreen (Range<int> range)
  141. {
  142. const int space = jmin (10, editor->getNumLinesOnScreen() / 3);
  143. const CodeDocument::Position start (editor->getDocument(), range.getStart());
  144. const CodeDocument::Position end (editor->getDocument(), range.getEnd());
  145. editor->scrollToKeepLinesOnScreen (Range<int> (start.getLineNumber() - space, end.getLineNumber() + space));
  146. }
  147. void SourceCodeEditor::highlight (Range<int> range, bool cursorAtStart)
  148. {
  149. scrollToKeepRangeOnScreen (range);
  150. if (cursorAtStart)
  151. {
  152. editor->moveCaretTo (CodeDocument::Position (editor->getDocument(), range.getEnd()), false);
  153. editor->moveCaretTo (CodeDocument::Position (editor->getDocument(), range.getStart()), true);
  154. }
  155. else
  156. {
  157. editor->setHighlightedRegion (range);
  158. }
  159. }
  160. void SourceCodeEditor::resized()
  161. {
  162. editor->setBounds (getLocalBounds());
  163. }
  164. void SourceCodeEditor::updateColourScheme() { getAppSettings().appearance.applyToCodeEditor (*editor); }
  165. void SourceCodeEditor::checkSaveState()
  166. {
  167. setEditedState (getDocument()->needsSaving());
  168. }
  169. void SourceCodeEditor::valueTreePropertyChanged (ValueTree&, const Identifier&) { updateColourScheme(); }
  170. void SourceCodeEditor::valueTreeChildAdded (ValueTree&, ValueTree&) { updateColourScheme(); }
  171. void SourceCodeEditor::valueTreeChildRemoved (ValueTree&, ValueTree&, int) { updateColourScheme(); }
  172. void SourceCodeEditor::valueTreeChildOrderChanged (ValueTree&, int, int) { updateColourScheme(); }
  173. void SourceCodeEditor::valueTreeParentChanged (ValueTree&) { updateColourScheme(); }
  174. void SourceCodeEditor::valueTreeRedirected (ValueTree&) { updateColourScheme(); }
  175. void SourceCodeEditor::codeDocumentTextInserted (const String&, int) { checkSaveState(); }
  176. void SourceCodeEditor::codeDocumentTextDeleted (int, int) { checkSaveState(); }
  177. //==============================================================================
  178. GenericCodeEditorComponent::GenericCodeEditorComponent (const File& f, CodeDocument& codeDocument,
  179. CodeTokeniser* tokeniser)
  180. : CodeEditorComponent (codeDocument, tokeniser), file (f)
  181. {
  182. setCommandManager (&IntrojucerApp::getCommandManager());
  183. }
  184. GenericCodeEditorComponent::~GenericCodeEditorComponent() {}
  185. enum
  186. {
  187. showInFinderID = 0x2fe821e3,
  188. insertComponentID = 0x2fe821e4
  189. };
  190. void GenericCodeEditorComponent::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 GenericCodeEditorComponent::performPopupMenuAction (int menuItemID)
  202. {
  203. if (menuItemID == showInFinderID)
  204. file.revealToUser();
  205. else
  206. CodeEditorComponent::performPopupMenuAction (menuItemID);
  207. }
  208. void GenericCodeEditorComponent::getAllCommands (Array <CommandID>& commands)
  209. {
  210. CodeEditorComponent::getAllCommands (commands);
  211. const CommandID ids[] = { CommandIDs::showFindPanel,
  212. CommandIDs::findSelection,
  213. CommandIDs::findNext,
  214. CommandIDs::findPrevious };
  215. commands.addArray (ids, numElementsInArray (ids));
  216. }
  217. void GenericCodeEditorComponent::getCommandInfo (const CommandID commandID, ApplicationCommandInfo& result)
  218. {
  219. const bool anythingSelected = isHighlightActive();
  220. switch (commandID)
  221. {
  222. case CommandIDs::showFindPanel:
  223. result.setInfo (TRANS ("Find"), TRANS ("Searches for text in the current document."), "Editing", 0);
  224. result.defaultKeypresses.add (KeyPress ('f', ModifierKeys::commandModifier, 0));
  225. break;
  226. case CommandIDs::findSelection:
  227. result.setInfo (TRANS ("Find Selection"), TRANS ("Searches for the currently selected text."), "Editing", 0);
  228. result.setActive (anythingSelected);
  229. result.defaultKeypresses.add (KeyPress ('l', ModifierKeys::commandModifier, 0));
  230. break;
  231. case CommandIDs::findNext:
  232. result.setInfo (TRANS ("Find Next"), TRANS ("Searches for the next occurrence of the current search-term."), "Editing", 0);
  233. result.defaultKeypresses.add (KeyPress ('g', ModifierKeys::commandModifier, 0));
  234. break;
  235. case CommandIDs::findPrevious:
  236. result.setInfo (TRANS ("Find Previous"), TRANS ("Searches for the previous occurrence of the current search-term."), "Editing", 0);
  237. result.defaultKeypresses.add (KeyPress ('g', ModifierKeys::commandModifier | ModifierKeys::shiftModifier, 0));
  238. result.defaultKeypresses.add (KeyPress ('d', ModifierKeys::commandModifier, 0));
  239. break;
  240. default:
  241. CodeEditorComponent::getCommandInfo (commandID, result);
  242. break;
  243. }
  244. }
  245. bool GenericCodeEditorComponent::perform (const InvocationInfo& info)
  246. {
  247. switch (info.commandID)
  248. {
  249. case CommandIDs::showFindPanel: showFindPanel(); return true;
  250. case CommandIDs::findSelection: findSelection(); return true;
  251. case CommandIDs::findNext: findNext (true, true); return true;
  252. case CommandIDs::findPrevious: findNext (false, false); return true;
  253. default: break;
  254. }
  255. return CodeEditorComponent::perform (info);
  256. }
  257. void GenericCodeEditorComponent::addListener (GenericCodeEditorComponent::Listener* listener)
  258. {
  259. listeners.add (listener);
  260. }
  261. void GenericCodeEditorComponent::removeListener (GenericCodeEditorComponent::Listener* listener)
  262. {
  263. listeners.remove (listener);
  264. }
  265. //==============================================================================
  266. class GenericCodeEditorComponent::FindPanel : public Component,
  267. private TextEditor::Listener,
  268. private ButtonListener
  269. {
  270. public:
  271. FindPanel()
  272. : caseButton ("Case-sensitive"),
  273. findPrev ("<"),
  274. findNext (">")
  275. {
  276. editor.setColour (CaretComponent::caretColourId, Colours::black);
  277. addAndMakeVisible (editor);
  278. label.setText ("Find:", dontSendNotification);
  279. label.setColour (Label::textColourId, Colours::white);
  280. label.attachToComponent (&editor, false);
  281. addAndMakeVisible (caseButton);
  282. caseButton.setColour (ToggleButton::textColourId, Colours::white);
  283. caseButton.setToggleState (isCaseSensitiveSearch(), dontSendNotification);
  284. caseButton.addListener (this);
  285. findPrev.setConnectedEdges (Button::ConnectedOnRight);
  286. findNext.setConnectedEdges (Button::ConnectedOnLeft);
  287. addAndMakeVisible (findPrev);
  288. addAndMakeVisible (findNext);
  289. setWantsKeyboardFocus (false);
  290. setFocusContainer (true);
  291. findPrev.setWantsKeyboardFocus (false);
  292. findNext.setWantsKeyboardFocus (false);
  293. editor.setText (getSearchString());
  294. editor.addListener (this);
  295. }
  296. void setCommandManager (ApplicationCommandManager* cm)
  297. {
  298. findPrev.setCommandToTrigger (cm, CommandIDs::findPrevious, true);
  299. findNext.setCommandToTrigger (cm, CommandIDs::findNext, true);
  300. }
  301. void paint (Graphics& g) override
  302. {
  303. Path outline;
  304. outline.addRoundedRectangle (1.0f, 1.0f, getWidth() - 2.0f, getHeight() - 2.0f, 8.0f);
  305. g.setColour (Colours::black.withAlpha (0.6f));
  306. g.fillPath (outline);
  307. g.setColour (Colours::white.withAlpha (0.8f));
  308. g.strokePath (outline, PathStrokeType (1.0f));
  309. }
  310. void resized() override
  311. {
  312. int y = 30;
  313. editor.setBounds (10, y, getWidth() - 20, 24);
  314. y += 30;
  315. caseButton.setBounds (10, y, getWidth() / 2 - 10, 22);
  316. findNext.setBounds (getWidth() - 40, y, 30, 22);
  317. findPrev.setBounds (getWidth() - 70, y, 30, 22);
  318. }
  319. void buttonClicked (Button*) override
  320. {
  321. setCaseSensitiveSearch (caseButton.getToggleState());
  322. }
  323. void textEditorTextChanged (TextEditor&) override
  324. {
  325. setSearchString (editor.getText());
  326. if (GenericCodeEditorComponent* ed = getOwner())
  327. ed->findNext (true, false);
  328. }
  329. void textEditorFocusLost (TextEditor&) override {}
  330. void textEditorReturnKeyPressed (TextEditor&) override
  331. {
  332. IntrojucerApp::getCommandManager().invokeDirectly (CommandIDs::findNext, true);
  333. }
  334. void textEditorEscapeKeyPressed (TextEditor&) override
  335. {
  336. if (GenericCodeEditorComponent* ed = getOwner())
  337. ed->hideFindPanel();
  338. }
  339. GenericCodeEditorComponent* getOwner() const
  340. {
  341. return findParentComponentOfClass <GenericCodeEditorComponent>();
  342. }
  343. TextEditor editor;
  344. Label label;
  345. ToggleButton caseButton;
  346. TextButton findPrev, findNext;
  347. };
  348. void GenericCodeEditorComponent::resized()
  349. {
  350. CodeEditorComponent::resized();
  351. if (findPanel != nullptr)
  352. {
  353. findPanel->setSize (jmin (260, getWidth() - 32), 100);
  354. findPanel->setTopRightPosition (getWidth() - 16, 8);
  355. }
  356. }
  357. void GenericCodeEditorComponent::showFindPanel()
  358. {
  359. if (findPanel == nullptr)
  360. {
  361. findPanel = new FindPanel();
  362. findPanel->setCommandManager (&IntrojucerApp::getCommandManager());
  363. addAndMakeVisible (findPanel);
  364. resized();
  365. }
  366. if (findPanel != nullptr)
  367. {
  368. findPanel->editor.grabKeyboardFocus();
  369. findPanel->editor.selectAll();
  370. }
  371. }
  372. void GenericCodeEditorComponent::hideFindPanel()
  373. {
  374. findPanel = nullptr;
  375. }
  376. void GenericCodeEditorComponent::findSelection()
  377. {
  378. const String selected (getTextInRange (getHighlightedRegion()));
  379. if (selected.isNotEmpty())
  380. {
  381. setSearchString (selected);
  382. findNext (true, true);
  383. }
  384. }
  385. void GenericCodeEditorComponent::findNext (bool forwards, bool skipCurrentSelection)
  386. {
  387. const Range<int> highlight (getHighlightedRegion());
  388. const CodeDocument::Position startPos (getDocument(), skipCurrentSelection ? highlight.getEnd()
  389. : highlight.getStart());
  390. int lineNum = startPos.getLineNumber();
  391. int linePos = startPos.getIndexInLine();
  392. const int totalLines = getDocument().getNumLines();
  393. const String searchText (getSearchString());
  394. const bool caseSensitive = isCaseSensitiveSearch();
  395. for (int linesToSearch = totalLines; --linesToSearch >= 0;)
  396. {
  397. String line (getDocument().getLine (lineNum));
  398. int index;
  399. if (forwards)
  400. {
  401. index = caseSensitive ? line.indexOf (linePos, searchText)
  402. : line.indexOfIgnoreCase (linePos, searchText);
  403. }
  404. else
  405. {
  406. if (linePos >= 0)
  407. line = line.substring (0, linePos);
  408. index = caseSensitive ? line.lastIndexOf (searchText)
  409. : line.lastIndexOfIgnoreCase (searchText);
  410. }
  411. if (index >= 0)
  412. {
  413. const CodeDocument::Position p (getDocument(), lineNum, index);
  414. selectRegion (p, p.movedBy (searchText.length()));
  415. break;
  416. }
  417. if (forwards)
  418. {
  419. linePos = 0;
  420. lineNum = (lineNum + 1) % totalLines;
  421. }
  422. else
  423. {
  424. if (--lineNum < 0)
  425. lineNum = totalLines - 1;
  426. linePos = -1;
  427. }
  428. }
  429. }
  430. void GenericCodeEditorComponent::handleEscapeKey()
  431. {
  432. CodeEditorComponent::handleEscapeKey();
  433. hideFindPanel();
  434. }
  435. void GenericCodeEditorComponent::editorViewportPositionChanged()
  436. {
  437. CodeEditorComponent::editorViewportPositionChanged();
  438. listeners.call (&Listener::codeEditorViewportMoved, *this);
  439. }
  440. //==============================================================================
  441. static CPlusPlusCodeTokeniser cppTokeniser;
  442. CppCodeEditorComponent::CppCodeEditorComponent (const File& f, CodeDocument& doc)
  443. : GenericCodeEditorComponent (f, doc, &cppTokeniser)
  444. {
  445. }
  446. CppCodeEditorComponent::~CppCodeEditorComponent() {}
  447. void CppCodeEditorComponent::handleReturnKey()
  448. {
  449. GenericCodeEditorComponent::handleReturnKey();
  450. CodeDocument::Position pos (getCaretPos());
  451. String blockIndent, lastLineIndent;
  452. CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent);
  453. const String remainderOfBrokenLine (pos.getLineText());
  454. const int numLeadingWSChars = CodeHelpers::getLeadingWhitespace (remainderOfBrokenLine).length();
  455. if (numLeadingWSChars > 0)
  456. getDocument().deleteSection (pos, pos.movedBy (numLeadingWSChars));
  457. if (remainderOfBrokenLine.trimStart().startsWithChar ('}'))
  458. insertTextAtCaret (blockIndent);
  459. else
  460. insertTextAtCaret (lastLineIndent);
  461. const String previousLine (pos.movedByLines (-1).getLineText());
  462. const String trimmedPreviousLine (previousLine.trim());
  463. if ((trimmedPreviousLine.startsWith ("if ")
  464. || trimmedPreviousLine.startsWith ("if(")
  465. || trimmedPreviousLine.startsWith ("for ")
  466. || trimmedPreviousLine.startsWith ("for(")
  467. || trimmedPreviousLine.startsWith ("while(")
  468. || trimmedPreviousLine.startsWith ("while "))
  469. && trimmedPreviousLine.endsWithChar (')'))
  470. {
  471. insertTabAtCaret();
  472. }
  473. }
  474. void CppCodeEditorComponent::insertTextAtCaret (const String& newText)
  475. {
  476. if (getHighlightedRegion().isEmpty())
  477. {
  478. const CodeDocument::Position pos (getCaretPos());
  479. if ((newText == "{" || newText == "}")
  480. && pos.getLineNumber() > 0
  481. && pos.getLineText().trim().isEmpty())
  482. {
  483. moveCaretToStartOfLine (true);
  484. String blockIndent, lastLineIndent;
  485. if (CodeHelpers::getIndentForCurrentBlock (pos, getTabString (getTabSize()), blockIndent, lastLineIndent))
  486. {
  487. GenericCodeEditorComponent::insertTextAtCaret (blockIndent);
  488. if (newText == "{")
  489. insertTabAtCaret();
  490. }
  491. }
  492. }
  493. GenericCodeEditorComponent::insertTextAtCaret (newText);
  494. }
  495. void CppCodeEditorComponent::addPopupMenuItems (PopupMenu& menu, const MouseEvent* e)
  496. {
  497. GenericCodeEditorComponent::addPopupMenuItems (menu, e);
  498. menu.addSeparator();
  499. menu.addItem (insertComponentID, TRANS("Insert code for a new Component class..."));
  500. }
  501. void CppCodeEditorComponent::performPopupMenuAction (int menuItemID)
  502. {
  503. if (menuItemID == insertComponentID)
  504. insertComponentClass();
  505. GenericCodeEditorComponent::performPopupMenuAction (menuItemID);
  506. }
  507. void CppCodeEditorComponent::insertComponentClass()
  508. {
  509. AlertWindow aw (TRANS ("Insert a new Component class"),
  510. TRANS ("Please enter a name for the new class"),
  511. AlertWindow::NoIcon, nullptr);
  512. const char* classNameField = "Class Name";
  513. aw.addTextEditor (classNameField, String::empty, String::empty, false);
  514. aw.addButton (TRANS ("Insert Code"), 1, KeyPress (KeyPress::returnKey));
  515. aw.addButton (TRANS ("Cancel"), 0, KeyPress (KeyPress::escapeKey));
  516. while (aw.runModalLoop() != 0)
  517. {
  518. const String className (aw.getTextEditorContents (classNameField).trim());
  519. if (className == CodeHelpers::makeValidIdentifier (className, false, true, false))
  520. {
  521. String code (BinaryData::jucer_InlineComponentTemplate_h);
  522. code = code.replace ("COMPONENTCLASS", className);
  523. insertTextAtCaret (code);
  524. break;
  525. }
  526. }
  527. }