use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class ParenthesesInsertHandler method findNextToken.
@Nullable
protected PsiElement findNextToken(@NotNull InsertionContext context) {
final PsiFile file = context.getFile();
PsiElement element = file.findElementAt(context.getTailOffset());
if (element instanceof PsiWhiteSpace) {
if (!myAllowParametersOnNextLine && element.getText().contains("\n")) {
return null;
}
element = file.findElementAt(element.getTextRange().getEndOffset());
}
return element;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class ParameterInfoUtils method findParentOfTypeWithStopElements.
@SafeVarargs
@Nullable
public static <T extends PsiElement> T findParentOfTypeWithStopElements(PsiFile file, int offset, Class<T> parentClass, @NotNull Class<? extends PsiElement>... stopAt) {
PsiElement element = file.findElementAt(offset);
if (element == null)
return null;
T parentOfType = PsiTreeUtil.getParentOfType(element, parentClass, true, stopAt);
if (element instanceof PsiWhiteSpace) {
parentOfType = PsiTreeUtil.getParentOfType(PsiTreeUtil.prevLeaf(element), parentClass, true, stopAt);
}
return parentOfType;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class ShowIntentionsPass method getActionsToShow.
public static void getActionsToShow(@NotNull final Editor hostEditor, @NotNull final PsiFile hostFile, @NotNull final IntentionsInfo intentions, int passIdToShowIntentionsFor) {
final PsiElement psiElement = hostFile.findElementAt(hostEditor.getCaretModel().getOffset());
LOG.assertTrue(psiElement == null || psiElement.isValid(), psiElement);
int offset = hostEditor.getCaretModel().getOffset();
final Project project = hostFile.getProject();
List<HighlightInfo.IntentionActionDescriptor> fixes = getAvailableFixes(hostEditor, hostFile, passIdToShowIntentionsFor);
final DaemonCodeAnalyzer codeAnalyzer = DaemonCodeAnalyzer.getInstance(project);
final Document hostDocument = hostEditor.getDocument();
HighlightInfo infoAtCursor = ((DaemonCodeAnalyzerImpl) codeAnalyzer).findHighlightByOffset(hostDocument, offset, true);
if (infoAtCursor == null) {
intentions.errorFixesToShow.addAll(fixes);
} else {
final boolean isError = infoAtCursor.getSeverity() == HighlightSeverity.ERROR;
for (HighlightInfo.IntentionActionDescriptor fix : fixes) {
if (fix.isError() && isError) {
intentions.errorFixesToShow.add(fix);
} else {
intentions.inspectionFixesToShow.add(fix);
}
}
}
for (final IntentionAction action : IntentionManager.getInstance().getAvailableIntentionActions()) {
Pair<PsiFile, Editor> place = ShowIntentionActionsHandler.chooseBetweenHostAndInjected(hostFile, hostEditor, (psiFile, editor) -> ShowIntentionActionsHandler.availableFor(psiFile, editor, action));
if (place != null) {
List<IntentionAction> enableDisableIntentionAction = new ArrayList<>();
enableDisableIntentionAction.add(new IntentionHintComponent.EnableDisableIntentionAction(action));
enableDisableIntentionAction.add(new IntentionHintComponent.EditIntentionSettingsAction(action));
HighlightInfo.IntentionActionDescriptor descriptor = new HighlightInfo.IntentionActionDescriptor(action, enableDisableIntentionAction, null);
if (!fixes.contains(descriptor)) {
intentions.intentionsToShow.add(descriptor);
}
}
}
if (HighlightingLevelManager.getInstance(project).shouldInspect(hostFile)) {
PsiElement intentionElement = psiElement;
int intentionOffset = offset;
if (psiElement instanceof PsiWhiteSpace && offset == psiElement.getTextRange().getStartOffset() && offset > 0) {
final PsiElement prev = hostFile.findElementAt(offset - 1);
if (prev != null && prev.isValid()) {
intentionElement = prev;
intentionOffset = offset - 1;
}
}
if (intentionElement != null && intentionElement.getManager().isInProject(intentionElement)) {
collectIntentionsFromDoNotShowLeveledInspections(project, hostFile, intentionElement, intentionOffset, intentions);
}
}
final int line = hostDocument.getLineNumber(offset);
MarkupModelEx model = (MarkupModelEx) DocumentMarkupModel.forDocument(hostDocument, project, true);
List<RangeHighlighterEx> result = new ArrayList<>();
Processor<RangeHighlighterEx> processor = Processors.cancelableCollectProcessor(result);
model.processRangeHighlightersOverlappingWith(hostDocument.getLineStartOffset(line), hostDocument.getLineEndOffset(line), processor);
GutterIntentionAction.addActions(hostEditor, intentions, project, result);
boolean cleanup = appendCleanupCode(intentions.inspectionFixesToShow, hostFile);
if (!cleanup) {
appendCleanupCode(intentions.errorFixesToShow, hostFile);
}
EditorNotificationActions.collectDescriptorsForEditor(hostEditor, intentions.notificationActionsToShow);
intentions.filterActions(hostFile);
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PsiBasedFormatterModelWithShiftIndentInside method replaceWithPsiInLeaf.
@Override
protected String replaceWithPsiInLeaf(final TextRange textRange, String whiteSpace, ASTNode leafElement) {
if (!myCanModifyAllWhiteSpaces) {
if (leafElement.getElementType() == TokenType.WHITE_SPACE)
return null;
ASTNode prevNode = TreeUtil.prevLeaf(leafElement);
if (prevNode != null) {
IElementType type = prevNode.getElementType();
if (type == TokenType.WHITE_SPACE) {
final String text = prevNode.getText();
@NonNls final String cdataStartMarker = "<![CDATA[";
final int cdataPos = text.indexOf(cdataStartMarker);
if (cdataPos != -1 && whiteSpace.indexOf(cdataStartMarker) == -1) {
whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, text, cdataPos);
if (whiteSpace == null)
return null;
}
prevNode = TreeUtil.prevLeaf(prevNode);
type = prevNode != null ? prevNode.getElementType() : null;
}
@NonNls final String cdataEndMarker = "]]>";
if (type == XmlTokenType.XML_CDATA_END && whiteSpace.indexOf(cdataEndMarker) == -1) {
final ASTNode at = findElementAt(prevNode.getStartOffset());
if (at != null && at.getPsi() instanceof PsiWhiteSpace) {
final String s = at.getText();
final int cdataEndPos = s.indexOf(cdataEndMarker);
whiteSpace = DocumentBasedFormattingModel.mergeWsWithCdataMarker(whiteSpace, s, cdataEndPos);
leafElement = at;
} else {
whiteSpace = null;
}
if (whiteSpace == null)
return null;
}
}
}
FormatterUtil.replaceWhiteSpace(whiteSpace, leafElement, TokenType.WHITE_SPACE, textRange);
return whiteSpace;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class DuplocatorUtil method isIgnoredNode.
public static boolean isIgnoredNode(PsiElement element) {
if (element instanceof PsiWhiteSpace || element instanceof PsiErrorElement || element instanceof PsiComment) {
return true;
}
if (!(element instanceof LeafElement)) {
return false;
}
if (CharArrayUtil.containsOnlyWhiteSpaces(element.getText())) {
return true;
}
EquivalenceDescriptorProvider descriptorProvider = EquivalenceDescriptorProvider.getInstance(element);
if (descriptorProvider == null) {
return false;
}
final IElementType elementType = ((LeafElement) element).getElementType();
return descriptorProvider.getIgnoredTokens().contains(elementType);
}
Aggregations