Search in sources :

Example 91 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-community by JetBrains.

the class GenericRepositoryUtil method substituteTemplateVariables.

public static String substituteTemplateVariables(String s, Collection<TemplateVariable> variables, boolean escape) throws Exception {
    Map<String, String> lookup = new HashMap<>();
    for (TemplateVariable v : variables) {
        lookup.put(v.getName(), v.getValue());
    }
    StringBuffer sb = new StringBuffer();
    Matcher m = PLACEHOLDER_PATTERN.matcher(s);
    while (m.find()) {
        String name = m.group(1);
        String replacement = lookup.get(name);
        if (replacement == null) {
            throw new Exception((String.format("Template variable '%s' is undefined", name)));
        }
        // TODO: add proper escape|unescape property to template variables
        if (escape && !name.equals(GenericRepository.SERVER_URL)) {
            m.appendReplacement(sb, URLEncoder.encode(replacement, "utf-8"));
        } else {
            m.appendReplacement(sb, replacement);
        }
    }
    return m.appendTail(sb).toString();
}
Also used : HashMap(com.intellij.util.containers.HashMap) Matcher(java.util.regex.Matcher)

Example 92 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-community by JetBrains.

the class ExtractUtil method mustAddVariableDeclaration.

/*
  To declare or not a variable to which method call result will be assigned.
   */
private static List<VariableInfo> mustAddVariableDeclaration(@NotNull GrStatement[] statements, @NotNull VariableInfo[] vars) {
    Map<String, VariableInfo> names = new HashMap<>();
    for (VariableInfo var : vars) {
        names.put(var.getName(), var);
    }
    List<VariableInfo> result = new ArrayList<>();
    for (GrStatement statement : statements) {
        if (statement instanceof GrVariableDeclaration) {
            GrVariableDeclaration declaration = (GrVariableDeclaration) statement;
            for (GrVariable variable : declaration.getVariables()) {
                final VariableInfo removed = names.remove(variable.getName());
                if (removed != null) {
                    result.add(removed);
                }
            }
        }
    }
    for (String varName : names.keySet()) {
        if (ResolveUtil.resolveProperty(statements[statements.length - 1], varName) == null) {
            result.add(names.get(varName));
        }
    }
    return result;
}
Also used : GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) HashMap(com.intellij.util.containers.HashMap) VariableInfo(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.VariableInfo) GrStatement(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)

Example 93 with HashMap

use of com.intellij.util.containers.HashMap in project intellij-community by JetBrains.

the class RenameTo method applyFix.

@SuppressWarnings({ "SSBasedInspection" })
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    DictionarySuggestionProvider provider = findProvider();
    if (provider != null) {
        provider.setActive(true);
    }
    Editor editor = getEditorFromFocus();
    HashMap<String, Object> map = new HashMap<>();
    PsiElement psiElement = descriptor.getPsiElement();
    if (psiElement == null)
        return;
    PsiFile containingFile = psiElement.getContainingFile();
    if (editor == null) {
        editor = InjectedLanguageUtil.openEditorFor(containingFile, project);
    }
    if (editor == null)
        return;
    if (editor instanceof EditorWindow) {
        map.put(CommonDataKeys.EDITOR.getName(), editor);
        map.put(CommonDataKeys.PSI_ELEMENT.getName(), psiElement);
    } else if (ApplicationManager.getApplication().isUnitTestMode()) {
        // TextEditorComponent / FiledEditorManagerImpl give away the data in real life
        map.put(CommonDataKeys.PSI_ELEMENT.getName(), new TextEditorPsiDataProvider().getData(CommonDataKeys.PSI_ELEMENT.getName(), editor, editor.getCaretModel().getCurrentCaret()));
    }
    final Boolean selectAll = editor.getUserData(RenameHandlerRegistry.SELECT_ALL);
    try {
        editor.putUserData(RenameHandlerRegistry.SELECT_ALL, true);
        DataContext dataContext = SimpleDataContext.getSimpleContext(map, DataManager.getInstance().getDataContext(editor.getComponent()));
        AnAction action = new RenameElementAction();
        AnActionEvent event = AnActionEvent.createFromAnAction(action, null, "", dataContext);
        action.actionPerformed(event);
        if (provider != null) {
            provider.setActive(false);
        }
    } finally {
        editor.putUserData(RenameHandlerRegistry.SELECT_ALL, selectAll);
    }
}
Also used : HashMap(com.intellij.util.containers.HashMap) EditorWindow(com.intellij.injected.editor.EditorWindow) RenameElementAction(com.intellij.refactoring.actions.RenameElementAction) TextEditorPsiDataProvider(com.intellij.openapi.fileEditor.impl.text.TextEditorPsiDataProvider) SimpleDataContext(com.intellij.openapi.actionSystem.impl.SimpleDataContext) PsiFile(com.intellij.psi.PsiFile) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement)

Example 94 with HashMap

use of com.intellij.util.containers.HashMap in project kotlin by JetBrains.

the class KotlinCallTreeStructure method collectNodeDescriptors.

protected Object[] collectNodeDescriptors(HierarchyNodeDescriptor descriptor, Map<PsiReference, PsiElement> referencesToCalleeElements, PsiClass basePsiClass) {
    HashMap<PsiElement, HierarchyNodeDescriptor> declarationToDescriptorMap = new HashMap<PsiElement, HierarchyNodeDescriptor>();
    for (Map.Entry<PsiReference, PsiElement> refToCallee : referencesToCalleeElements.entrySet()) {
        PsiReference ref = refToCallee.getKey();
        PsiElement callee = refToCallee.getValue();
        if (basePsiClass != null && !isInScope(basePsiClass, callee, scopeType))
            continue;
        addNodeDescriptorForElement(ref, callee, declarationToDescriptorMap, descriptor, false);
    }
    return declarationToDescriptorMap.values().toArray(new Object[declarationToDescriptorMap.size()]);
}
Also used : HashMap(com.intellij.util.containers.HashMap) PsiReference(com.intellij.psi.PsiReference) HierarchyNodeDescriptor(com.intellij.ide.hierarchy.HierarchyNodeDescriptor) CallHierarchyNodeDescriptor(com.intellij.ide.hierarchy.call.CallHierarchyNodeDescriptor) HashMap(com.intellij.util.containers.HashMap) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement)

Example 95 with HashMap

use of com.intellij.util.containers.HashMap in project kotlin by JetBrains.

the class KotlinCallerMethodsTreeStructure method buildChildren.

@NotNull
@Override
protected Object[] buildChildren(@NotNull HierarchyNodeDescriptor descriptor) {
    final PsiElement element = getTargetElement(descriptor);
    KtElement codeBlockForLocalDeclaration = getEnclosingElementForLocalDeclaration(element);
    if (codeBlockForLocalDeclaration != null) {
        BindingContext bindingContext = ResolutionUtils.analyze((KtElement) element, BodyResolveMode.FULL);
        final Map<PsiReference, PsiElement> referencesToElements = new HashMap<PsiReference, PsiElement>();
        codeBlockForLocalDeclaration.accept(new CalleeReferenceVisitorBase(bindingContext, true) {

            @Override
            protected void processDeclaration(KtSimpleNameExpression reference, PsiElement declaration) {
                if (!declaration.equals(element))
                    return;
                //noinspection unchecked
                PsiElement container = PsiTreeUtil.getParentOfType(reference, KtNamedFunction.class, KtPropertyAccessor.class, KtClassOrObject.class);
                if (container instanceof KtPropertyAccessor) {
                    container = PsiTreeUtil.getParentOfType(container, KtProperty.class);
                }
                if (container != null) {
                    referencesToElements.put(ReferenceUtilKt.getMainReference(reference), container);
                }
            }
        });
        return collectNodeDescriptors(descriptor, referencesToElements, null);
    }
    SearchScope searchScope = getSearchScope(scopeType, basePsiClass);
    Map<PsiElement, HierarchyNodeDescriptor> methodToDescriptorMap = Maps.newHashMap();
    Object[] javaCallers = null;
    if (element instanceof PsiMethod) {
        javaCallers = javaTreeStructure.getChildElements(getJavaNodeDescriptor(descriptor));
        processPsiMethodCallers(Collections.singleton((PsiMethod) element), descriptor, methodToDescriptorMap, searchScope, true);
    }
    if (element instanceof KtNamedFunction || element instanceof KtSecondaryConstructor) {
        Collection<PsiMethod> lightMethods = LightClassUtil.INSTANCE.getLightClassMethods((KtFunction) element);
        processPsiMethodCallers(lightMethods, descriptor, methodToDescriptorMap, searchScope, false);
    }
    if (element instanceof KtProperty) {
        LightClassUtil.PropertyAccessorsPsiMethods propertyMethods = LightClassUtil.INSTANCE.getLightClassPropertyMethods((KtProperty) element);
        processPsiMethodCallers(propertyMethods, descriptor, methodToDescriptorMap, searchScope, false);
    }
    if (element instanceof KtClassOrObject) {
        KtPrimaryConstructor constructor = ((KtClassOrObject) element).getPrimaryConstructor();
        if (constructor != null) {
            PsiMethod lightMethod = LightClassUtil.INSTANCE.getLightClassMethod(constructor);
            processPsiMethodCallers(Collections.singleton(lightMethod), descriptor, methodToDescriptorMap, searchScope, false);
        } else {
            processKtClassOrObjectCallers((KtClassOrObject) element, descriptor, methodToDescriptorMap, searchScope);
        }
    }
    Object[] callers = methodToDescriptorMap.values().toArray(new Object[methodToDescriptorMap.size()]);
    return (javaCallers != null) ? ArrayUtil.mergeArrays(javaCallers, callers) : callers;
}
Also used : HashMap(com.intellij.util.containers.HashMap) BindingContext(org.jetbrains.kotlin.resolve.BindingContext) SearchScope(com.intellij.psi.search.SearchScope) LightClassUtil(org.jetbrains.kotlin.asJava.LightClassUtil) HierarchyNodeDescriptor(com.intellij.ide.hierarchy.HierarchyNodeDescriptor) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

HashMap (com.intellij.util.containers.HashMap)118 NotNull (org.jetbrains.annotations.NotNull)23 VirtualFile (com.intellij.openapi.vfs.VirtualFile)22 File (java.io.File)22 Nullable (org.jetbrains.annotations.Nullable)18 Map (java.util.Map)17 Module (com.intellij.openapi.module.Module)15 Project (com.intellij.openapi.project.Project)15 ArrayList (java.util.ArrayList)14 PsiElement (com.intellij.psi.PsiElement)11 HashSet (com.intellij.util.containers.HashSet)10 List (java.util.List)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 IOException (java.io.IOException)8 Pair (com.intellij.openapi.util.Pair)6 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)6 SearchScope (com.intellij.psi.search.SearchScope)6 Logger (com.intellij.openapi.diagnostic.Logger)5 PsiFile (com.intellij.psi.PsiFile)5 UsageInfo (com.intellij.usageView.UsageInfo)5