Search in sources :

Example 46 with PsiNamedElement

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

the class ResourceBundleStructureViewComponent method getData.

@Override
public Object getData(final String dataId) {
    if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
        return new ResourceBundleAsVirtualFile(myResourceBundle);
    } else if (PlatformDataKeys.FILE_EDITOR.is(dataId)) {
        return getFileEditor();
    } else if (ResourceBundle.ARRAY_DATA_KEY.is(dataId)) {
        return new ResourceBundle[] { myResourceBundle };
    } else if (IProperty.ARRAY_KEY.is(dataId)) {
        final Collection<ResourceBundleEditorViewElement> selectedElements = ((ResourceBundleEditor) getFileEditor()).getSelectedElements();
        if (selectedElements.isEmpty()) {
            return null;
        } else if (selectedElements.size() == 1) {
            return ContainerUtil.getFirstItem(selectedElements).getProperties();
        } else {
            return ContainerUtil.toArray(ContainerUtil.flatten(ContainerUtil.mapNotNull(selectedElements, (NullableFunction<ResourceBundleEditorViewElement, List<IProperty>>) element -> {
                final IProperty[] properties = element.getProperties();
                return properties == null ? null : ContainerUtil.newArrayList(properties);
            })), IProperty[]::new);
        }
    } else if (LangDataKeys.PSI_ELEMENT_ARRAY.is(dataId)) {
        final List<PsiElement> elements = new ArrayList<>();
        Collections.addAll(elements, getSelectedPsiFiles());
        final IProperty[] properties = (IProperty[]) getData(IProperty.ARRAY_KEY.getName());
        if (properties != null) {
            for (IProperty property : properties) {
                final PsiElement element = property.getPsiElement();
                if (element.isValid()) {
                    elements.add(element);
                }
            }
        }
        return elements.toArray(new PsiElement[elements.size()]);
    } else if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(dataId)) {
        if (getSelectedPsiFiles().length != 0) {
            return new ResourceBundleDeleteProvider();
        }
        final IProperty[] properties = IProperty.ARRAY_KEY.getData(this);
        if (properties != null && properties.length != 0) {
            return new PropertiesDeleteProvider(((ResourceBundleEditor) getFileEditor()).getPropertiesInsertDeleteManager(), properties);
        }
    } else if (UsageView.USAGE_TARGETS_KEY.is(dataId)) {
        final PsiElement[] chosenElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
        if (chosenElements != null) {
            final UsageTarget[] usageTargets = new UsageTarget[chosenElements.length];
            for (int i = 0; i < chosenElements.length; i++) {
                usageTargets[i] = new PsiElement2UsageTargetAdapter(chosenElements[i]);
            }
            return usageTargets;
        }
    } else if (PlatformDataKeys.COPY_PROVIDER.is(dataId)) {
        return new CopyProvider() {

            @Override
            public void performCopy(@NotNull final DataContext dataContext) {
                final PsiElement[] selectedPsiElements = (PsiElement[]) getData(LangDataKeys.PSI_ELEMENT_ARRAY.getName());
                if (selectedPsiElements != null) {
                    final List<String> names = new ArrayList<>(selectedPsiElements.length);
                    for (final PsiElement element : selectedPsiElements) {
                        if (element instanceof PsiNamedElement) {
                            names.add(((PsiNamedElement) element).getName());
                        }
                    }
                    CopyPasteManager.getInstance().setContents(new StringSelection(StringUtil.join(names, "\n")));
                }
            }

            @Override
            public boolean isCopyEnabled(@NotNull final DataContext dataContext) {
                return true;
            }

            @Override
            public boolean isCopyVisible(@NotNull final DataContext dataContext) {
                return true;
            }
        };
    }
    return super.getData(dataId);
}
Also used : PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PopupHandler(com.intellij.ui.PopupHandler) NonNls(org.jetbrains.annotations.NonNls) ResourceBundleEditorRenderer(com.intellij.lang.properties.editor.inspections.ResourceBundleEditorRenderer) ContainerUtil(com.intellij.util.containers.ContainerUtil) DeleteProvider(com.intellij.ide.DeleteProvider) ArrayList(java.util.ArrayList) PsiElement(com.intellij.psi.PsiElement) StringSelection(java.awt.datatransfer.StringSelection) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) ResourceBundle(com.intellij.lang.properties.ResourceBundle) PsiNamedElement(com.intellij.psi.PsiNamedElement) SafeDeleteHandler(com.intellij.refactoring.safeDelete.SafeDeleteHandler) Logger(com.intellij.openapi.diagnostic.Logger) StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) NullableFunction(com.intellij.util.NullableFunction) UsageTarget(com.intellij.usages.UsageTarget) com.intellij.openapi.actionSystem(com.intellij.openapi.actionSystem) UsageView(com.intellij.usages.UsageView) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) List(java.util.List) IProperty(com.intellij.lang.properties.IProperty) PsiUtilCore(com.intellij.psi.util.PsiUtilCore) CopyProvider(com.intellij.ide.CopyProvider) ResourceBundleDeleteProvider(com.intellij.lang.properties.projectView.ResourceBundleDeleteProvider) ContextHelpAction(com.intellij.ide.actions.ContextHelpAction) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) CopyPasteManager(com.intellij.openapi.ide.CopyPasteManager) CopyProvider(com.intellij.ide.CopyProvider) PsiNamedElement(com.intellij.psi.PsiNamedElement) ArrayList(java.util.ArrayList) ResourceBundleDeleteProvider(com.intellij.lang.properties.projectView.ResourceBundleDeleteProvider) StringSelection(java.awt.datatransfer.StringSelection) IProperty(com.intellij.lang.properties.IProperty) ArrayList(java.util.ArrayList) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) UsageTarget(com.intellij.usages.UsageTarget) PsiElement2UsageTargetAdapter(com.intellij.find.findUsages.PsiElement2UsageTargetAdapter) Collection(java.util.Collection) ResourceBundle(com.intellij.lang.properties.ResourceBundle)

Example 47 with PsiNamedElement

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

the class AntRenameTest method doTest.

private void doTest() throws Exception {
    final String filename = getTestName(true) + ".xml";
    VirtualFile vfile = VirtualFileManager.getInstance().findFileByUrl("file://" + getTestDataPath() + filename);
    String text = FileDocumentManager.getInstance().getDocument(vfile).getText();
    final int off = text.indexOf("<ren>");
    text = text.replace("<ren>", "");
    configureFromFileText(filename, text);
    assertNotNull(myFile);
    PsiElement element = TargetElementUtil.getInstance().findTargetElement(getEditor(), TargetElementUtil.REFERENCED_ELEMENT_ACCEPTED | TargetElementUtil.ELEMENT_NAME_ACCEPTED, off);
    assertNotNull(element);
    assertTrue(element instanceof PsiNamedElement);
    final RenameRefactoring rename = RefactoringFactory.getInstance(getProject()).createRename(element, ((PsiNamedElement) element).getName() + "-after");
    rename.setSearchInComments(false);
    rename.setSearchInNonJavaFiles(false);
    rename.run();
    checkResultByFile(filename + "-after");
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RenameRefactoring(com.intellij.refactoring.RenameRefactoring) PsiNamedElement(com.intellij.psi.PsiNamedElement) PsiElement(com.intellij.psi.PsiElement)

Example 48 with PsiNamedElement

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

the class AutomaticRenamingDialog method createCenterPanel.

@Override
protected JComponent createCenterPanel() {
    myTable.setModel(myTableModel);
    myTableModel.getSpaceAction().register();
    myTableModel.addTableModelListener(e -> handleChanges());
    myTable.addMouseListener(new PopupHandler() {

        @Override
        public void invokePopup(Component comp, int x, int y) {
            final int[] selectionRows = myTable.getSelectedRows();
            if (selectionRows != null) {
                compoundPopup().show(comp, x, y);
            }
        }
    });
    final TableColumnModel columnModel = myTable.getColumnModel();
    columnModel.getColumn(CHECK_COLUMN).setCellRenderer(new BooleanTableCellRenderer());
    TableUtil.setupCheckboxColumn(columnModel.getColumn(CHECK_COLUMN));
    columnModel.getColumn(NEW_NAME_COLUMN).setCellEditor(new StringTableCellEditor(myProject));
    mySelectAllButton.addActionListener(e -> {
        for (int i = 0; i < myShouldRename.length; i++) {
            myShouldRename[i] = true;
        }
        fireDataChanged();
    });
    myUnselectAllButton.addActionListener(e -> {
        for (int i = 0; i < myShouldRename.length; i++) {
            myShouldRename[i] = false;
        }
        fireDataChanged();
    });
    myListSelectionListener = e -> {
        myUsageFileLabel.setText("");
        int index = myTable.getSelectionModel().getLeadSelectionIndex();
        if (index != -1) {
            PsiNamedElement element = myRenames[index];
            UsageInfo usageInfo = new UsageInfo(element);
            myUsagePreviewPanel.updateLayout(Collections.singletonList(usageInfo));
            final PsiFile containingFile = element.getContainingFile();
            if (containingFile != null) {
                final VirtualFile virtualFile = containingFile.getVirtualFile();
                if (virtualFile != null) {
                    myUsageFileLabel.setText(virtualFile.getName());
                }
            }
        } else {
            myUsagePreviewPanel.updateLayout(null);
        }
    };
    myTable.getSelectionModel().addListSelectionListener(myListSelectionListener);
    myPanelForPreview.add(myUsagePreviewPanel, BorderLayout.CENTER);
    myUsagePreviewPanel.updateLayout(null);
    myPanelForPreview.add(myUsageFileLabel, BorderLayout.NORTH);
    mySplitPane.setDividerLocation(0.5);
    GuiUtils.replaceJSplitPaneWithIDEASplitter(myPanel);
    if (myTableModel.getRowCount() != 0) {
        myTable.getSelectionModel().addSelectionInterval(0, 0);
    }
    return myPanel;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PopupHandler(com.intellij.ui.PopupHandler) PsiNamedElement(com.intellij.psi.PsiNamedElement) TableColumnModel(javax.swing.table.TableColumnModel) StringTableCellEditor(com.intellij.refactoring.ui.StringTableCellEditor) PsiFile(com.intellij.psi.PsiFile) BooleanTableCellRenderer(com.intellij.ui.BooleanTableCellRenderer) UsageInfo(com.intellij.usageView.UsageInfo)

Example 49 with PsiNamedElement

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

the class AutomaticRenamingDialog method populateData.

private void populateData() {
    final Map<PsiNamedElement, String> renames = myRenamer.getRenames();
    List<PsiNamedElement> temp = new ArrayList<>();
    for (final PsiNamedElement namedElement : renames.keySet()) {
        final String newName = renames.get(namedElement);
        if (newName != null)
            temp.add(namedElement);
    }
    myRenames = temp.toArray(new PsiNamedElement[temp.size()]);
    Arrays.sort(myRenames, (e1, e2) -> Comparing.compare(e1.getName(), e2.getName()));
    myNewNames = new String[myRenames.length];
    for (int i = 0; i < myNewNames.length; i++) {
        myNewNames[i] = renames.get(myRenames[i]);
    }
    myShouldRename = new boolean[myRenames.length];
    if (myRenamer.isSelectedByDefault()) {
        for (int i = 0; i < myShouldRename.length; i++) {
            myShouldRename[i] = true;
        }
    }
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement)

Example 50 with PsiNamedElement

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

the class ReplacementContext method getNewName2PatternNameMap.

public Map<String, String> getNewName2PatternNameMap() {
    Map<String, String> newNameToSearchPatternNameMap = new HashMap<>(1);
    final Map<String, MatchResult> variableMap = replacementInfo.getVariableMap();
    if (variableMap != null) {
        for (String s : variableMap.keySet()) {
            final MatchResult matchResult = replacementInfo.getVariableMap().get(s);
            PsiElement match = matchResult.getMatch();
            match = StructuralSearchUtil.getParentIfIdentifier(match);
            if (match instanceof PsiNamedElement) {
                final String name = ((PsiNamedElement) match).getName();
                newNameToSearchPatternNameMap.put(name, s);
            }
        }
    }
    return newNameToSearchPatternNameMap;
}
Also used : HashMap(java.util.HashMap) PsiNamedElement(com.intellij.psi.PsiNamedElement) MatchResult(com.intellij.structuralsearch.MatchResult) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiNamedElement (com.intellij.psi.PsiNamedElement)65 PsiElement (com.intellij.psi.PsiElement)29 NotNull (org.jetbrains.annotations.NotNull)16 ArrayList (java.util.ArrayList)14 PsiFile (com.intellij.psi.PsiFile)8 LookupElement (com.intellij.codeInsight.lookup.LookupElement)7 Pair (com.intellij.openapi.util.Pair)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Project (com.intellij.openapi.project.Project)4 UsageInfo (com.intellij.usageView.UsageInfo)4 Nullable (org.jetbrains.annotations.Nullable)4 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)3 PsiReference (com.intellij.psi.PsiReference)3 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)3 PyClass (com.jetbrains.python.psi.PyClass)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 BuildFile (com.google.idea.blaze.base.lang.buildfile.psi.BuildFile)2 LoadedSymbol (com.google.idea.blaze.base.lang.buildfile.psi.LoadedSymbol)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)2