Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct fix with string concat #946

Merged
merged 14 commits into from
Jul 2, 2021
Merged

Conversation

kentr0w
Copy link
Collaborator

@kentr0w kentr0w commented Jun 30, 2021

What's done:

### What's done:
Fixed bug
Added comment and tests
wrongStringTemplate.node.treeParent.replaceChild(wrongStringTemplate.node, KotlinParser().createNode("$firstPart\" +\n\"$secondPart"))
val correctNode =
try {
KotlinParser().createNode("$firstPart\" +\n\"$secondPart")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to just not put any pluses in ${} block, no?
Need to check if we are in ${} write now or not. If yes - add a new line

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just afraid that simple parsing of code can be inaccurate, but not sure, may be other reviewers will be ok

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to suggest the same. We can find the current node by offset, and if it is text - add concatenation, else - simply add a line break

@codecov
Copy link

codecov bot commented Jun 30, 2021

Codecov Report

Merging #946 (37ab7d5) into master (14c6f8a) will increase coverage by 0.08%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #946      +/-   ##
============================================
+ Coverage     83.85%   83.93%   +0.08%     
- Complexity     2380     2392      +12     
============================================
  Files           101      101              
  Lines          6025     6050      +25     
  Branches       1777     1785       +8     
============================================
+ Hits           5052     5078      +26     
  Misses          264      264              
+ Partials        709      708       -1     
Flag Coverage Δ
unittests 83.93% <100.00%> (+0.08%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...g/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt 92.36% <100.00%> (+1.29%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 14c6f8a...37ab7d5. Read the comment docs.

### What's done:
Fixed logic to resolve bug
### What's done:
Fix to build
### What's done:
Fix to build
### What's done:
Updated
@@ -457,7 +480,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
val hasNewLineBefore: Boolean,
val indexLastSpace: Int = 0) : LongLineFixableCases()

class StringTemplate(val node: ASTNode, val delimiterIndex: Int) : LongLineFixableCases()
class StringTemplate(val node: ASTNode, val delimiterIndex: Int, val multiLineOffset: Int) : LongLineFixableCases()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe, make multiLineOffset Int? to better indicate, that string is single line if it is null instead of 0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it will be better when we check if multiLineOffset == 0, but when we calculate this variable, it will be worse

### What's done:
Fixed after review
@@ -293,6 +294,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
val incorrectText = wrongStringTemplate.node.text
val firstPart = incorrectText.substring(0, wrongStringTemplate.delimiterIndex)
val secondPart = incorrectText.substring(wrongStringTemplate.delimiterIndex, incorrectText.length)
// wrongStringTemplate.multiLineOffset equals zero if string in one line
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not what I meant :) Why do you check if split is at white space only for single-line string?

### What's done:
Fixed after review
private fun fixStringTemplate(wrongStringTemplate: LongLineFixableCases.StringTemplate) {
val incorrectText = wrongStringTemplate.node.text
val firstPart = incorrectText.substring(0, wrongStringTemplate.delimiterIndex)
val secondPart = incorrectText.substring(wrongStringTemplate.delimiterIndex, incorrectText.length)
wrongStringTemplate.node.treeParent.replaceChild(wrongStringTemplate.node, KotlinParser().createNode("$firstPart\" +\n\"$secondPart"))
// wrongStringTemplate.multiLineOffset equals zero if string in one line
// if wrongStringTemplate.multiLineOffset isn't equal to 0, it means that string is multiline, which means that split should be without quote
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With or without? If we are inside a multiline string, we should split it with quote and concatenation, shouldn't we? Also, for multiline strings we are not checking any methods like trimIndent():

val string = """
    first line
    veryy looooooooooooooooong second line
""".trimIndent()

how will we split it?

Copy link
Member

@orchestr7 orchestr7 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix the remarks and merge

### What's done:
Changed logic
### What's done:
Fixed to pass all tests
### What's done:
Fixed according to our code style
### What's done:
Fixed according to our code style
### What's done:
Fixed according to our code style
@kentr0w kentr0w merged commit ab8d603 into master Jul 2, 2021
@kentr0w kentr0w deleted the bugfix/long-line-string-concat branch July 2, 2021 12:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

KotlinParseException: Your text is not valid
3 participants