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.

99 lines
2.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #pragma once
  14. //==============================================================================
  15. struct SourceCodeRange
  16. {
  17. SourceCodeRange() = default;
  18. SourceCodeRange (const String& f, int start, int end)
  19. : file (f), range (start, end)
  20. {
  21. #if JUCE_WINDOWS
  22. file = file.replaceCharacter ('/', '\\');
  23. #endif
  24. }
  25. SourceCodeRange (const String& s)
  26. {
  27. String::CharPointerType colon1 (nullptr), colon2 (nullptr);
  28. for (auto p = s.getCharPointer(); ! p.isEmpty(); ++p)
  29. {
  30. if (*p == ':')
  31. {
  32. colon1 = colon2;
  33. colon2 = p;
  34. }
  35. }
  36. if (colon1.getAddress() != nullptr && colon2.getAddress() != nullptr)
  37. {
  38. file = String (s.getCharPointer(), colon1);
  39. range = Range<int> ((colon1 + 1).getIntValue32(),
  40. (colon2 + 1).getIntValue32());
  41. }
  42. }
  43. String file;
  44. Range<int> range;
  45. bool isValid() const noexcept { return file.isNotEmpty() && range != Range<int>(); }
  46. void nudge (const String& changedFile, const int insertPoint, const int delta) noexcept
  47. {
  48. if (range.getEnd() >= insertPoint && file == changedFile)
  49. {
  50. const int newEnd = range.getEnd() + delta;
  51. int newStart = range.getStart();
  52. if (newStart > insertPoint)
  53. newStart += delta;
  54. range = Range<int> (newStart, newEnd);
  55. }
  56. }
  57. void fileContentChanged (const String& changedFile) noexcept
  58. {
  59. if (file == changedFile)
  60. range = Range<int>();
  61. }
  62. String toString() const
  63. {
  64. if (file.isEmpty() && range.isEmpty())
  65. return String();
  66. return file + ":" + String (range.getStart()) + ":" + String (range.getEnd());
  67. }
  68. void writeToValueTree (ValueTree& v, const Identifier& prop) const
  69. {
  70. const String s (toString());
  71. if (s.isNotEmpty())
  72. v.setProperty (prop, s, nullptr);
  73. }
  74. bool operator== (const SourceCodeRange& other) const noexcept { return range == other.range && file == other.file; }
  75. bool operator!= (const SourceCodeRange& other) const noexcept { return ! operator== (other); }
  76. };