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.

DefaultHighlighting.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.api.batch.sensor.highlighting.internal;
  21. import java.util.ArrayList;
  22. import java.util.Collections;
  23. import java.util.Iterator;
  24. import java.util.List;
  25. import org.sonar.api.batch.fs.InputFile;
  26. import org.sonar.api.batch.fs.TextRange;
  27. import org.sonar.api.batch.sensor.highlighting.NewHighlighting;
  28. import org.sonar.api.batch.sensor.highlighting.TypeOfText;
  29. import org.sonar.api.batch.sensor.internal.SensorStorage;
  30. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  31. import org.sonar.api.batch.sensor.internal.DefaultStorable;
  32. import static java.util.Objects.requireNonNull;
  33. import static org.sonar.api.utils.Preconditions.checkState;
  34. public class DefaultHighlighting extends DefaultStorable implements NewHighlighting {
  35. private final List<SyntaxHighlightingRule> syntaxHighlightingRules;
  36. private DefaultInputFile inputFile;
  37. public DefaultHighlighting(SensorStorage storage) {
  38. super(storage);
  39. syntaxHighlightingRules = new ArrayList<>();
  40. }
  41. public List<SyntaxHighlightingRule> getSyntaxHighlightingRuleSet() {
  42. return syntaxHighlightingRules;
  43. }
  44. private void checkOverlappingBoundaries() {
  45. if (syntaxHighlightingRules.size() > 1) {
  46. Iterator<SyntaxHighlightingRule> it = syntaxHighlightingRules.iterator();
  47. SyntaxHighlightingRule previous = it.next();
  48. while (it.hasNext()) {
  49. SyntaxHighlightingRule current = it.next();
  50. if (previous.range().end().compareTo(current.range().start()) > 0 && (previous.range().end().compareTo(current.range().end()) < 0)) {
  51. String errorMsg = String.format("Cannot register highlighting rule for characters at %s as it " +
  52. "overlaps at least one existing rule", current.range());
  53. throw new IllegalStateException(errorMsg);
  54. }
  55. previous = current;
  56. }
  57. }
  58. }
  59. @Override
  60. public DefaultHighlighting onFile(InputFile inputFile) {
  61. requireNonNull(inputFile, "file can't be null");
  62. this.inputFile = (DefaultInputFile) inputFile;
  63. return this;
  64. }
  65. public InputFile inputFile() {
  66. return inputFile;
  67. }
  68. @Override
  69. public DefaultHighlighting highlight(int startLine, int startLineOffset, int endLine, int endLineOffset, TypeOfText typeOfText) {
  70. checkInputFileNotNull();
  71. TextRange newRange;
  72. try {
  73. newRange = inputFile.newRange(startLine, startLineOffset, endLine, endLineOffset);
  74. } catch (Exception e) {
  75. throw new IllegalArgumentException("Unable to highlight file " + inputFile, e);
  76. }
  77. return highlight(newRange, typeOfText);
  78. }
  79. @Override
  80. public DefaultHighlighting highlight(TextRange range, TypeOfText typeOfText) {
  81. SyntaxHighlightingRule syntaxHighlightingRule = SyntaxHighlightingRule.create(range, typeOfText);
  82. this.syntaxHighlightingRules.add(syntaxHighlightingRule);
  83. return this;
  84. }
  85. @Override
  86. protected void doSave() {
  87. checkInputFileNotNull();
  88. // Sort rules to avoid variation during consecutive runs
  89. Collections.sort(syntaxHighlightingRules, (left, right) -> {
  90. int result = left.range().start().compareTo(right.range().start());
  91. if (result == 0) {
  92. result = right.range().end().compareTo(left.range().end());
  93. }
  94. return result;
  95. });
  96. checkOverlappingBoundaries();
  97. storage.store(this);
  98. }
  99. private void checkInputFileNotNull() {
  100. checkState(inputFile != null, "Call onFile() first");
  101. }
  102. }