Esempio n. 1
0
  public static void doI18nSelectedString(
      final @NotNull Project project,
      final @NotNull Editor editor,
      final @NotNull PsiFile psiFile,
      final @NotNull I18nQuickFixHandler handler) {
    try {
      handler.checkApplicability(psiFile, editor);
    } catch (IncorrectOperationException ex) {
      CommonRefactoringUtil.showErrorHint(
          project, editor, ex.getMessage(), CodeInsightBundle.message("i18nize.error.title"), null);
      return;
    }

    final JavaI18nizeQuickFixDialog dialog = handler.createDialog(project, editor, psiFile);
    if (dialog == null) return;
    dialog.show();
    if (!dialog.isOK()) return;

    if (!CodeInsightUtilBase.prepareFileForWrite(psiFile)) return;
    final Collection<PropertiesFile> propertiesFiles = dialog.getAllPropertiesFiles();
    for (PropertiesFile file : propertiesFiles) {
      if (!CodeInsightUtilBase.prepareFileForWrite(file.getContainingFile())) return;
    }

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                CommandProcessor.getInstance()
                    .executeCommand(
                        project,
                        new Runnable() {
                          public void run() {
                            try {
                              handler.performI18nization(
                                  psiFile,
                                  editor,
                                  dialog.getLiteralExpression(),
                                  propertiesFiles,
                                  dialog.getKey(),
                                  dialog.getValue(),
                                  dialog.getI18nizedText(),
                                  dialog.getParameters(),
                                  dialog.getPropertyCreationHandler());
                            } catch (IncorrectOperationException e) {
                              LOG.error(e);
                            }
                          }
                        },
                        CodeInsightBundle.message("quickfix.i18n.command.name"),
                        project);
              }
            });
  }
Esempio n. 2
0
  public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    PsiStatement anchorStatement = PsiTreeUtil.getParentOfType(element, PsiStatement.class);
    LOG.assertTrue(anchorStatement != null);
    Editor editor = getEditor(project, element);
    if (editor == null) return;
    PsiFile file = element.getContainingFile();
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    Document document = documentManager.getDocument(file);
    if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
    PsiElement[] elements = {anchorStatement};
    PsiElement prev = PsiTreeUtil.skipSiblingsBackward(anchorStatement, PsiWhiteSpace.class);
    if (prev instanceof PsiComment
        && SuppressManager.getInstance().getSuppressedInspectionIdsIn(prev) != null) {
      elements = new PsiElement[] {prev, anchorStatement};
    }
    try {
      TextRange textRange = new JavaWithIfSurrounder().surroundElements(project, editor, elements);
      if (textRange == null) return;

      @NonNls String newText = myText + " != null";
      document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), newText);
      editor.getCaretModel().moveToOffset(textRange.getEndOffset() + newText.length());
      editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);

    } catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }
 @Override
 public void invoke(@NotNull final Project project, final Editor editor, PsiFile file) {
   if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
   if (candidates.size() == 1) {
     final PsiMethod toImport = candidates.get(0);
     doImport(toImport);
   } else {
     chooseAndImport(editor, project);
   }
 }
 @Override
 public void invoke(
     @NotNull Project project,
     @NotNull PsiFile file,
     @Nullable("is null when called from inspection") Editor editor,
     @NotNull PsiElement startElement,
     @NotNull PsiElement endElement) {
   if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
   addTypeCast(project, (PsiExpression) startElement, myType);
 }
  @Override
  public void invoke(@NotNull Project project, Editor editor, PsiFile file) {
    if (!CodeInsightUtilBase.prepareFileForWrite(myMethod.getContainingFile())) return;

    try {
      PsiUtil.setModifierProperty(myMethod, PsiModifier.ABSTRACT, false);
      CreateFromUsageUtils.setupMethodBody(myMethod);
      CreateFromUsageUtils.setupEditor(myMethod, editor);
    } catch (IncorrectOperationException e) {
      LOG.error(e);
    }
  }
Esempio n. 6
0
 public void applyFix(final Project project, final PsiFile file, final Editor editor) {
   if (!CodeInsightUtilBase.prepareFileForWrite(myClass.getContainingFile())) return;
   PsiCodeBlock body;
   if (myClass.isInterface() && (body = myMethod.getBody()) != null) body.delete();
   for (String exception : myExceptions) {
     PsiUtil.addException(myMethod, exception);
   }
   PsiMethod method = (PsiMethod) myClass.add(myMethod);
   method = (PsiMethod) method.replace(reformat(project, method));
   if (editor != null) {
     GenerateMembersUtil.positionCaret(editor, method, true);
   }
 }
  public void applyFix(
      @NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    PsiElement element = descriptor.getPsiElement();
    if (element == null) return;
    final PsiFile myFile = element.getContainingFile();

    if (!CodeInsightUtilBase.prepareFileForWrite(myFile)) return;

    ApplicationManager.getApplication()
        .invokeLater(
            new Runnable() {
              public void run() {
                chooseDirectoryAndMove(project, myFile);
              }
            });
  }
 @Override
 public void invoke(
     @NotNull Project project,
     @NotNull PsiFile file,
     @Nullable("is null when called from inspection") Editor editor,
     @NotNull PsiElement startElement,
     @NotNull PsiElement endElement) {
   if (!CodeInsightUtilBase.prepareFileForWrite(startElement.getContainingFile())) {
     return;
   }
   PsiElement newPsi =
       GosuPsiParseUtil.parseIdentifierOrTokenOrRelativeType(_correctName, startElement);
   newPsi = findIdentifierOrToken(newPsi);
   PsiElement oldPsi = findIdentifierOrToken(startElement);
   oldPsi.replace(newPsi);
 }
 @Override
 public void invoke(
     @NotNull Project project,
     @NotNull PsiFile file,
     @Nullable("is null when called from inspection") Editor editor,
     @NotNull PsiElement startElement,
     @NotNull PsiElement endElement) {
   final PsiClass myClass = (PsiClass) startElement;
   if (!CodeInsightUtilBase.prepareFileForWrite(myClass.getContainingFile())) return;
   PsiCodeBlock body;
   if (myClass.isInterface() && (body = myMethodPrototype.getBody()) != null) body.delete();
   for (String exception : myExceptions) {
     PsiUtil.addException(myMethodPrototype, exception);
   }
   PsiMethod method = (PsiMethod) myClass.add(myMethodPrototype);
   method = (PsiMethod) method.replace(reformat(project, method));
   if (editor != null) {
     GenerateMembersUtil.positionCaret(editor, method, true);
   }
 }
  public void invoke(
      @NotNull Project project, @NotNull final Editor editor, @NotNull PsiFile file) {
    if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;

    LookupManager.getInstance(project).hideActiveLookup();

    final CharSequence charsSequence = editor.getDocument().getCharsSequence();

    final CompletionData data = computeData(editor, charsSequence);
    String currentPrefix = data.myPrefix;

    final CompletionState completionState = getCompletionState(editor);

    String oldPrefix = completionState.oldPrefix;
    CompletionVariant lastProposedVariant = completionState.lastProposedVariant;

    if (lastProposedVariant == null
        || oldPrefix == null
        || !new CamelHumpMatcher(oldPrefix).prefixMatches(currentPrefix)
        || // oldPrefix.length() == 0 ||
        !currentPrefix.equals(lastProposedVariant.variant)) {
      // we are starting over
      oldPrefix = currentPrefix;
      completionState.oldPrefix = oldPrefix;
      lastProposedVariant = null;
    }

    CompletionVariant nextVariant =
        computeNextVariant(editor, oldPrefix, lastProposedVariant, data);
    if (nextVariant == null) return;

    int replacementEnd = data.startOffset + data.myWordUnderCursor.length();
    editor.getDocument().replaceString(data.startOffset, replacementEnd, nextVariant.variant);
    editor.getCaretModel().moveToOffset(data.startOffset + nextVariant.variant.length());
    completionState.lastProposedVariant = nextVariant;
    highlightWord(editor, nextVariant, project, data);
  }
  @Override
  public void invoke(
      @NotNull Project project,
      @NotNull PsiFile file,
      @Nullable("is null when called from inspection") Editor editor,
      @NotNull PsiElement startElement,
      @NotNull PsiElement endElement) {
    if (!CodeInsightUtilBase.prepareFileForWrite(startElement.getContainingFile())) {
      return;
    }
    if (!(startElement instanceof GosuMethodImpl)) {
      return;
    }
    String src = startElement.getText();
    String property = isGetter ? "property get" : "property set";
    String newMethodName = methodName.substring(3);
    src = src.replace("function", property);
    src = src.replaceFirst(methodName, newMethodName);
    PsiElement stub = GosuPsiParseUtil.parseProgramm(src, startElement, file.getManager(), null);

    PsiElement newMethod =
        new PsiMatcherImpl(stub).descendant(hasClass(GosuMethodImpl.class)).getElement();
    startElement.replace(newMethod);
  }
 @Override
 public void invoke(@NotNull Project project, Editor editor, PsiFile file)
     throws IncorrectOperationException {
   if (!CodeInsightUtilBase.prepareFileForWrite(file)) return;
   changeNewOperatorType(myExpression, myType, editor);
 }
Esempio n. 13
0
  @Override
  public void invoke(
      @NotNull Project project,
      @NotNull PsiFile file,
      @Nullable("is null when called from inspection") Editor editor,
      @NotNull PsiElement startElement,
      @NotNull PsiElement endElement) {
    final PsiModifierList myModifierList = (PsiModifierList) startElement;
    final PsiVariable variable = myVariable == null ? null : myVariable.getElement();
    if (!CodeInsightUtilBase.preparePsiElementForWrite(myModifierList)) return;
    final List<PsiModifierList> modifierLists = new ArrayList<PsiModifierList>();
    final PsiFile containingFile = myModifierList.getContainingFile();
    final PsiModifierList modifierList;
    if (variable != null && variable.isValid()) {
      ApplicationManager.getApplication()
          .runWriteAction(
              new Runnable() {
                public void run() {
                  try {
                    variable.normalizeDeclaration();
                  } catch (IncorrectOperationException e) {
                    LOG.error(e);
                  }
                }
              });
      modifierList = variable.getModifierList();
      assert modifierList != null;
    } else {
      modifierList = myModifierList;
    }
    PsiElement owner = modifierList.getParent();
    if (owner instanceof PsiMethod) {
      PsiModifierList copy = (PsiModifierList) myModifierList.copy();
      changeModifierList(copy);
      final int accessLevel = PsiUtil.getAccessLevel(copy);

      OverridingMethodsSearch.search((PsiMethod) owner, owner.getResolveScope(), true)
          .forEach(
              new PsiElementProcessorAdapter<PsiMethod>(
                  new PsiElementProcessor<PsiMethod>() {
                    public boolean execute(@NotNull PsiMethod inheritor) {
                      PsiModifierList list = inheritor.getModifierList();
                      if (inheritor.getManager().isInProject(inheritor)
                          && PsiUtil.getAccessLevel(list) < accessLevel) {
                        modifierLists.add(list);
                      }
                      return true;
                    }
                  }));
    }

    if (!CodeInsightUtilBase.prepareFileForWrite(containingFile)) return;

    if (!modifierLists.isEmpty()) {
      if (Messages.showYesNoDialog(
              project,
              QuickFixBundle.message("change.inheritors.visibility.warning.text"),
              QuickFixBundle.message("change.inheritors.visibility.warning.title"),
              Messages.getQuestionIcon())
          == DialogWrapper.OK_EXIT_CODE) {
        ApplicationManager.getApplication()
            .runWriteAction(
                new Runnable() {
                  public void run() {
                    if (!CodeInsightUtilBase.preparePsiElementsForWrite(modifierLists)) {
                      return;
                    }

                    for (final PsiModifierList modifierList : modifierLists) {
                      changeModifierList(modifierList);
                    }
                  }
                });
      }
    }

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              public void run() {
                changeModifierList(modifierList);
                UndoUtil.markPsiFileForUndo(containingFile);
              }
            });
  }