use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class TypeParameterExtendsBoundsListElement method addInternal.
@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
if (first == last && first.getElementType() == JAVA_CODE_REFERENCE) {
if (getLastChildNode() != null && getLastChildNode().getElementType() == ERROR_ELEMENT) {
super.deleteChildInternal(getLastChildNode());
}
}
final TreeElement firstAdded = super.addInternal(first, last, anchor, before);
final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
if (first == last && first.getElementType() == JAVA_CODE_REFERENCE) {
ASTNode element = first;
for (ASTNode child = element.getTreeNext(); child != null; child = child.getTreeNext()) {
if (child.getElementType() == AND)
break;
if (child.getElementType() == JAVA_CODE_REFERENCE) {
TreeElement comma = Factory.createSingleLeafElement(AND, "&", 0, 1, treeCharTab, getManager());
super.addInternal(comma, comma, element, Boolean.FALSE);
break;
}
}
for (ASTNode child = element.getTreePrev(); child != null; child = child.getTreePrev()) {
if (child.getElementType() == AND)
break;
if (child.getElementType() == JAVA_CODE_REFERENCE) {
TreeElement comma = Factory.createSingleLeafElement(AND, "&", 0, 1, treeCharTab, getManager());
super.addInternal(comma, comma, child, Boolean.FALSE);
break;
}
}
}
final IElementType keywordType = JavaTokenType.EXTENDS_KEYWORD;
final String keywordText = PsiKeyword.EXTENDS;
if (findChildByType(keywordType) == null && findChildByType(JAVA_CODE_REFERENCE) != null) {
LeafElement keyword = Factory.createSingleLeafElement(keywordType, keywordText, treeCharTab, getManager());
super.addInternal(keyword, keyword, getFirstChildNode(), Boolean.TRUE);
}
return firstAdded;
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class ReferenceListElement method addInternal.
@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
if (first == last && first.getElementType() == JavaElementType.JAVA_CODE_REFERENCE) {
if (getLastChildNode() != null && getLastChildNode().getElementType() == TokenType.ERROR_ELEMENT) {
super.deleteChildInternal(getLastChildNode());
}
}
final TreeElement firstAdded = super.addInternal(first, last, anchor, before);
final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
if (first == last && first.getElementType() == JavaElementType.JAVA_CODE_REFERENCE) {
ASTNode element = first;
for (ASTNode child = element.getTreeNext(); child != null; child = child.getTreeNext()) {
if (child.getElementType() == JavaTokenType.COMMA)
break;
if (child.getElementType() == JavaElementType.JAVA_CODE_REFERENCE) {
TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, treeCharTab, getManager());
super.addInternal(comma, comma, element, Boolean.FALSE);
break;
}
}
for (ASTNode child = element.getTreePrev(); child != null; child = child.getTreePrev()) {
if (child.getElementType() == JavaTokenType.COMMA)
break;
if (child.getElementType() == JavaElementType.JAVA_CODE_REFERENCE) {
TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, treeCharTab, getManager());
super.addInternal(comma, comma, child, Boolean.FALSE);
break;
}
}
}
IElementType keywordType = getKeywordType();
String keywordText = getKeywordText();
if (findChildByType(keywordType) == null && findChildByType(JavaElementType.JAVA_CODE_REFERENCE) != null) {
LeafElement keyword = Factory.createSingleLeafElement(keywordType, keywordText, SharedImplUtil.findCharTableByTree(this), getManager());
super.addInternal(keyword, keyword, getFirstChildNode(), Boolean.TRUE);
}
return firstAdded;
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class PsiExpressionListImpl method addInternal.
@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(this);
if (anchor == null) {
if (before == null || before.booleanValue()) {
anchor = findChildByRole(ChildRole.RPARENTH);
if (anchor == null) {
LeafElement lparenth = Factory.createSingleLeafElement(JavaTokenType.LPARENTH, "(", 0, 1, treeCharTab, getManager());
super.addInternal(lparenth, lparenth, null, Boolean.FALSE);
LeafElement rparenth = Factory.createSingleLeafElement(JavaTokenType.RPARENTH, ")", 0, 1, treeCharTab, getManager());
super.addInternal(rparenth, rparenth, null, Boolean.TRUE);
anchor = findChildByRole(ChildRole.RPARENTH);
LOG.assertTrue(anchor != null);
}
before = Boolean.TRUE;
} else {
anchor = findChildByRole(ChildRole.LPARENTH);
if (anchor == null) {
LeafElement lparenth = Factory.createSingleLeafElement(JavaTokenType.LPARENTH, "(", 0, 1, treeCharTab, getManager());
super.addInternal(lparenth, lparenth, null, Boolean.FALSE);
LeafElement rparenth = Factory.createSingleLeafElement(JavaTokenType.RPARENTH, ")", 0, 1, treeCharTab, getManager());
super.addInternal(rparenth, rparenth, null, Boolean.TRUE);
anchor = findChildByRole(ChildRole.LPARENTH);
LOG.assertTrue(anchor != null);
}
before = Boolean.FALSE;
}
}
TreeElement firstAdded = super.addInternal(first, last, anchor, before);
if (ElementType.EXPRESSION_BIT_SET.contains(first.getElementType())) {
JavaSourceUtil.addSeparatingComma(this, first, ElementType.EXPRESSION_BIT_SET);
}
return firstAdded;
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class CodeStyleManagerImpl method doFindWhiteSpaceNode.
@NotNull
private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@NotNull PsiFile file, int offset) {
ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file);
if (!(astNode instanceof FileElement)) {
return new Pair<>(null, null);
}
PsiElement elementAt = InjectedLanguageUtil.findInjectedElementNoCommit(file, offset);
final CharTable charTable = ((FileElement) astNode).getCharTable();
if (elementAt == null) {
elementAt = findElementInTreeWithFormatterEnabled(file, offset);
}
if (elementAt == null) {
return new Pair<>(null, charTable);
}
ASTNode node = elementAt.getNode();
if (node == null || node.getElementType() != TokenType.WHITE_SPACE) {
return new Pair<>(null, charTable);
}
return Pair.create(elementAt, charTable);
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class FormatterUtil method replaceWhiteSpace.
public static void replaceWhiteSpace(final String whiteSpace, final ASTNode leafElement, final IElementType whiteSpaceToken, @Nullable final TextRange textRange) {
final CharTable charTable = SharedImplUtil.findCharTableByTree(leafElement);
ASTNode treePrev = findPreviousWhiteSpace(leafElement, whiteSpaceToken);
if (treePrev == null) {
treePrev = getWsCandidate(leafElement);
}
if (treePrev != null && treePrev.getText().trim().isEmpty() && treePrev.getElementType() != whiteSpaceToken && treePrev.getTextLength() > 0 && !whiteSpace.isEmpty()) {
LeafElement whiteSpaceElement = Factory.createSingleLeafElement(treePrev.getElementType(), whiteSpace, charTable, SharedImplUtil.getManagerByTree(leafElement));
ASTNode treeParent = treePrev.getTreeParent();
treeParent.replaceChild(treePrev, whiteSpaceElement);
} else {
LeafElement whiteSpaceElement = Factory.createSingleLeafElement(whiteSpaceToken, whiteSpace, charTable, SharedImplUtil.getManagerByTree(leafElement));
if (treePrev == null) {
if (!whiteSpace.isEmpty()) {
addWhiteSpace(leafElement, whiteSpaceElement);
}
} else {
if (!(treePrev.getElementType() == whiteSpaceToken)) {
if (!whiteSpace.isEmpty()) {
addWhiteSpace(treePrev, whiteSpaceElement);
}
} else {
if (treePrev.getElementType() == whiteSpaceToken) {
final CompositeElement treeParent = (CompositeElement) treePrev.getTreeParent();
if (!whiteSpace.isEmpty()) {
// LOG.assertTrue(textRange == null || treeParent.getTextRange().equals(textRange));
treeParent.replaceChild(treePrev, whiteSpaceElement);
} else {
treeParent.removeChild(treePrev);
}
// There is a possible case that more than one PSI element is matched by the target text range.
// That is the case, for example, for Python's multi-line expression. It may looks like below:
// import contextlib,\
// math, decimal
// Here single range contains two blocks: '\' & '\n '. So, we may want to replace that range to another text, hence,
// we replace last element located there with it ('\n ') and want to remove any remaining elements ('\').
ASTNode removeCandidate = findPreviousWhiteSpace(whiteSpaceElement, whiteSpaceToken);
while (textRange != null && removeCandidate != null && removeCandidate.getStartOffset() >= textRange.getStartOffset()) {
treePrev = findPreviousWhiteSpace(removeCandidate, whiteSpaceToken);
removeCandidate.getTreeParent().removeChild(removeCandidate);
removeCandidate = treePrev;
}
//treeParent.subtreeChanged();
}
}
}
}
}
Aggregations