Translation key detection: Allow methods directly returning the nls key
293060
diff --git a/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/nls/PsiTranslationPatterns.kt b/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/nls/PsiTranslationPatterns.kt
index 434f46e..d8dba50 100644
--- a/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/nls/PsiTranslationPatterns.kt
+++ b/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/nls/PsiTranslationPatterns.kt
@@ -47,15 +47,35 @@
val JAVA_NLS_KEY_PATTERN = or(javaNlsKeyArgumentPattern(), javaTextsGetPattern() /* legacy case */)
private fun getTranslationKeyFromJava(element: PsiElement?): String? {
+ val key = resolveExpressionToString(element) {
+ JAVA_NLS_KEY_PATTERN.accepts(it)
+ }
+ if (key != null) {
+ return key
+ }
+
+ // a method call directly returning the nls key
+ return (element as? PsiCall)
+ ?.takeIf { JAVA_NLS_KEY_PATTERN.accepts(it) }
+ ?.resolveMethod()
+ ?.body
+ ?.children
+ ?.mapNotNull { it as? PsiReturnStatement }
+ ?.map { it.returnValue }
+ ?.mapNotNull { resolveExpressionToString(it) }
+ ?.firstOrNull()
+ }
+
+ private fun resolveExpressionToString(element: PsiElement?, locationFilter: ((Any?) -> Boolean)? = null): String? {
if (element is PsiLiteralExpressionImpl) {
return asStringLiteral(element)
- .takeIf { JAVA_NLS_KEY_PATTERN.accepts(it) }
+ .takeIf { locationFilter == null || locationFilter(it) }
?.value as? String
}
// reference to variable or constant
val variable = (element as? PsiReference)
- ?.takeIf { JAVA_NLS_KEY_PATTERN.accepts(it) }
+ ?.takeIf { locationFilter == null || locationFilter(it) }
?.resolve() as? PsiVariable ?: return null
val constant = variable.computeConstantValue() as? String
if (constant != null) {
diff --git a/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/nls/folding/NlsFoldingBuilderForJava.kt b/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/nls/folding/NlsFoldingBuilderForJava.kt
index a904164..cba156e 100644
--- a/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/nls/folding/NlsFoldingBuilderForJava.kt
+++ b/org.eclipse.scout.sdk.s2i/src/main/kotlin/org/eclipse/scout/sdk/s2i/nls/folding/NlsFoldingBuilderForJava.kt
@@ -31,6 +31,11 @@
visitElement(expression)
}
+ override fun visitMethodCallExpression(expression: PsiMethodCallExpression) {
+ super.visitMethodCallExpression(expression)
+ visitElement(expression)
+ }
+
private fun visitElement(expression: PsiExpression) {
val translationKey = expression.translationSpec()?.resolveTranslationKey() ?: return
createFoldingDescriptor(translationKey, expression, stack)?.let { folds.add(it) }