com.intellij.refactoring.rename.RenamePsiElementProcessor Java Examples

The following examples show how to use com.intellij.refactoring.rename.RenamePsiElementProcessor. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LombokElementRenameHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file, DataContext dataContext) {
  PsiElement element = PsiElementRenameHandler.getElement(dataContext);
  if (null == element) {
    element = BaseRefactoringAction.getElementAtCaret(editor, file);
  }

  if (null != element) {
    RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(element);
    element = processor.substituteElementToRename(element, editor);
  }

  if (null != element) {
    editor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
    PsiElement nameSuggestionContext = file.getViewProvider().findElementAt(editor.getCaretModel().getOffset());
    PsiElementRenameHandler.invoke(element, project, nameSuggestionContext, editor);
  }
}
 
Example #2
Source File: MemberInplaceRenamer.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean appendAdditionalElement(Collection<PsiReference> refs, Collection<Pair<PsiElement, TextRange>> stringUsages) {
  boolean showChooser = super.appendAdditionalElement(refs, stringUsages);
  PsiNamedElement variable = getVariable();
  if (variable != null) {
    final PsiElement substituted = getSubstituted();
    if (substituted != null) {
      appendAdditionalElement(stringUsages, variable, substituted);
      RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(substituted);
      final HashMap<PsiElement, String> allRenames = new HashMap<PsiElement, String>();
      PsiFile currentFile = PsiDocumentManager.getInstance(myProject).getPsiFile(myEditor.getDocument());
      processor.prepareRenaming(substituted, "", allRenames, new LocalSearchScope(currentFile));
      for (PsiElement element : allRenames.keySet()) {
        appendAdditionalElement(stringUsages, variable, element);
      }
    }
  }
  return showChooser;
}
 
Example #3
Source File: MemberInplaceRenamer.java    From consulo with Apache License 2.0 6 votes vote down vote up
protected void performRenameInner(PsiElement element, String newName) {
  final RenamePsiElementProcessor elementProcessor = RenamePsiElementProcessor.forElement(element);
  final RenameProcessor
    renameProcessor = new RenameProcessor(myProject, element, newName,
                                          elementProcessor.isToSearchInComments(element),
                                          elementProcessor.isToSearchForTextOccurrences(element)){
    @Override
    public void doRun() {
      try {
        super.doRun();
      }
      finally {
        restoreCaretOffsetAfterRename();
      }
    }
  };
  for (AutomaticRenamerFactory factory : Extensions.getExtensions(AutomaticRenamerFactory.EP_NAME)) {
    if (factory.getOptionName() != null && factory.isApplicable(element)) {
      renameProcessor.addRenamerFactory(factory);
    }
  }
  renameProcessor.run();
}
 
Example #4
Source File: LSPRenameHandler.java    From lsp4intellij with Apache License 2.0 5 votes vote down vote up
private InplaceRefactoring doRename(PsiElement elementToRename, Editor editor) {
    if (elementToRename instanceof PsiNameIdentifierOwner) {
        RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(elementToRename);
        if (processor.isInplaceRenameSupported()) {
            StartMarkAction startMarkAction = StartMarkAction.canStart(elementToRename.getProject());
            if (startMarkAction == null || (processor.substituteElementToRename(elementToRename, editor)
                    == elementToRename)) {
                processor.substituteElementToRename(elementToRename, editor, new Pass<PsiElement>() {
                    @Override
                    public void pass(PsiElement element) {
                        MemberInplaceRenamer renamer = createMemberRenamer(element,
                                (PsiNameIdentifierOwner) elementToRename, editor);
                        boolean startedRename = renamer.performInplaceRename();
                        if (!startedRename) {
                            performDialogRename(editor);
                        }
                    }
                });
                return null;
            }
        } else {
            InplaceRefactoring inplaceRefactoring = editor.getUserData(InplaceRefactoring.INPLACE_RENAMER);
            if ((inplaceRefactoring instanceof MemberInplaceRenamer)) {
                TemplateState templateState = TemplateManagerImpl
                        .getTemplateState(InjectedLanguageUtil.getTopLevelEditor(editor));
                if (templateState != null) {
                    templateState.gotoEnd(true);
                }
            }
        }
    }
    performDialogRename(editor);
    return null;
}
 
Example #5
Source File: RenameRefactoringTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenameSuggestionForBuildFile() {
  BuildFile buildFile = createBuildFile(new WorkspacePath("java/com/google/BUILD"));
  RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(buildFile);
  RenameDialog dialog = processor.createRenameDialog(getProject(), buildFile, buildFile, null);
  String[] suggestions = dialog.getSuggestedNames();
  assertThat(suggestions[0]).isEqualTo("BUILD");
}
 
Example #6
Source File: RenameRefactoringTest.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Test
public void testRenameSuggestionForSkylarkFile() {
  BuildFile buildFile =
      createBuildFile(new WorkspacePath("java/com/google/tools/build_defs.bzl"));
  RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(buildFile);
  RenameDialog dialog = processor.createRenameDialog(getProject(), buildFile, buildFile, null);
  String[] suggestions = dialog.getSuggestedNames();
  assertThat(suggestions[0]).isEqualTo("build_defs.bzl");
}
 
Example #7
Source File: MemberInplaceRenameHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
public InplaceRefactoring doRename(@Nonnull final PsiElement elementToRename, final Editor editor, final DataContext dataContext) {
  if (elementToRename instanceof PsiNameIdentifierOwner) {
    final RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(elementToRename);
    if (processor.isInplaceRenameSupported()) {
      final StartMarkAction startMarkAction = StartMarkAction.canStart(elementToRename.getProject());
      if (startMarkAction == null || processor.substituteElementToRename(elementToRename, editor) == elementToRename) {
        processor.substituteElementToRename(elementToRename, editor, new Pass<PsiElement>() {
          @Override
          public void pass(PsiElement element) {
            final MemberInplaceRenamer renamer = createMemberRenamer(element, (PsiNameIdentifierOwner)elementToRename, editor);
            boolean startedRename = renamer.performInplaceRename();
            if (!startedRename) {
              performDialogRename(elementToRename, editor, dataContext);
            }
          }
        });
        return null;
      }
      else {
        final InplaceRefactoring inplaceRefactoring = editor.getUserData(InplaceRefactoring.INPLACE_RENAMER);
        if (inplaceRefactoring != null && inplaceRefactoring.getClass() == MemberInplaceRenamer.class) {
          final TemplateState templateState = TemplateManagerImpl.getTemplateState(InjectedLanguageUtil.getTopLevelEditor(editor));
          if (templateState != null) {
            templateState.gotoEnd(true);
          }
        }
      }
    }
  }
  performDialogRename(elementToRename, editor, dataContext);
  return null;
}
 
Example #8
Source File: LombokElementRenameHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public InplaceRefactoring doRename(@NotNull PsiElement elementToRename, @NotNull Editor editor, @Nullable DataContext dataContext) {
  RenamePsiElementProcessor processor = RenamePsiElementProcessor.forElement(elementToRename);
  PsiElement actualElem = processor.substituteElementToRename(elementToRename, editor);
  return super.doRename(actualElem, editor, dataContext);
}
 
Example #9
Source File: CodeInsightTestFixtureImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void renameElement(final PsiElement element, final String newName, final boolean searchInComments, final boolean searchTextOccurrences) {
  final PsiElement substitution = RenamePsiElementProcessor.forElement(element).substituteElementToRename(element, myEditor);
  if (substitution == null) return;
  new RenameProcessor(getProject(), substitution, newName, searchInComments, searchTextOccurrences).run();
}