use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class RestBlock method buildSubBlocks.
private List<RestBlock> buildSubBlocks() {
List<RestBlock> blocks = new ArrayList<>();
for (ASTNode child = myNode.getFirstChildNode(); child != null; child = child.getTreeNext()) {
IElementType childType = child.getElementType();
if (child.getTextRange().getLength() == 0)
continue;
if (childType == RestTokenTypes.WHITESPACE) {
continue;
}
blocks.add(buildSubBlock(child));
}
return Collections.unmodifiableList(blocks);
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class PyClassRefactoringUtil method addSuperClassExpressions.
/**
* Adds expressions to superclass list
*
* @param project project
* @param clazz class to add expressions to superclass list
* @param paramExpressions param expressions. Like "object" or "MySuperClass". Will not add any param exp. if null.
* @param keywordArguments keyword args like "metaclass=ABCMeta". key-value pairs. Will not add any keyword arg. if null.
*/
public static void addSuperClassExpressions(@NotNull final Project project, @NotNull final PyClass clazz, @Nullable final Collection<String> paramExpressions, @Nullable final Collection<Pair<String, String>> keywordArguments) {
final PyElementGenerator generator = PyElementGenerator.getInstance(project);
final LanguageLevel languageLevel = LanguageLevel.forElement(clazz);
PyArgumentList superClassExpressionList = clazz.getSuperClassExpressionList();
boolean addExpression = false;
if (superClassExpressionList == null) {
superClassExpressionList = generator.createFromText(languageLevel, PyClass.class, "class foo():pass").getSuperClassExpressionList();
assert superClassExpressionList != null : "expression not created";
addExpression = true;
}
generator.createFromText(LanguageLevel.PYTHON34, PyClass.class, "class foo(object, metaclass=Foo): pass").getSuperClassExpressionList();
if (paramExpressions != null) {
for (final String paramExpression : paramExpressions) {
superClassExpressionList.addArgument(generator.createParameter(paramExpression));
}
}
if (keywordArguments != null) {
for (final Pair<String, String> keywordArgument : keywordArguments) {
superClassExpressionList.addArgument(generator.createKeywordArgument(languageLevel, keywordArgument.first, keywordArgument.second));
}
}
// If class has no expression list, then we need to add it manually.
if (addExpression) {
// For nameless classes we simply add expression list directly to them
final ASTNode classNameNode = clazz.getNameNode();
final PsiElement elementToAddAfter = (classNameNode == null) ? clazz.getFirstChild() : classNameNode.getPsi();
clazz.addAfter(superClassExpressionList, elementToAddAfter);
}
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class PyReferenceImpl method handleElementRename.
@Override
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
ASTNode nameElement = myElement.getNameElement();
newElementName = StringUtil.trimEnd(newElementName, PyNames.DOT_PY);
if (nameElement != null && PyNames.isIdentifier(newElementName)) {
final ASTNode newNameElement = PyUtil.createNewName(myElement, newElementName);
myElement.getNode().replaceChild(nameElement, newNameElement);
}
return myElement;
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class XmlAttributeImpl method getDisplayValue.
@Override
public String getDisplayValue() {
String displayText = myDisplayText;
if (displayText != null)
return displayText;
XmlAttributeValue value = getValueElement();
if (value == null)
return null;
PsiElement firstChild = value.getFirstChild();
if (firstChild == null)
return null;
ASTNode child = firstChild.getNode();
TextRange valueTextRange = new TextRange(0, value.getTextLength());
if (child != null && child.getElementType() == XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER) {
valueTextRange = new TextRange(child.getTextLength(), valueTextRange.getEndOffset());
child = child.getTreeNext();
}
final TIntArrayList gapsStarts = new TIntArrayList();
final TIntArrayList gapsShifts = new TIntArrayList();
StringBuilder buffer = new StringBuilder(getTextLength());
while (child != null) {
final int start = buffer.length();
IElementType elementType = child.getElementType();
if (elementType == XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER) {
valueTextRange = new TextRange(valueTextRange.getStartOffset(), child.getTextRange().getStartOffset() - value.getTextRange().getStartOffset());
break;
}
if (elementType == XmlTokenType.XML_CHAR_ENTITY_REF) {
buffer.append(XmlUtil.getCharFromEntityRef(child.getText()));
} else if (elementType == XmlElementType.XML_ENTITY_REF) {
buffer.append(XmlUtil.getEntityValue((XmlEntityRef) child));
} else {
appendChildToDisplayValue(buffer, child);
}
int end = buffer.length();
int originalLength = child.getTextLength();
if (end - start != originalLength) {
gapsStarts.add(start);
gapsShifts.add(originalLength - (end - start));
}
child = child.getTreeNext();
}
int[] gapDisplayStarts = ArrayUtil.newIntArray(gapsShifts.size());
int[] gapPhysicalStarts = ArrayUtil.newIntArray(gapsShifts.size());
int currentGapsSum = 0;
for (int i = 0; i < gapDisplayStarts.length; i++) {
currentGapsSum += gapsShifts.get(i);
gapDisplayStarts[i] = gapsStarts.get(i);
gapPhysicalStarts[i] = gapDisplayStarts[i] + currentGapsSum;
}
myGapDisplayStarts = gapDisplayStarts;
myGapPhysicalStarts = gapPhysicalStarts;
myValueTextRange = valueTextRange;
return myDisplayText = buffer.toString();
}
use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.
the class XmlAttributeImpl method setValue.
@Override
public void setValue(String valueText) throws IncorrectOperationException {
final ASTNode value = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild(this);
final PomModel model = PomManager.getModel(getProject());
final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute("a", valueText, this);
final ASTNode newValue = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild((ASTNode) attribute);
final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
model.runTransaction(new PomTransactionBase(this, aspect) {
@Override
public PomModelEvent runInner() {
final XmlAttributeImpl att = XmlAttributeImpl.this;
if (value != null) {
if (newValue != null) {
att.replaceChild(value, newValue.copyElement());
} else {
att.removeChild(value);
}
} else {
if (newValue != null) {
att.addChild(newValue.getTreePrev().copyElement());
att.addChild(newValue.copyElement());
}
}
return XmlAttributeSetImpl.createXmlAttributeSet(model, getParent(), getName(), newValue != null ? newValue.getText() : null);
}
});
}
Aggregations