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.

895 lines
27KB

  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 "../Application/jucer_Application.h"
  21. #include "../ProjectSaving/jucer_ProjectExporter.h"
  22. #include "jucer_MessageIDs.h"
  23. #include "jucer_CppHelpers.h"
  24. #include "jucer_SourceCodeRange.h"
  25. #include "jucer_ClassDatabase.h"
  26. #include "jucer_DiagnosticMessage.h"
  27. #include "jucer_ProjectBuildInfo.h"
  28. #include "jucer_ClientServerMessages.h"
  29. #include "jucer_CompileEngineClient.h"
  30. #include "jucer_CompileEngineServer.h"
  31. #include "jucer_CompileEngineSettings.h"
  32. #ifndef RUN_CLANG_IN_CHILD_PROCESS
  33. #error
  34. #endif
  35. //==============================================================================
  36. static File getProjucerTempFolder() noexcept
  37. {
  38. #if JUCE_MAC
  39. return { "~/Library/Caches/com.juce.projucer" };
  40. #else
  41. return File::getSpecialLocation (File::tempDirectory).getChildFile ("com.juce.projucer");
  42. #endif
  43. }
  44. static File getCacheLocationForProject (Project& project) noexcept
  45. {
  46. auto cacheFolderName = project.getProjectFilenameRootString() + "_" + project.getProjectUIDString();
  47. #if JUCE_DEBUG
  48. cacheFolderName += "_debug";
  49. #endif
  50. return getProjucerTempFolder().getChildFile ("Intermediate Files").getChildFile (cacheFolderName);
  51. }
  52. //==============================================================================
  53. class ClientIPC : public MessageHandler,
  54. private InterprocessConnection,
  55. private Timer
  56. {
  57. public:
  58. ClientIPC (CompileEngineChildProcess& cp)
  59. : InterprocessConnection (true), owner (cp)
  60. {
  61. launchServer();
  62. }
  63. ~ClientIPC()
  64. {
  65. #if RUN_CLANG_IN_CHILD_PROCESS
  66. if (childProcess.isRunning())
  67. {
  68. #if JUCE_DEBUG
  69. killServerPolitely();
  70. #else
  71. // in release builds we don't want to wait
  72. // for the server to clean up and shut down
  73. killServerWithoutMercy();
  74. #endif
  75. }
  76. #endif
  77. }
  78. void launchServer()
  79. {
  80. DBG ("Client: Launching Server...");
  81. auto pipeName = "ipc_" + String::toHexString (Random().nextInt64());
  82. auto command = createCommandLineForLaunchingServer (pipeName, owner.project.getProjectUIDString(),
  83. getCacheLocationForProject (owner.project));
  84. #if RUN_CLANG_IN_CHILD_PROCESS
  85. if (! childProcess.start (command))
  86. jassertfalse;
  87. #else
  88. server = createClangServer (command);
  89. #endif
  90. if (connectToPipe (pipeName, 10000))
  91. MessageTypes::sendPing (*this);
  92. else
  93. jassertfalse;
  94. startTimer (serverKeepAliveTimeout);
  95. }
  96. void killServerPolitely()
  97. {
  98. DBG ("Client: Killing Server...");
  99. MessageTypes::sendQuit (*this);
  100. disconnect();
  101. stopTimer();
  102. #if RUN_CLANG_IN_CHILD_PROCESS
  103. childProcess.waitForProcessToFinish (5000);
  104. #endif
  105. killServerWithoutMercy();
  106. }
  107. void killServerWithoutMercy()
  108. {
  109. disconnect();
  110. stopTimer();
  111. #if RUN_CLANG_IN_CHILD_PROCESS
  112. childProcess.kill();
  113. #else
  114. destroyClangServer (server);
  115. server = nullptr;
  116. #endif
  117. }
  118. void connectionMade()
  119. {
  120. DBG ("Client: connected");
  121. stopTimer();
  122. }
  123. void connectionLost()
  124. {
  125. DBG ("Client: disconnected");
  126. startTimer (100);
  127. }
  128. bool sendMessage (const ValueTree& m)
  129. {
  130. return InterprocessConnection::sendMessage (MessageHandler::convertMessage (m));
  131. }
  132. void messageReceived (const MemoryBlock& message)
  133. {
  134. #if RUN_CLANG_IN_CHILD_PROCESS
  135. startTimer (serverKeepAliveTimeout);
  136. #else
  137. stopTimer();
  138. #endif
  139. MessageTypes::dispatchToClient (owner, MessageHandler::convertMessage (message));
  140. }
  141. enum { serverKeepAliveTimeout = 10000 };
  142. private:
  143. CompileEngineChildProcess& owner;
  144. #if RUN_CLANG_IN_CHILD_PROCESS
  145. ChildProcess childProcess;
  146. #else
  147. void* server;
  148. #endif
  149. void timerCallback()
  150. {
  151. stopTimer();
  152. owner.handleCrash (String());
  153. }
  154. };
  155. //==============================================================================
  156. class CompileEngineChildProcess::ChildProcess : private ValueTree::Listener,
  157. private Timer
  158. {
  159. public:
  160. ChildProcess (CompileEngineChildProcess& proc, Project& p)
  161. : owner (proc), project (p)
  162. {
  163. projectRoot = project.getProjectRoot();
  164. restartServer();
  165. projectRoot.addListener (this);
  166. openedOk = true;
  167. }
  168. ~ChildProcess()
  169. {
  170. projectRoot.removeListener (this);
  171. if (isRunningApp && server != nullptr)
  172. server->killServerWithoutMercy();
  173. server.reset();
  174. }
  175. void restartServer()
  176. {
  177. server.reset (new ClientIPC (owner));
  178. sendRebuild();
  179. }
  180. void sendRebuild()
  181. {
  182. stopTimer();
  183. ProjectBuildInfo build;
  184. if (! doesProjectMatchSavedHeaderState (project))
  185. {
  186. MessageTypes::sendNewBuild (*server, build);
  187. owner.errorList.resetToError ("Project structure does not match the saved headers! "
  188. "Please re-save your project to enable compilation");
  189. return;
  190. }
  191. if (areAnyModulesMissing (project))
  192. {
  193. MessageTypes::sendNewBuild (*server, build);
  194. owner.errorList.resetToError ("Some of your JUCE modules can't be found! "
  195. "Please check that all the module paths are correct");
  196. return;
  197. }
  198. build.setSystemIncludes (getSystemIncludePaths());
  199. build.setUserIncludes (getUserIncludes());
  200. build.setGlobalDefs (getGlobalDefs());
  201. build.setCompileFlags (project.getCompileEngineSettings().getExtraCompilerFlagsString());
  202. build.setExtraDLLs (getExtraDLLs());
  203. build.setJuceModulesFolder (EnabledModuleList::findDefaultModulesFolder (project).getFullPathName());
  204. build.setUtilsCppInclude (project.getAppIncludeFile().getFullPathName());
  205. build.setWindowsTargetPlatformVersion (project.getCompileEngineSettings().getWindowsTargetPlatformVersionString());
  206. scanForProjectFiles (project, build);
  207. owner.updateAllEditors();
  208. MessageTypes::sendNewBuild (*server, build);
  209. }
  210. void cleanAll()
  211. {
  212. MessageTypes::sendCleanAll (*server);
  213. sendRebuild();
  214. }
  215. void reinstantiatePreviews()
  216. {
  217. MessageTypes::sendReinstantiate (*server);
  218. }
  219. bool launchApp()
  220. {
  221. MessageTypes::sendLaunchApp (*server);
  222. return true;
  223. }
  224. std::unique_ptr<ClientIPC> server;
  225. bool openedOk = false;
  226. bool isRunningApp = false;
  227. private:
  228. CompileEngineChildProcess& owner;
  229. Project& project;
  230. ValueTree projectRoot;
  231. void projectStructureChanged()
  232. {
  233. startTimer (100);
  234. }
  235. void timerCallback() override
  236. {
  237. sendRebuild();
  238. }
  239. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { projectStructureChanged(); }
  240. void valueTreeChildAdded (ValueTree&, ValueTree&) override { projectStructureChanged(); }
  241. void valueTreeChildRemoved (ValueTree&, ValueTree&, int) override { projectStructureChanged(); }
  242. void valueTreeParentChanged (ValueTree&) override { projectStructureChanged(); }
  243. void valueTreeChildOrderChanged (ValueTree&, int, int) override {}
  244. String getGlobalDefs()
  245. {
  246. StringArray defs;
  247. defs.add (project.getCompileEngineSettings().getExtraPreprocessorDefsString());
  248. {
  249. auto projectDefines = project.getPreprocessorDefs();
  250. for (int i = 0; i < projectDefines.size(); ++i)
  251. {
  252. auto def = projectDefines.getAllKeys()[i];
  253. auto value = projectDefines.getAllValues()[i];
  254. if (value.isNotEmpty())
  255. def << "=" << value;
  256. defs.add (def);
  257. }
  258. }
  259. for (Project::ExporterIterator exporter (project); exporter.next();)
  260. if (exporter->canLaunchProject())
  261. defs.add (exporter->getExporterIdentifierMacro() + "=1");
  262. // Use the JUCE implementation of std::function until the live build
  263. // engine can compile the one from the standard library
  264. defs.add (" _LIBCPP_FUNCTIONAL=1");
  265. defs.removeEmptyStrings();
  266. return defs.joinIntoString (" ");
  267. }
  268. static void scanProjectItem (const Project::Item& projectItem, Array<File>& compileUnits, Array<File>& userFiles)
  269. {
  270. if (projectItem.isGroup())
  271. {
  272. for (int i = 0; i < projectItem.getNumChildren(); ++i)
  273. scanProjectItem (projectItem.getChild(i), compileUnits, userFiles);
  274. return;
  275. }
  276. if (projectItem.shouldBeCompiled())
  277. {
  278. auto f = projectItem.getFile();
  279. if (f.exists())
  280. compileUnits.add (f);
  281. }
  282. if (projectItem.shouldBeAddedToTargetProject() && ! projectItem.shouldBeAddedToBinaryResources())
  283. {
  284. auto f = projectItem.getFile();
  285. if (f.exists())
  286. userFiles.add (f);
  287. }
  288. }
  289. void scanForProjectFiles (Project& proj, ProjectBuildInfo& build)
  290. {
  291. Array<File> compileUnits, userFiles;
  292. scanProjectItem (proj.getMainGroup(), compileUnits, userFiles);
  293. {
  294. auto isVST3Host = project.getModules().isModuleEnabled ("juce_audio_processors")
  295. && project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST3");
  296. auto isPluginProject = proj.getProjectType().isAudioPlugin();
  297. OwnedArray<LibraryModule> modules;
  298. proj.getModules().createRequiredModules (modules);
  299. for (Project::ExporterIterator exporter (proj); exporter.next();)
  300. {
  301. if (exporter->canLaunchProject())
  302. {
  303. for (auto* m : modules)
  304. {
  305. auto localModuleFolder = proj.getModules().shouldCopyModuleFilesLocally (m->moduleInfo.getID()).getValue()
  306. ? proj.getLocalModuleFolder (m->moduleInfo.getID())
  307. : m->moduleInfo.getFolder();
  308. m->findAndAddCompiledUnits (*exporter, nullptr, compileUnits,
  309. isPluginProject || isVST3Host ? ProjectType::Target::SharedCodeTarget
  310. : ProjectType::Target::unspecified);
  311. if (isPluginProject || isVST3Host)
  312. m->findAndAddCompiledUnits (*exporter, nullptr, compileUnits, ProjectType::Target::StandalonePlugIn);
  313. }
  314. break;
  315. }
  316. }
  317. }
  318. for (int i = 0; ; ++i)
  319. {
  320. auto binaryDataCpp = proj.getBinaryDataCppFile (i);
  321. if (! binaryDataCpp.exists())
  322. break;
  323. compileUnits.add (binaryDataCpp);
  324. }
  325. for (auto i = compileUnits.size(); --i >= 0;)
  326. if (compileUnits.getReference(i).hasFileExtension (".r"))
  327. compileUnits.remove (i);
  328. build.setFiles (compileUnits, userFiles);
  329. }
  330. static bool doesProjectMatchSavedHeaderState (Project& project)
  331. {
  332. auto liveModules = project.getProjectRoot().getChildWithName (Ids::MODULES);
  333. std::unique_ptr<XmlElement> xml (XmlDocument::parse (project.getFile()));
  334. if (xml == nullptr || ! xml->hasTagName (Ids::JUCERPROJECT.toString()))
  335. return false;
  336. auto diskModules = ValueTree::fromXml (*xml).getChildWithName (Ids::MODULES);
  337. return liveModules.isEquivalentTo (diskModules);
  338. }
  339. static bool areAnyModulesMissing (Project& project)
  340. {
  341. OwnedArray<LibraryModule> modules;
  342. project.getModules().createRequiredModules (modules);
  343. for (auto* module : modules)
  344. if (! module->getFolder().isDirectory())
  345. return true;
  346. return false;
  347. }
  348. StringArray getUserIncludes()
  349. {
  350. StringArray paths;
  351. paths.add (project.getGeneratedCodeFolder().getFullPathName());
  352. paths.addArray (getSearchPathsFromString (project.getCompileEngineSettings().getUserHeaderPathString()));
  353. return convertSearchPathsToAbsolute (paths);
  354. }
  355. StringArray getSystemIncludePaths()
  356. {
  357. StringArray paths;
  358. paths.addArray (getSearchPathsFromString (project.getCompileEngineSettings().getSystemHeaderPathString()));
  359. auto isVST3Host = project.getModules().isModuleEnabled ("juce_audio_processors")
  360. && project.isConfigFlagEnabled ("JUCE_PLUGINHOST_VST3");
  361. if (project.getProjectType().isAudioPlugin() || isVST3Host)
  362. paths.add (getAppSettings().getStoredPath (Ids::vst3Path).toString());
  363. OwnedArray<LibraryModule> modules;
  364. project.getModules().createRequiredModules (modules);
  365. for (auto* module : modules)
  366. paths.addIfNotAlreadyThere (module->getFolder().getParentDirectory().getFullPathName());
  367. return convertSearchPathsToAbsolute (paths);
  368. }
  369. StringArray convertSearchPathsToAbsolute (const StringArray& paths) const
  370. {
  371. StringArray s;
  372. const File root (project.getProjectFolder());
  373. for (String p : paths)
  374. s.add (root.getChildFile (p).getFullPathName());
  375. return s;
  376. }
  377. StringArray getExtraDLLs()
  378. {
  379. auto dlls = StringArray::fromTokens (project.getCompileEngineSettings().getExtraDLLsString(), "\n\r,", {});
  380. dlls.trim();
  381. dlls.removeEmptyStrings();
  382. return dlls;
  383. }
  384. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ChildProcess)
  385. };
  386. //==============================================================================
  387. CompileEngineChildProcess::CompileEngineChildProcess (Project& p)
  388. : project (p)
  389. {
  390. ProjucerApplication::getApp().openDocumentManager.addListener (this);
  391. createProcess();
  392. errorList.setWarningsEnabled (project.getCompileEngineSettings().areWarningsEnabled());
  393. }
  394. CompileEngineChildProcess::~CompileEngineChildProcess()
  395. {
  396. ProjucerApplication::getApp().openDocumentManager.removeListener (this);
  397. process.reset();
  398. lastComponentList.clear();
  399. }
  400. void CompileEngineChildProcess::createProcess()
  401. {
  402. jassert (process == nullptr);
  403. process.reset (new ChildProcess (*this, project));
  404. if (! process->openedOk)
  405. process.reset();
  406. updateAllEditors();
  407. }
  408. void CompileEngineChildProcess::cleanAll()
  409. {
  410. if (process != nullptr)
  411. process->cleanAll();
  412. }
  413. void CompileEngineChildProcess::openPreview (const ClassDatabase::Class& comp)
  414. {
  415. if (process != nullptr)
  416. {
  417. MainWindow* projectWindow = nullptr;
  418. OwnedArray<MainWindow>& windows = ProjucerApplication::getApp().mainWindowList.windows;
  419. for (int i = 0; i < windows.size(); ++i)
  420. {
  421. if (MainWindow* w = windows[i])
  422. {
  423. if (w->getProject() == &project)
  424. {
  425. projectWindow = w;
  426. break;
  427. }
  428. }
  429. }
  430. Rectangle<int> mainWindowRect;
  431. if (projectWindow != nullptr)
  432. mainWindowRect = projectWindow->getBounds();
  433. MessageTypes::sendOpenPreview (*process->server, comp, mainWindowRect);
  434. }
  435. }
  436. void CompileEngineChildProcess::reinstantiatePreviews()
  437. {
  438. if (process != nullptr)
  439. process->reinstantiatePreviews();
  440. }
  441. void CompileEngineChildProcess::processActivationChanged (bool isForeground)
  442. {
  443. if (process != nullptr)
  444. MessageTypes::sendProcessActivationState (*process->server, isForeground);
  445. }
  446. //==============================================================================
  447. bool CompileEngineChildProcess::canLaunchApp() const
  448. {
  449. return process != nullptr
  450. && runningAppProcess == nullptr
  451. && activityList.getNumActivities() == 0
  452. && errorList.getNumErrors() == 0
  453. && project.getProjectType().isGUIApplication();
  454. }
  455. void CompileEngineChildProcess::launchApp()
  456. {
  457. if (process != nullptr)
  458. process->launchApp();
  459. }
  460. bool CompileEngineChildProcess::canKillApp() const
  461. {
  462. return runningAppProcess != nullptr;
  463. }
  464. void CompileEngineChildProcess::killApp()
  465. {
  466. runningAppProcess.reset();
  467. }
  468. void CompileEngineChildProcess::handleAppLaunched()
  469. {
  470. runningAppProcess.reset (process.release());
  471. runningAppProcess->isRunningApp = true;
  472. createProcess();
  473. }
  474. void CompileEngineChildProcess::handleAppQuit()
  475. {
  476. DBG ("handleAppQuit");
  477. runningAppProcess.reset();
  478. }
  479. bool CompileEngineChildProcess::isAppRunning() const noexcept
  480. {
  481. return runningAppProcess != nullptr && runningAppProcess->isRunningApp;
  482. }
  483. //==============================================================================
  484. struct CompileEngineChildProcess::Editor : private CodeDocument::Listener,
  485. private Timer
  486. {
  487. Editor (CompileEngineChildProcess& ccp, const File& f, CodeDocument& doc)
  488. : owner (ccp), file (f), document (doc), transactionTimer (doc)
  489. {
  490. sendFullUpdate();
  491. document.addListener (this);
  492. }
  493. ~Editor()
  494. {
  495. document.removeListener (this);
  496. }
  497. void codeDocumentTextInserted (const String& newText, int insertIndex) override
  498. {
  499. CodeChange (Range<int> (insertIndex, insertIndex), newText).addToList (pendingChanges);
  500. startEditorChangeTimer();
  501. transactionTimer.stopTimer();
  502. owner.lastComponentList.globalNamespace
  503. .nudgeAllCodeRanges (file.getFullPathName(), insertIndex, newText.length());
  504. }
  505. void codeDocumentTextDeleted (int start, int end) override
  506. {
  507. CodeChange (Range<int> (start, end), String()).addToList (pendingChanges);
  508. startEditorChangeTimer();
  509. transactionTimer.stopTimer();
  510. owner.lastComponentList.globalNamespace
  511. .nudgeAllCodeRanges (file.getFullPathName(), start, start - end);
  512. }
  513. void sendFullUpdate()
  514. {
  515. reset();
  516. if (owner.process != nullptr)
  517. MessageTypes::sendFileContentFullUpdate (*owner.process->server, file, document.getAllContent());
  518. }
  519. bool flushEditorChanges()
  520. {
  521. if (pendingChanges.size() > 0)
  522. {
  523. if (owner.process != nullptr && owner.process->server != nullptr)
  524. MessageTypes::sendFileChanges (*owner.process->server, pendingChanges, file);
  525. reset();
  526. return true;
  527. }
  528. stopTimer();
  529. return false;
  530. }
  531. void reset()
  532. {
  533. stopTimer();
  534. pendingChanges.clear();
  535. }
  536. void startTransactionTimer()
  537. {
  538. transactionTimer.startTimer (1000);
  539. }
  540. void startEditorChangeTimer()
  541. {
  542. startTimer (200);
  543. }
  544. CompileEngineChildProcess& owner;
  545. File file;
  546. CodeDocument& document;
  547. private:
  548. Array<CodeChange> pendingChanges;
  549. void timerCallback() override
  550. {
  551. if (owner.project.getCompileEngineSettings().isContinuousRebuildEnabled())
  552. flushEditorChanges();
  553. else
  554. stopTimer();
  555. }
  556. struct TransactionTimer : public Timer
  557. {
  558. TransactionTimer (CodeDocument& doc) : document (doc) {}
  559. void timerCallback() override
  560. {
  561. stopTimer();
  562. document.newTransaction();
  563. }
  564. CodeDocument& document;
  565. };
  566. TransactionTimer transactionTimer;
  567. };
  568. void CompileEngineChildProcess::editorOpened (const File& file, CodeDocument& document)
  569. {
  570. editors.add (new Editor (*this, file, document));
  571. }
  572. bool CompileEngineChildProcess::documentAboutToClose (OpenDocumentManager::Document* document)
  573. {
  574. for (int i = editors.size(); --i >= 0;)
  575. {
  576. if (document->getFile() == editors.getUnchecked(i)->file)
  577. {
  578. const File f (editors.getUnchecked(i)->file);
  579. editors.remove (i);
  580. if (process != nullptr)
  581. MessageTypes::sendHandleFileReset (*process->server, f);
  582. }
  583. }
  584. return true;
  585. }
  586. void CompileEngineChildProcess::updateAllEditors()
  587. {
  588. for (int i = editors.size(); --i >= 0;)
  589. editors.getUnchecked(i)->sendFullUpdate();
  590. }
  591. //==============================================================================
  592. void CompileEngineChildProcess::handleCrash (const String& message)
  593. {
  594. Logger::writeToLog ("*** Child process crashed: " + message);
  595. if (crashHandler != nullptr)
  596. crashHandler (message);
  597. }
  598. void CompileEngineChildProcess::handleNewDiagnosticList (const ValueTree& l) { errorList.setList (l); }
  599. void CompileEngineChildProcess::handleActivityListChanged (const StringArray& l) { activityList.setList (l); }
  600. void CompileEngineChildProcess::handleCloseIDE()
  601. {
  602. if (JUCEApplication* app = JUCEApplication::getInstance())
  603. app->systemRequestedQuit();
  604. }
  605. void CompileEngineChildProcess::handleMissingSystemHeaders()
  606. {
  607. if (ProjectContentComponent* p = findProjectContentComponent())
  608. p->handleMissingSystemHeaders();
  609. }
  610. void CompileEngineChildProcess::handleKeyPress (const String& className, const KeyPress& key)
  611. {
  612. ApplicationCommandManager& commandManager = ProjucerApplication::getCommandManager();
  613. CommandID command = commandManager.getKeyMappings()->findCommandForKeyPress (key);
  614. if (command == StandardApplicationCommandIDs::undo)
  615. {
  616. handleUndoInEditor (className);
  617. }
  618. else if (command == StandardApplicationCommandIDs::redo)
  619. {
  620. handleRedoInEditor (className);
  621. }
  622. else if (ApplicationCommandTarget* const target = ApplicationCommandManager::findTargetForComponent (findProjectContentComponent()))
  623. {
  624. commandManager.setFirstCommandTarget (target);
  625. commandManager.getKeyMappings()->keyPressed (key, findProjectContentComponent());
  626. commandManager.setFirstCommandTarget (nullptr);
  627. }
  628. }
  629. void CompileEngineChildProcess::handleUndoInEditor (const String& /*className*/)
  630. {
  631. }
  632. void CompileEngineChildProcess::handleRedoInEditor (const String& /*className*/)
  633. {
  634. }
  635. void CompileEngineChildProcess::handleClassListChanged (const ValueTree& newList)
  636. {
  637. lastComponentList = ClassDatabase::ClassList::fromValueTree (newList);
  638. activityList.sendClassListChangedMessage (lastComponentList);
  639. }
  640. void CompileEngineChildProcess::handleBuildFailed()
  641. {
  642. ProjucerApplication::getCommandManager().commandStatusChanged();
  643. }
  644. void CompileEngineChildProcess::handleChangeCode (const SourceCodeRange& location, const String& newText)
  645. {
  646. if (Editor* ed = getOrOpenEditorFor (location.file))
  647. {
  648. if (ed->flushEditorChanges())
  649. return; // client-side editor changes were pending, so deal with them first, and discard
  650. // the incoming change, whose position may now be wrong.
  651. ed->document.deleteSection (location.range.getStart(), location.range.getEnd());
  652. ed->document.insertText (location.range.getStart(), newText);
  653. // deliberately clear the messages that we just added, to avoid these changes being
  654. // sent to the server (which will already have processed the same ones locally)
  655. ed->reset();
  656. ed->startTransactionTimer();
  657. }
  658. }
  659. void CompileEngineChildProcess::handlePing()
  660. {
  661. }
  662. //==============================================================================
  663. void CompileEngineChildProcess::flushEditorChanges()
  664. {
  665. for (Editor* ed : editors)
  666. ed->flushEditorChanges();
  667. }
  668. ProjectContentComponent* CompileEngineChildProcess::findProjectContentComponent() const
  669. {
  670. for (MainWindow* mw : ProjucerApplication::getApp().mainWindowList.windows)
  671. if (mw->getProject() == &project)
  672. return mw->getProjectContentComponent();
  673. return nullptr;
  674. }
  675. CompileEngineChildProcess::Editor* CompileEngineChildProcess::getOrOpenEditorFor (const File& file)
  676. {
  677. for (Editor* ed : editors)
  678. if (ed->file == file)
  679. return ed;
  680. if (ProjectContentComponent* pcc = findProjectContentComponent())
  681. if (pcc->showEditorForFile (file, false))
  682. return getOrOpenEditorFor (file);
  683. return nullptr;
  684. }
  685. void CompileEngineChildProcess::handleHighlightCode (const SourceCodeRange& location)
  686. {
  687. ProjectContentComponent* pcc = findProjectContentComponent();
  688. if (pcc != nullptr && pcc->showEditorForFile (location.file, false))
  689. {
  690. SourceCodeEditor* sce = dynamic_cast <SourceCodeEditor*> (pcc->getEditorComponent());
  691. if (sce != nullptr && sce->editor != nullptr)
  692. {
  693. sce->highlight (location.range, true);
  694. Process::makeForegroundProcess();
  695. CodeEditorComponent& ed = *sce->editor;
  696. ed.getTopLevelComponent()->toFront (false);
  697. ed.grabKeyboardFocus();
  698. }
  699. }
  700. }
  701. void CompileEngineChildProcess::cleanAllCachedFilesForProject (Project& p)
  702. {
  703. File cacheFolder (getCacheLocationForProject (p));
  704. if (cacheFolder.isDirectory())
  705. cacheFolder.deleteRecursively();
  706. }