use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PyFillParagraphHandler method getIndent.
private static String getIndent(PyStringLiteralExpression stringLiteralExpression) {
final PyStatementList statementList = PsiTreeUtil.getParentOfType(stringLiteralExpression, PyStatementList.class);
String indent = "";
if (statementList != null) {
final PsiElement whiteSpace = statementList.getPrevSibling();
if (whiteSpace instanceof PsiWhiteSpace)
indent = whiteSpace.getText();
else
indent = "\n";
}
return indent;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PyCommentBreakerEnterProcessor method doEnter.
public boolean doEnter(Editor editor, PsiElement psiElement, boolean isModified) {
if (isModified) {
return false;
}
final CaretModel caretModel = editor.getCaretModel();
PsiElement atCaret = psiElement.getContainingFile().findElementAt(caretModel.getOffset());
if (atCaret instanceof PsiWhiteSpace) {
atCaret = atCaret.getPrevSibling();
}
final PsiElement comment = PsiTreeUtil.getParentOfType(atCaret, PsiComment.class, false);
if (comment != null) {
SmartEnterUtil.plainEnter(editor);
editor.getDocument().insertString(caretModel.getOffset(), "# ");
caretModel.moveToOffset(caretModel.getOffset() + 2);
return true;
}
return false;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PyCodeBlockProvider method getCodeBlockRange.
@Nullable
@Override
public TextRange getCodeBlockRange(Editor editor, PsiFile psiFile) {
int caretOffset = editor.getCaretModel().getOffset();
PsiElement element = psiFile.findElementAt(caretOffset);
if (element == null) {
return null;
}
while (caretOffset > 0 && element instanceof PsiWhiteSpace) {
caretOffset--;
element = psiFile.findElementAt(caretOffset);
}
PyStatement statement = PsiTreeUtil.getParentOfType(element, PyStatement.class);
if (statement != null) {
PyStatementList statementList = PsiTreeUtil.findChildOfType(statement, PyStatementList.class);
// that statement list
if (statementList == null) {
statementList = PsiTreeUtil.getParentOfType(statement, PyStatementList.class);
if (statementList != null) {
statement = PsiTreeUtil.getParentOfType(statementList, PyStatement.class);
}
}
if (statement != null) {
// if we're in the beginning of the statement already, pressing Ctrl-[ again should move the caret one statement higher
final int statementStart = statement.getTextRange().getStartOffset();
int statementEnd = statement.getTextRange().getEndOffset();
while (statementEnd > statementStart && psiFile.findElementAt(statementEnd) instanceof PsiWhiteSpace) {
statementEnd--;
}
if (caretOffset == statementStart || caretOffset == statementEnd) {
final PyStatement statementAbove = PsiTreeUtil.getParentOfType(statement, PyStatement.class);
if (statementAbove != null) {
if (caretOffset == statementStart) {
return new TextRange(statementAbove.getTextRange().getStartOffset(), statementEnd);
} else {
return new TextRange(statementStart, statementAbove.getTextRange().getEndOffset());
}
}
}
return statement.getTextRange();
}
}
return null;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PythonFoldingBuilder method foldSequentialComments.
private static void foldSequentialComments(ASTNode node, List<FoldingDescriptor> descriptors) {
//do not start folded comments from custom region
if (isCustomRegionElement(node.getPsi())) {
return;
}
//need to skip previous comments in sequence
ASTNode curNode = node.getTreePrev();
while (curNode != null) {
if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
if (isCustomRegionElement(curNode.getPsi())) {
break;
}
return;
}
curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreePrev() : null;
}
//fold sequence comments in one block
curNode = node.getTreeNext();
ASTNode lastCommentNode = node;
while (curNode != null) {
if (curNode.getElementType() == PyTokenTypes.END_OF_LINE_COMMENT) {
//do not end folded comments with custom region
if (isCustomRegionElement(curNode.getPsi())) {
break;
}
lastCommentNode = curNode;
curNode = curNode.getTreeNext();
continue;
}
curNode = curNode.getPsi() instanceof PsiWhiteSpace ? curNode.getTreeNext() : null;
}
if (lastCommentNode != node) {
descriptors.add(new FoldingDescriptor(node, TextRange.create(node.getStartOffset(), lastCommentNode.getTextRange().getEndOffset())));
}
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class XmlAutoPopupHandler method isLanguageRelevant.
private static boolean isLanguageRelevant(final PsiElement element, final PsiFile file, final Ref<Boolean> isRelevantLanguage, final Ref<Boolean> isAnt) {
Boolean isAntFile = isAnt.get();
if (isAntFile == null) {
isAntFile = XmlUtil.isAntFile(file);
isAnt.set(isAntFile);
}
Boolean result = isRelevantLanguage.get();
if (result == null) {
Language language = element.getLanguage();
PsiElement parent = element.getParent();
if (element instanceof PsiWhiteSpace && parent != null) {
language = parent.getLanguage();
}
result = language instanceof XMLLanguage || HtmlUtil.supportsXmlTypedHandlers(file) || isAntFile.booleanValue();
isRelevantLanguage.set(result);
}
return result.booleanValue();
}
Aggregations