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