use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class PyElementGeneratorImpl method createComma.
public ASTNode createComma() {
final PsiFile dummyFile = createDummyFile(LanguageLevel.getDefault(), "[0,]");
final PyExpressionStatement expressionStatement = (PyExpressionStatement) dummyFile.getFirstChild();
ASTNode zero = expressionStatement.getFirstChild().getNode().getFirstChildNode().getTreeNext();
return zero.getTreeNext().copyElement();
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class PyElementGeneratorImpl method insertItemIntoList.
// TODO: Adds comma to empty list: adding "foo" to () will create (foo,). That is why "insertItemIntoListRemoveRedundantCommas" was created.
// We probably need to fix this method and delete insertItemIntoListRemoveRedundantCommas
public PsiElement insertItemIntoList(PyElement list, @Nullable PyExpression afterThis, PyExpression toInsert) throws IncorrectOperationException {
ASTNode add = toInsert.getNode().copyElement();
if (afterThis == null) {
ASTNode exprNode = list.getNode();
ASTNode[] closingTokens = exprNode.getChildren(TokenSet.create(PyTokenTypes.LBRACKET, PyTokenTypes.LPAR));
if (closingTokens.length == 0) {
// we tried our best. let's just insert it at the end
exprNode.addChild(add);
} else {
ASTNode next = PyPsiUtils.getNextNonWhitespaceSibling(closingTokens[closingTokens.length - 1]);
if (next != null) {
ASTNode comma = createComma();
exprNode.addChild(comma, next);
exprNode.addChild(add, comma);
} else {
exprNode.addChild(add);
}
}
} else {
ASTNode lastArgNode = afterThis.getNode();
ASTNode comma = createComma();
ASTNode parent = lastArgNode.getTreeParent();
ASTNode afterLast = lastArgNode.getTreeNext();
if (afterLast == null) {
parent.addChild(add);
} else {
parent.addChild(add, afterLast);
}
parent.addChild(comma, add);
}
return add.getPsi();
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class PyFunctionImpl method setName.
public PsiElement setName(@NotNull String name) throws IncorrectOperationException {
final ASTNode nameElement = PyUtil.createNewName(this, name);
final ASTNode nameNode = getNameNode();
if (nameNode != null) {
getNode().replaceChild(nameNode, nameElement);
}
return this;
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class PyLineMarkerProviderTest method testOverriding.
/**
* Checks method has "up" arrow when overrides, and this arrow works
*/
public void testOverriding() throws Exception {
myFixture.copyDirectoryToProject("lineMarkerTest", "");
myFixture.configureByFile("spam.py");
final ASTNode functionNode = myFixture.getElementAtCaret().getNode();
// We need IDENTIFIER node
final ASTNode[] functionChildren = functionNode.getChildren(TokenSet.create(PyTokenTypes.IDENTIFIER));
assert functionChildren.length == 1 : "Wrong number of identifiers: " + functionChildren.length;
final PsiElement element = functionChildren[0].getPsi();
@SuppressWarnings("unchecked") final LineMarkerInfo<PsiElement> lineMarkerInfo = new PyLineMarkerProvider().getLineMarkerInfo(element);
Assert.assertNotNull("No gutter displayed", lineMarkerInfo);
final GutterIconNavigationHandler<PsiElement> handler = lineMarkerInfo.getNavigationHandler();
Assert.assertNotNull("Gutter has no navigation handle", handler);
handler.navigate(new MouseEvent(new JLabel(), 0, 0, 0, 0, 0, 0, false), element);
final NavigatablePsiElement[] targets = PyLineMarkerNavigator.getNavigationTargets(element);
Assert.assertNotNull("No navigation targets found", targets);
Assert.assertThat("Wrong number of targets found", targets, Matchers.arrayWithSize(1));
final NavigatablePsiElement parentMethod = targets[0];
Assert.assertThat("Navigation target has wrong type", parentMethod, Matchers.instanceOf(PyPossibleClassMember.class));
final PyClass parentClass = ((PyPossibleClassMember) parentMethod).getContainingClass();
Assert.assertNotNull("Function overrides other function, but no parent displayed", parentClass);
Assert.assertEquals("Wrong parent class name", "Eggs", parentClass.getName());
}
use of com.intellij.lang.ASTNode 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