Search in sources :

Example 16 with PsiElementFactory

use of com.intellij.psi.PsiElementFactory in project intellij-community by JetBrains.

the class ChangeToCStyleCommentIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element, @NotNull Project project, Editor editor) throws IncorrectOperationException {
    final PsiComment selectedComment = (PsiComment) element;
    PsiComment firstComment = selectedComment;
    while (true) {
        final PsiElement prevComment = getPrevNonWhiteSpace(firstComment);
        if (!isEndOfLineComment(prevComment)) {
            break;
        }
        firstComment = (PsiComment) prevComment;
    }
    final JavaPsiFacade manager = JavaPsiFacade.getInstance(selectedComment.getProject());
    final PsiElementFactory factory = manager.getElementFactory();
    String text = getCommentContents(firstComment);
    final List<PsiElement> commentsToDelete = new ArrayList<>();
    PsiElement nextComment = firstComment;
    while (true) {
        nextComment = getNextNonWhiteSpace(nextComment);
        if (!isEndOfLineComment(nextComment)) {
            break;
        }
        text += //to get the whitespace for proper spacing
        nextComment.getPrevSibling().getText() + "  " + getCommentContents((PsiComment) nextComment);
        commentsToDelete.add(nextComment);
    }
    final PsiComment newComment = factory.createCommentFromText("/*" + text + " */", selectedComment.getParent());
    firstComment.replace(newComment);
    for (PsiElement commentToDelete : commentsToDelete) {
        commentToDelete.delete();
    }
}
Also used : JavaPsiFacade(com.intellij.psi.JavaPsiFacade) PsiComment(com.intellij.psi.PsiComment) PsiElementFactory(com.intellij.psi.PsiElementFactory) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement)

Example 17 with PsiElementFactory

use of com.intellij.psi.PsiElementFactory in project intellij-community by JetBrains.

the class InstanceValueDescriptor method getDescriptorEvaluation.

@Override
public PsiExpression getDescriptorEvaluation(DebuggerContext debuggerContext) throws EvaluateException {
    PsiElementFactory elementFactory = JavaPsiFacade.getInstance(myProject).getElementFactory();
    ObjectReference ref = ((ObjectReference) getValue());
    String name = NamesUtils.getUniqueName(ref).replace("@", "");
    String presentation = String.format("%s_DebugLabel", name);
    return elementFactory.createExpressionFromText(presentation, ContextUtil.getContextElement(debuggerContext));
}
Also used : ObjectReference(com.sun.jdi.ObjectReference) PsiElementFactory(com.intellij.psi.PsiElementFactory)

Example 18 with PsiElementFactory

use of com.intellij.psi.PsiElementFactory in project kotlin by JetBrains.

the class KotlinFindClassUsagesDialog method getRepresentingPsiClass.

@NotNull
private static PsiClass getRepresentingPsiClass(@NotNull KtClassOrObject classOrObject) {
    PsiClass lightClass = toLightClass(classOrObject);
    if (lightClass != null)
        return lightClass;
    // TODO: Remove this code when light classes are generated for builtins
    PsiElementFactory factory = PsiElementFactory.SERVICE.getInstance(classOrObject.getProject());
    String name = classOrObject.getName();
    if (name == null || name.isEmpty()) {
        name = "Anonymous";
    }
    PsiClass javaClass;
    if (classOrObject instanceof KtClass) {
        KtClass klass = (KtClass) classOrObject;
        javaClass = !klass.isInterface() ? factory.createClass(name) : klass.isAnnotation() ? factory.createAnnotationType(name) : factory.createInterface(name);
    } else {
        javaClass = factory.createClass(name);
    }
    //noinspection ConstantConditions
    javaClass.getModifierList().setModifierProperty(PsiModifier.FINAL, !(classOrObject instanceof KtClass && KtPsiUtilKt.isInheritable((KtClass) classOrObject)));
    javaClass.putUserData(ORIGINAL_CLASS, classOrObject);
    return javaClass;
}
Also used : KtClass(org.jetbrains.kotlin.psi.KtClass) PsiClass(com.intellij.psi.PsiClass) PsiElementFactory(com.intellij.psi.PsiElementFactory) NotNull(org.jetbrains.annotations.NotNull)

Example 19 with PsiElementFactory

use of com.intellij.psi.PsiElementFactory in project google-cloud-intellij by GoogleCloudPlatform.

the class ConstructorInspectionTest method testQuickFix_classWithNullaryConstructor.

public void testQuickFix_classWithNullaryConstructor() {
    ProblemDescriptorImpl problemDescriptorMock = mock(ProblemDescriptorImpl.class);
    MockitoAnnotations.initMocks(this);
    PsiElementFactory factory = JavaPsiFacade.getInstance(myFixture.getProject()).getElementFactory();
    PsiClass psiClass = factory.createClass(getName());
    PsiMethod nullaryConstructor = factory.createMethodFromText("public " + psiClass.getName() + "() { }", psiClass);
    psiClass.addAfter(nullaryConstructor, null);
    ConstructorInspection constructorInspection = new ConstructorInspection();
    ConstructorInspection.MyQuickFix myQuickFix = constructorInspection.new MyQuickFix(psiClass);
    myQuickFix.applyFix(myFixture.getProject(), problemDescriptorMock);
    Assert.assertEquals(1, psiClass.getConstructors().length);
    Assert.assertTrue(EndpointUtilities.isPublicNullaryConstructor(psiClass.getConstructors()[0]));
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiElementFactory(com.intellij.psi.PsiElementFactory) PsiClass(com.intellij.psi.PsiClass) ProblemDescriptorImpl(com.intellij.codeInspection.ex.ProblemDescriptorImpl)

Example 20 with PsiElementFactory

use of com.intellij.psi.PsiElementFactory in project google-cloud-intellij by GoogleCloudPlatform.

the class ConstructorInspectionTest method testQuickFix_classWithNonNullaryConstructor.

public void testQuickFix_classWithNonNullaryConstructor() {
    ProblemDescriptorImpl problemDescriptorMock = mock(ProblemDescriptorImpl.class);
    MockitoAnnotations.initMocks(this);
    PsiElementFactory factory = JavaPsiFacade.getInstance(myFixture.getProject()).getElementFactory();
    PsiClass psiClass = factory.createClass(getName());
    PsiMethod nullaryConstructor = factory.createMethodFromText("public " + psiClass.getName() + "(String param) { }", psiClass);
    psiClass.addAfter(nullaryConstructor, null);
    ConstructorInspection constructorInspection = new ConstructorInspection();
    ConstructorInspection.MyQuickFix myQuickFix = constructorInspection.new MyQuickFix(psiClass);
    myQuickFix.applyFix(myFixture.getProject(), problemDescriptorMock);
    Assert.assertEquals(2, psiClass.getConstructors().length);
    Assert.assertTrue(EndpointUtilities.isPublicNullaryConstructor(psiClass.getConstructors()[0]));
}
Also used : PsiMethod(com.intellij.psi.PsiMethod) PsiElementFactory(com.intellij.psi.PsiElementFactory) PsiClass(com.intellij.psi.PsiClass) ProblemDescriptorImpl(com.intellij.codeInspection.ex.ProblemDescriptorImpl)

Aggregations

PsiElementFactory (com.intellij.psi.PsiElementFactory)20 PsiClass (com.intellij.psi.PsiClass)6 PsiElement (com.intellij.psi.PsiElement)6 PsiExpression (com.intellij.psi.PsiExpression)5 IncorrectOperationException (com.intellij.util.IncorrectOperationException)5 ProblemDescriptorImpl (com.intellij.codeInspection.ex.ProblemDescriptorImpl)2 EvaluateException (com.intellij.debugger.engine.evaluation.EvaluateException)2 JavaPsiFacade (com.intellij.psi.JavaPsiFacade)2 PsiComment (com.intellij.psi.PsiComment)2 PsiMethod (com.intellij.psi.PsiMethod)2 PsiType (com.intellij.psi.PsiType)2 JBColor (com.intellij.ui.JBColor)2 ArrayList (java.util.ArrayList)2 ArrayElementDescriptorImpl (com.intellij.debugger.ui.impl.watch.ArrayElementDescriptorImpl)1 Editor (com.intellij.openapi.editor.Editor)1 Project (com.intellij.openapi.project.Project)1 LanguageLevel (com.intellij.pom.java.LanguageLevel)1 PsiClassType (com.intellij.psi.PsiClassType)1 PsiField (com.intellij.psi.PsiField)1 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)1