use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class ChangeSignatureUtil method synchronizeList.
public static <Parent extends PsiElement, Child extends PsiElement> void synchronizeList(Parent list, List<Child> newElements, ChildrenGenerator<Parent, Child> generator, boolean[] shouldRemoveChild) throws IncorrectOperationException {
List<Child> elementsToRemove = null;
List<Child> elements;
int index = 0;
while (true) {
elements = generator.getChildren(list);
if (index == newElements.size())
break;
if (elementsToRemove == null) {
elementsToRemove = new ArrayList<>();
for (int i = 0; i < shouldRemoveChild.length; i++) {
if (shouldRemoveChild[i] && i < elements.size()) {
elementsToRemove.add(elements.get(i));
}
}
}
Child oldElement = index < elements.size() ? elements.get(index) : null;
Child newElement = newElements.get(index);
if (newElement != null) {
if (!newElement.equals(oldElement)) {
if (oldElement != null && elementsToRemove.contains(oldElement)) {
oldElement.delete();
index--;
} else {
assert list.isWritable() : PsiUtilCore.getVirtualFile(list);
list.addBefore(newElement, oldElement);
if (list.equals(newElement.getParent())) {
newElement.delete();
}
}
}
} else {
if (newElements.size() > 1 && (!elements.isEmpty() || index < newElements.size() - 1)) {
PsiElement anchor;
if (index == 0) {
anchor = list.getFirstChild();
} else {
anchor = index - 1 < elements.size() ? elements.get(index - 1) : null;
}
CharTable charTable = SharedImplUtil.findCharTableByTree(list.getNode());
PsiElement psi = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, charTable, list.getManager()).getPsi();
if (anchor != null) {
list.addAfter(psi, anchor);
} else {
list.add(psi);
}
}
}
index++;
}
for (int i = newElements.size(); i < elements.size(); i++) {
Child element = elements.get(i);
element.delete();
}
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class PsiDocParamRef method getReference.
@Override
public PsiReference getReference() {
final PsiDocComment comment = PsiTreeUtil.getParentOfType(this, PsiDocComment.class);
if (comment == null)
return null;
final PsiJavaDocumentedElement owner = comment.getOwner();
if (!(owner instanceof PsiMethod) && !(owner instanceof PsiClass))
return null;
final ASTNode valueToken = findChildByType(JavaDocTokenType.DOC_TAG_VALUE_TOKEN);
if (valueToken == null)
return null;
final String name = valueToken.getText();
PsiElement reference = null;
final PsiElement firstChild = getFirstChild();
if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
final PsiTypeParameter[] typeParameters = ((PsiTypeParameterListOwner) owner).getTypeParameters();
for (PsiTypeParameter typeParameter : typeParameters) {
if (typeParameter.getName().equals(name)) {
reference = typeParameter;
}
}
} else if (owner instanceof PsiMethod) {
final PsiParameter[] parameters = ((PsiMethod) owner).getParameterList().getParameters();
for (PsiParameter parameter : parameters) {
if (parameter.getName().equals(name)) {
reference = parameter;
}
}
}
final PsiElement resultReference = reference;
return new PsiJavaReference() {
@Override
public PsiElement resolve() {
return resultReference;
}
@Override
@NotNull
public String getCanonicalText() {
return valueToken.getText();
}
@Override
public PsiElement handleElementRename(String newElementName) {
final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(getNode());
LeafElement newElement = Factory.createSingleLeafElement(JavaDocTokenType.DOC_TAG_VALUE_TOKEN, newElementName, charTableByTree, getManager());
replaceChild(valueToken, newElement);
return PsiDocParamRef.this;
}
@Override
public PsiElement bindToElement(@NotNull PsiElement element) throws IncorrectOperationException {
if (isReferenceTo(element))
return PsiDocParamRef.this;
if (!(element instanceof PsiParameter)) {
throw new IncorrectOperationException("Unsupported operation");
}
return handleElementRename(((PsiParameter) element).getName());
}
@Override
public boolean isReferenceTo(PsiElement element) {
if (!(element instanceof PsiNamedElement))
return false;
PsiNamedElement namedElement = (PsiNamedElement) element;
if (!getCanonicalText().equals(namedElement.getName()))
return false;
return getManager().areElementsEquivalent(resolve(), element);
}
@Override
@NotNull
public PsiElement[] getVariants() {
final PsiElement firstChild = getFirstChild();
Set<String> usedNames = new HashSet<>();
for (PsiDocTag tag : comment.getTags()) {
if (tag.getName().equals("param")) {
PsiDocTagValue valueElement = tag.getValueElement();
if (valueElement != null) {
usedNames.add(valueElement.getText());
}
}
}
PsiNamedElement[] result = PsiNamedElement.EMPTY_ARRAY;
if (firstChild instanceof PsiDocToken && ((PsiDocToken) firstChild).getTokenType().equals(JavaDocTokenType.DOC_TAG_VALUE_LT)) {
result = ((PsiTypeParameterListOwner) owner).getTypeParameters();
} else if (owner instanceof PsiMethod) {
result = ((PsiMethod) owner).getParameterList().getParameters();
}
List<PsiElement> filtered = new ArrayList<>();
for (PsiNamedElement namedElement : result) {
if (!usedNames.contains(namedElement.getName())) {
filtered.add(namedElement);
}
}
return filtered.toArray(new PsiElement[filtered.size()]);
}
@Override
public boolean isSoft() {
return false;
}
@Override
public TextRange getRangeInElement() {
final int startOffsetInParent = valueToken.getPsi().getStartOffsetInParent();
return new TextRange(startOffsetInParent, startOffsetInParent + valueToken.getTextLength());
}
@Override
public PsiElement getElement() {
return PsiDocParamRef.this;
}
@Override
public void processVariants(@NotNull PsiScopeProcessor processor) {
for (final PsiElement element : getVariants()) {
if (!processor.execute(element, ResolveState.initial())) {
return;
}
}
}
@Override
@NotNull
public JavaResolveResult advancedResolve(boolean incompleteCode) {
return resultReference == null ? JavaResolveResult.EMPTY : new CandidateInfo(resultReference, PsiSubstitutor.EMPTY);
}
@Override
@NotNull
public JavaResolveResult[] multiResolve(boolean incompleteCode) {
return resultReference == null ? JavaResolveResult.EMPTY_ARRAY : new JavaResolveResult[] { new CandidateInfo(resultReference, PsiSubstitutor.EMPTY) };
}
};
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class PsiDocCommentImpl method addInternal.
@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
boolean needToAddNewline = false;
if (first == last && first.getElementType() == DOC_TAG) {
if (anchor == null) {
// this is a '*/'
anchor = getLastChildNode();
final ASTNode prevBeforeWS = TreeUtil.skipElementsBack(anchor.getTreePrev(), ElementType.JAVA_WHITESPACE_BIT_SET);
if (prevBeforeWS != null) {
anchor = prevBeforeWS;
before = Boolean.FALSE;
} else {
before = Boolean.TRUE;
}
needToAddNewline = true;
}
if (anchor.getElementType() != DOC_TAG) {
if (nodeOnSameLineWithCommentStartBlock(anchor) || !nodeIsNextAfterAsterisks(anchor) || !docTagEndsWithLineFeedAndAsterisks(first)) {
final CharTable charTable = SharedImplUtil.findCharTableByTree(this);
final TreeElement newLine = Factory.createSingleLeafElement(DOC_COMMENT_DATA, "\n", 0, 1, charTable, getManager());
final TreeElement leadingAsterisk = Factory.createSingleLeafElement(DOC_COMMENT_LEADING_ASTERISKS, "*", 0, 1, charTable, getManager());
final TreeElement commentData = Factory.createSingleLeafElement(DOC_COMMENT_DATA, " ", 0, 1, charTable, getManager());
final TreeElement indentWS = Factory.createSingleLeafElement(DOC_COMMENT_DATA, " ", 0, 1, charTable, getManager());
newLine.getTreeParent().addChild(indentWS);
newLine.getTreeParent().addChild(leadingAsterisk);
newLine.getTreeParent().addChild(commentData);
super.addInternal(newLine, commentData, anchor, Boolean.FALSE);
anchor = commentData;
before = Boolean.FALSE;
}
} else {
needToAddNewline = true;
}
}
if (before)
anchor.getTreeParent().addChildren(first, last.getTreeNext(), anchor);
else
anchor.getTreeParent().addChildren(first, last.getTreeNext(), anchor.getTreeNext());
if (needToAddNewline) {
if (first.getTreePrev() != null && first.getTreePrev().getElementType() == DOC_TAG) {
addNewLineToTag((CompositeElement) first.getTreePrev(), getProject());
}
if (first.getTreeNext() != null && first.getTreeNext().getElementType() == DOC_TAG) {
addNewLineToTag((CompositeElement) first, getProject());
} else {
removeEndingAsterisksFromTag((CompositeElement) first);
}
}
return first;
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class MethodElement method copyElement.
@Override
public ASTNode copyElement() {
CharTable table = SharedImplUtil.findCharTableByTree(this);
final PsiClass psiClass = ((PsiMethod) getPsi()).getContainingClass();
return psiClass != null ? ChangeUtil.copyElement(this, psiClass.getTypeParameterList(), table) : super.copyElement();
}
use of com.intellij.util.CharTable in project intellij-community by JetBrains.
the class PsiArrayInitializerExpressionImpl method addInternal.
@Override
public TreeElement addInternal(TreeElement first, ASTNode last, ASTNode anchor, Boolean before) {
if (anchor == null) {
if (before == null || before.booleanValue()) {
anchor = findChildByRole(ChildRole.RBRACE);
before = Boolean.TRUE;
} else {
anchor = findChildByRole(ChildRole.LBRACE);
before = Boolean.FALSE;
}
}
final TreeElement firstAdded = super.addInternal(first, last, anchor, before);
if (ElementType.EXPRESSION_BIT_SET.contains(first.getElementType())) {
final CharTable charTab = SharedImplUtil.findCharTableByTree(this);
for (ASTNode child = first.getTreeNext(); child != null; child = child.getTreeNext()) {
if (child.getElementType() == JavaTokenType.COMMA)
break;
if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) {
TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, charTab, getManager());
super.addInternal(comma, comma, first, Boolean.FALSE);
break;
}
}
for (ASTNode child = first.getTreePrev(); child != null; child = child.getTreePrev()) {
if (child.getElementType() == JavaTokenType.COMMA)
break;
if (ElementType.EXPRESSION_BIT_SET.contains(child.getElementType())) {
TreeElement comma = Factory.createSingleLeafElement(JavaTokenType.COMMA, ",", 0, 1, charTab, getManager());
super.addInternal(comma, comma, child, Boolean.FALSE);
break;
}
}
}
return firstAdded;
}
Aggregations