Skip to content

Commit

Permalink
Backport "Completions should prepend, not replace as it is for Scala …
Browse files Browse the repository at this point in the history
…2" to LTS (#20756)

Backports #18803 to the LTS branch.

PR submitted by the release tooling.
[skip ci]
  • Loading branch information
WojciechMazur authored Jun 23, 2024
2 parents abae804 + baae0c9 commit c09877d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ case class CompletionPos(
):

def sourcePos: SourcePosition = cursorPos.withSpan(Spans.Span(start, end))
def stripSuffixEditRange: l.Range = new l.Range(cursorPos.offsetToPos(start), cursorPos.offsetToPos(end))
def toEditRange: l.Range = cursorPos.withStart(start).withEnd(cursorPos.point).toLsp

def toEditRange: l.Range =
new l.Range(cursorPos.offsetToPos(start), cursorPos.offsetToPos(end))
end toEditRange
end CompletionPos

object CompletionPos:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,7 @@ class CompletionProvider(
indexedContext: IndexedContext
)(using ctx: Context): CompletionItem =
val printer =
ShortenedTypePrinter(search, IncludeDefaultParam.ResolveLater)(using
indexedContext
)
val editRange = completionPos.toEditRange
ShortenedTypePrinter(search, IncludeDefaultParam.ResolveLater)(using indexedContext)

// For overloaded signatures we get multiple symbols, so we need
// to recalculate the description
Expand All @@ -165,24 +162,22 @@ class CompletionProvider(
val ident = completion.insertText.getOrElse(completion.label)

def mkItem(
insertText: String,
newText: String,
additionalEdits: List[TextEdit] = Nil,
range: Option[LspRange] = None
): CompletionItem =
val nameEdit = new TextEdit(
range.getOrElse(editRange),
insertText
)
val oldText = params.text.substring(completionPos.start, completionPos.end)
val editRange = if newText.startsWith(oldText) then completionPos.stripSuffixEditRange
else completionPos.toEditRange

val textEdit = new TextEdit(range.getOrElse(editRange), newText)

val item = new CompletionItem(label)
item.setSortText(f"${idx}%05d")
item.setDetail(description)
item.setFilterText(
completion.filterText.getOrElse(completion.label)
)
item.setTextEdit(nameEdit)
item.setAdditionalTextEdits(
(completion.additionalEdits ++ additionalEdits).asJava
)
item.setFilterText(completion.filterText.getOrElse(completion.label))
item.setTextEdit(textEdit)
item.setAdditionalTextEdits((completion.additionalEdits ++ additionalEdits).asJava)
completion.insertMode.foreach(item.setInsertTextMode)

completion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1500,3 +1500,47 @@ class CompletionSuite extends BaseCompletionSuite:
|""".stripMargin,
)

@Test def `prepend-instead-of-replace` =
checkEdit(
"""|object O:
| printl@@println()
|""".stripMargin,
"""|object O:
| printlnprintln()
|""".stripMargin,
assertSingleItem = false
)

@Test def `prepend-instead-of-replace-duplicate-word` =
checkEdit(
"""|object O:
| println@@println()
|""".stripMargin,
"""|object O:
| printlnprintln()
|""".stripMargin,
assertSingleItem = false
)

@Test def `replace-when-inside` =
checkEdit(
"""|object O:
| print@@ln()
|""".stripMargin,
"""|object O:
| println()
|""".stripMargin,
assertSingleItem = false
)

@Test def `replace-exact-same` =
checkEdit(
"""|object O:
| println@@()
|""".stripMargin,
"""|object O:
| println()
|""".stripMargin,
assertSingleItem = false
)

0 comments on commit c09877d

Please sign in to comment.