Java Code Examples for com.intellij.codeInspection.ProblemDescriptor#getPsiElement()

The following examples show how to use com.intellij.codeInspection.ProblemDescriptor#getPsiElement() . 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: LegacyClassesForIdeQuickFix.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {

    PsiElement psiElement = descriptor.getPsiElement();
    if (DumbService.isDumb(project)) {
        showIsInDumpModeMessage(project, psiElement);
        return;
    }

    if (psiElement instanceof ClassReference) {
        ClassReference classReference = (ClassReference) psiElement;
        String fqn = classReference.getFQN();
        if (fqn != null) {
            String replacementFQN = LegacyClassesForIDEIndex.findReplacementClass(project, fqn);
            if (replacementFQN != null) {
                try {
                    classReference.replace(PhpPsiElementFactory.createClassReference(project, replacementFQN));
                } catch (IncorrectOperationException e) {
                    showErrorMessage(project, "Could not replace class reference", psiElement);
                }
            }
        }
    }
}
 
Example 2
Source File: CreateMissingTranslationQuickFix.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
/**
 * Called to apply the fix.
 *
 * @param project    {@link Project}
 * @param descriptor problem reported by the tool which provided this quick fix action
 */
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement psiElement = descriptor.getPsiElement();
    if (psiElement instanceof StringLiteralExpression) {
        StringLiteralExpression stringElement = (StringLiteralExpression) psiElement;
        String contents = stringElement.getContents();

        String fileName = TranslationUtil.extractResourceFilenameFromTranslationString(contents);
        String key = TranslationUtil.extractTranslationKeyTranslationString(contents);
        if (fileName != null) {
            PsiElement[] elementsForKey = ResourcePathIndex.findElementsForKey(project, fileName);
            if (elementsForKey.length > 0) {
                // TranslationUtil.add();
            }
        }
    }
}
 
Example 3
Source File: CsvValidationInspection.java    From intellij-csv-validator with Apache License 2.0 6 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
        PsiElement element = descriptor.getPsiElement();
        Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
        List<Integer> quotePositions = new ArrayList<>();

        int quotePosition = CsvIntentionHelper.getOpeningQuotePosition(element);
        if (quotePosition != -1) {
            quotePositions.add(quotePosition);
        }
        PsiElement endSeparatorElement = CsvIntentionHelper.findQuotePositionsUntilSeparator(element, quotePositions, true);
        if (endSeparatorElement == null) {
            quotePositions.add(document.getTextLength());
        } else {
            quotePositions.add(endSeparatorElement.getTextOffset());
        }
        String text = CsvIntentionHelper.addQuotes(document.getText(), quotePositions);
        document.setText(text);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
 
Example 4
Source File: InconsistentLineSeparatorsInspection.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void applyFix(@Nonnull Project project, @Nonnull ProblemDescriptor descriptor) {
  final PsiElement psiElement = descriptor.getPsiElement();
  if (!(psiElement instanceof PsiFile)) {
    return;
  }

  final String lineSeparator = CodeStyleFacade.getInstance(project).getLineSeparator();
  if (lineSeparator == null) {
    return;
  }

  final VirtualFile virtualFile = ((PsiFile)psiElement).getVirtualFile();
  if (virtualFile != null) {
    AbstractConvertLineSeparatorsAction.changeLineSeparators(project, virtualFile, lineSeparator);
  }
}
 
Example 5
Source File: RemoveUnusedAtFixBase.java    From bamboo-soy with Apache License 2.0 5 votes vote down vote up
@Override
public void applyFix(@NotNull Project project,
    @NotNull ProblemDescriptor descriptor) {
  final PsiElement element = descriptor.getPsiElement();
  if (!(element instanceof AtElementSingle)) {
    return;
  }

  // We expect the right concrete type here.
  runFix((T) element);
}
 
Example 6
Source File: CsvValidationInspection.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
        PsiElement element = descriptor.getPsiElement();
        Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
        CsvValueSeparator separator = CsvHelper.getValueSeparator(element.getContainingFile());
        String text = document.getText();
        document.setText(text.substring(0, element.getTextOffset()) + separator.getCharacter() + text.substring(element.getTextOffset()));
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
 
Example 7
Source File: CsvValidationInspection.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
        PsiElement element = descriptor.getPsiElement();
        Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
        document.setText(document.getText() + "\"");
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
 
Example 8
Source File: DeprecatedLoadQuickFix.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Override
public void applyFix(Project project, ProblemDescriptor descriptor) {
  PsiElement element = descriptor.getPsiElement();
  if (element instanceof StringLiteral) {
    fixLoadString(project, (StringLiteral) element);
  }
}
 
Example 9
Source File: CorrectClassNameCasingYamlLocalQuickFix.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement psiElement1 = descriptor.getPsiElement();
    YAMLKeyValue replacement = YamlPsiElementFactory.createFromText(
            project,
            YAMLKeyValue.class,
            "class: " + replacementFQN
    );

    if (replacement != null && replacement.getValue() != null) {
        psiElement1.replace(replacement.getValue());
    }
}
 
Example 10
Source File: DoctrineOrmRepositoryIntention.java    From idea-php-annotation-plugin with MIT License 5 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor)
{
    if (descriptor.getPsiElement() == null) return;
    if (descriptor.getPsiElement().getContainingFile() == null) return;

    invoke(project, null, descriptor.getPsiElement().getContainingFile());
}
 
Example 11
Source File: Fix.java    From CppTools with Apache License 2.0 5 votes vote down vote up
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
  final PsiElement psiElement = problemDescriptor.getPsiElement();
  final PsiFile psiFile = psiElement.getContainingFile();
  final Editor editor = FileEditorManager.getInstance(project).openTextEditor(
    new OpenFileDescriptor(project, psiFile.getVirtualFile(), psiElement.getTextOffset()), false
  );
  try {
    invoke(project, editor, psiFile);
  } catch (IncorrectOperationException e) {
    Logger.getInstance(getClass().getName()).error(e);
  }
}
 
Example 12
Source File: RemoveElementQuickFix.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor problemDescriptor) {
    PsiElement psiElement = problemDescriptor.getPsiElement();
    if (psiElement != null) psiElement.delete();
}