Skip to content

Commit

Permalink
bugfix: skip empty lines when calculating indent for auto import
Browse files Browse the repository at this point in the history
  • Loading branch information
kasiaMarek committed Aug 30, 2023
1 parent f371c35 commit 7b59a3a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package scala.meta.internal.pc

import scala.annotation.tailrec

/**
* A position to insert new imports
*
Expand All @@ -19,18 +21,25 @@ case class AutoImportPosition(
}

object AutoImportPosition {
private val endOfLineCharacters = Set('\r', '\n')

// Infers the indentation at the completion position by counting the number of leading
// spaces in the line.
// For example:
// class Main {
// def foo<COMPLETE> // inferred indent is 2 spaces.
// }
@tailrec
def inferIndent(lineStart: Int, text: String): Int = {
var i = 0
while (lineStart + i < text.length && text.charAt(lineStart + i) == ' ') {
i += 1
}
i

val pos = lineStart + i
if (pos < text.length() && endOfLineCharacters(text.charAt(pos))) {
// skip any empty lines
inferIndent(pos + 1, text)
} else i
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,32 @@ class ImportMissingSymbolLspSuite
expectNoDiagnostics = false,
)

check(
"i5567",
"""package p {
|
| import scala.collection.mutable
|
| class C {
| def f = mutable.Map.empty[Int, Int]
| val p = <<Instant.now>>
| }
|}
|""".stripMargin,
s"""|${ImportMissingSymbol.title("Instant", "java.time")}
|${CreateNewSymbol.title("Instant")}
|""".stripMargin,
"""|package p {
|
| import scala.collection.mutable
| import java.time.Instant
|
| class C {
| def f = mutable.Map.empty[Int, Int]
| val p = Instant.now
| }
|}
|""".stripMargin,
)

}

0 comments on commit 7b59a3a

Please sign in to comment.