为此,必须使用 IntelliJ Idea 和子类 JavaDocumentationProvider 编写插件。
以防万一有人感兴趣:
in build.gradle:
intellij {
plugins 'java'
plugins 'android'
version '191.8026.42'
type 'IC'
}
runIde {
ideDirectory 'C:/Program Files/Android/Android Studio'
}
在 plugin.xml 中:
<extensions defaultExtensionNs="com.intellij">
<lang.documentationProvider order="first" language="JAVA" implementationClass="smpxg.TranslationDocumentationProvider" />
</extensions>
其余的 - 默认情况下。
TranslationDocumentationProvider.java :(虽然 isTextEnum 和 getTextForEnum 方法特定于我的案例)
public class TranslationDocumentationProvider extends JavaDocumentationProvider
{
@Override
public String getQuickNavigateInfo(PsiElement element, PsiElement originalElement)
{
if (isTextEnum(element))
{
return getTextForEnum(element);
}
else
{
return super.getQuickNavigateInfo(element, originalElement);
}
}
String getTextForEnum(PsiElement element)
{
final PsiClass base = JavaPsiFacade.getInstance(element.getProject()).findClass("smpxg.langloc.RstEng", element.getResolveScope());
if (base != null)
{
String const_name = element.getText();
String src = base.getText();
int ind = src.indexOf("ELT." + const_name);
int len = ("ELT." + const_name).length();
if (ind != -1)
{
int ind1 = src.indexOf('\n', ind + len);
String s = "";
if (ind1 == -1)
s = src.substring(ind + len);
else
s = src.substring(ind + len, ind1);
if (s.length() > 0)
{
int from = 0;
for (int i = 0; i < s.length(); i++)
{
if (Character.isLetterOrDigit(s.charAt(i)))
break;
from++;
}
int to = s.length() - 1;
for (int i = s.length() - 1; i >= 0; i--)
{
if (Character.isLetterOrDigit(s.charAt(i)))
break;
to--;
}
if (from < to)
{
s = s.substring(from, to + 1);
if (s.length() > 150)
s = s.substring(0, 150) + "...";
return "<b>Eng:</b> " + s;
}
}
}
}
return element.getText();
}
boolean isTextEnum(PsiElement element)
{
if (element != null)
{
ASTNode ast_node = element.getNode();
if (ast_node == null)
{
return false;
}
String element_type = ast_node.getElementType().toString();
if (element_type.equals("ENUM_CONSTANT"))
{
PsiEnumConstantImpl en = null;
try
{
en = (PsiEnumConstantImpl) element;
}
catch (Throwable e)
{
}
if (en != null)
{
PsiClass enum_class = en.getContainingClass();
if (enum_class != null)
{
String class_name = enum_class.getQualifiedName();
if (class_name != null && class_name.equals("smpxg.langloc.ELT"))
return true;
}
}
}
}
return false;
}
}