3030import org .sonar .python .quickfix .IssueWithQuickFix ;
3131import org .sonar .python .quickfix .PythonQuickFix ;
3232
33- import static org .sonar .python .quickfix .PythonTextEdit .insertAfter ;
33+ import static org .sonar .python .quickfix .PythonTextEdit .replaceRange ;
3434
3535@ Rule (key = "S5799" )
3636public class ImplicitStringConcatenationCheck extends PythonSubscriptionCheck {
@@ -67,12 +67,12 @@ private static void checkStringLiteral(StringLiteral stringLiteral, Subscription
6767 continue ;
6868 }
6969 if (current .firstToken ().line () == previous .firstToken ().line ()) {
70- createQuickFix (ctx .addIssue (previous .firstToken (), MESSAGE_SINGLE_LINE ).secondary (current .firstToken (), null ), previous );
70+ createQuickFix (ctx .addIssue (previous .firstToken (), MESSAGE_SINGLE_LINE ).secondary (current .firstToken (), null ), previous , current );
7171 // Only raise 1 issue per string literal
7272 return ;
7373 }
7474 if ((isWithinCollection (stringLiteral ) && !isException (previous , current ))) {
75- createQuickFix (ctx .addIssue (previous .firstToken (), MESSAGE_MULTIPLE_LINES ).secondary (current .firstToken (), null ), previous );
75+ createQuickFix (ctx .addIssue (previous .firstToken (), MESSAGE_MULTIPLE_LINES ).secondary (current .firstToken (), null ), previous , current );
7676 return ;
7777 }
7878 }
@@ -94,26 +94,31 @@ private static boolean haveSameQuotes(StringElement first, StringElement second)
9494 first .value ().charAt (first .value ().length () - 1 ) == second .value ().charAt (second .value ().length () - 1 );
9595 }
9696
97- private static boolean isInFunctionOrArrayOrTupleOrExpression (StringElement token ) {
97+ private static boolean isInFunctionOrArrayOrTupleOrExpressionOrSet (StringElement token ) {
9898 Tree t = token ;
9999 while (t .parent ().is (Tree .Kind .STRING_LITERAL )) {
100100 t = t .parent ();
101101 }
102- return t .parent ().is (Tree .Kind .ARG_LIST , Tree .Kind .EXPRESSION_LIST , Tree .Kind .PARAMETER_LIST , Tree .Kind .TUPLE );
102+ Tree parent = t .parent ();
103+
104+ return parent .is (Tree .Kind .EXPRESSION_LIST , Tree .Kind .PLUS , Tree .Kind .REGULAR_ARGUMENT ,
105+ Tree .Kind .SET_LITERAL , Tree .Kind .TUPLE );
103106 }
104107
105- private static void createQuickFix (PreciseIssue issueRaised , StringElement stringElement ) {
108+ private static void createQuickFix (PreciseIssue issueRaised , StringElement start , StringElement end ) {
106109 IssueWithQuickFix issue = (IssueWithQuickFix ) issueRaised ;
110+ String textStart = start .value ();
111+ String textEnd = end .value ();
107112
108- if (isInFunctionOrArrayOrTupleOrExpression ( stringElement )) {
113+ if (isInFunctionOrArrayOrTupleOrExpressionOrSet ( start )) {
109114 PythonQuickFix quickFix = PythonQuickFix .newQuickFix ("Add the comma between string or byte tokens." )
110- .addTextEdit (insertAfter ( stringElement , "," ))
115+ .addTextEdit (replaceRange ( start , end , textStart + ", " + textEnd ))
111116 .build ();
112117 issue .addQuickFix (quickFix );
113118 }
114119
115120 PythonQuickFix quickFix = PythonQuickFix .newQuickFix ("Make the addition sign between string or byte tokens explicit." )
116- .addTextEdit (insertAfter ( stringElement , "+" ))
121+ .addTextEdit (replaceRange ( start , end , textStart + " + " + textEnd ))
117122 .build ();
118123 issue .addQuickFix (quickFix );
119124 }
0 commit comments