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.

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