GroovyHighlighterAndTokenizer { public void processFile(SensorContext context) { List<GroovyToken> tokens = new ArrayList<>(); isAnnotation = false; try (InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file), context.fileSystem().encoding())) { GroovyLexer groovyLexer = new GroovyLexer(streamReader); groovyLexer.setWhitespaceIncluded(true); TokenStream tokenStream = groovyLexer.plumb(); Token token = tokenStream.nextToken(); int type = token.getType(); while (type != Token.EOF_TYPE) { String text = token.getText(); TypeOfText typeOfText = typeOfText(type, text).orElse(null); GroovySourceToken gst = (GroovySourceToken) token; if (StringUtils.isNotBlank(text)) { tokens.add(new GroovyToken(token.getLine(), token.getColumn(), gst.getLineLast(), gst.getColumnLast(), getImage(token, text), typeOfText)); } token = tokenStream.nextToken(); type = token.getType(); } } catch (TokenStreamException e) { LOG.error("Unexpected token when lexing file: " + file.getName(), e); } catch (IOException e) { LOG.error("Unable to read file: " + file.getName(), e); } if (!tokens.isEmpty()) { boolean isNotTest = inputFile.type() != InputFile.Type.TEST; NewCpdTokens cpdTokens = isNotTest ? context.newCpdTokens().onFile(inputFile) : null; NewHighlighting highlighting = context.newHighlighting().onFile(inputFile); for (GroovyToken groovyToken : tokens) { if (isNotTest) { cpdTokens = cpdTokens.addToken(groovyToken.startLine, groovyToken.startColumn, groovyToken.endLine, groovyToken.endColumn, groovyToken.value); } if (groovyToken.typeOfText != null) { highlighting = highlighting.highlight(groovyToken.startLine, groovyToken.startColumn, groovyToken.endLine, groovyToken.endColumn, groovyToken.typeOfText); } } highlighting.save(); if (isNotTest) { cpdTokens.save(); } } } GroovyHighlighterAndTokenizer(InputFile inputFile); void processFile(SensorContext context);  }
@Test public void should_highlight_keywords() throws Exception { File file = TestUtils.getResource("/org/sonar/plugins/groovy/foundation/Greet.groovy"); SensorContextTester context = SensorContextTester.create(file.getParentFile()); DefaultInputFile inputFile = new DefaultInputFile("", "Greet.groovy") .setLanguage(Groovy.KEY) .setType(Type.MAIN) .initMetadata(new String(Files.readAllBytes(file.toPath()), "UTF-8")); context.fileSystem().add(inputFile); GroovyHighlighterAndTokenizer highlighter = new GroovyHighlighterAndTokenizer(inputFile); context = Mockito.spy(context); highlighter.processFile(context); assertThat(context.highlightingTypeAt(":Greet.groovy", 1, 0)).containsOnly(TypeOfText.KEYWORD); assertThat(context.highlightingTypeAt(":Greet.groovy", 2, 2)).containsOnly(TypeOfText.KEYWORD); assertThat(context.highlightingTypeAt(":Greet.groovy", 4, 2)).containsOnly(TypeOfText.KEYWORD); assertThat(context.highlightingTypeAt(":Greet.groovy", 4, 25)).containsOnly(TypeOfText.STRING); assertThat(context.highlightingTypeAt(":Greet.groovy", 4, 32)).containsOnly(TypeOfText.STRING); assertThat(context.highlightingTypeAt(":Greet.groovy", 7, 0)).containsOnly(TypeOfText.STRUCTURED_COMMENT); assertThat(context.highlightingTypeAt(":Greet.groovy", 8, 1)).containsOnly(TypeOfText.STRUCTURED_COMMENT); assertThat(context.highlightingTypeAt(":Greet.groovy", 9, 1)).containsOnly(TypeOfText.STRUCTURED_COMMENT); assertThat(context.highlightingTypeAt(":Greet.groovy", 10, 0)).containsOnly(TypeOfText.ANNOTATION); assertThat(context.highlightingTypeAt(":Greet.groovy", 10, 21)).containsOnly(TypeOfText.ANNOTATION); assertThat(context.highlightingTypeAt(":Greet.groovy", 12, 2)).containsOnly(TypeOfText.KEYWORD); assertThat(context.highlightingTypeAt(":Greet.groovy", 12, 13)).containsOnly(TypeOfText.CONSTANT); assertThat(context.highlightingTypeAt(":Greet.groovy", 12, 17)).containsOnly(TypeOfText.COMMENT); Mockito.verify(context, Mockito.times(1)).newHighlighting(); }
@Test public void should_tokenize_for_cpd() throws Exception { File file = TestUtils.getResource("/org/sonar/plugins/groovy/foundation/Greet.groovy"); SensorContextTester context = SensorContextTester.create(file.getParentFile()); DefaultInputFile inputFile = new DefaultInputFile("", "Greet.groovy") .setLanguage(Groovy.KEY) .setType(Type.MAIN) .initMetadata(new String(Files.readAllBytes(file.toPath()), "UTF-8")); context.fileSystem().add(inputFile); GroovyHighlighterAndTokenizer highlighter = new GroovyHighlighterAndTokenizer(inputFile); context = Mockito.spy(context); highlighter.processFile(context); assertThat(context.cpdTokens(":Greet.groovy")).extracting("value").containsExactly("classGreet{", "defname", "Greet(who){name=who}", "defsalute(){printlnLITERALnameLITERALnameLITERAL}", "}", "", "@groovy.beans.Bindable", "classCool{", "doublex=1.4 "}"); Mockito.verify(context, Mockito.times(1)).newCpdTokens(); }
@Test public void should_highlight_nothing_if_file_is_missing() throws Exception { File file = TestUtils.getResource("/org/sonar/plugins/groovy/foundation/Greet.groovy"); SensorContextTester context = SensorContextTester.create(file.getParentFile()); DefaultInputFile inputFile = new DefaultInputFile("", "Greet-fake.groovy") .setLanguage(Groovy.KEY) .setType(Type.MAIN); context.fileSystem().add(inputFile); GroovyHighlighterAndTokenizer highlighter = new GroovyHighlighterAndTokenizer(inputFile); context = Mockito.spy(context); highlighter.processFile(context); Mockito.verify(context, Mockito.never()).newHighlighting(); }
@Test public void should_highlight_only_partially_if_file_can_not_be_lexed() throws Exception { File file = TestUtils.getResource("/org/sonar/plugins/groovy/foundation/Error.groovy"); SensorContextTester context = SensorContextTester.create(file.getParentFile()); DefaultInputFile inputFile = new DefaultInputFile("", "Error.groovy") .setLanguage(Groovy.KEY) .setType(Type.MAIN) .initMetadata(new String(Files.readAllBytes(file.toPath()), "UTF-8")); context.fileSystem().add(inputFile); GroovyHighlighterAndTokenizer highlighter = new GroovyHighlighterAndTokenizer(inputFile); context = Mockito.spy(context); highlighter.processFile(context); assertThat(context.highlightingTypeAt(":Error.groovy", 1, 0)).containsOnly(TypeOfText.KEYWORD); assertThat(context.highlightingTypeAt(":Error.groovy", 2, 2)).containsOnly(TypeOfText.KEYWORD); assertThat(context.highlightingTypeAt(":Error.groovy", 3, 2)).isEmpty(); Mockito.verify(context, Mockito.times(1)).newHighlighting(); }