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

Initial version of declarative hints #44

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ pluginGroup = space.whitememory.pythoninlayparams
pluginName = Python Inlay Params
pluginRepositoryUrl = https://github.com/WhiteMemory99/Intellij-Python-Inlay-Params

pluginVersion = 0.3.4
pluginVersion = 1.0.0

# See https://plugins.jetbrains.com/docs/intellij/build-number-ranges.html
# for insight into build numbers and IntelliJ Platform versions.
pluginSinceBuild = 223
pluginUntilBuild = 233.*
pluginSinceBuild = 231
pluginUntilBuild = 241.*

# IntelliJ Platform Properties -> https://plugins.jetbrains.com/docs/intellij/tools-gradle-intellij-plugin.html#configuration-intellij-extension
platformType = PC
platformVersion = 2022.3.3
platformVersion = 2023.1

# Plugin Dependencies -> https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
# Example: platformPlugins = com.intellij.java, com.jetbrains.php:203.4449.22
Expand All @@ -30,4 +30,4 @@ org.gradle.configuration-cache = true
org.gradle.caching = true

# Enable Gradle Kotlin DSL Lazy Property Assignment -> https://docs.gradle.org/current/userguide/kotlin_dsl.html#kotdsl:assignment
systemProp.org.gradle.unsafe.kotlin.assignment = true
systemProp.org.gradle.unsafe.kotlin.assignment = true
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package space.whitememory.pythoninlayparams

import com.intellij.codeInsight.hints.declarative.*
import com.intellij.codeInsight.hints.declarative.InlayHintsProvider
import com.intellij.openapi.editor.Editor
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
import com.intellij.psi.SmartPointerManager
import com.intellij.refactoring.suggested.endOffset
import com.jetbrains.python.psi.PyTargetExpression
import com.jetbrains.python.psi.types.TypeEvalContext
import space.whitememory.pythoninlayparams.types.hints.HintGenerator
import space.whitememory.pythoninlayparams.types.hints.HintResolver
import space.whitememory.pythoninlayparams.types.hints.InlayElement

class InlayHintsProvider : InlayHintsProvider {
companion object {
const val PROVIDER_ID = "python.implicit.types"
const val PROVIDER_NAME = "Implicit types"
}

override fun createCollector(file: PsiFile, editor: Editor): InlayHintsCollector {
return Collector()
}

private class Collector() : SharedBypassCollector {

override fun collectFromElement(element: PsiElement, sink: InlayTreeSink) {
if (!element.isValid || element.project.isDefault || element !is PyTargetExpression) return

val typeEvalContext = TypeEvalContext.codeCompletion(element.project, element.containingFile)

if (HintResolver.resolve(element, typeEvalContext)) return

val typeAnnotation = HintResolver.getExpressionAnnotationType(element, typeEvalContext)
val inlayHint = HintGenerator.generateTypeHintText(
element, typeAnnotation, typeEvalContext
)

if (inlayHint.isEmpty()) return

sink.addPresentation(
position = InlineInlayPosition(element.endOffset, true),
hasBackground = true,
builder = {
text(": ")

generateClickableInlayHint(this, inlayHint, createCollapsed = true)
}
)
}

private fun createRootInlayHint(builder: PresentationTreeBuilder, hintElement: InlayElement) {
if (hintElement.element == null) return

builder.text(
hintElement.element.text, actionData = when (hintElement.element.psiElement) {
null -> null
else -> {
InlayActionData(
PsiPointerInlayActionPayload(
SmartPointerManager.createPointer(
hintElement.element.psiElement
)
),
handlerId = PsiPointerInlayActionNavigationHandler.HANDLER_ID
)
}
}
)
}

private fun generateClickableInlayHint(
builder: PresentationTreeBuilder,
hintElement: InlayElement,
createCollapsed: Boolean = false
) {
if (createCollapsed && hintElement.isTooLong()) {
return builder.collapsibleList(
state = CollapseState.Collapsed,
expandedState = { toggleButton { generateClickableInlayHint(this, hintElement) } },
collapsedState = {
toggleButton {
createRootInlayHint(this, hintElement)
if (hintElement.isGenericType()) {
text("[...]")
} else {
text("${hintElement.inlayType.shortPrefix}[...]")
}
}
}
)
} else {
createRootInlayHint(builder, hintElement)
}

if (hintElement.isGenericType()) builder.text("[")

val childrenIterator = hintElement.children.iterator()

while (childrenIterator.hasNext()) {
generateClickableInlayHint(builder, childrenIterator.next())

if (childrenIterator.hasNext()) {
builder.text(hintElement.separatorType.separator)
}
}

if (hintElement.isGenericType()) builder.text("]")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ abstract class AbstractPythonInlayTypeHintsCollector(editor: Editor) :
return
}

val resolvedHintName = resolveInlayPresentation(hintName)
displayTypeHint(element, sink, resolvedHintName)
// val resolvedHintName = resolveInlayPresentation(hintName)
// displayTypeHint(element, sink, resolvedHintName)
}

private fun resolveInlayPresentation(
Expand Down
Loading
Loading