33 * Copyright (C) 2021 Eric Le Goff
44 * mailto:community-rust AT pm DOT me
55 * http://github.com/elegoff/sonar-rust
6- *
6+ * <p>
77 * This program is free software; you can redistribute it and/or
88 * modify it under the terms of the GNU Lesser General Public
99 * License as published by the Free Software Foundation; either
1010 * version 3 of the License, or (at your option) any later version.
11- *
11+ * <p>
1212 * This program is distributed in the hope that it will be useful,
1313 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1414 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
1515 * Lesser General Public License for more details.
16- *
16+ * <p>
1717 * You should have received a copy of the GNU Lesser General Public License
1818 * along with this program; if not, write to the Free Software Foundation,
1919 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
@@ -47,10 +47,12 @@ public class RustTokensVisitor {
4747 private final Set <String > keywords = new HashSet <>(Arrays .asList (RustKeyword .keywordValues ()));
4848 private final SensorContext context ;
4949 private final ParserAdapter <LexerlessGrammar > lexer ;
50+ private final boolean ignoreCPDTests ;
5051
5152 public RustTokensVisitor (SensorContext context , ParserAdapter <LexerlessGrammar > lexer ) {
5253 this .context = context ;
5354 this .lexer = lexer ;
55+ this .ignoreCPDTests = context .config ().getBoolean (CommunityRustPlugin .IGNORE_DUPLICATION_FOR_TESTS ).orElse (false );
5456 }
5557
5658 private static String getTokenImage (Token token ) {
@@ -79,26 +81,10 @@ public void scanFile(InputFile inputFile, RustVisitorContext visitorContext) {
7981 Set <Token > unitTestTokens = identifyUnitTestTokens (parsedTokens );
8082
8183 for (Token token : parsedTokens ) {
82- final String tokenImage = getTokenImage (token );
83- final var tokenLocation = tokenLocation (token );
84-
85- if (token .getType ().equals (RustTokenType .CHARACTER_LITERAL )
86- || token .getType ().equals (RustTokenType .STRING_LITERAL )
87- || token .getType ().equals (RustTokenType .RAW_STRING_LITERAL )
88- || token .getType ().equals (RustTokenType .RAW_BYTE_STRING_LITERAL )
8984
90- ) {
91- highlight (highlighting , tokenLocation , TypeOfText .STRING );
92-
93- } else if (keywords .contains (tokenImage )) {
94- highlight (highlighting , tokenLocation , TypeOfText .KEYWORD );
95- }
85+ final var tokenLocation = tokenLocation (token );
9686
97- if (token .getType ().equals (RustTokenType .FLOAT_LITERAL )
98- || token .getType ().equals (RustTokenType .BOOLEAN_LITERAL )
99- || token .getType ().equals (RustTokenType .INTEGER_LITERAL )) {
100- highlight (highlighting , tokenLocation , TypeOfText .CONSTANT );
101- }
87+ highlightToken (token ,tokenLocation , highlighting );
10288
10389 for (Trivia trivia : token .getTrivia ()) {
10490 highlight (highlighting , tokenLocation (trivia .getToken ()), TypeOfText .COMMENT );
@@ -109,15 +95,36 @@ public void scanFile(InputFile inputFile, RustVisitorContext visitorContext) {
10995 );
11096 }
11197
112- if (!GenericTokenType .EOF .equals (token .getType ())) {
113- cpdTokens .addToken (tokenLocation .startLine (), tokenLocation .startLineOffset (), tokenLocation .endLine (), tokenLocation .endLineOffset (), tokenImage );
98+ if (!GenericTokenType .EOF .equals (token .getType ()) && !( unitTestTokens . contains ( token ) && this . ignoreCPDTests ) ) {
99+ cpdTokens .addToken (tokenLocation .startLine (), tokenLocation .startLineOffset (), tokenLocation .endLine (), tokenLocation .endLineOffset (), getTokenImage ( token ) );
114100 }
115101 }
116102
117103 highlighting .save ();
118104 cpdTokens .save ();
119105 }
120106
107+ private void highlightToken (Token token , TokenLocation tokenLocation , NewHighlighting highlighting ){
108+ final String tokenImage = getTokenImage (token );
109+ if (token .getType ().equals (RustTokenType .CHARACTER_LITERAL )
110+ || token .getType ().equals (RustTokenType .STRING_LITERAL )
111+ || token .getType ().equals (RustTokenType .RAW_STRING_LITERAL )
112+ || token .getType ().equals (RustTokenType .RAW_BYTE_STRING_LITERAL )
113+
114+ ) {
115+ highlight (highlighting , tokenLocation , TypeOfText .STRING );
116+
117+ } else if (keywords .contains (tokenImage )) {
118+ highlight (highlighting , tokenLocation , TypeOfText .KEYWORD );
119+ }
120+
121+ if (token .getType ().equals (RustTokenType .FLOAT_LITERAL )
122+ || token .getType ().equals (RustTokenType .BOOLEAN_LITERAL )
123+ || token .getType ().equals (RustTokenType .INTEGER_LITERAL )) {
124+ highlight (highlighting , tokenLocation , TypeOfText .CONSTANT );
125+ }
126+ }
127+
121128 private Set <Token > identifyUnitTestTokens (List <Token > parsedTokens ) {
122129 Set <Token > testTokens = new HashSet <>();
123130 Set <String > unitTestsAttributes = getUnitTestAttributes ();
0 commit comments