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.

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