Skip to content

Commit

Permalink
Make all monkeypatches optional
Browse files Browse the repository at this point in the history
  • Loading branch information
InSyncWithFoo committed Jun 22, 2024
1 parent 72079fc commit 9efacf5
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 21 deletions.
10 changes: 6 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ For code changes, see [`CHANGELOG_CODE.md`][_-1].
* The list of recognized file extensions can now be configured
and includes `py` and `pyi` by default.
Previously, only `.py` files are recognized.
* PyCharm has a bug leading to extra quotes being added
when autocompleting `Literal` strings and `TypedDict` keys.
A monkeypatch has been added to mitigate the issues.
It can be disabled using an UI option.
* The diagnostic mode is now configurable.
Previously, no corresponding value is sent to the language server.
* The server can now be asked not to
Expand All @@ -24,10 +28,8 @@ For code changes, see [`CHANGELOG_CODE.md`][_-1].

### Changed

* PyCharm has a bug leading to extra quotes being added
when autocompleting `Literal` strings and `TypedDict` keys.
A monkeypatch has been added to mitigate the issues.
This patch might be reverted in the future, when the bug is fixed.
* The "Auto-import" completion item detail monkeypatch
introduced in the previous version can now be disabled.


## [0.5.0] - 2024-05-27
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG_CODE.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For user-facing changes, see [`CHANGELOG.md`][_-1].

* Project option "Targeted file extensions" is added. (d5c69dbb, 541c5630)
* Project option "Diagnostic mode" is added. (553c2ae2)
* Project option "Auto search paths" is added. (HEAD)
* Project option "Auto search paths" is added. (90b572b7)

### Changed

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ kover {

tasks {
runIde {
// From https://app.slack.com/client/T5P9YATH9/C5U8BM1MK
// From https://app.slack.com/client/T5P9YATH9/C5U8BM1MK/1715448677.615749
systemProperty("ide.experimental.ui", "true")
systemProperty("projectView.hide.dot.idea", "false")
systemProperty("terminal.new.ui", "false")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ internal infix fun ApplicationConfigurations.mergeWith(other: ProjectConfigurati
autocompleteParentheses = this.autocompleteParentheses,
diagnosticsSupport = this.diagnosticsSupport,
autoRestartServer = this.autoRestartServer,
monkeypatchAutoImportDetails = this.monkeypatchAutoImportDetails,
monkeypatchTrailingQuoteBug = this.monkeypatchTrailingQuoteBug,

projectExecutable = other.projectExecutable,
autoSuggestExecutable = other.autoSuggestExecutable,
Expand All @@ -52,6 +54,8 @@ internal data class AllConfigurations(
val autocompleteParentheses: Boolean,
val diagnosticsSupport: Boolean,
val autoRestartServer: Boolean,
val monkeypatchAutoImportDetails: Boolean,
val monkeypatchTrailingQuoteBug: Boolean,

val projectExecutable: @SystemDependent String?,
val autoSuggestExecutable: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import kotlin.io.path.isDirectory
internal enum class HintIcon {
Success, Info, Warning, Error;

// TODO: Re-add icons
override fun toString() = ""
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.insyncwithfoo.pyrightls.configuration.secondColumnPathInput
import com.insyncwithfoo.pyrightls.message
import com.intellij.openapi.ui.ComboBox
import com.intellij.openapi.ui.TextFieldWithBrowseButton
import com.intellij.openapi.util.text.HtmlChunk
import com.intellij.ui.SimpleListCellRenderer
import com.intellij.ui.components.JBCheckBox
import com.intellij.ui.dsl.builder.Cell
Expand Down Expand Up @@ -85,10 +86,21 @@ private fun Row.makeAutoRestartServerInput(block: Cell<JBCheckBox>.() -> Unit) =
checkBox(message("configurations.autoRestartServer.label")).apply(block)


private fun Row.makeMonkeypatchAutoImportDetailsInput(block: Cell<JBCheckBox>.() -> Unit) =
checkBox(message("configurations.monkeypatchAutoImportDetails.label")).apply(block)


private fun Row.makeMonkeypatchTrailingQuoteBugInput(block: Cell<JBCheckBox>.() -> Unit) = run {
val issueLink = HtmlChunk.link("https://youtrack.jetbrains.com/issue/IJPL-155741", " IJPL-155741")
val label = message("configurations.monkeypatchTrailingQuoteBug.label")
val comment = message("configurations.monkeypatchTrailingQuoteBug.comment", issueLink)

checkBox(label).comment(comment).apply(block)
}


@Suppress("DialogTitleCapitalization")
internal fun configurationPanel(state: Configurations) = panel {
// FIXME: The onInput() callbacks are too deeply nested.

row {
makeAlwaysUseGlobalInput { bindSelected(state::alwaysUseGlobal) }
}
Expand Down Expand Up @@ -133,9 +145,17 @@ internal fun configurationPanel(state: Configurations) = panel {
row {
makeAutoImportCompletionsInput { bindSelected(state::autoImportCompletions) }
}
indent {
row {
makeMonkeypatchAutoImportDetailsInput { bindSelected(state::monkeypatchAutoImportDetails) }
}
}
row {
makeAutocompleteParenthesesInput { bindSelected(state::autocompleteParentheses) }
}
row {
makeMonkeypatchTrailingQuoteBugInput { bindSelected(state::monkeypatchTrailingQuoteBug) }
}
}

row {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ internal class Configurations : BaseState() {
var autocompleteParentheses by property(false)
var diagnosticsSupport by property(true)
var autoRestartServer by property(false)
var monkeypatchAutoImportDetails by property(true)
var monkeypatchTrailingQuoteBug by property(true)
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ private fun Row.makeAutoSearchPathsInput(block: Cell<JBCheckBox>.() -> Unit) =


internal fun Configurable.configurationPanel(state: Configurations) = panel {
// FIXME: The onInput() callbacks are too deeply nested.

row {
makeAutoSuggestExecutableInput { bindSelected(state::autoSuggestExecutable) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.intellij.platform.lsp.api.customization.LspCompletionSupport
import org.eclipse.lsp4j.CompletionItem
import org.eclipse.lsp4j.CompletionItemKind
import org.eclipse.lsp4j.InsertTextFormat
import org.eclipse.lsp4j.TextEdit


private const val CARET_POSITION = "\$0"
Expand Down Expand Up @@ -51,6 +52,11 @@ private val CompletionParameters.followingCharacters: CharSequence
get() = editor.document.charsSequence.slice(offset..offset + 2)


private fun CompletionParameters.itemMightTriggerTrailingQuoteBug(item: CompletionItem): Boolean {
return item.isQuoted && followingCharacters.startsWith(item.quoteSequence!!)
}


private fun CompletionItem.completeWithParentheses() {
insertText = "$label($CARET_POSITION)"
insertTextFormat = InsertTextFormat.Snippet
Expand All @@ -64,7 +70,13 @@ private fun CompletionItem.useSourceAsDetailIfPossible() {


private fun CompletionItem.removeTrailingQuoteSequence() {
insertText = label.dropLast(quoteSequence!!.length)
val trailingQuoteSequence = quoteSequence!!

insertText = label.removeSuffix(trailingQuoteSequence)

(textEdit.get() as? TextEdit)?.apply {
newText = newText.removeSuffix(trailingQuoteSequence)
}
}


Expand All @@ -78,11 +90,11 @@ internal class CompletionSupport(project: Project) : LspCompletionSupport() {
item.completeWithParentheses()
}

if (item.isAutoImportCompletion) {
if (item.isAutoImportCompletion && configurations.monkeypatchAutoImportDetails) {
item.useSourceAsDetailIfPossible()
}

if (item.isQuoted && parameters.followingCharacters.startsWith(item.quoteSequence!!)) {
if (parameters.itemMightTriggerTrailingQuoteBug(item) && configurations.monkeypatchTrailingQuoteBug) {
item.removeTrailingQuoteSequence()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ private val Project.osDependentInterpreterPath: String?
private fun Project.createPyrightLSSettingsObject() = Settings().apply {
val configurations = pyrightLSConfigurations

python.apply {
python {
pythonPath = osDependentInterpreterPath

analysis.apply {
analysis {
logLevel = configurations.logLevel.label
autoImportCompletions = configurations.autoImportCompletions
diagnosticMode = configurations.diagnosticMode.value
autoSearchPaths = configurations.autoSearchPaths
}
}

pyright.apply {
pyright {
disableTaggedHints = !configurations.taggedHints
}
}
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/com/insyncwithfoo/pyrightls/server/Settings.kt
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
package com.insyncwithfoo.pyrightls.server


private interface Builder


internal operator fun <T : Builder> T.invoke(block: T.() -> Unit) {
this.apply(block)
}


internal data class Analysis(
var logLevel: String? = null,
var autoImportCompletions: Boolean? = null,
var diagnosticMode: String? = null,
var autoSearchPaths: Boolean = true
)
) : Builder


internal data class Python(
var pythonPath: String? = null,
val analysis: Analysis = Analysis()
)
) : Builder


internal data class Pyright(
internal class Pyright(
var disableTaggedHints: Boolean? = null
)
) : Builder


internal data class Settings(
Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/messages/pyrightls.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ configurations.logLevel.warning = Warning
configurations.logLevel.information = Information
configurations.logLevel.trace = Trace

configurations.monkeypatchAutoImportDetails.label = Display source for auto-import completions

configurations.monkeypatchTrailingQuoteBug.label = Avoid inserting extraneous trailing quotes
configurations.monkeypatchTrailingQuoteBug.comment = YouTrack issue: {0}

configurations.projectExecutable.label = Project executable:
configurations.autoSuggestExecutable.label = Suggest executable on project open

Expand Down

0 comments on commit 9efacf5

Please sign in to comment.