Search in sources :

Example 16 with PsiMember

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

the class CompletionTestBase method doTest.

protected void doTest(String directory) {
    CamelHumpMatcher.forceStartMatching(myFixture.getTestRootDisposable());
    final List<String> stringList = TestUtils.readInput(getTestDataPath() + "/" + getTestName(true) + ".test");
    if (directory.length() != 0)
        directory += "/";
    final String fileName = directory + getTestName(true) + "." + getExtension();
    myFixture.addFileToProject(fileName, stringList.get(0));
    myFixture.configureByFile(fileName);
    boolean old = CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX;
    CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX = false;
    CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = false;
    String result = "";
    try {
        myFixture.completeBasic();
        final LookupImpl lookup = (LookupImpl) LookupManager.getActiveLookup(myFixture.getEditor());
        if (lookup != null) {
            List<LookupElement> items = lookup.getItems();
            if (!addReferenceVariants()) {
                items = ContainerUtil.findAll(items, lookupElement -> {
                    final Object o = lookupElement.getObject();
                    return !(o instanceof PsiMember) && !(o instanceof GrVariable) && !(o instanceof GroovyResolveResult) && !(o instanceof PsiPackage);
                });
            }
            Collections.sort(items, (o1, o2) -> o1.getLookupString().compareTo(o2.getLookupString()));
            result = "";
            for (LookupElement item : items) {
                result = result + "\n" + item.getLookupString();
            }
            result = result.trim();
            LookupManager.getInstance(myFixture.getProject()).hideActiveLookup();
        }
    } finally {
        CodeInsightSettings.getInstance().AUTOCOMPLETE_ON_CODE_COMPLETION = true;
        CodeInsightSettings.getInstance().AUTOCOMPLETE_COMMON_PREFIX = old;
    }
    assertEquals(StringUtil.trimEnd(stringList.get(1), "\n"), result);
}
Also used : PsiPackage(com.intellij.psi.PsiPackage) LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) PsiMember(com.intellij.psi.PsiMember) JavaCodeInsightFixtureTestCase(com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) LookupManager(com.intellij.codeInsight.lookup.LookupManager) CodeInsightSettings(com.intellij.codeInsight.CodeInsightSettings) TestUtils(org.jetbrains.plugins.groovy.util.TestUtils) ContainerUtil(com.intellij.util.containers.ContainerUtil) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) List(java.util.List) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) CamelHumpMatcher(com.intellij.codeInsight.completion.impl.CamelHumpMatcher) Collections(java.util.Collections) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) LookupImpl(com.intellij.codeInsight.lookup.impl.LookupImpl) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) PsiPackage(com.intellij.psi.PsiPackage) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PsiMember(com.intellij.psi.PsiMember)

Example 17 with PsiMember

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

the class GenerationUtil method velocityGenerateCode.

/**
   * Generates the code using Velocity.
   * <p/>
   * This is used to create the {@code toString} method body and it's javadoc.
   *
   * @param selectedMembers the selected members as both {@link PsiField} and {@link PsiMethod}.
   * @param params          additional parameters stored with key/value in the map.
   * @param templateMacro   the velocity macro template
   * @param useAccessors    if true, accessor property for FieldElement bean would be assigned to field getter name append with () 
   * @return code (usually javacode). Returns null if templateMacro is null.
   * @throws GenerateCodeException is thrown when there is an error generating the javacode.
   */
public static String velocityGenerateCode(@Nullable PsiClass clazz, Collection<? extends PsiMember> selectedMembers, Collection<? extends PsiMember> selectedNotNullMembers, Map<String, String> params, Map<String, Object> contextMap, String templateMacro, int sortElements, boolean useFullyQualifiedName, boolean useAccessors) throws GenerateCodeException {
    if (templateMacro == null) {
        return null;
    }
    StringWriter sw = new StringWriter();
    try {
        VelocityContext vc = new VelocityContext();
        // field information
        logger.debug("Velocity Context - adding fields");
        final List<FieldElement> fieldElements = ElementUtils.getOnlyAsFieldElements(selectedMembers, selectedNotNullMembers, useAccessors);
        vc.put("fields", fieldElements);
        if (fieldElements.size() == 1) {
            vc.put("field", fieldElements.get(0));
        }
        PsiMember member = clazz != null ? clazz : ContainerUtil.getFirstItem(selectedMembers);
        // method information
        logger.debug("Velocity Context - adding methods");
        vc.put("methods", ElementUtils.getOnlyAsMethodElements(selectedMembers));
        // element information (both fields and methods)
        logger.debug("Velocity Context - adding members (fields and methods)");
        List<Element> elements = ElementUtils.getOnlyAsFieldAndMethodElements(selectedMembers, selectedNotNullMembers, useAccessors);
        // sort elements if enabled and not using chooser dialog
        if (sortElements != 0 && sortElements < 3) {
            Collections.sort(elements, new ElementComparator(sortElements));
        }
        vc.put("members", elements);
        // class information
        if (clazz != null) {
            ClassElement ce = ElementFactory.newClassElement(clazz);
            vc.put("class", ce);
            if (logger.isDebugEnabled())
                logger.debug("Velocity Context - adding class: " + ce);
            // information to keep as it is to avoid breaking compatibility with prior releases
            vc.put("classname", useFullyQualifiedName ? ce.getQualifiedName() : ce.getName());
            vc.put("FQClassname", ce.getQualifiedName());
        }
        if (member != null) {
            vc.put("java_version", PsiAdapter.getJavaVersion(member));
            final Project project = member.getProject();
            vc.put("settings", CodeStyleSettingsManager.getSettings(project));
            vc.put("project", project);
        }
        vc.put("helper", GenerationHelper.class);
        vc.put("StringUtil", StringUtil.class);
        vc.put("NameUtil", NameUtil.class);
        for (String paramName : contextMap.keySet()) {
            vc.put(paramName, contextMap.get(paramName));
        }
        if (logger.isDebugEnabled())
            logger.debug("Velocity Macro:\n" + templateMacro);
        // velocity
        VelocityEngine velocity = VelocityFactory.getVelocityEngine();
        logger.debug("Executing velocity +++ START +++");
        velocity.evaluate(vc, sw, GenerateToStringWorker.class.getName(), templateMacro);
        logger.debug("Executing velocity +++ END +++");
        // any additional packages to import returned from velocity?
        if (vc.get("autoImportPackages") != null) {
            params.put("autoImportPackages", (String) vc.get("autoImportPackages"));
        }
    } catch (ProcessCanceledException e) {
        throw e;
    } catch (Exception e) {
        throw new GenerateCodeException("Error in Velocity code generator", e);
    }
    return StringUtil.convertLineSeparators(sw.getBuffer().toString());
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) VelocityContext(org.apache.velocity.VelocityContext) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException) GenerateCodeException(org.jetbrains.java.generate.exception.GenerateCodeException) PluginException(org.jetbrains.java.generate.exception.PluginException) GenerateCodeException(org.jetbrains.java.generate.exception.GenerateCodeException) Project(com.intellij.openapi.project.Project) StringWriter(java.io.StringWriter) PsiMember(com.intellij.psi.PsiMember) ProcessCanceledException(com.intellij.openapi.progress.ProcessCanceledException)

Example 18 with PsiMember

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

the class SamePsiMemberWeigher method weigh.

@Override
public Comparable weigh(@NotNull final PsiElement element, @NotNull final ProximityLocation location) {
    PsiElement position = location.getPosition();
    if (position == null) {
        return null;
    }
    if (!INSIDE_PSI_MEMBER.getValue(location)) {
        return 0;
    }
    if (element.isPhysical()) {
        position = PHYSICAL_POSITION.getValue(location);
    }
    final PsiMember member = PsiTreeUtil.getContextOfType(PsiTreeUtil.findCommonContext(position, element), PsiMember.class, false);
    if (member instanceof PsiClass)
        return 1;
    if (member != null)
        return 2;
    return 0;
}
Also used : PsiClass(com.intellij.psi.PsiClass) PsiElement(com.intellij.psi.PsiElement) PsiMember(com.intellij.psi.PsiMember)

Example 19 with PsiMember

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

the class MemberSelectionTable method setVisibilityIcon.

@Override
protected void setVisibilityIcon(MemberInfo memberInfo, RowIcon icon) {
    PsiMember member = memberInfo.getMember();
    PsiModifierList modifiers = member != null ? member.getModifierList() : null;
    if (modifiers != null) {
        VisibilityIcons.setVisibilityIcon(modifiers, icon);
    } else {
        icon.setIcon(IconUtil.getEmptyIcon(true), VISIBILITY_ICON_POSITION);
    }
}
Also used : PsiMember(com.intellij.psi.PsiMember) PsiModifierList(com.intellij.psi.PsiModifierList)

Aggregations

PsiMember (com.intellij.psi.PsiMember)19 PsiElement (com.intellij.psi.PsiElement)5 PsiMethod (com.intellij.psi.PsiMethod)5 PsiClass (com.intellij.psi.PsiClass)4 MemberInfo (com.intellij.refactoring.util.classMembers.MemberInfo)4 Project (com.intellij.openapi.project.Project)3 ArrayList (java.util.ArrayList)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 PsiModifierList (com.intellij.psi.PsiModifierList)2 MemberSelectionPanel (com.intellij.refactoring.ui.MemberSelectionPanel)2 MemberSelectionTable (com.intellij.refactoring.ui.MemberSelectionTable)2 RowIcon (com.intellij.ui.RowIcon)2 ContainerUtil (com.intellij.util.containers.ContainerUtil)2 List (java.util.List)2 AnalysisScope (com.intellij.analysis.AnalysisScope)1 CodeInsightBundle (com.intellij.codeInsight.CodeInsightBundle)1 CodeInsightSettings (com.intellij.codeInsight.CodeInsightSettings)1 CamelHumpMatcher (com.intellij.codeInsight.completion.impl.CamelHumpMatcher)1 LookupManager (com.intellij.codeInsight.lookup.LookupManager)1