use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PyCommaSelectionHandler method addNextComma.
/**
* add next comma and whitespace to selection
* @param e is crrent element
* @param cursorOffset is current cursor offset
* @return result selection TextRange
*/
private static List<TextRange> addNextComma(PsiElement e, int cursorOffset) {
PsiElement nextCommaSibling = e.getNextSibling();
if (nextCommaSibling != null) {
ASTNode node = nextCommaSibling.getNode();
if (node != null) {
IElementType commaType = node.getElementType();
if (commaType == PyTokenTypes.COMMA) {
PsiElement nextSpaceSibling = nextCommaSibling.getNextSibling();
if (nextSpaceSibling != null) {
TextRange textRange = e.getTextRange();
TextRange offsetRange;
if (nextSpaceSibling instanceof PsiWhiteSpace) {
offsetRange = new TextRange(textRange.getStartOffset(), textRange.getEndOffset() + 2);
} else {
offsetRange = new TextRange(textRange.getStartOffset(), textRange.getEndOffset() + 1);
}
if (offsetRange.contains(cursorOffset) && offsetRange.getLength() > 1) {
return Collections.singletonList(offsetRange);
}
}
}
}
}
return Collections.emptyList();
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PyStatementSelectionHandler method select.
public List<TextRange> select(final PsiElement e, final CharSequence editorText, final int cursorOffset, final Editor editor) {
List<TextRange> result = new ArrayList<>();
PsiElement endElement = e;
while (endElement.getLastChild() != null) {
endElement = endElement.getLastChild();
}
if (endElement instanceof PsiWhiteSpace) {
final PsiElement prevSibling = endElement.getPrevSibling();
if (prevSibling != null) {
endElement = prevSibling;
}
}
result.addAll(expandToWholeLine(editorText, new TextRange(e.getTextRange().getStartOffset(), endElement.getTextRange().getEndOffset())));
return result;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PyDocstringGenerator method buildAndInsert.
@NotNull
public PyDocStringOwner buildAndInsert() {
Preconditions.checkNotNull(myDocStringOwner, "For this action docstring owner must be supplied");
final String replacementText = buildDocString();
final Project project = myDocStringOwner.getProject();
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final PyExpressionStatement replacement = elementGenerator.createDocstring(replacementText);
final PyStringLiteralExpression docStringExpression = getDocStringExpression();
if (docStringExpression != null) {
docStringExpression.replace(replacement.getExpression());
} else {
PyStatementListContainer container = PyUtil.as(myDocStringOwner, PyStatementListContainer.class);
if (container == null) {
throw new IllegalStateException("Should be a function or class");
}
final PyStatementList statements = container.getStatementList();
final String indentation = PyIndentUtil.getElementIndent(statements);
final Document document = PsiDocumentManager.getInstance(project).getDocument(myDocStringOwner.getContainingFile());
if (document != null) {
final PsiElement beforeStatements = statements.getPrevSibling();
final boolean onSameLine = !(beforeStatements instanceof PsiWhiteSpace) || !beforeStatements.textContains('\n');
if (onSameLine || statements.getStatements().length == 0) {
String replacementWithLineBreaks = "\n" + indentation + replacementText;
if (statements.getStatements().length > 0) {
replacementWithLineBreaks += "\n" + indentation;
}
final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
documentManager.doPostponedOperationsAndUnblockDocument(document);
final TextRange range = beforeStatements.getTextRange();
try {
if (beforeStatements instanceof PsiWhiteSpace) {
if (statements.getStatements().length > 0) {
document.replaceString(range.getStartOffset(), range.getEndOffset(), replacementWithLineBreaks);
} else {
// preserve original spacing, since it probably separates function and other declarations
document.insertString(range.getStartOffset(), replacementWithLineBreaks);
}
} else {
document.insertString(range.getEndOffset(), replacementWithLineBreaks);
}
} finally {
documentManager.commitDocument(document);
}
} else {
statements.addBefore(replacement, statements.getStatements()[0]);
}
}
}
myDocStringOwner = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(myDocStringOwner);
return myDocStringOwner;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class RncFoldingBuilder method checkNodeAndSiblings.
@Nullable
private static ASTNode checkNodeAndSiblings(@Nullable ASTNode node, TokenSet tokens, ArrayList<FoldingDescriptor> regions, Document document) {
if (node != null && tokens.contains(node.getElementType())) {
final ASTNode start = node;
ASTNode end = start;
node = node.getTreeNext();
if (node != null) {
do {
end = node;
node = node.getTreeNext();
} while (node != null && tokens.contains(node.getElementType()));
}
if (end != start) {
while (end.getPsi() instanceof PsiWhiteSpace) {
end = end.getTreePrev();
}
if (isOnDifferentLine(start, end, document)) {
regions.add(new FoldingDescriptor(start, new TextRange(start.getStartOffset(), end.getTextRange().getEndOffset())));
}
}
}
return node;
}
use of com.intellij.psi.PsiWhiteSpace in project intellij-community by JetBrains.
the class PropertiesRootBlock method buildChildren.
@Override
protected List<Block> buildChildren() {
final List<Block> result = new ArrayList<>();
ASTNode child = myNode.getFirstChildNode();
while (child != null) {
if (!(child instanceof PsiWhiteSpace)) {
if (child.getElementType() instanceof PropertyListStubElementType) {
ASTNode propertyNode = child.getFirstChildNode();
while (propertyNode != null) {
if (propertyNode.getElementType() instanceof PropertyStubElementType) {
collectPropertyBlock(propertyNode, result);
} else if (PropertiesTokenTypes.END_OF_LINE_COMMENT.equals(propertyNode.getElementType()) || PropertiesTokenTypes.BAD_CHARACTER.equals(propertyNode.getElementType())) {
result.add(new PropertyBlock(propertyNode, null));
}
propertyNode = propertyNode.getTreeNext();
}
} else if (PropertiesTokenTypes.BAD_CHARACTER.equals(child.getElementType())) {
result.add(new PropertyBlock(child, null));
}
}
if (PropertiesTokenTypes.END_OF_LINE_COMMENT.equals(child.getElementType())) {
result.add(new PropertyBlock(child, null));
}
child = child.getTreeNext();
}
return result;
}
Aggregations