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.

106 lines
3.2KB

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