Skip to content

Commit ec93898

Browse files
Prepend kotlin.operator. to the operator symbol for later lookup in the tooltip database
1 parent 680cf4a commit ec93898

2 files changed

Lines changed: 46 additions & 22 deletions

File tree

app/src/main/java/com/itsaky/androidide/actions/file/ShowTooltipAction.kt

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.itsaky.androidide.R
2323
import com.itsaky.androidide.actions.ActionData
2424
import com.itsaky.androidide.actions.ActionItem
2525
import com.itsaky.androidide.actions.BaseEditorAction
26+
import com.itsaky.androidide.editor.utils.isOperatorToken
2627
import com.itsaky.androidide.editor.utils.isXmlAttribute
2728
import com.itsaky.androidide.idetooltips.TooltipCategory
2829
import com.itsaky.androidide.idetooltips.TooltipManager
@@ -58,29 +59,19 @@ class ShowTooltipAction(private val context: Context, override val order: Int) :
5859
val anchorView = target.getAnchorView() ?: return false
5960
val editor = getEditor(data)
6061

61-
val category: String
62-
val tag: String
63-
64-
if (editor != null) {
65-
val selectedText = target.getSelectedText()
66-
category = when (editor.file?.extension) {
67-
"java" -> TooltipCategory.CATEGORY_JAVA
68-
"kt" -> TooltipCategory.CATEGORY_KOTLIN
69-
"xml" -> TooltipCategory.CATEGORY_XML
70-
else -> TooltipCategory.CATEGORY_IDE
71-
}
72-
73-
val useEditorTag = editor.tag != null
74-
val textToUse = selectedText ?: ""
75-
tag = when {
76-
useEditorTag -> editor.tag.toString()
77-
category == TooltipCategory.CATEGORY_XML && editor.isXmlAttribute() -> textToUse.substringAfterLast(":")
78-
else -> textToUse
62+
val categoryAndTag =
63+
if (editor != null) {
64+
val category = tooltipCategoryForExtension(editor.file?.extension)
65+
resolveTooltipTag(
66+
category = category,
67+
selectedText = target.getSelectedText(),
68+
editorTag = editor.tag?.toString(),
69+
isXmlAttribute = category == TooltipCategory.CATEGORY_XML && editor.isXmlAttribute(),
70+
).let { tag -> category to tag }
71+
} else {
72+
TooltipCategory.CATEGORY_IDE to TooltipTag.DIALOG_FIND_IN_PROJECT
7973
}
80-
} else {
81-
category = TooltipCategory.CATEGORY_IDE
82-
tag = TooltipTag.DIALOG_FIND_IN_PROJECT
83-
}
74+
val (category, tag) = categoryAndTag
8475

8576
if (tag.isEmpty()) return false
8677

@@ -95,4 +86,27 @@ class ShowTooltipAction(private val context: Context, override val order: Int) :
9586
}
9687

9788
override fun retrieveTooltipTag(isAlternateContext: Boolean) = TooltipTag.EDITOR_TOOLBAR_HELP
89+
}
90+
91+
internal fun tooltipCategoryForExtension(extension: String?): String =
92+
when (extension) {
93+
"java" -> TooltipCategory.CATEGORY_JAVA
94+
"kt" -> TooltipCategory.CATEGORY_KOTLIN
95+
"xml" -> TooltipCategory.CATEGORY_XML
96+
else -> TooltipCategory.CATEGORY_IDE
97+
}
98+
99+
internal fun resolveTooltipTag(
100+
category: String,
101+
selectedText: String?,
102+
editorTag: String?,
103+
isXmlAttribute: Boolean,
104+
): String {
105+
val textToUse = selectedText ?: ""
106+
return when {
107+
!editorTag.isNullOrEmpty() -> editorTag
108+
category == TooltipCategory.CATEGORY_XML && isXmlAttribute -> textToUse.substringAfterLast(":")
109+
category == TooltipCategory.CATEGORY_KOTLIN && isOperatorToken(textToUse) -> "kotlin.operator.$textToUse"
110+
else -> textToUse
111+
}
98112
}

editor/src/main/java/com/itsaky/androidide/editor/utils/OperatorSelection.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,16 @@ private val OPERATORS: List<String> =
8383
"}",
8484
)
8585

86+
private val OPERATOR_SET: Set<String> = OPERATORS.toSet()
87+
88+
/**
89+
* Returns true when [text] is exactly one supported Java/Kotlin operator token.
90+
*/
91+
fun isOperatorToken(text: CharSequence): Boolean {
92+
if (text.isEmpty()) return false
93+
return OPERATOR_SET.contains(text.toString())
94+
}
95+
8696
/**
8797
* Returns the column range of the operator at the given position, if any.
8898
* Columns are 0-based; endColumn is exclusive (one past the last character).

0 commit comments

Comments
 (0)