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();
}
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;
}
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);
}
}
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()]);
}
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;
}
Aggregations