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.

209 lines
5.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. //==============================================================================
  20. struct DiagnosticMessage
  21. {
  22. DiagnosticMessage() = default;
  23. DiagnosticMessage (const DiagnosticMessage& other)
  24. : associatedDiagnostic (createCopyIfNotNull (other.associatedDiagnostic.get())),
  25. message (other.message),
  26. mainFile (other.mainFile),
  27. range (other.range),
  28. type (other.type)
  29. {
  30. }
  31. DiagnosticMessage& operator= (const DiagnosticMessage& other)
  32. {
  33. associatedDiagnostic.reset (createCopyIfNotNull (other.associatedDiagnostic.get()));
  34. message = other.message;
  35. mainFile = other.mainFile;
  36. range = other.range;
  37. type = other.type;
  38. return *this;
  39. }
  40. enum Type
  41. {
  42. error = 0,
  43. warning = 1,
  44. note = 2
  45. };
  46. std::unique_ptr<DiagnosticMessage> associatedDiagnostic;
  47. String message;
  48. String mainFile;
  49. SourceCodeRange range;
  50. Type type;
  51. bool isError() const noexcept { return type == error; }
  52. bool isWarning() const noexcept { return type == warning; }
  53. bool isNote() const noexcept { return type == note; }
  54. String toString() const
  55. {
  56. // todo: copy recursively from root
  57. String res;
  58. switch (type)
  59. {
  60. case error: res << "error: "; break;
  61. case warning: res << "warning: "; break;
  62. case note: res << "note: "; break;
  63. default: break;
  64. };
  65. res << mainFile << ": ";
  66. res << message << "\n";
  67. return res;
  68. }
  69. ValueTree toValueTree() const
  70. {
  71. ValueTree v (MessageTypes::DIAGNOSTIC);
  72. v.setProperty (Ids::text, message, nullptr);
  73. v.setProperty (Ids::file, mainFile, nullptr);
  74. v.setProperty (Ids::range, range.toString(), nullptr);
  75. v.setProperty (Ids::type, (int) type, nullptr);
  76. if (associatedDiagnostic != nullptr)
  77. v.addChild (associatedDiagnostic->toValueTree(), 0, nullptr);
  78. return v;
  79. }
  80. static DiagnosticMessage fromValueTree (const ValueTree& v)
  81. {
  82. DiagnosticMessage d;
  83. d.message = v[Ids::text];
  84. d.mainFile = v[Ids::file];
  85. d.range = SourceCodeRange (v [Ids::range]);
  86. d.type = (Type) static_cast<int> (v[Ids::type]);
  87. auto associated = v.getChild (0);
  88. if (associated.isValid())
  89. d.associatedDiagnostic.reset (new DiagnosticMessage (fromValueTree (associated)));
  90. return d;
  91. }
  92. bool operator== (const DiagnosticMessage& other) const noexcept
  93. {
  94. return range == other.range
  95. && message == other.message
  96. && mainFile == other.mainFile;
  97. }
  98. bool operator!= (const DiagnosticMessage& other) const noexcept { return ! operator== (other); }
  99. };
  100. //==============================================================================
  101. struct DiagnosticList
  102. {
  103. // after some research, it seems that notes never come on their own
  104. // i.e. they always have a warning / error preceding them
  105. // so we can keep notes and their associated diagnostics
  106. // together by keeping track of the last message
  107. DiagnosticMessage lastMessage;
  108. ValueTree list { MessageTypes::DIAGNOSTIC_LIST };
  109. void clear()
  110. {
  111. list = ValueTree { MessageTypes::DIAGNOSTIC_LIST };
  112. lastMessage = DiagnosticMessage();
  113. }
  114. void add (DiagnosticMessage m)
  115. {
  116. if (m.isNote())
  117. {
  118. if (lastMessage.message.isEmpty())
  119. return; // seems to happen sometimes, but with seemingly duplicated messages (?)
  120. m.associatedDiagnostic.reset (new DiagnosticMessage (lastMessage));
  121. }
  122. else
  123. {
  124. lastMessage = m;
  125. }
  126. list.appendChild (m.toValueTree(), nullptr);
  127. }
  128. void add (const DiagnosticList& l)
  129. {
  130. jassert (l.list != list);
  131. for (int i = 0; i < l.list.getNumChildren(); ++i)
  132. list.appendChild (l.list.getChild(i).createCopy(), nullptr);
  133. }
  134. void remove (DiagnosticMessage m)
  135. {
  136. auto n = m.toValueTree();
  137. for (int i = 0; i < list.getNumChildren(); ++i)
  138. {
  139. if (list.getChild (i).isEquivalentTo (n))
  140. {
  141. list.removeChild (i, nullptr);
  142. return;
  143. }
  144. }
  145. jassertfalse;
  146. }
  147. bool hasRecoveryWarning (DiagnosticMessage m) const
  148. {
  149. auto n = m.toValueTree();
  150. for (int i = 0; i < list.getNumChildren(); ++i)
  151. if (list.getChild (i).isEquivalentTo (n))
  152. return true;
  153. return false;
  154. }
  155. const ValueTree& toValueTree() const noexcept
  156. {
  157. return list;
  158. }
  159. void loadFromChildOfValueTree (ValueTree& parent)
  160. {
  161. list = parent.getChildWithName (MessageTypes::DIAGNOSTIC_LIST).createCopy();
  162. }
  163. };