public static String toCssClass(HighlightingType type) { return toBatchType(type).cssClass(); } }
private static void processLine(File highlightingFile, int lineNumber, NewHighlighting highlighting, String line) { try { String[] split = line.split(":"); if (split.length == 3) { int startOffset = parseInt(split[0]); int endOffset = parseInt(split[1]); highlighting.highlight(startOffset, endOffset, TypeOfText.forCssClass(split[2])); } else if (split.length == 5) { int startLine = parseInt(split[0]); int startLineOffset = parseInt(split[1]); int endLine = parseInt(split[2]); int endLineOffset = parseInt(split[3]); highlighting.highlight(startLine, startLineOffset, endLine, endLineOffset, TypeOfText.forCssClass(split[4])); } else { throw new IllegalStateException("Illegal number of elements separated by ':'. " + "Must either be startOffset:endOffset:class (offset in whole file) or startLine:startLineOffset:endLine:endLineOffset:class"); } } catch (Exception e) { throw new IllegalStateException("Error processing line " + lineNumber + " of file " + highlightingFile.getAbsolutePath(), e); } }
@Override public void execute(SensorContext context) { Iterator<InputFile> inputFiles = context.fileSystem().inputFiles(context.fileSystem().predicates().all()).iterator(); if (!inputFiles.hasNext()) { throw new IllegalStateException("No files indexed"); } InputFile file = inputFiles.next(); context.newHighlighting() .onFile(file) .highlight(file.selectLine(1), TypeOfText.CONSTANT) .save(); context.newHighlighting() .onFile(file) .highlight(file.selectLine(file.lines()), TypeOfText.COMMENT) .save(); } }
private static void saveHighlights(SensorContext sensorContext, SensorContextUtils.Highlight[] highlights, InputFile inputFile) { NewHighlighting highlighting = sensorContext.newHighlighting().onFile(inputFile); for (SensorContextUtils.Highlight highlight : highlights) { highlighting.highlight(highlight.startLine, highlight.startCol, highlight.endLine, highlight.endCol, TypeOfText.valueOf(highlight.textType.toUpperCase(Locale.ENGLISH))); } highlighting.save(); }
private void processFileHighlighting(InputFile inputFile, SensorContext context) { File ioFile = inputFile.file(); File highlightingFile = new File(ioFile.getParentFile(), ioFile.getName() + HIGHLIGHTING_EXTENSION); if (highlightingFile.exists()) { LOG.debug("Processing " + highlightingFile.getAbsolutePath()); try { List<String> lines = FileUtils.readLines(highlightingFile, context.fileSystem().encoding().name()); int lineNumber = 0; NewHighlighting highlighting = context.newHighlighting() .onFile(inputFile); for (String line : lines) { lineNumber++; if (StringUtils.isBlank(line) || line.startsWith("#")) { continue; } processLine(highlightingFile, lineNumber, highlighting, line); } highlighting.save(); } catch (IOException e) { throw new IllegalStateException(e); } } }
@Override public void leaveFile(Tree scriptTree) { highlighting.save(); }
/** * Highlights the portion of code in SonarQube as described by this class * * @param highlighting SonarQube's highlighting for this portion of code */ public void highlight(NewHighlighting highlighting) { highlighting.highlight(startLine, startColumnOffset, endLine, endColumnOffset, typeOfText); }
@Test public void testForCss() { assertThat(TypeOfText.forCssClass("k")).isEqualTo(TypeOfText.KEYWORD); assertThat(TypeOfText.KEYWORD_LIGHT.cssClass()).isEqualTo("h"); }
@Override public HighlightingBuilder highlight(int startOffset, int endOffset, String typeOfText) { TypeOfText type = org.sonar.api.batch.sensor.highlighting.TypeOfText.forCssClass(typeOfText); defaultHighlighting.highlight(startOffset, endOffset, type); return this; }
@Test(expected = IllegalArgumentException.class) public void throwIfUnknowCss() { TypeOfText.forCssClass("w"); }
@Test(expected = UnsupportedOperationException.class) public void duplicateHighlighting() { tester.newHighlighting() .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build()) .highlight(1, 0, 1, 5, TypeOfText.ANNOTATION) .save(); tester.newHighlighting() .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build()) .highlight(1, 0, 1, 5, TypeOfText.ANNOTATION) .save(); }
@Override public void leaveFile(AstNode astNode) { if (highlighting != null) { highlighting.save(); } }
private void highlightingComment(int lineNumber, int end) { highlighting.highlight(lineNumber, 0, lineNumber, end, TypeOfText.COMMENT); }
@Override public String toString() { return "" + startPosition + "," + endPosition + "," + textType.cssClass(); } }
@Override public HighlightingBuilder highlight(int startOffset, int endOffset, String typeOfText) { TypeOfText type = org.sonar.api.batch.sensor.highlighting.TypeOfText.forCssClass(typeOfText); builder.registerHighlightingRule(startOffset, endOffset, type); return this; }
@Test public void testHighlighting() { assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).isEmpty(); tester.newHighlighting() .onFile(new TestInputFileBuilder("foo", "src/Foo.java").initMetadata("annot dsf fds foo bar").build()) .highlight(1, 0, 1, 5, TypeOfText.ANNOTATION) .highlight(8, 10, TypeOfText.CONSTANT) .highlight(9, 10, TypeOfText.COMMENT) .save(); assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 3)).containsExactly(TypeOfText.ANNOTATION); assertThat(tester.highlightingTypeAt("foo:src/Foo.java", 1, 9)).containsExactly(TypeOfText.CONSTANT, TypeOfText.COMMENT); }
@Override public void leaveFile(Tree scriptTree) { highlighting.save(); }
private void addHighlighting(XmlTextRange textRange, TypeOfText typeOfText) { highlighting.highlight( textRange.getStartLine(), textRange.getStartColumn(), textRange.getEndLine(), textRange.getEndColumn(), typeOfText); } }
public void save() { newHighlighting.save(); }