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.

101 lines
3.1KB

  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. struct SourceCodeRange
  18. {
  19. SourceCodeRange() = default;
  20. SourceCodeRange (const String& f, int start, int end)
  21. : file (f), range (start, end)
  22. {
  23. #if JUCE_WINDOWS
  24. file = file.replaceCharacter ('/', '\\');
  25. #endif
  26. }
  27. SourceCodeRange (const String& s)
  28. {
  29. String::CharPointerType colon1 (nullptr), colon2 (nullptr);
  30. for (auto p = s.getCharPointer(); ! p.isEmpty(); ++p)
  31. {
  32. if (*p == ':')
  33. {
  34. colon1 = colon2;
  35. colon2 = p;
  36. }
  37. }
  38. if (colon1.getAddress() != nullptr && colon2.getAddress() != nullptr)
  39. {
  40. file = String (s.getCharPointer(), colon1);
  41. range = Range<int> ((colon1 + 1).getIntValue32(),
  42. (colon2 + 1).getIntValue32());
  43. }
  44. }
  45. String file;
  46. Range<int> range;
  47. bool isValid() const noexcept { return file.isNotEmpty() && range != Range<int>(); }
  48. void nudge (const String& changedFile, const int insertPoint, const int delta) noexcept
  49. {
  50. if (range.getEnd() >= insertPoint && file == changedFile)
  51. {
  52. const int newEnd = range.getEnd() + delta;
  53. int newStart = range.getStart();
  54. if (newStart > insertPoint)
  55. newStart += delta;
  56. range = Range<int> (newStart, newEnd);
  57. }
  58. }
  59. void fileContentChanged (const String& changedFile) noexcept
  60. {
  61. if (file == changedFile)
  62. range = Range<int>();
  63. }
  64. String toString() const
  65. {
  66. if (file.isEmpty() && range.isEmpty())
  67. return String();
  68. return file + ":" + String (range.getStart()) + ":" + String (range.getEnd());
  69. }
  70. void writeToValueTree (ValueTree& v, const Identifier& prop) const
  71. {
  72. const String s (toString());
  73. if (s.isNotEmpty())
  74. v.setProperty (prop, s, nullptr);
  75. }
  76. bool operator== (const SourceCodeRange& other) const noexcept { return range == other.range && file == other.file; }
  77. bool operator!= (const SourceCodeRange& other) const noexcept { return ! operator== (other); }
  78. };