use of com.intellij.psi.PsiWhiteSpace in project android by JetBrains.
the class AndroidBaseXmlRefactoringAction method getExtractableRange.
@Nullable
private static Pair<PsiElement, PsiElement> getExtractableRange(PsiFile file, int start, int end) {
PsiElement startElement = file.findElementAt(start);
PsiElement parent = startElement != null ? startElement.getParent() : null;
while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getStartOffset() == startElement.getTextRange().getStartOffset()) {
startElement = parent;
parent = parent.getParent();
}
PsiElement endElement = file.findElementAt(end - 1);
parent = endElement != null ? endElement.getParent() : null;
while (parent != null && !(parent instanceof PsiFile) && parent.getTextRange().getEndOffset() == endElement.getTextRange().getEndOffset()) {
endElement = parent;
parent = parent.getParent();
}
if (startElement == null || endElement == null) {
return null;
}
final PsiElement commonParent = startElement.getParent();
if (commonParent == null || !(commonParent instanceof XmlTag) || commonParent != endElement.getParent()) {
return null;
}
PsiElement e = startElement;
boolean containTag = false;
while (e != null) {
if (!(e instanceof XmlText) && !(e instanceof XmlTag) && !(e instanceof PsiWhiteSpace) && !(e instanceof PsiComment)) {
return null;
}
if (e instanceof XmlTag) {
containTag = true;
}
if (e == endElement) {
break;
}
e = e.getNextSibling();
}
return e != null && containTag ? Pair.create(startElement, endElement) : null;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class DartFoldingBuilder method foldFileHeader.
@Nullable
private static TextRange foldFileHeader(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Document document) {
PsiElement firstComment = dartFile.getFirstChild();
if (firstComment instanceof PsiWhiteSpace)
firstComment = firstComment.getNextSibling();
if (!(firstComment instanceof PsiComment))
return null;
boolean containsCustomRegionMarker = false;
PsiElement nextAfterComments = firstComment;
while (nextAfterComments instanceof PsiComment || nextAfterComments instanceof PsiWhiteSpace) {
containsCustomRegionMarker |= isCustomRegionElement(nextAfterComments);
nextAfterComments = nextAfterComments.getNextSibling();
}
if (nextAfterComments == null)
return null;
if (nextAfterComments instanceof DartLibraryStatement || nextAfterComments instanceof DartPartStatement || nextAfterComments instanceof DartPartOfStatement || nextAfterComments instanceof DartImportOrExportStatement) {
if (nextAfterComments.getPrevSibling() instanceof PsiWhiteSpace)
nextAfterComments = nextAfterComments.getPrevSibling();
if (nextAfterComments.equals(firstComment))
return null;
final TextRange fileHeaderCommentsRange = new UnfairTextRange(firstComment.getTextRange().getStartOffset(), nextAfterComments.getTextRange().getStartOffset());
if (fileHeaderCommentsRange.getLength() > 1 && document.getLineNumber(fileHeaderCommentsRange.getEndOffset()) > document.getLineNumber(fileHeaderCommentsRange.getStartOffset())) {
if (!containsCustomRegionMarker) {
descriptors.add(new FoldingDescriptor(dartFile, fileHeaderCommentsRange));
}
return fileHeaderCommentsRange;
}
}
return null;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class DartFoldingBuilder method foldConsequentStatements.
private static <T extends PsiElement> void foldConsequentStatements(@NotNull final List<FoldingDescriptor> descriptors, @NotNull final DartFile dartFile, @NotNull final Class<T> aClass) {
final T firstStatement = PsiTreeUtil.getChildOfType(dartFile, aClass);
if (firstStatement == null)
return;
PsiElement lastStatement = firstStatement;
PsiElement nextElement = firstStatement;
while (aClass.isInstance(nextElement) || nextElement instanceof PsiComment || nextElement instanceof PsiWhiteSpace) {
if (aClass.isInstance(nextElement)) {
lastStatement = nextElement;
}
nextElement = nextElement.getNextSibling();
}
if (lastStatement != firstStatement) {
// after "import " or "export " or "part "
final int startOffset = firstStatement.getTextRange().getStartOffset() + firstStatement.getFirstChild().getTextLength() + 1;
final int endOffset = lastStatement.getTextRange().getEndOffset();
final FoldingDescriptor descriptor = new FoldingDescriptor(firstStatement, TextRange.create(startOffset, endOffset));
if (aClass == DartImportOrExportStatement.class) {
// imports are often added/removed automatically, so we enable autoupdate of folded region for foldings even if it's collapsed
descriptor.setCanBeRemovedWhenCollapsed(true);
}
descriptors.add(descriptor);
}
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class DartTypeHandler method beforeCharTyped.
@Override
public Result beforeCharTyped(final char c, final Project project, final Editor editor, final PsiFile file, final FileType fileType) {
myAfterTypeOrComponentName = false;
myAfterDollarInStringInterpolation = false;
if (fileType != DartFileType.INSTANCE)
return Result.CONTINUE;
if (c == '<' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
TypedHandler.commitDocumentIfCurrentCaretIsNotTheFirstOne(editor, project);
myAfterTypeOrComponentName = isAfterTypeOrComponentName(file, editor.getCaretModel().getOffset());
return Result.CONTINUE;
}
if (c == '>' && CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET) {
if (handleDartGT(editor, DartTokenTypes.LT, DartTokenTypes.GT, INVALID_INSIDE_REFERENCE)) {
return Result.STOP;
}
return Result.CONTINUE;
}
if (c == '{') {
TypedHandler.commitDocumentIfCurrentCaretIsNotTheFirstOne(editor, project);
final PsiElement element = file.findElementAt(editor.getCaretModel().getOffset() - 1);
if (CodeInsightSettings.getInstance().AUTOINSERT_PAIR_BRACKET && isAfterDollarInStringInterpolation(element)) {
myAfterDollarInStringInterpolation = true;
return Result.CONTINUE;
}
PsiElement nextLeaf = element == null ? null : PsiTreeUtil.nextLeaf(element);
if (nextLeaf instanceof PsiWhiteSpace) {
nextLeaf = PsiTreeUtil.nextLeaf(nextLeaf);
}
if (PsiTreeUtil.getParentOfType(element, DartLazyParseableBlock.class, false) != null || nextLeaf != null && nextLeaf.getText().equals("=>")) {
// Use case 1: manually wrapping code with {} in 'if', 'while', etc (if (a) <caret>return;). Closing '}' will be auto-inserted on Enter.
// Use case 2: manual transformation of arrow block to standard (foo()<caret> => 499;)
EditorModificationUtil.insertStringAtCaret(editor, "{");
return Result.STOP;
}
}
return Result.CONTINUE;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-plugins by JetBrains.
the class DartEnterInDocLineCommentHandler method preprocessEnter.
// EnterInLineCommentHandler doesn't work well enough for Dart doc comments
@Override
public Result preprocessEnter(@NotNull final PsiFile file, @NotNull final Editor editor, @NotNull final Ref<Integer> caretOffsetRef, @NotNull final Ref<Integer> caretAdvance, @NotNull final DataContext dataContext, final EditorActionHandler originalHandler) {
if (file.getLanguage() != DartLanguage.INSTANCE && !HtmlUtil.isHtmlFile(file))
return Result.Continue;
final int caretOffset = caretOffsetRef.get().intValue();
final Document document = editor.getDocument();
PsiDocumentManager.getInstance(file.getProject()).commitDocument(editor.getDocument());
final PsiElement psiAtOffset = file.findElementAt(caretOffset);
final PsiElement probablyDocComment = psiAtOffset instanceof PsiWhiteSpace && psiAtOffset.getText().startsWith("\n") ? psiAtOffset.getPrevSibling() : psiAtOffset == null && caretOffset > 0 && caretOffset == document.getTextLength() ? file.findElementAt(caretOffset - 1) : psiAtOffset;
if (probablyDocComment != null && probablyDocComment.getTextRange().getStartOffset() < caretOffset && probablyDocComment.getNode().getElementType() == DartTokenTypesSets.SINGLE_LINE_DOC_COMMENT) {
final CharSequence text = document.getCharsSequence();
final int offset = CharArrayUtil.shiftForward(text, caretOffset, " \t");
if (StringUtil.startsWith(text, offset, DartDocUtil.SINGLE_LINE_DOC_COMMENT)) {
caretOffsetRef.set(offset);
} else {
final String docText = StringUtil.trimStart(probablyDocComment.getText(), DartDocUtil.SINGLE_LINE_DOC_COMMENT);
final int spacesBeforeText = StringUtil.isEmptyOrSpaces(docText) ? 1 : StringUtil.countChars(docText, ' ', 0, true);
final int spacesToAdd = Math.max(0, spacesBeforeText - StringUtil.countChars(text, ' ', caretOffset, true));
document.insertString(caretOffset, DartDocUtil.SINGLE_LINE_DOC_COMMENT + StringUtil.repeatSymbol(' ', spacesToAdd));
caretAdvance.set(spacesBeforeText);
}
return Result.Default;
}
return Result.Continue;
}
Aggregations