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.

103 lines
3.1KB

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