Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,85 +30,95 @@ import com.itsaky.androidide.idetooltips.TooltipCategory
import com.itsaky.androidide.idetooltips.TooltipManager
import com.itsaky.androidide.idetooltips.TooltipTag

class ShowTooltipAction(private val context: Context, override val order: Int) :
BaseEditorAction() {

companion object {
const val ID = "ide.editor.code.text.show_tooltip"
}

override val id: String = ID
override var location: ActionItem.Location = ActionItem.Location.EDITOR_TEXT_ACTIONS

init {
label = context.getString(R.string.title_show_tooltip)
val drawable = ContextCompat.getDrawable(context, R.drawable.ic_action_help_outlined)
icon = drawable?.let { tintDrawable(context, it) }
}

override fun prepare(data: ActionData) {
super.prepare(data)
if (!visible) return

val target = getTextTarget(data)
visible = target != null
enabled = visible
}

override suspend fun execAction(data: ActionData): Boolean {
val target = getTextTarget(data) ?: return false
val anchorView = target.getAnchorView() ?: return false
val editor = getEditor(data)

val categoryAndTag =
if (editor != null) {
val category = tooltipCategoryForExtension(editor.file?.extension)
resolveTooltipTag(
category = category,
selectedText = target.getSelectedText(),
editorTag = editor.tag?.toString(),
isXmlAttribute = category == TooltipCategory.CATEGORY_XML && editor.isXmlAttribute(),
).let { tag -> category to tag }
} else {
TooltipCategory.CATEGORY_IDE to TooltipTag.DIALOG_FIND_IN_PROJECT
}
val (category, tag) = categoryAndTag

if (tag.isEmpty()) return false

TooltipManager.showTooltip(
context = anchorView.context,
anchorView = anchorView,
category = category,
tag = tag,
)

return true
}

override fun retrieveTooltipTag(isAlternateContext: Boolean) = TooltipTag.EDITOR_TOOLBAR_HELP
/**
* Editor text action that shows the tooltip for the current selection.
*
* EDITOR_TEXT_ACTIONS is never cleared (see EditorActivityActions.clear), so the static
* ActionsRegistry outlives every editor activity and an action holding a Context leaks it.
* [execAction] uses the anchor view's context instead.
*
* @param context initializes [label] and [icon] only; deliberately not retained.
*/
class ShowTooltipAction(
context: Context,
override val order: Int,
) : BaseEditorAction() {
companion object {
const val ID = "ide.editor.code.text.show_tooltip"
}

override val id: String = ID
override var location: ActionItem.Location = ActionItem.Location.EDITOR_TEXT_ACTIONS

init {
label = context.getString(R.string.title_show_tooltip)
val drawable = ContextCompat.getDrawable(context, R.drawable.ic_action_help_outlined)
icon = drawable?.let { tintDrawable(context, it) }
}

override fun prepare(data: ActionData) {
super.prepare(data)
if (!visible) return

val target = getTextTarget(data)
visible = target != null
enabled = visible
}

override suspend fun execAction(data: ActionData): Boolean {
val target = getTextTarget(data) ?: return false
val anchorView = target.getAnchorView() ?: return false
val editor = getEditor(data)

val categoryAndTag =
if (editor != null) {
val category = tooltipCategoryForExtension(editor.file?.extension)
resolveTooltipTag(
category = category,
selectedText = target.getSelectedText(),
editorTag = editor.tag?.toString(),
isXmlAttribute = category == TooltipCategory.CATEGORY_XML && editor.isXmlAttribute(),
).let { tag -> category to tag }
} else {
TooltipCategory.CATEGORY_IDE to TooltipTag.DIALOG_FIND_IN_PROJECT
}
val (category, tag) = categoryAndTag

if (tag.isEmpty()) return false

TooltipManager.showTooltip(
context = anchorView.context,
anchorView = anchorView,
category = category,
tag = tag,
)

return true
}

override fun retrieveTooltipTag(isAlternateContext: Boolean) = TooltipTag.EDITOR_TOOLBAR_HELP
}

internal fun tooltipCategoryForExtension(extension: String?): String =
when (extension) {
"java" -> TooltipCategory.CATEGORY_JAVA
"kt" -> TooltipCategory.CATEGORY_KOTLIN
"xml" -> TooltipCategory.CATEGORY_XML
else -> TooltipCategory.CATEGORY_IDE
}
when (extension) {
"java" -> TooltipCategory.CATEGORY_JAVA
"kt" -> TooltipCategory.CATEGORY_KOTLIN
"xml" -> TooltipCategory.CATEGORY_XML
else -> TooltipCategory.CATEGORY_IDE
}

internal fun resolveTooltipTag(
category: String,
selectedText: String?,
editorTag: String?,
isXmlAttribute: Boolean,
category: String,
selectedText: String?,
editorTag: String?,
isXmlAttribute: Boolean,
): String {
val textToUse = selectedText ?: ""
return when {
!editorTag.isNullOrEmpty() -> editorTag
category == TooltipCategory.CATEGORY_XML && isXmlAttribute -> textToUse.substringAfterLast(":")
category == TooltipCategory.CATEGORY_KOTLIN && isKotlinOperatorToken(textToUse) -> "kotlin.operator.$textToUse"
category == TooltipCategory.CATEGORY_JAVA && isJavaOperatorToken(textToUse) -> "java.operator.$textToUse"
else -> textToUse
}
}
val textToUse = selectedText ?: ""
return when {
!editorTag.isNullOrEmpty() -> editorTag
category == TooltipCategory.CATEGORY_XML && isXmlAttribute -> textToUse.substringAfterLast(":")
category == TooltipCategory.CATEGORY_KOTLIN && isKotlinOperatorToken(textToUse) -> "kotlin.operator.$textToUse"
category == TooltipCategory.CATEGORY_JAVA && isJavaOperatorToken(textToUse) -> "java.operator.$textToUse"
else -> textToUse
}
}
Loading
Loading