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.

107 lines
3.2KB

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