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.

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