mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-18 09:34:34 +07:00
Move PsiFormatUtilBase to core-api and PsiFormatUtil to java-psi-api
This commit is contained in:
@@ -0,0 +1,265 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.psi.util;
|
||||
|
||||
import com.intellij.openapi.util.Comparing;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.search.GlobalSearchScope;
|
||||
import com.intellij.util.StringBuilderSpinAllocator;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class ClassUtil {
|
||||
private ClassUtil() {}
|
||||
|
||||
public static String extractPackageName(String className) {
|
||||
if (className != null) {
|
||||
int i = className.lastIndexOf('.');
|
||||
return i == -1 ? "" : className.substring(0, i);
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String extractClassName(@NotNull String fqName) {
|
||||
int i = fqName.lastIndexOf('.');
|
||||
return i == -1 ? fqName : fqName.substring(i + 1);
|
||||
}
|
||||
|
||||
public static String createNewClassQualifiedName(String qualifiedName, String className) {
|
||||
if (className == null){
|
||||
return null;
|
||||
}
|
||||
if (qualifiedName == null || qualifiedName.length() == 0){
|
||||
return className;
|
||||
}
|
||||
return qualifiedName + "." + extractClassName(className);
|
||||
}
|
||||
|
||||
public static PsiDirectory sourceRoot(PsiDirectory containingDirectory) {
|
||||
while (containingDirectory != null) {
|
||||
if (JavaDirectoryService.getInstance().isSourceRoot(containingDirectory)) {
|
||||
return containingDirectory;
|
||||
}
|
||||
containingDirectory = containingDirectory.getParentDirectory();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void formatClassName(@NotNull final PsiClass aClass, final StringBuilder buf) {
|
||||
final String qName = aClass.getQualifiedName();
|
||||
if (qName != null) {
|
||||
buf.append(qName);
|
||||
}
|
||||
else {
|
||||
final PsiClass parentClass = getContainerClass(aClass);
|
||||
if (parentClass != null) {
|
||||
formatClassName(parentClass, buf);
|
||||
buf.append("$");
|
||||
buf.append(getNonQualifiedClassIdx(aClass));
|
||||
final String name = aClass.getName();
|
||||
if (name != null) {
|
||||
buf.append(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiClass getContainerClass(final PsiClass aClass) {
|
||||
PsiElement parent = aClass.getContext();
|
||||
while (parent != null && !(parent instanceof PsiClass)) {
|
||||
parent = parent.getContext();
|
||||
}
|
||||
return (PsiClass)parent;
|
||||
}
|
||||
|
||||
public static int getNonQualifiedClassIdx(@NotNull final PsiClass psiClass) {
|
||||
final int[] result = {-1};
|
||||
final PsiClass containingClass = getContainerClass(psiClass);
|
||||
if (containingClass != null) {
|
||||
containingClass.accept(new JavaRecursiveElementVisitor() {
|
||||
private int myCurrentIdx = 0;
|
||||
|
||||
@Override public void visitElement(PsiElement element) {
|
||||
if (result[0] == -1) {
|
||||
super.visitElement(element);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void visitClass(PsiClass aClass) {
|
||||
super.visitClass(aClass);
|
||||
if (aClass.getQualifiedName() == null) {
|
||||
myCurrentIdx++;
|
||||
if (psiClass == aClass) {
|
||||
result[0] = myCurrentIdx;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
return result[0];
|
||||
}
|
||||
|
||||
public static PsiClass findNonQualifiedClassByIndex(final String indexName, @NotNull final PsiClass containingClass) {
|
||||
return findNonQualifiedClassByIndex(indexName, containingClass, false);
|
||||
}
|
||||
|
||||
public static PsiClass findNonQualifiedClassByIndex(final String indexName, @NotNull final PsiClass containingClass,
|
||||
final boolean jvmCompatible) {
|
||||
String prefix = getDigitPrefix(indexName);
|
||||
final int idx = prefix.length() > 0 ? Integer.parseInt(prefix) : -1;
|
||||
final String name = prefix.length() < indexName.length() ? indexName.substring(prefix.length()) : null;
|
||||
final PsiClass[] result = new PsiClass[1];
|
||||
containingClass.accept(new JavaRecursiveElementVisitor() {
|
||||
private int myCurrentIdx = 0;
|
||||
|
||||
@Override public void visitElement(PsiElement element) {
|
||||
if (result[0] == null) {
|
||||
super.visitElement(element);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void visitClass(PsiClass aClass) {
|
||||
if (!jvmCompatible) {
|
||||
super.visitClass(aClass);
|
||||
if (aClass.getQualifiedName() == null) {
|
||||
myCurrentIdx++;
|
||||
if (myCurrentIdx == idx && Comparing.strEqual(name, aClass.getName())) {
|
||||
result[0] = aClass;
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (aClass == containingClass) {
|
||||
super.visitClass(aClass);
|
||||
return;
|
||||
}
|
||||
if (Comparing.strEqual(name, aClass.getName())) {
|
||||
myCurrentIdx++;
|
||||
if (myCurrentIdx == idx || idx == -1) {
|
||||
result[0] = aClass;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override public void visitTypeParameter(final PsiTypeParameter classParameter) {
|
||||
if (!jvmCompatible) {
|
||||
super.visitTypeParameter(classParameter);
|
||||
}
|
||||
else {
|
||||
visitElement(classParameter);
|
||||
}
|
||||
}
|
||||
});
|
||||
return result[0];
|
||||
}
|
||||
|
||||
private static String getDigitPrefix(final String indexName) {
|
||||
final StringBuilder builder = StringBuilderSpinAllocator.alloc();
|
||||
try {
|
||||
for (int i = 0; i < indexName.length(); i++) {
|
||||
final char c = indexName.charAt(i);
|
||||
if (Character.isDigit(c)) {
|
||||
builder.append(c);
|
||||
}
|
||||
else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
finally {
|
||||
StringBuilderSpinAllocator.dispose(builder);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Finds anonymous classes. Uses javac notation.
|
||||
* @param psiManager project to search
|
||||
* @param externalName class qualified name
|
||||
* @return found psiClass
|
||||
*/
|
||||
@Nullable
|
||||
public static PsiClass findPsiClass(final PsiManager psiManager, String externalName){
|
||||
return findPsiClass(psiManager, externalName, null, false);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiClass findPsiClass(final PsiManager psiManager,
|
||||
String externalName,
|
||||
PsiClass psiClass,
|
||||
boolean jvmCompatible) {
|
||||
return findPsiClass(psiManager, externalName, psiClass, jvmCompatible, GlobalSearchScope.allScope(psiManager.getProject()));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static PsiClass findPsiClass(final PsiManager psiManager,
|
||||
String externalName,
|
||||
@Nullable PsiClass psiClass,
|
||||
boolean jvmCompatible,
|
||||
final GlobalSearchScope scope) {
|
||||
final int topIdx = externalName.indexOf('$');
|
||||
if (topIdx > -1) {
|
||||
if (psiClass == null) {
|
||||
psiClass = JavaPsiFacade.getInstance(psiManager.getProject())
|
||||
.findClass(externalName.substring(0, topIdx), scope);
|
||||
}
|
||||
if (psiClass == null) return null;
|
||||
externalName = externalName.substring(topIdx + 1);
|
||||
return findSubclass(psiManager, externalName, psiClass, jvmCompatible);
|
||||
} else {
|
||||
return JavaPsiFacade.getInstance(psiManager.getProject()).findClass(externalName, scope);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static PsiClass findSubclass(final PsiManager psiManager,
|
||||
final String externalName,
|
||||
final PsiClass psiClass,
|
||||
final boolean jvmCompatible) {
|
||||
final int nextIdx = externalName.indexOf('$');
|
||||
if (nextIdx > -1) {
|
||||
final PsiClass anonymousClass = findNonQualifiedClassByIndex(externalName.substring(0, nextIdx), psiClass, jvmCompatible);
|
||||
if (anonymousClass == null) return null;
|
||||
return findPsiClass(psiManager, externalName.substring(nextIdx), anonymousClass, jvmCompatible);
|
||||
}
|
||||
else {
|
||||
return findNonQualifiedClassByIndex(externalName, psiClass, jvmCompatible);
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getJVMClassName(PsiClass aClass) {
|
||||
final PsiClass containingClass = aClass.getContainingClass();
|
||||
if (containingClass != null) {
|
||||
String parentName = getJVMClassName(containingClass);
|
||||
if (parentName == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return parentName + "$" + aClass.getName();
|
||||
}
|
||||
return aClass.getQualifiedName();
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
public static PsiClass findPsiClassByJVMName(final PsiManager manager, final String jvmClassName) {
|
||||
return findPsiClass(manager, jvmClassName.replace('/', '.'), null, true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
/*
|
||||
* Copyright 2000-2009 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/*
|
||||
* User: anna
|
||||
* Date: 28-Oct-2008
|
||||
*/
|
||||
package com.intellij.psi.util;
|
||||
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.util.Function;
|
||||
|
||||
public class PsiExpressionTrimRenderer extends JavaRecursiveElementWalkingVisitor {
|
||||
private final StringBuilder myBuf;
|
||||
|
||||
public PsiExpressionTrimRenderer(final StringBuilder buf) {
|
||||
myBuf = buf;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitExpression(final PsiExpression expression) {
|
||||
myBuf.append(expression.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitInstanceOfExpression(final PsiInstanceOfExpression expression) {
|
||||
expression.getOperand().accept(this);
|
||||
myBuf.append(" ").append(PsiKeyword.INSTANCEOF).append(" ");
|
||||
final PsiTypeElement checkType = expression.getCheckType();
|
||||
if (checkType != null) {
|
||||
myBuf.append(checkType.getText());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitParenthesizedExpression(final PsiParenthesizedExpression expression) {
|
||||
myBuf.append("(");
|
||||
final PsiExpression expr = expression.getExpression();
|
||||
if (expr != null) {
|
||||
expr.accept(this);
|
||||
}
|
||||
myBuf.append(")");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitTypeCastExpression(final PsiTypeCastExpression expression) {
|
||||
final PsiTypeElement castType = expression.getCastType();
|
||||
if (castType != null) {
|
||||
myBuf.append("(").append(castType.getText()).append(")");
|
||||
}
|
||||
final PsiExpression operand = expression.getOperand();
|
||||
if (operand != null) {
|
||||
operand.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitArrayAccessExpression(final PsiArrayAccessExpression expression) {
|
||||
expression.getArrayExpression().accept(this);
|
||||
myBuf.append("[");
|
||||
final PsiExpression indexExpression = expression.getIndexExpression();
|
||||
if (indexExpression != null) {
|
||||
indexExpression.accept(this);
|
||||
}
|
||||
myBuf.append("]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPrefixExpression(final PsiPrefixExpression expression) {
|
||||
myBuf.append(expression.getOperationSign().getText());
|
||||
final PsiExpression operand = expression.getOperand();
|
||||
if (operand != null) {
|
||||
operand.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPostfixExpression(final PsiPostfixExpression expression) {
|
||||
expression.getOperand().accept(this);
|
||||
myBuf.append(expression.getOperationSign().getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitPolyadicExpression(PsiPolyadicExpression expression) {
|
||||
PsiExpression[] operands = expression.getOperands();
|
||||
for (int i = 0; i < operands.length; i++) {
|
||||
PsiExpression operand = operands[i];
|
||||
if (i != 0) {
|
||||
PsiJavaToken token = expression.getTokenBeforeOperand(operand);
|
||||
myBuf.append(" ").append(token.getText()).append(" ");
|
||||
}
|
||||
operand.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitConditionalExpression(final PsiConditionalExpression expression) {
|
||||
expression.getCondition().accept(this);
|
||||
|
||||
myBuf.append(" ? ");
|
||||
final PsiExpression thenExpression = expression.getThenExpression();
|
||||
if (thenExpression != null) {
|
||||
thenExpression.accept(this);
|
||||
}
|
||||
|
||||
myBuf.append(" : ");
|
||||
final PsiExpression elseExpression = expression.getElseExpression();
|
||||
if (elseExpression != null) {
|
||||
elseExpression.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitAssignmentExpression(final PsiAssignmentExpression expression) {
|
||||
expression.getLExpression().accept(this);
|
||||
myBuf.append(expression.getOperationSign().getText());
|
||||
final PsiExpression rExpression = expression.getRExpression();
|
||||
if (rExpression != null) {
|
||||
rExpression.accept(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitReferenceExpression(final PsiReferenceExpression expr) {
|
||||
final PsiExpression qualifierExpression = expr.getQualifierExpression();
|
||||
if (qualifierExpression != null) {
|
||||
qualifierExpression.accept(this);
|
||||
myBuf.append(".");
|
||||
}
|
||||
myBuf.append(expr.getReferenceName());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitMethodCallExpression(final PsiMethodCallExpression expr) {
|
||||
expr.getMethodExpression().accept(this);
|
||||
expr.getArgumentList().accept(this);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void visitArrayInitializerExpression(final PsiArrayInitializerExpression expression) {
|
||||
myBuf.append("{");
|
||||
boolean first = true;
|
||||
for (PsiExpression expr : expression.getInitializers()) {
|
||||
if (!first) {
|
||||
myBuf.append(", ");
|
||||
}
|
||||
first = false;
|
||||
expr.accept(this);
|
||||
}
|
||||
myBuf.append("}");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitExpressionList(final PsiExpressionList list) {
|
||||
final PsiExpression[] args = list.getExpressions();
|
||||
if (args.length > 0) {
|
||||
myBuf.append("(...)");
|
||||
}
|
||||
else {
|
||||
myBuf.append("()");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitNewExpression(final PsiNewExpression expr) {
|
||||
final PsiAnonymousClass anonymousClass = expr.getAnonymousClass();
|
||||
|
||||
final PsiExpressionList argumentList = expr.getArgumentList();
|
||||
|
||||
if (anonymousClass != null) {
|
||||
myBuf.append(PsiKeyword.NEW).append(" ").append(anonymousClass.getBaseClassType().getPresentableText());
|
||||
if (argumentList != null) argumentList.accept(this);
|
||||
myBuf.append(" {...}");
|
||||
}
|
||||
else {
|
||||
final PsiJavaCodeReferenceElement reference = expr.getClassReference();
|
||||
if (reference != null) {
|
||||
myBuf.append(PsiKeyword.NEW).append(" ").append(reference.getText());
|
||||
|
||||
final PsiExpression[] arrayDimensions = expr.getArrayDimensions();
|
||||
final PsiType type = expr.getType();
|
||||
final int dimensions = type != null ? type.getArrayDimensions() : arrayDimensions.length;
|
||||
if (arrayDimensions.length > 0) myBuf.append("[");
|
||||
for (int i = 0, arrayDimensionsLength = arrayDimensions.length; i < dimensions; i++) {
|
||||
final PsiExpression dimension = i < arrayDimensionsLength ? arrayDimensions[i] : null;
|
||||
if (i > 0) myBuf.append("][");
|
||||
if (dimension != null) {
|
||||
dimension.accept(this);
|
||||
}
|
||||
}
|
||||
if (arrayDimensions.length > 0) myBuf.append("]");
|
||||
|
||||
if (argumentList != null) {
|
||||
argumentList.accept(this);
|
||||
}
|
||||
|
||||
final PsiArrayInitializerExpression arrayInitializer = expr.getArrayInitializer();
|
||||
if (arrayInitializer != null) {
|
||||
arrayInitializer.accept(this);
|
||||
}
|
||||
}
|
||||
else {
|
||||
myBuf.append(expr.getText());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class RenderFunction implements Function<PsiExpression, String> {
|
||||
@Override
|
||||
public String fun(PsiExpression psiExpression) {
|
||||
return render(psiExpression);
|
||||
}
|
||||
}
|
||||
|
||||
public static String render(PsiExpression expression) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
expression.accept(new PsiExpressionTrimRenderer(buf));
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,441 @@
|
||||
/*
|
||||
* Copyright 2000-2011 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.psi.util;
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.psi.*;
|
||||
import org.intellij.lang.annotations.MagicConstant;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class PsiFormatUtil extends PsiFormatUtilBase {
|
||||
@MagicConstant(flags = {SHOW_MODIFIERS, SHOW_TYPE, TYPE_AFTER, SHOW_CONTAINING_CLASS, SHOW_FQ_NAME, SHOW_NAME, SHOW_MODIFIERS, SHOW_INITIALIZER, SHOW_RAW_TYPE, SHOW_RAW_NON_TOP_TYPE, SHOW_FQ_CLASS_NAMES})
|
||||
public @interface FormatVariableOptions {}
|
||||
|
||||
public static String formatVariable(PsiVariable variable, @FormatVariableOptions int options, PsiSubstitutor substitutor){
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
formatVariable(variable, options, substitutor,buffer);
|
||||
return buffer.toString();
|
||||
}
|
||||
private static void formatVariable(PsiVariable variable,
|
||||
@FormatVariableOptions int options,
|
||||
PsiSubstitutor substitutor,
|
||||
@NotNull StringBuilder buffer){
|
||||
if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) == 0){
|
||||
formatModifiers(variable, options,buffer);
|
||||
}
|
||||
if ((options & SHOW_TYPE) != 0 && (options & TYPE_AFTER) == 0){
|
||||
appendSpaceIfNeeded(buffer);
|
||||
buffer.append(formatType(variable.getType(), options, substitutor));
|
||||
}
|
||||
if (variable instanceof PsiField && (options & SHOW_CONTAINING_CLASS) != 0){
|
||||
PsiClass aClass = ((PsiField)variable).getContainingClass();
|
||||
if (aClass != null){
|
||||
String className = aClass.getName();
|
||||
if (className != null) {
|
||||
appendSpaceIfNeeded(buffer);
|
||||
if ((options & SHOW_FQ_NAME) != 0){
|
||||
String qName = aClass.getQualifiedName();
|
||||
if (qName != null){
|
||||
buffer.append(qName);
|
||||
}
|
||||
else{
|
||||
buffer.append(className);
|
||||
}
|
||||
}
|
||||
else{
|
||||
buffer.append(className);
|
||||
}
|
||||
buffer.append('.');
|
||||
}
|
||||
}
|
||||
if ((options & SHOW_NAME) != 0){
|
||||
buffer.append(variable.getName());
|
||||
}
|
||||
}
|
||||
else{
|
||||
if ((options & SHOW_NAME) != 0){
|
||||
String name = variable.getName();
|
||||
if (StringUtil.isNotEmpty(name)){
|
||||
appendSpaceIfNeeded(buffer);
|
||||
buffer.append(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((options & SHOW_TYPE) != 0 && (options & TYPE_AFTER) != 0){
|
||||
if ((options & SHOW_NAME) != 0 && variable.getName() != null){
|
||||
buffer.append(':');
|
||||
}
|
||||
buffer.append(formatType(variable.getType(), options, substitutor));
|
||||
}
|
||||
if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) != 0){
|
||||
formatModifiers(variable, options,buffer);
|
||||
}
|
||||
if ((options & SHOW_INITIALIZER) != 0){
|
||||
PsiExpression initializer = variable.getInitializer();
|
||||
if (initializer != null){
|
||||
buffer.append(" = ");
|
||||
String text = PsiExpressionTrimRenderer.render(initializer);
|
||||
int index1 = text.lastIndexOf('\n');
|
||||
if (index1 < 0) index1 = text.length();
|
||||
int index2 = text.lastIndexOf('\r');
|
||||
if (index2 < 0) index2 = text.length();
|
||||
int index = Math.min(index1, index2);
|
||||
buffer.append(text.substring(0, index));
|
||||
if (index < text.length()) {
|
||||
buffer.append(" ...");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static String formatMethod(PsiMethod method, PsiSubstitutor substitutor, @FormatMethodOptions int options, @FormatVariableOptions int parameterOptions){
|
||||
return formatMethod(method, substitutor, options, parameterOptions, MAX_PARAMS_TO_SHOW);
|
||||
}
|
||||
|
||||
public static String formatMethod(PsiMethod method, PsiSubstitutor substitutor, @FormatMethodOptions int options, @FormatVariableOptions int parameterOptions, int maxParametersToShow){
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
formatMethod(method, substitutor, options, parameterOptions, maxParametersToShow,buffer);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
@MagicConstant(flags = {SHOW_MODIFIERS, MODIFIERS_AFTER, SHOW_TYPE, TYPE_AFTER, SHOW_CONTAINING_CLASS, SHOW_FQ_NAME, SHOW_NAME, SHOW_PARAMETERS, SHOW_THROWS, SHOW_RAW_TYPE, SHOW_RAW_NON_TOP_TYPE, SHOW_FQ_CLASS_NAMES})
|
||||
public @interface FormatMethodOptions {}
|
||||
|
||||
private static void formatMethod(PsiMethod method, PsiSubstitutor substitutor, @FormatMethodOptions int options, @FormatVariableOptions int parameterOptions, int maxParametersToShow, StringBuilder buffer){
|
||||
if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) == 0){
|
||||
formatModifiers(method, options,buffer);
|
||||
}
|
||||
if ((options & SHOW_TYPE) != 0 && (options & TYPE_AFTER) == 0){
|
||||
PsiType type = method.getReturnType();
|
||||
if (type != null){
|
||||
appendSpaceIfNeeded(buffer);
|
||||
buffer.append(formatType(type, options, substitutor));
|
||||
}
|
||||
}
|
||||
if ((options & SHOW_CONTAINING_CLASS) != 0){
|
||||
PsiClass aClass = method.getContainingClass();
|
||||
if (aClass != null){
|
||||
appendSpaceIfNeeded(buffer);
|
||||
String name = aClass.getName();
|
||||
if (name != null) {
|
||||
if ((options & SHOW_FQ_NAME) != 0){
|
||||
String qName = aClass.getQualifiedName();
|
||||
if (qName != null){
|
||||
buffer.append(qName);
|
||||
}
|
||||
else{
|
||||
buffer.append(name);
|
||||
}
|
||||
}
|
||||
else{
|
||||
buffer.append(name);
|
||||
}
|
||||
buffer.append('.');
|
||||
}
|
||||
}
|
||||
if ((options & SHOW_NAME) != 0){
|
||||
buffer.append(method.getName());
|
||||
}
|
||||
}
|
||||
else{
|
||||
if ((options & SHOW_NAME) != 0){
|
||||
appendSpaceIfNeeded(buffer);
|
||||
buffer.append(method.getName());
|
||||
}
|
||||
}
|
||||
if ((options & SHOW_PARAMETERS) != 0){
|
||||
buffer.append('(');
|
||||
PsiParameter[] parms = method.getParameterList().getParameters();
|
||||
for(int i = 0; i < Math.min(parms.length, maxParametersToShow); i++) {
|
||||
PsiParameter parm = parms[i];
|
||||
if (i > 0){
|
||||
buffer.append(", ");
|
||||
}
|
||||
buffer.append(formatVariable(parm, parameterOptions, substitutor));
|
||||
}
|
||||
if(parms.length > maxParametersToShow) {
|
||||
buffer.append (", ...");
|
||||
}
|
||||
buffer.append(')');
|
||||
}
|
||||
if ((options & SHOW_TYPE) != 0 && (options & TYPE_AFTER) != 0){
|
||||
PsiType type = method.getReturnType();
|
||||
if (type != null){
|
||||
if (buffer.length() > 0){
|
||||
buffer.append(':');
|
||||
}
|
||||
buffer.append(formatType(type, options, substitutor));
|
||||
}
|
||||
}
|
||||
if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) != 0){
|
||||
formatModifiers(method, options,buffer);
|
||||
}
|
||||
if ((options & SHOW_THROWS) != 0){
|
||||
String throwsText = formatReferenceList(method.getThrowsList(), options);
|
||||
if (!throwsText.isEmpty()){
|
||||
appendSpaceIfNeeded(buffer);
|
||||
//noinspection HardCodedStringLiteral
|
||||
buffer.append("throws ");
|
||||
buffer.append(throwsText);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@MagicConstant(flags = {SHOW_MODIFIERS, SHOW_NAME, SHOW_ANONYMOUS_CLASS_VERBOSE, SHOW_FQ_NAME, MODIFIERS_AFTER, SHOW_EXTENDS_IMPLEMENTS, SHOW_REDUNDANT_MODIFIERS, JAVADOC_MODIFIERS_ONLY})
|
||||
public @interface FormatClassOptions {}
|
||||
|
||||
@NotNull
|
||||
public static String formatClass(@NotNull PsiClass aClass, @FormatClassOptions int options){
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) == 0){
|
||||
formatModifiers(aClass, options,buffer);
|
||||
}
|
||||
if ((options & SHOW_NAME) != 0){
|
||||
if (aClass instanceof PsiAnonymousClass && (options & SHOW_ANONYMOUS_CLASS_VERBOSE) != 0) {
|
||||
final PsiClassType baseClassReference = ((PsiAnonymousClass) aClass).getBaseClassType();
|
||||
PsiClass baseClass = baseClassReference.resolve();
|
||||
String name = baseClass == null ? baseClassReference.getPresentableText() : formatClass(baseClass, options);
|
||||
buffer.append(PsiBundle.message("anonymous.class.derived.display", name));
|
||||
}
|
||||
else {
|
||||
String name = aClass.getName();
|
||||
if (name != null) {
|
||||
appendSpaceIfNeeded(buffer);
|
||||
if ((options & SHOW_FQ_NAME) != 0) {
|
||||
String qName = aClass.getQualifiedName();
|
||||
if (qName != null) {
|
||||
buffer.append(qName);
|
||||
}
|
||||
else {
|
||||
buffer.append(aClass.getName());
|
||||
}
|
||||
}
|
||||
else {
|
||||
buffer.append(aClass.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((options & SHOW_MODIFIERS) != 0 && (options & MODIFIERS_AFTER) != 0){
|
||||
formatModifiers(aClass, options,buffer);
|
||||
}
|
||||
if ((options & SHOW_EXTENDS_IMPLEMENTS) != 0){
|
||||
String extendsText = formatReferenceList(aClass.getExtendsList(), options);
|
||||
if (!extendsText.isEmpty()){
|
||||
appendSpaceIfNeeded(buffer);
|
||||
//noinspection HardCodedStringLiteral
|
||||
buffer.append("extends ");
|
||||
buffer.append(extendsText);
|
||||
}
|
||||
String implementsText = formatReferenceList(aClass.getImplementsList(), options);
|
||||
if (!implementsText.isEmpty()){
|
||||
appendSpaceIfNeeded(buffer);
|
||||
//noinspection HardCodedStringLiteral
|
||||
buffer.append("implements ");
|
||||
buffer.append(implementsText);
|
||||
}
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static String formatModifiers(PsiElement element, int options) throws IllegalArgumentException{
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
formatModifiers(element, options,buffer);
|
||||
return buffer.toString();
|
||||
}
|
||||
private static void formatModifiers(PsiElement element, int options, StringBuilder buffer) throws IllegalArgumentException{
|
||||
PsiModifierList list;
|
||||
boolean isInterface = false;
|
||||
if (element instanceof PsiVariable){
|
||||
list = ((PsiVariable)element).getModifierList();
|
||||
}
|
||||
else if (element instanceof PsiMethod){
|
||||
list = ((PsiMethod)element).getModifierList();
|
||||
}
|
||||
else if (element instanceof PsiClass){
|
||||
isInterface = ((PsiClass)element).isInterface();
|
||||
list = ((PsiClass)element).getModifierList();
|
||||
if (list == null) return;
|
||||
}
|
||||
else if (element instanceof PsiClassInitializer){
|
||||
list = ((PsiClassInitializer)element).getModifierList();
|
||||
if (list == null) return;
|
||||
}
|
||||
else{
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
if (list == null) return;
|
||||
if ((options & SHOW_REDUNDANT_MODIFIERS) == 0
|
||||
? list.hasExplicitModifier(PsiModifier.PUBLIC)
|
||||
: list.hasModifierProperty(PsiModifier.PUBLIC)) {
|
||||
appendModifier(buffer, PsiModifier.PUBLIC);
|
||||
}
|
||||
|
||||
if (list.hasModifierProperty(PsiModifier.PROTECTED)){
|
||||
appendModifier(buffer, PsiModifier.PROTECTED);
|
||||
}
|
||||
if (list.hasModifierProperty(PsiModifier.PRIVATE)){
|
||||
appendModifier(buffer, PsiModifier.PRIVATE);
|
||||
}
|
||||
|
||||
if ((options & SHOW_REDUNDANT_MODIFIERS) == 0
|
||||
? list.hasExplicitModifier(PsiModifier.PACKAGE_LOCAL)
|
||||
: list.hasModifierProperty(PsiModifier.PACKAGE_LOCAL)) {
|
||||
if (element instanceof PsiClass && element.getParent() instanceof PsiDeclarationStatement) {// local class
|
||||
appendModifier(buffer, PsiBundle.message("local.class.preposition"));
|
||||
}
|
||||
else {
|
||||
appendModifier(buffer, PsiBundle.visibilityPresentation(PsiModifier.PACKAGE_LOCAL));
|
||||
}
|
||||
}
|
||||
|
||||
if ((options & SHOW_REDUNDANT_MODIFIERS) == 0
|
||||
? list.hasExplicitModifier(PsiModifier.STATIC)
|
||||
: list.hasModifierProperty(PsiModifier.STATIC)) appendModifier(buffer, PsiModifier.STATIC);
|
||||
|
||||
if (!isInterface && //cls modifier list
|
||||
((options & SHOW_REDUNDANT_MODIFIERS) == 0
|
||||
? list.hasExplicitModifier(PsiModifier.ABSTRACT)
|
||||
: list.hasModifierProperty(PsiModifier.ABSTRACT))) appendModifier(buffer, PsiModifier.ABSTRACT);
|
||||
|
||||
if ((options & SHOW_REDUNDANT_MODIFIERS) == 0
|
||||
? list.hasExplicitModifier(PsiModifier.FINAL)
|
||||
: list.hasModifierProperty(PsiModifier.FINAL)) appendModifier(buffer, PsiModifier.FINAL);
|
||||
|
||||
if (list.hasModifierProperty(PsiModifier.NATIVE) && (options & JAVADOC_MODIFIERS_ONLY) == 0){
|
||||
appendModifier(buffer, PsiModifier.NATIVE);
|
||||
}
|
||||
if (list.hasModifierProperty(PsiModifier.SYNCHRONIZED) && (options & JAVADOC_MODIFIERS_ONLY) == 0){
|
||||
appendModifier(buffer, PsiModifier.SYNCHRONIZED);
|
||||
}
|
||||
if (list.hasModifierProperty(PsiModifier.STRICTFP) && (options & JAVADOC_MODIFIERS_ONLY) == 0){
|
||||
appendModifier(buffer, PsiModifier.STRICTFP);
|
||||
}
|
||||
if (list.hasModifierProperty(PsiModifier.TRANSIENT) &&
|
||||
element instanceof PsiVariable // javac 5 puts transient attr for methods
|
||||
){
|
||||
appendModifier(buffer, PsiModifier.TRANSIENT);
|
||||
}
|
||||
if (list.hasModifierProperty(PsiModifier.VOLATILE)){
|
||||
appendModifier(buffer, PsiModifier.VOLATILE);
|
||||
}
|
||||
}
|
||||
|
||||
private static void appendModifier(final StringBuilder buffer, final String modifier) {
|
||||
appendSpaceIfNeeded(buffer);
|
||||
buffer.append(modifier);
|
||||
}
|
||||
|
||||
public static String formatReferenceList(PsiReferenceList list, int options){
|
||||
StringBuilder buffer = new StringBuilder();
|
||||
PsiJavaCodeReferenceElement[] refs = list.getReferenceElements();
|
||||
for(int i = 0; i < refs.length; i++) {
|
||||
PsiJavaCodeReferenceElement ref = refs[i];
|
||||
if (i > 0){
|
||||
buffer.append(", ");
|
||||
}
|
||||
buffer.append(formatReference(ref, options));
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public static String formatType(PsiType type, int options, @NotNull PsiSubstitutor substitutor){
|
||||
type = substitutor.substitute(type);
|
||||
if ((options & SHOW_RAW_TYPE) != 0) {
|
||||
type = TypeConversionUtil.erasure(type);
|
||||
} else if ((options & SHOW_RAW_NON_TOP_TYPE) != 0) {
|
||||
if (!(PsiUtil.resolveClassInType(type) instanceof PsiTypeParameter)) {
|
||||
final boolean preserveEllipsis = type instanceof PsiEllipsisType;
|
||||
type = TypeConversionUtil.erasure(type);
|
||||
if (preserveEllipsis && type instanceof PsiArrayType) {
|
||||
type = new PsiEllipsisType(((PsiArrayType)type).getComponentType());
|
||||
}
|
||||
}
|
||||
}
|
||||
return (options & SHOW_FQ_CLASS_NAMES) == 0 ? type.getPresentableText() : type.getInternalCanonicalText();
|
||||
}
|
||||
|
||||
public static String formatReference(PsiJavaCodeReferenceElement ref, int options){
|
||||
return (options & SHOW_FQ_CLASS_NAMES) == 0 ? ref.getText() : ref.getCanonicalText();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getExternalName(PsiModifierListOwner owner) {
|
||||
return getExternalName(owner, true);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getExternalName(PsiModifierListOwner owner, final boolean showParamName) {
|
||||
return getExternalName(owner, showParamName, MAX_PARAMS_TO_SHOW);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getExternalName(PsiModifierListOwner owner, final boolean showParamName, int maxParamsToShow) {
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
if (owner instanceof PsiClass) {
|
||||
ClassUtil.formatClassName((PsiClass)owner, builder);
|
||||
return builder.toString();
|
||||
}
|
||||
final PsiClass psiClass = PsiTreeUtil.getParentOfType(owner, PsiClass.class, false);
|
||||
if (psiClass == null) return null;
|
||||
ClassUtil.formatClassName(psiClass, builder);
|
||||
if (owner instanceof PsiMethod) {
|
||||
builder.append(" ");
|
||||
formatMethod((PsiMethod)owner, PsiSubstitutor.EMPTY,
|
||||
SHOW_NAME | SHOW_FQ_NAME | SHOW_TYPE | SHOW_PARAMETERS | SHOW_FQ_CLASS_NAMES,
|
||||
showParamName ? SHOW_NAME | SHOW_TYPE | SHOW_FQ_CLASS_NAMES : SHOW_TYPE | SHOW_FQ_CLASS_NAMES, maxParamsToShow, builder);
|
||||
}
|
||||
else if (owner instanceof PsiField) {
|
||||
builder.append(" ").append(((PsiField)owner).getName());
|
||||
}
|
||||
else if (owner instanceof PsiParameter) {
|
||||
final PsiElement declarationScope = ((PsiParameter)owner).getDeclarationScope();
|
||||
if (!(declarationScope instanceof PsiMethod)) {
|
||||
return null;
|
||||
}
|
||||
final PsiMethod psiMethod = (PsiMethod)declarationScope;
|
||||
|
||||
builder.append(" ");
|
||||
formatMethod(psiMethod, PsiSubstitutor.EMPTY,
|
||||
SHOW_NAME | SHOW_FQ_NAME | SHOW_TYPE | SHOW_PARAMETERS | SHOW_FQ_CLASS_NAMES,
|
||||
showParamName ? SHOW_NAME | SHOW_TYPE | SHOW_FQ_CLASS_NAMES : SHOW_TYPE | SHOW_FQ_CLASS_NAMES, maxParamsToShow, builder);
|
||||
builder.append(" ");
|
||||
|
||||
if (showParamName) {
|
||||
formatVariable((PsiVariable)owner, SHOW_NAME, PsiSubstitutor.EMPTY, builder);
|
||||
}
|
||||
else {
|
||||
builder.append(psiMethod.getParameterList().getParameterIndex((PsiParameter)owner));
|
||||
}
|
||||
}
|
||||
else {
|
||||
return null;
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static String getPackageDisplayName(@NotNull final PsiClass psiClass) {
|
||||
@NonNls String packageName = psiClass.getQualifiedName();
|
||||
packageName = packageName == null || packageName.lastIndexOf('.') <= 0 ? "" : packageName.substring(0, packageName.lastIndexOf('.'));
|
||||
if (packageName.isEmpty()) {
|
||||
packageName = "default package";
|
||||
}
|
||||
return packageName;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user