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.

671 lines
21KB

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