use of com.intellij.lang.ASTNode in project buck by facebook.
the class BuckBlock method isIncomplete.
@Override
public boolean isIncomplete() {
final ASTNode lastChildNode = myNode.getLastChildNode();
boolean ret = false;
if (hasElementType(myNode, TokenSet.create(BuckTypes.ARRAY_ELEMENTS))) {
ret = lastChildNode != null && lastChildNode.getElementType() != BuckTypes.R_BRACKET;
} else if (hasElementType(myNode, TokenSet.create(BuckTypes.RULE_BODY))) {
ret = lastChildNode != null && lastChildNode.getElementType() != BuckTypes.R_PARENTHESES;
} else if (hasElementType(myNode, TokenSet.create(BuckTypes.LIST_ELEMENTS))) {
ret = lastChildNode != null && lastChildNode.getElementType() != BuckTypes.R_PARENTHESES;
}
return ret;
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class GroovyEnterHandler method handleInString.
private static boolean handleInString(Editor editor, int caretOffset, DataContext dataContext, EditorActionHandler originalHandler) {
Project project = CommonDataKeys.PROJECT.getData(dataContext);
if (project == null)
return false;
final VirtualFile vfile = FileDocumentManager.getInstance().getFile(editor.getDocument());
assert vfile != null;
PsiFile file = PsiManager.getInstance(project).findFile(vfile);
Document document = editor.getDocument();
String fileText = document.getText();
if (fileText.length() == caretOffset)
return false;
if (!checkStringApplicable(editor, caretOffset))
return false;
if (file == null)
return false;
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiElement stringElement = inferStringPair(file, caretOffset);
if (stringElement == null)
return false;
ASTNode node = stringElement.getNode();
final IElementType nodeElementType = node.getElementType();
boolean isInsertIndent = isInsertIndent(caretOffset, stringElement.getTextRange().getStartOffset(), fileText);
// For simple String literals like 'abc'
CaretModel caretModel = editor.getCaretModel();
if (nodeElementType == GroovyTokenTypes.mSTRING_LITERAL) {
if (isSingleQuoteString(stringElement)) {
//the case of print '\<caret>'
if (isSlashBeforeCaret(caretOffset, fileText)) {
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else if (stringElement.getParent() instanceof GrReferenceExpression) {
TextRange range = stringElement.getTextRange();
convertEndToMultiline(range.getEndOffset(), document, fileText, '\'');
document.insertString(range.getStartOffset(), "''");
caretModel.moveToOffset(caretOffset + 2);
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else {
EditorModificationUtil.insertStringAtCaret(editor, "'+");
originalHandler.execute(editor, dataContext);
EditorModificationUtil.insertStringAtCaret(editor, "'");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
}
} else {
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
}
return true;
}
if (GSTRING_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && GSTRING_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.GSTRING) {
PsiElement parent = stringElement.getParent();
if (nodeElementType == GroovyTokenTypes.mGSTRING_LITERAL) {
parent = stringElement;
} else {
while (parent != null && !(parent instanceof GrLiteral)) {
parent = parent.getParent();
}
}
if (parent == null)
return false;
if (isDoubleQuotedString(parent)) {
PsiElement exprSibling = stringElement.getNextSibling();
boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
if (rightFromDollar)
caretOffset--;
TextRange parentRange = parent.getTextRange();
if (rightFromDollar || parent.getParent() instanceof GrReferenceExpression) {
convertEndToMultiline(parent.getTextRange().getEndOffset(), document, fileText, '"');
document.insertString(parentRange.getStartOffset(), "\"\"");
caretModel.moveToOffset(caretOffset + 2);
EditorModificationUtil.insertStringAtCaret(editor, "\n");
if (rightFromDollar) {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
} else if (isSlashBeforeCaret(caretOffset, fileText)) {
EditorModificationUtil.insertStringAtCaret(editor, "\n");
} else {
EditorModificationUtil.insertStringAtCaret(editor, "\"+");
originalHandler.execute(editor, dataContext);
EditorModificationUtil.insertStringAtCaret(editor, "\"");
PsiDocumentManager.getInstance(project).commitDocument(document);
CodeStyleManager.getInstance(project).reformatRange(file, caretOffset, caretModel.getOffset());
}
} else {
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
}
return true;
}
if (REGEX_TOKENS.contains(nodeElementType) || nodeElementType == GroovyElementTypes.GSTRING_CONTENT && REGEX_TOKENS.contains(node.getFirstChildNode().getElementType()) || nodeElementType == GroovyTokenTypes.mDOLLAR && node.getTreeParent().getTreeParent().getElementType() == GroovyElementTypes.REGEX) {
PsiElement parent = stringElement.getParent();
if (nodeElementType == GroovyTokenTypes.mREGEX_LITERAL || nodeElementType == GroovyTokenTypes.mDOLLAR_SLASH_REGEX_LITERAL) {
parent = stringElement;
} else {
while (parent != null && !(parent instanceof GrLiteral)) {
parent = parent.getParent();
}
}
if (parent == null || parent.getLastChild() instanceof PsiErrorElement)
return false;
PsiElement exprSibling = stringElement.getNextSibling();
boolean rightFromDollar = exprSibling instanceof GrExpression && exprSibling.getTextRange().getStartOffset() == caretOffset;
if (rightFromDollar) {
caretModel.moveToOffset(caretOffset - 1);
}
insertLineFeedInString(editor, dataContext, originalHandler, isInsertIndent);
if (rightFromDollar) {
caretModel.moveCaretRelatively(1, 0, false, false, true);
}
return true;
}
return false;
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class GroovyEnterHandler method inferStringPair.
@Nullable
private static PsiElement inferStringPair(PsiFile file, int caretOffset) {
PsiElement stringElement = file.findElementAt(caretOffset - 1);
if (stringElement == null)
return null;
ASTNode node = stringElement.getNode();
if (node == null)
return null;
// For expression injection in GString like "abc ${}<caret> abc"
if (!INNER_STRING_TOKENS.contains(node.getElementType()) && checkGStringInjection(stringElement)) {
stringElement = stringElement.getParent().getParent().getNextSibling();
if (stringElement == null)
return null;
}
return stringElement;
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class GroovyBlockGenerator method addBinaryChildrenRecursively.
/**
* Adds all children of specified element to given list
*
* @param elem
* @param list
* @param indent
* @param aligner
*/
private void addBinaryChildrenRecursively(PsiElement elem, List<Block> list, Indent indent, @Nullable AlignmentProvider.Aligner aligner) {
if (elem == null)
return;
// For binary expressions
if ((elem instanceof GrBinaryExpression)) {
GrBinaryExpression myExpr = ((GrBinaryExpression) elem);
if (myExpr.getLeftOperand() instanceof GrBinaryExpression) {
addBinaryChildrenRecursively(myExpr.getLeftOperand(), list, Indent.getContinuationWithoutFirstIndent(), aligner);
}
PsiElement op = ((GrBinaryExpression) elem).getOperationToken();
for (ASTNode childNode : visibleChildren(elem.getNode())) {
PsiElement psi = childNode.getPsi();
if (!(psi instanceof GrBinaryExpression)) {
if (op != psi && aligner != null) {
aligner.append(psi);
}
list.add(new GroovyBlock(childNode, indent, getChildWrap(childNode), myContext));
}
}
if (myExpr.getRightOperand() instanceof GrBinaryExpression) {
addBinaryChildrenRecursively(myExpr.getRightOperand(), list, Indent.getContinuationWithoutFirstIndent(), aligner);
}
}
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class GroovyBlockGenerator method processNestedChildrenPrefix.
private void processNestedChildrenPrefix(List<Block> list, @Nullable AlignmentProvider.Aligner aligner, boolean topLevel, List<ASTNode> children, int limit, Wrap wrap) {
ASTNode fst = children.get(0);
LOG.assertTrue(limit > 0);
if (NESTED.contains(fst.getElementType())) {
addNestedChildren(fst.getPsi(), list, aligner, false, wrap);
} else {
Indent indent = Indent.getContinuationWithoutFirstIndent();
list.add(new GroovyBlock(fst, indent, getChildWrap(fst), myContext));
}
addNestedChildrenSuffix(list, aligner, topLevel, children, limit);
}
Aggregations