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(); } }
@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); }
@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(); }
/** * 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); }
private void highlightingComment(int lineNumber, int end) { highlighting.highlight(lineNumber, 0, lineNumber, end, TypeOfText.COMMENT); }
private void processLine(File highlightingFile, int lineNumber, NewHighlighting highlightingBuilder, String line) { try { Iterator<String> split = Splitter.on(":").split(line).iterator(); int startOffset = Integer.parseInt(split.next()); int endOffset = Integer.parseInt(split.next()); TypeOfText type = TypeOfText.forCssClass(split.next()); highlightingBuilder.highlight(startOffset, endOffset, type); } catch (Exception e) { throw new IllegalStateException("Error processing line " + lineNumber + " of file " + highlightingFile.getAbsolutePath(), e); } }
@Override public void visitTrivia(SyntaxTrivia syntaxTrivia) { String comment = syntaxTrivia.comment(); int startLine = syntaxTrivia.startLine(); int startColumn = syntaxTrivia.column(); String[] lines = comment.split("\\r\\n|\\n|\\r"); int numberLines = lines.length; int endLine = startLine + numberLines - 1; int endColumn = numberLines == 1 ? (startColumn + comment.length()) : lines[numberLines - 1].length(); boolean isJavadoc = lines[0].trim().startsWith("/**"); highlighting.highlight(startLine, startColumn, endLine, endColumn, isJavadoc ? TypeOfText.STRUCTURED_COMMENT : TypeOfText.COMMENT); } }
@Override public void visitTrivia(SyntaxTrivia syntaxTrivia) { String comment = syntaxTrivia.comment(); int startLine = syntaxTrivia.startLine(); int startColumn = syntaxTrivia.column(); String[] lines = comment.split("\\r\\n|\\n|\\r"); int numberLines = lines.length; int endLine = startLine + numberLines - 1; int endColumn = numberLines == 1 ? (startColumn + comment.length()) : lines[numberLines - 1].length(); boolean isJavadoc = lines[0].trim().startsWith("/**"); highlighting.highlight(startLine, startColumn, endLine, endColumn, isJavadoc ? TypeOfText.STRUCTURED_COMMENT : TypeOfText.COMMENT); } }
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 addHighlighting(XmlTextRange textRange, TypeOfText typeOfText) { highlighting.highlight( textRange.getStartLine(), textRange.getStartColumn(), textRange.getEndLine(), textRange.getEndColumn(), typeOfText); } }
private void highlight(SyntaxToken token, TypeOfText typeOfText) { highlighting.highlight(token.line(), token.column(), token.endLine(), token.endColumn(), typeOfText); }
private void highlight(Tree from, Tree to, TypeOfText typeOfText) { SyntaxToken firstToken = from.firstToken(); SyntaxToken lastToken = to.lastToken(); highlighting.highlight(firstToken.line(), firstToken.column(), lastToken.line(), lastToken.column() + lastToken.text().length(), typeOfText); }
private void highlight(Tree from, Tree to, TypeOfText typeOfText) { SyntaxToken firstToken = from.firstToken(); SyntaxToken lastToken = to.lastToken(); highlighting.highlight(firstToken.line(), firstToken.column(), lastToken.line(), lastToken.column() + lastToken.text().length(), typeOfText); }
private void highlight(Token token, TypeOfText code) { TokenLocation location = TokenLocation.from(token); if (highlighting != null) { highlighting.highlight(location.line(), location.column(), location.endLine(), location.endColumn(), code); } }
private void highlight(Token token, TypeOfText typeOfText) { TokenLocation tokenLocation = new TokenLocation(token); newHighlighting.highlight(tokenLocation.startLine(), tokenLocation.startLineOffset(), tokenLocation.endLine(), tokenLocation.endLineOffset(), typeOfText); }
private void highlight(Token token, TypeOfText typeOfText) { TokenLocation tokenLocation = new TokenLocation(token); newHighlighting.highlight(tokenLocation.startLine(), tokenLocation.startLineOffset(), tokenLocation.endLine(), tokenLocation.endLineOffset(), typeOfText); }
private static void highlight(NewHighlighting highlighting, Token token, TypeOfText typeOfText) { TokenLocation tokenLocation = new TokenLocation(token); highlighting.highlight(tokenLocation.startLine(), tokenLocation.startCharacter(), tokenLocation.endLine(), tokenLocation.endCharacter(), typeOfText); }
private void highlight(Token token, TypeOfText typeOfText) { TokenLocation tokenLocation = new TokenLocation(token); newHighlighting.highlight(tokenLocation.startLine(), tokenLocation.startLineOffset(), tokenLocation.endLine(), tokenLocation.endLineOffset(), typeOfText); }
private static void highlight(NewHighlighting highlighting, Token token, TypeOfText typeOfText) { TokenLocation tokenLocation = new TokenLocation(token); highlighting.highlight(tokenLocation.startLine(), tokenLocation.startCharacter(), tokenLocation.endLine(), tokenLocation.endCharacter(), typeOfText); }