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

Fix multi-line range formatting when on first line #1245

Merged
merged 1 commit into from
Jan 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ final class MultilineStringFormattingProvider(
line: Int,
lines: Array[String],
defaultIndent: String
): TextEdit = {
): Option[TextEdit] = {
val zeroPos = new Position(line, 0)
val lineText = lines(line)
val firstChar = lineText.trim.headOption
Expand All @@ -159,14 +159,24 @@ final class MultilineStringFormattingProvider(
case Some('|') =>
val secondPipeIndex = lineText.indexOf('|', firstPipeIndex + 1)
val secondPipePos = new Position(line, secondPipeIndex)
new TextEdit(new Range(zeroPos, secondPipePos), defaultIndent)
val textEdit =
new TextEdit(new Range(zeroPos, secondPipePos), defaultIndent)
Some(textEdit)
case _ =>
val pipePos = new Position(line, firstPipeIndex)
new TextEdit(new Range(zeroPos, pipePos), defaultIndent)
val textEdit =
new TextEdit(new Range(zeroPos, pipePos), defaultIndent)
Some(textEdit)
}
case _ =>
val newText = defaultIndent + "|"
new TextEdit(new Range(zeroPos, zeroPos), newText)
val isFirstLineOfMultiLine = lineText.trim.contains("\"\"\"|")
Copy link
Member

Choose a reason for hiding this comment

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

What happens if the line doesn’t have a pipe? It’s optional

Copy link
Member Author

Choose a reason for hiding this comment

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

If there is no pipe on the line it gets filtered out beforehand and the line never enters the formatPipeLine method. I suppose I should have added a test for this as well. I did just check it locally and that indeed is how it works.

Copy link
Member Author

Choose a reason for hiding this comment

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

I just added another test for this #1249

if (isFirstLineOfMultiLine) {
None
} else {
val newText = defaultIndent + "|"
val textEdit = new TextEdit(new Range(zeroPos, zeroPos), newText)
Some(textEdit)
}
}
}

Expand All @@ -190,7 +200,7 @@ final class MultilineStringFormattingProvider(
range.getStart().getLine().to(range.getEnd().getLine())

linesToFormat
.map(line => formatPipeLine(line, splitLines, defaultIndent))
.flatMap(line => formatPipeLine(line, splitLines, defaultIndent))
.toList
}
}
Expand Down
17 changes: 17 additions & 0 deletions tests/unit/src/test/scala/tests/RangeFormattingSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ object RangeFormattingSuite extends BaseLspSuite("rangeFormatting") {
|}""".stripMargin
)

check(
"paste-on-fist-line",
s"""
|object Main {
| val str = '''| hi @@
| |
| '''.stripMargin
|}""".stripMargin,
s"""|first line""".stripMargin,
s"""
|object Main {
| val str = '''| hi first line
| |
| '''.stripMargin
|}""".stripMargin
)

check(
"without-stripmargin",
s"""
Expand Down