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 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(); }
@Override public void leaveFile(Tree scriptTree) { highlighting.save(); }
@Override public void leaveFile(AstNode astNode) { if (highlighting != null) { highlighting.save(); } }
@Override public void leaveFile(Tree scriptTree) { highlighting.save(); }
public void save() { newHighlighting.save(); }
@Override public void leaveFile(@Nullable AstNode astNode) { newHighlighting.save(); }
@Override public void leaveFile(@Nullable AstNode astNode) { newHighlighting.save(); }
@Override public void leaveFile(@Nullable AstNode astNode) { super.leaveFile(astNode); newHighlighting.save(); }
public void colorize(InputFile i) { final File f = i.file(); LOGGER.debug("Coloring the file: " + f.getPath()); highlighting.onFile(i); try (final BufferedReader br = new BufferedReader( new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8))) { String line; int lineNumber = 1; while ((line = br.readLine()) != null) { searchAndColor(line, lineNumber); lineNumber++; } } catch (final IOException e) { LOGGER.error("IO Exception", e); } try { highlighting.save(); } catch (Exception ex) { LOGGER.error("Highlighting problem on save", ex.getMessage()); } }
public void render(Reader code, List<? extends Channel<HtmlCodeBuilder>> tokenizers, NewHighlighting highlighting) { List<Channel<HtmlCodeBuilder>> allTokenizers = new ArrayList<>(); HighlightingCodeBuilder codeBuilder = new HighlightingCodeBuilder(highlighting); allTokenizers.addAll(tokenizers); new TokenizerDispatcher(allTokenizers).colorize(new CodeReader(code), codeBuilder); highlighting.save(); } }
@Override public void scanFile(JavaFileScannerContext context) { File file = context.getFile(); highlighting = sonarComponents.highlightableFor(file); super.scanFile(context); highlighting.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(); }
@Override public void scanFile(JavaFileScannerContext context) { File file = context.getFile(); highlighting = sonarComponents.highlightableFor(file); super.scanFile(context); highlighting.save(); }
private void highlight(SensorContext context) { highlighting = context.newHighlighting().onFile(xmlFile.getInputFile()); xmlFile.getPrologElement().ifPresent(this::highlightProlog); children(xmlFile.getDocument()).forEach(this::highlightNode); highlighting.save(); }
@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(); } }
/** * Saves the syntax highlighting for the analyzed code * * @param context the sensor context * @param sourceCode the YAML source code */ private static void saveSyntaxHighlighting(SensorContext context, YamlSourceCode sourceCode) throws IOException { List<HighlightingData> highlightingDataList = new YamlHighlighting(sourceCode).getHighlightingData(); NewHighlighting highlighting = context.newHighlighting().onFile(sourceCode.getYamlFile()); for (HighlightingData highlightingData : highlightingDataList) { highlightingData.highlight(highlighting); } highlighting.save(); }
private void highlight(String s) { DefaultInputFile inputFile = TestInputFileBuilder.create("moduleKey", file.getName()) .setLanguage("php") .setType(Type.MAIN) .initMetadata(s) .build(); fileSystem.add(inputFile); context = SensorContextTester.create(tempFolder.getRoot()); NewHighlighting highlighting = context.newHighlighting().onFile(inputFile); Tree tree = PARSER.parse(s); SyntaxHighlighterVisitor.highlight(tree, highlighting); highlighting.save(); }