Skip to content

Commit

Permalink
Avoid inserting extraneous quote in Literal and TypedDict complet…
Browse files Browse the repository at this point in the history
…ions (#39)
  • Loading branch information
InSyncWithFoo committed May 24, 2024
1 parent 2834e69 commit fef3b4e
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ import org.eclipse.lsp4j.InsertTextFormat
private const val CARET_POSITION = "\$0"


private val String.isSingleQuoted: Boolean
get() = this.startsWith("'") && this.endsWith("'")


private val String.isDoubleQuoted: Boolean
get() = this.startsWith("\"") && this.endsWith("\"")


private val CompletionItem.isCallable: Boolean
get() = kind in listOf(
CompletionItemKind.Method,
Expand All @@ -29,6 +37,18 @@ private val CompletionItem.isAutoImportCompletion: Boolean
}


private val CompletionItem.isQuoted: Boolean
get() = label.isSingleQuoted || label.isDoubleQuoted


private val CompletionItem.quote: Char
get() = label[0]


private val CompletionParameters.nextCharacter: Char
get() = editor.document.charsSequence[offset]


private fun CompletionItem.completeWithParentheses() {
insertText = "$label($CARET_POSITION)"
insertTextFormat = InsertTextFormat.Snippet
Expand All @@ -41,6 +61,11 @@ private fun CompletionItem.useSourceAsDetailIfPossible() {
}


private fun CompletionItem.removeTrailingQuote() {
insertText = label.dropLast(1)
}


@Suppress("UnstableApiUsage")
internal class CompletionSupport(project: Project) : LspCompletionSupport() {

Expand All @@ -55,6 +80,10 @@ internal class CompletionSupport(project: Project) : LspCompletionSupport() {
item.useSourceAsDetailIfPossible()
}

if (item.isQuoted && parameters.nextCharacter == item.quote) {
item.removeTrailingQuote()
}

return super.createLookupElement(parameters, item)
}

Expand Down

0 comments on commit fef3b4e

Please sign in to comment.