30
30
import org .sonar .python .quickfix .IssueWithQuickFix ;
31
31
import org .sonar .python .quickfix .PythonQuickFix ;
32
32
33
- import static org .sonar .python .quickfix .PythonTextEdit .insertAfter ;
33
+ import static org .sonar .python .quickfix .PythonTextEdit .replaceRange ;
34
34
35
35
@ Rule (key = "S5799" )
36
36
public class ImplicitStringConcatenationCheck extends PythonSubscriptionCheck {
@@ -67,12 +67,12 @@ private static void checkStringLiteral(StringLiteral stringLiteral, Subscription
67
67
continue ;
68
68
}
69
69
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 );
71
71
// Only raise 1 issue per string literal
72
72
return ;
73
73
}
74
74
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 );
76
76
return ;
77
77
}
78
78
}
@@ -94,26 +94,31 @@ private static boolean haveSameQuotes(StringElement first, StringElement second)
94
94
first .value ().charAt (first .value ().length () - 1 ) == second .value ().charAt (second .value ().length () - 1 );
95
95
}
96
96
97
- private static boolean isInFunctionOrArrayOrTupleOrExpression (StringElement token ) {
97
+ private static boolean isInFunctionOrArrayOrTupleOrExpressionOrSet (StringElement token ) {
98
98
Tree t = token ;
99
99
while (t .parent ().is (Tree .Kind .STRING_LITERAL )) {
100
100
t = t .parent ();
101
101
}
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 );
103
106
}
104
107
105
- private static void createQuickFix (PreciseIssue issueRaised , StringElement stringElement ) {
108
+ private static void createQuickFix (PreciseIssue issueRaised , StringElement start , StringElement end ) {
106
109
IssueWithQuickFix issue = (IssueWithQuickFix ) issueRaised ;
110
+ String textStart = start .value ();
111
+ String textEnd = end .value ();
107
112
108
- if (isInFunctionOrArrayOrTupleOrExpression ( stringElement )) {
113
+ if (isInFunctionOrArrayOrTupleOrExpressionOrSet ( start )) {
109
114
PythonQuickFix quickFix = PythonQuickFix .newQuickFix ("Add the comma between string or byte tokens." )
110
- .addTextEdit (insertAfter ( stringElement , "," ))
115
+ .addTextEdit (replaceRange ( start , end , textStart + ", " + textEnd ))
111
116
.build ();
112
117
issue .addQuickFix (quickFix );
113
118
}
114
119
115
120
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 ))
117
122
.build ();
118
123
issue .addQuickFix (quickFix );
119
124
}
0 commit comments