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.

525 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #include "jucer_CodeGenerator.h"
  19. //==============================================================================
  20. CodeGenerator::CodeGenerator()
  21. {
  22. }
  23. CodeGenerator::~CodeGenerator()
  24. {
  25. }
  26. int CodeGenerator::getUniqueSuffix()
  27. {
  28. return ++suffix;
  29. }
  30. //==============================================================================
  31. String& CodeGenerator::getCallbackCode (const String& requiredParentClass,
  32. const String& returnType,
  33. const String& prototype,
  34. const bool hasPrePostUserSections)
  35. {
  36. String parentClass (requiredParentClass);
  37. if (parentClass.isNotEmpty()
  38. && ! (parentClass.startsWith ("public ")
  39. || parentClass.startsWith ("private ")
  40. || parentClass.startsWith ("protected ")))
  41. {
  42. parentClass = "public " + parentClass;
  43. }
  44. for (int i = callbacks.size(); --i >= 0;)
  45. {
  46. CallbackMethod* const cm = callbacks.getUnchecked(i);
  47. if (cm->requiredParentClass == parentClass
  48. && cm->returnType == returnType
  49. && cm->prototype == prototype)
  50. return cm->content;
  51. }
  52. CallbackMethod* const cm = new CallbackMethod();
  53. callbacks.add (cm);
  54. cm->requiredParentClass = parentClass;
  55. cm->returnType = returnType;
  56. cm->prototype = prototype;
  57. cm->hasPrePostUserSections = hasPrePostUserSections;
  58. return cm->content;
  59. }
  60. void CodeGenerator::removeCallback (const String& returnType, const String& prototype)
  61. {
  62. for (int i = callbacks.size(); --i >= 0;)
  63. {
  64. CallbackMethod* const cm = callbacks.getUnchecked(i);
  65. if (cm->returnType == returnType && cm->prototype == prototype)
  66. callbacks.remove (i);
  67. }
  68. }
  69. const StringArray CodeGenerator::getExtraParentClasses() const
  70. {
  71. StringArray s;
  72. for (int i = 0; i < callbacks.size(); ++i)
  73. {
  74. CallbackMethod* const cm = callbacks.getUnchecked(i);
  75. s.add (cm->requiredParentClass);
  76. }
  77. return s;
  78. }
  79. const String CodeGenerator::getCallbackDeclarations() const
  80. {
  81. String s;
  82. for (int i = 0; i < callbacks.size(); ++i)
  83. {
  84. CallbackMethod* const cm = callbacks.getUnchecked(i);
  85. s << cm->returnType << " " << cm->prototype << ";" << newLine;
  86. }
  87. return s;
  88. }
  89. const String CodeGenerator::getCallbackDefinitions() const
  90. {
  91. String s;
  92. for (int i = 0; i < callbacks.size(); ++i)
  93. {
  94. CallbackMethod* const cm = callbacks.getUnchecked(i);
  95. const String userCodeBlockName ("User" + makeValidCppIdentifier (cm->prototype.upToFirstOccurrenceOf ("(", false, false),
  96. true, true, false).trim());
  97. if (userCodeBlockName.isNotEmpty() && cm->hasPrePostUserSections)
  98. {
  99. s << cm->returnType << " " << className << "::" << cm->prototype
  100. << newLine
  101. << "{" << newLine
  102. << " //[" << userCodeBlockName << "_Pre]" << newLine
  103. << " //[/" << userCodeBlockName
  104. << "_Pre]" << newLine << newLine
  105. << " " << indentCode (cm->content.trim(), 4) << newLine
  106. << newLine
  107. << " //[" << userCodeBlockName << "_Post]" << newLine
  108. << " //[/" << userCodeBlockName << "_Post]" << newLine
  109. << "}" << newLine
  110. << newLine;
  111. }
  112. else
  113. {
  114. s << cm->returnType << " " << className << "::" << cm->prototype << newLine
  115. << "{" << newLine
  116. << " " << indentCode (cm->content.trim(), 4) << newLine
  117. << "}" << newLine
  118. << newLine;
  119. }
  120. }
  121. return s;
  122. }
  123. //==============================================================================
  124. const String CodeGenerator::getClassDeclaration() const
  125. {
  126. StringArray parentClassLines;
  127. parentClassLines.addTokens (parentClasses, ",", String::empty);
  128. parentClassLines.addArray (getExtraParentClasses());
  129. parentClassLines.trim();
  130. parentClassLines.removeEmptyStrings();
  131. parentClassLines.removeDuplicates (false);
  132. if (parentClassLines.contains ("public Button", false))
  133. parentClassLines.removeString ("public Component", false);
  134. String r;
  135. r << "class " << className << " : ";
  136. r << parentClassLines.joinIntoString ("," + String (newLine) + String::repeatedString (" ", r.length()));
  137. return r;
  138. }
  139. const String CodeGenerator::getInitialiserList() const
  140. {
  141. String s;
  142. StringArray inits (memberInitialisers);
  143. if (parentClassInitialiser.isNotEmpty())
  144. inits.insert (0, parentClassInitialiser);
  145. inits.trim();
  146. inits.removeEmptyStrings();
  147. inits.removeDuplicates (false);
  148. if (inits.size() > 0)
  149. {
  150. s << " : ";
  151. for (int i = 0; i < inits.size(); ++i)
  152. {
  153. String init (inits[i]);
  154. while (init.endsWithChar (','))
  155. init = init.dropLastCharacters (1);
  156. s << init;
  157. if (i < inits.size() - 1)
  158. s << "," << newLine << " ";
  159. else
  160. s << newLine;
  161. }
  162. }
  163. return s;
  164. }
  165. static const String getIncludeFileCode (StringArray files)
  166. {
  167. files.trim();
  168. files.removeEmptyStrings();
  169. files.removeDuplicates (false);
  170. String s;
  171. for (int i = 0; i < files.size(); ++i)
  172. s << "#include \"" << files[i] << "\"" << newLine;
  173. return s;
  174. }
  175. //==============================================================================
  176. static void replaceTemplate (String& text, const String& itemName, const String& value)
  177. {
  178. for (;;)
  179. {
  180. const int index = text.indexOf ("%%" + itemName + "%%");
  181. if (index < 0)
  182. break;
  183. int indentLevel = 0;
  184. for (int i = index; --i >= 0;)
  185. {
  186. if (text[i] == '\n')
  187. break;
  188. ++indentLevel;
  189. }
  190. text = text.replaceSection (index, itemName.length() + 4,
  191. indentCode (value, indentLevel));
  192. }
  193. }
  194. //==============================================================================
  195. void CodeGenerator::applyToCode (String& code, const File& targetFile,
  196. bool isForPreview, Project* project) const
  197. {
  198. replaceTemplate (code, "juceVersion", SystemStats::getJUCEVersion());
  199. replaceTemplate (code, "headerGuard", makeHeaderGuardName (targetFile));
  200. replaceTemplate (code, "className", className);
  201. replaceTemplate (code, "constructorParams", constructorParams);
  202. replaceTemplate (code, "initialisers", getInitialiserList());
  203. replaceTemplate (code, "classDeclaration", getClassDeclaration());
  204. replaceTemplate (code, "privateMemberDeclarations", privateMemberDeclarations);
  205. replaceTemplate (code, "publicMemberDeclarations", getCallbackDeclarations() + newLine + publicMemberDeclarations);
  206. replaceTemplate (code, "methodDefinitions", getCallbackDefinitions());
  207. if (project != 0)
  208. replaceTemplate (code, "defaultJuceInclude", createIncludeStatement (project->getAppIncludeFile(), targetFile));
  209. else
  210. replaceTemplate (code, "defaultJuceInclude", "#include \"juce_amalgamated.h\"");
  211. replaceTemplate (code, "includeFilesH", getIncludeFileCode (includeFilesH));
  212. replaceTemplate (code, "includeFilesCPP", getIncludeFileCode (includeFilesCPP));
  213. replaceTemplate (code, "constructor", constructorCode);
  214. replaceTemplate (code, "destructor", destructorCode);
  215. if (! isForPreview)
  216. {
  217. replaceTemplate (code, "metadata", jucerMetadata);
  218. replaceTemplate (code, "staticMemberDefinitions", staticMemberDefinitions);
  219. }
  220. else
  221. {
  222. replaceTemplate (code, "metadata", " << Metadata isn't shown in the code preview >>" + String (newLine));
  223. replaceTemplate (code, "staticMemberDefinitions", "// Static member declarations and resources would go here... (these aren't shown in the code preview)");
  224. }
  225. }
  226. //==============================================================================
  227. CodeGenerator::CustomCodeList::Iterator::Iterator (const String& documentText, CustomCodeList& customCode_)
  228. : customCode (customCode_), i (0), codeDocument (0)
  229. {
  230. lines.addLines (documentText);
  231. }
  232. CodeGenerator::CustomCodeList::Iterator::~Iterator()
  233. {
  234. }
  235. bool CodeGenerator::CustomCodeList::Iterator::next()
  236. {
  237. textBefore = String::empty;
  238. textAfter = String::empty;
  239. while (i < lines.size())
  240. {
  241. textBefore += lines[i] + "\n";
  242. if (lines[i].trimStart().startsWith ("//["))
  243. {
  244. String tag (lines[i].trimStart().substring (3));
  245. tag = tag.upToFirstOccurrenceOf ("]", false, false).trim();
  246. if (! (tag.isEmpty() || tag.startsWithChar ('/')))
  247. {
  248. const int endLine = indexOfLineStartingWith (lines, "//[/" + tag + "]", i + 1);
  249. if (endLine > i)
  250. {
  251. sectionName = tag;
  252. codeDocument = customCode.getDocumentFor (tag, true);
  253. i = endLine;
  254. bool isLastTag = true;
  255. for (int j = i + 1; j < lines.size(); ++j)
  256. {
  257. if (lines[j].trimStart().startsWith ("//["))
  258. {
  259. isLastTag = false;
  260. break;
  261. }
  262. }
  263. if (isLastTag)
  264. {
  265. textAfter = lines.joinIntoString (newLine, i, lines.size() - i);
  266. i = lines.size();
  267. }
  268. return true;
  269. }
  270. }
  271. }
  272. ++i;
  273. }
  274. return false;
  275. }
  276. //==============================================================================
  277. CodeGenerator::CustomCodeList::CustomCodeList()
  278. {
  279. }
  280. CodeGenerator::CustomCodeList::~CustomCodeList()
  281. {
  282. }
  283. void CodeGenerator::CustomCodeList::reloadFrom (const String& fileContent)
  284. {
  285. StringArray newNames;
  286. ReferenceCountedArray<CodeDocumentRef> newContent;
  287. StringArray lines;
  288. lines.addLines (fileContent);
  289. for (int i = 0; i < lines.size(); ++i)
  290. {
  291. if (lines[i].trimStart().startsWith ("//["))
  292. {
  293. String tag (lines[i].trimStart().substring (3));
  294. tag = tag.upToFirstOccurrenceOf ("]", false, false).trim();
  295. jassert (! (tag.isEmpty() || tag.startsWithChar ('/')));
  296. if (! (tag.isEmpty() || tag.startsWithChar ('/')))
  297. {
  298. const int endLine = indexOfLineStartingWith (lines, "//[/" + tag + "]", i + 1);
  299. if (endLine > i)
  300. {
  301. String content (lines.joinIntoString (newLine, i + 1, endLine - i - 1));
  302. newNames.add (tag);
  303. CodeDocumentRef::Ptr doc (getDocumentFor (tag, false));
  304. if (doc == 0)
  305. {
  306. CodeDocument* const codeDoc = new CodeDocument();
  307. doc = new CodeDocumentRef (codeDoc);
  308. codeDoc->replaceAllContent (content);
  309. codeDoc->clearUndoHistory();
  310. codeDoc->setSavePoint();
  311. }
  312. newContent.add (doc);
  313. i = endLine;
  314. }
  315. }
  316. }
  317. }
  318. sectionNames = newNames;
  319. sectionContent = newContent;
  320. sendSynchronousChangeMessage (this);
  321. }
  322. void CodeGenerator::CustomCodeList::applyTo (String& fileContent) const
  323. {
  324. StringArray lines;
  325. lines.addLines (fileContent);
  326. for (int i = 0; i < lines.size(); ++i)
  327. {
  328. if (lines[i].trimStart().startsWith ("//["))
  329. {
  330. String tag (lines[i].trimStart().substring (3));
  331. tag = tag.upToFirstOccurrenceOf ("]", false, false);
  332. jassert (! tag.startsWithChar ('/'));
  333. if (! tag.startsWithChar ('/'))
  334. {
  335. const int endLine = indexOfLineStartingWith (lines, "//[/" + tag + "]", i + 1);
  336. if (endLine > i)
  337. {
  338. StringArray sourceLines;
  339. sourceLines.addLines (getSectionContent (tag));
  340. if (sourceLines.size() > 0)
  341. {
  342. lines.removeRange (i + 1, endLine - i - 1);
  343. for (int j = 0; j < sourceLines.size(); ++j)
  344. lines.insert (++i, sourceLines [j].trimEnd());
  345. ++i;
  346. }
  347. else
  348. {
  349. i = endLine;
  350. }
  351. }
  352. }
  353. }
  354. lines.set (i, lines[i].trimEnd());
  355. }
  356. if (lines[lines.size() - 1].isNotEmpty())
  357. lines.add (String::empty);
  358. fileContent = lines.joinIntoString (newLine);
  359. }
  360. bool CodeGenerator::CustomCodeList::needsSaving() const
  361. {
  362. for (int i = sectionContent.size(); --i >= 0;)
  363. if (sectionContent.getUnchecked(i)->getDocument().hasChangedSinceSavePoint())
  364. return true;
  365. return false;
  366. }
  367. int CodeGenerator::CustomCodeList::getNumSections() const
  368. {
  369. return sectionNames.size();
  370. }
  371. const String CodeGenerator::CustomCodeList::getSectionName (int index) const
  372. {
  373. return sectionNames [index];
  374. }
  375. const CodeGenerator::CustomCodeList::CodeDocumentRef::Ptr CodeGenerator::CustomCodeList::getDocument (int index) const
  376. {
  377. return sectionContent [index];
  378. }
  379. const CodeGenerator::CustomCodeList::CodeDocumentRef::Ptr CodeGenerator::CustomCodeList::getDocumentFor (const String& sectionName, bool createIfNotFound)
  380. {
  381. const int index = sectionNames.indexOf (sectionName);
  382. if (index >= 0)
  383. return sectionContent [index];
  384. if (createIfNotFound)
  385. {
  386. sectionNames.add (sectionName);
  387. const CodeDocumentRef::Ptr doc (new CodeDocumentRef (new CodeDocument()));
  388. sectionContent.add (doc);
  389. return doc;
  390. }
  391. return 0;
  392. }
  393. const String CodeGenerator::CustomCodeList::getSectionContent (const String& sectionName) const
  394. {
  395. const int index = sectionNames.indexOf (sectionName);
  396. if (index >= 0)
  397. return sectionContent[index]->getDocument().getAllContent();
  398. return String::empty;
  399. }
  400. void CodeGenerator::CustomCodeList::removeSection (const String& sectionName)
  401. {
  402. const int index = sectionNames.indexOf (sectionName);
  403. if (index >= 0)
  404. {
  405. sectionNames.remove (index);
  406. sectionContent.remove (index);
  407. }
  408. }