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.

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