use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class MethodOrClassSelectioner method select.
@Override
public List<TextRange> select(PsiElement e, CharSequence editorText, int cursorOffset, Editor editor) {
List<TextRange> result = ContainerUtil.newArrayList();
PsiElement firstChild = e.getFirstChild();
PsiElement[] children = e.getChildren();
int i = 1;
if (firstChild instanceof PsiDocComment) {
while (children[i] instanceof PsiWhiteSpace) {
i++;
}
TextRange range = new TextRange(children[i].getTextRange().getStartOffset(), e.getTextRange().getEndOffset());
result.add(range);
result.addAll(expandToWholeLinesWithBlanks(editorText, range));
range = TextRange.create(firstChild.getTextRange());
result.addAll(expandToWholeLinesWithBlanks(editorText, range));
firstChild = children[i++];
}
if (firstChild instanceof PsiComment) {
while (children[i] instanceof PsiComment || children[i] instanceof PsiWhiteSpace) {
i++;
}
PsiElement last = children[i - 1] instanceof PsiWhiteSpace ? children[i - 2] : children[i - 1];
TextRange range = new TextRange(firstChild.getTextRange().getStartOffset(), last.getTextRange().getEndOffset());
if (range.contains(cursorOffset)) {
result.addAll(expandToWholeLinesWithBlanks(editorText, range));
}
range = new TextRange(children[i].getTextRange().getStartOffset(), e.getTextRange().getEndOffset());
result.add(range);
result.addAll(expandToWholeLinesWithBlanks(editorText, range));
}
result.add(e.getTextRange());
result.addAll(expandToWholeLinesWithBlanks(editorText, e.getTextRange()));
if (e instanceof PsiClass) {
result.addAll(selectWithTypeParameters((PsiClass) e));
result.addAll(selectBetweenBracesLines(children, editorText));
}
if (e instanceof PsiAnonymousClass) {
result.addAll(selectWholeBlock((PsiAnonymousClass) e));
}
return result;
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class GenerateConstructorHandler method generateConstructorPrototype.
public static PsiMethod generateConstructorPrototype(PsiClass aClass, PsiMethod baseConstructor, boolean copyJavaDoc, PsiField[] fields) throws IncorrectOperationException {
PsiManager manager = aClass.getManager();
Project project = aClass.getProject();
JVMElementFactory factory = JVMElementFactories.requireFactory(aClass.getLanguage(), project);
CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject());
String className = aClass.getName();
assert className != null : aClass;
PsiMethod constructor = factory.createConstructor(className, aClass);
GenerateMembersUtil.setVisibility(aClass, constructor);
if (baseConstructor != null) {
PsiJavaCodeReferenceElement[] throwRefs = baseConstructor.getThrowsList().getReferenceElements();
for (PsiJavaCodeReferenceElement ref : throwRefs) {
constructor.getThrowsList().add(ref);
}
if (copyJavaDoc) {
final PsiDocComment docComment = ((PsiMethod) baseConstructor.getNavigationElement()).getDocComment();
if (docComment != null) {
constructor.addAfter(docComment, null);
}
}
}
boolean isNotEnum = false;
if (baseConstructor != null) {
PsiClass superClass = aClass.getSuperClass();
LOG.assertTrue(superClass != null);
if (!CommonClassNames.JAVA_LANG_ENUM.equals(superClass.getQualifiedName())) {
isNotEnum = true;
if (baseConstructor instanceof PsiCompiledElement) {
// to get some parameter names
PsiClass dummyClass = JVMElementFactories.requireFactory(baseConstructor.getLanguage(), project).createClass("Dummy");
baseConstructor = (PsiMethod) dummyClass.add(baseConstructor);
}
PsiParameter[] params = baseConstructor.getParameterList().getParameters();
for (PsiParameter param : params) {
String name = param.getName();
assert name != null : param;
PsiParameter newParam = factory.createParameter(name, param.getType(), aClass);
GenerateMembersUtil.copyOrReplaceModifierList(param, aClass, newParam);
constructor.getParameterList().add(newParam);
}
}
}
JavaCodeStyleManager javaStyle = JavaCodeStyleManager.getInstance(project);
final PsiMethod dummyConstructor = factory.createConstructor(className);
dummyConstructor.getParameterList().replace(constructor.getParameterList().copy());
List<PsiParameter> fieldParams = new ArrayList<>();
for (PsiField field : fields) {
String fieldName = field.getName();
assert fieldName != null : field;
String name = javaStyle.variableNameToPropertyName(fieldName, VariableKind.FIELD);
String parmName = javaStyle.propertyNameToVariableName(name, VariableKind.PARAMETER);
parmName = javaStyle.suggestUniqueVariableName(parmName, dummyConstructor, true);
PsiParameter parm = factory.createParameter(parmName, field.getType(), aClass);
NullableNotNullManager.getInstance(project).copyNotNullAnnotation(field, parm);
if (constructor.isVarArgs()) {
final PsiParameterList parameterList = constructor.getParameterList();
parameterList.addBefore(parm, parameterList.getParameters()[parameterList.getParametersCount() - 1]);
final PsiParameterList dummyParameterList = dummyConstructor.getParameterList();
dummyParameterList.addBefore(parm.copy(), dummyParameterList.getParameters()[dummyParameterList.getParametersCount() - 1]);
} else {
constructor.getParameterList().add(parm);
dummyConstructor.getParameterList().add(parm.copy());
}
fieldParams.add(parm);
}
ConstructorBodyGenerator generator = ConstructorBodyGenerator.INSTANCE.forLanguage(aClass.getLanguage());
if (generator != null) {
StringBuilder buffer = new StringBuilder();
generator.start(buffer, constructor.getName(), PsiParameter.EMPTY_ARRAY);
if (isNotEnum) {
generator.generateSuperCallIfNeeded(buffer, baseConstructor.getParameterList().getParameters());
}
final PsiParameter[] parameters = fieldParams.toArray(new PsiParameter[fieldParams.size()]);
final List<String> existingNames = ContainerUtil.map(dummyConstructor.getParameterList().getParameters(), parameter -> parameter.getName());
if (generator instanceof ConstructorBodyGeneratorEx) {
((ConstructorBodyGeneratorEx) generator).generateFieldInitialization(buffer, fields, parameters, existingNames);
} else {
generator.generateFieldInitialization(buffer, fields, parameters);
}
generator.finish(buffer);
PsiMethod stub = factory.createMethodFromText(buffer.toString(), aClass);
PsiCodeBlock original = constructor.getBody(), replacement = stub.getBody();
assert original != null : constructor;
assert replacement != null : stub;
original.replace(replacement);
}
constructor = (PsiMethod) codeStyleManager.reformat(constructor);
return constructor;
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class GenerateDelegateHandler method clearMethod.
private void clearMethod(PsiMethod method) throws IncorrectOperationException {
LOG.assertTrue(!method.isPhysical());
PsiCodeBlock codeBlock = JavaPsiFacade.getInstance(method.getProject()).getElementFactory().createCodeBlock();
if (method.getBody() != null) {
method.getBody().replace(codeBlock);
} else {
method.add(codeBlock);
}
if (!myToCopyJavaDoc) {
final PsiDocComment docComment = method.getDocComment();
if (docComment != null) {
docComment.delete();
}
}
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JavaImplementationTextSelectioner method getTextStartOffset.
@Override
public int getTextStartOffset(@NotNull final PsiElement parent) {
PsiElement element = parent;
if (element instanceof PsiDocCommentOwner) {
PsiDocComment comment = ((PsiDocCommentOwner) element).getDocComment();
if (comment != null) {
element = comment.getNextSibling();
while (element instanceof PsiWhiteSpace) {
element = element.getNextSibling();
}
}
}
if (element != null) {
TextRange range = element.getTextRange();
if (range != null) {
return range.getStartOffset();
}
LOG.error("Range should not be null: " + element + "; " + element.getClass());
}
LOG.error("Element should not be null: " + parent.getText());
return parent.getTextRange().getStartOffset();
}
use of com.intellij.psi.javadoc.PsiDocComment in project intellij-community by JetBrains.
the class JavaFoldingBuilderBase method isRegionCollapsedByDefault.
@Override
protected boolean isRegionCollapsedByDefault(@NotNull ASTNode node) {
final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(node);
JavaCodeFoldingSettings settings = JavaCodeFoldingSettings.getInstance();
if (element instanceof PsiNewExpression || element instanceof PsiJavaToken && element.getParent() instanceof PsiAnonymousClass) {
return settings.isCollapseLambdas();
}
if (element instanceof PsiJavaToken && element.getParent() instanceof PsiCodeBlock && element.getParent().getParent() instanceof PsiMethod) {
return settings.isCollapseOneLineMethods();
}
if (element instanceof PsiReferenceParameterList) {
return settings.isCollapseConstructorGenericParameters();
}
if (element instanceof PsiImportList) {
return settings.isCollapseImports();
} else if (element instanceof PsiMethod || element instanceof PsiClassInitializer || element instanceof PsiCodeBlock) {
if (element instanceof PsiMethod) {
if (!settings.isCollapseAccessors() && !settings.isCollapseMethods()) {
return false;
}
if (isSimplePropertyAccessor((PsiMethod) element)) {
return settings.isCollapseAccessors();
}
}
return settings.isCollapseMethods();
} else if (element instanceof PsiAnonymousClass) {
return settings.isCollapseAnonymousClasses();
} else if (element instanceof PsiClass) {
return !(element.getParent() instanceof PsiFile) && settings.isCollapseInnerClasses();
} else if (element instanceof PsiDocComment) {
PsiElement parent = element.getParent();
if (parent instanceof PsiJavaFile) {
if (((PsiJavaFile) parent).getName().equals(PsiPackage.PACKAGE_INFO_FILE)) {
return false;
}
PsiElement firstChild = parent.getFirstChild();
if (firstChild instanceof PsiWhiteSpace) {
firstChild = firstChild.getNextSibling();
}
if (element.equals(firstChild)) {
return settings.isCollapseFileHeader();
}
}
return settings.isCollapseJavadocs();
} else if (element instanceof PsiJavaFile) {
return settings.isCollapseFileHeader();
} else if (element instanceof PsiAnnotation) {
return settings.isCollapseAnnotations();
} else if (element instanceof PsiComment) {
return settings.isCollapseEndOfLineComments();
} else if (element instanceof PsiLambdaExpression) {
return settings.isCollapseAnonymousClasses();
} else {
LOG.error("Unknown element:" + element);
return false;
}
}
Aggregations