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.

920 lines
29KB

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