use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class GroovyChangeUtilSupport method decodeInformation.
@Override
public TreeElement decodeInformation(TreeElement element, final Map<Object, Object> decodingState) {
if (element instanceof CompositeElement) {
if (element.getElementType() == GroovyElementTypes.REFERENCE_ELEMENT || element.getElementType() == GroovyElementTypes.REFERENCE_EXPRESSION) {
GrReferenceElement ref = (GrReferenceElement) SourceTreeToPsiMap.treeElementToPsi(element);
final PsiMember refMember = element.getCopyableUserData(REFERENCED_MEMBER_KEY);
if (refMember != null) {
element.putCopyableUserData(REFERENCED_MEMBER_KEY, null);
PsiElement refElement1 = ref.resolve();
if (!refMember.getManager().areElementsEquivalent(refMember, refElement1)) {
try {
if (!(refMember instanceof PsiClass) || ref.getQualifier() == null) {
// can restore only if short (otherwise qualifier should be already restored)
ref = (GrReferenceElement) ref.bindToElement(refMember);
}
} catch (IncorrectOperationException ignored) {
}
return (TreeElement) SourceTreeToPsiMap.psiElementToTree(ref);
}
}
return element;
}
}
return null;
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class PsiImplUtil method getTextSkipWhiteSpaceAndComments.
/**
* see {@link com.intellij.psi.impl.source.tree.AstBufferUtil#getTextSkippingWhitespaceComments(com.intellij.lang.ASTNode)}
*/
public static String getTextSkipWhiteSpaceAndComments(ASTNode node) {
final TreeElement treeElement = (TreeElement) node;
final int length;
{
final GroovyBufferVisitor lengthVisitor = new GroovyBufferVisitor(true, true, 0, null);
treeElement.acceptTree(lengthVisitor);
length = lengthVisitor.getEnd();
}
final char[] buffer = new char[length];
{
final GroovyBufferVisitor textVisitor = new GroovyBufferVisitor(true, true, 0, buffer);
treeElement.acceptTree(textVisitor);
}
return StringFactory.createShared(buffer);
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class LowLevelSearchUtil method processTreeUp.
/**
* @return null to stop or last found TreeElement
* to be reused via <code>lastElement<code/> param in subsequent calls to avoid full tree rescan (n^2->n).
*/
private static TreeElement processTreeUp(@NotNull Project project, @NotNull TextOccurenceProcessor processor, @NotNull PsiElement scope, @NotNull StringSearcher searcher, final int offset, final boolean processInjectedPsi, @NotNull ProgressIndicator progress, TreeElement lastElement) {
if (scope instanceof PsiCompiledElement) {
throw new IllegalArgumentException("Scope is compiled, can't scan: " + scope);
}
final int scopeStartOffset = scope.getTextRange().getStartOffset();
final int patternLength = searcher.getPatternLength();
ASTNode scopeNode = scope.getNode();
boolean useTree = scopeNode != null;
assert scope.isValid();
int start;
TreeElement leafNode = null;
PsiElement leafElement = null;
if (useTree) {
leafNode = findNextLeafElementAt(scopeNode, lastElement, offset);
if (leafNode == null)
return lastElement;
start = offset - leafNode.getStartOffset() + scopeStartOffset;
} else {
if (scope instanceof PsiFile) {
leafElement = ((PsiFile) scope).getViewProvider().findElementAt(offset, scope.getLanguage());
} else {
leafElement = scope.findElementAt(offset);
}
if (leafElement == null)
return lastElement;
assert leafElement.isValid();
start = offset - leafElement.getTextRange().getStartOffset() + scopeStartOffset;
}
if (start < 0) {
throw new AssertionError("offset=" + offset + "; scopeStartOffset=" + scopeStartOffset + "; leafElement=" + leafElement + "; scope=" + scope + "; leafElement.isValid(): " + (leafElement == null ? null : leafElement.isValid()));
}
InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(project);
lastElement = leafNode;
boolean contains = false;
PsiElement prev = null;
TreeElement prevNode = null;
PsiElement run = null;
while (run != scope) {
progress.checkCanceled();
if (useTree) {
start += prevNode == null ? 0 : prevNode.getStartOffsetInParent();
prevNode = leafNode;
run = leafNode.getPsi();
} else {
start += prev == null ? 0 : prev.getStartOffsetInParent();
prev = run;
run = leafElement;
}
//do not compute if already contains
if (!contains)
contains = run.getTextLength() - start >= patternLength;
if (contains) {
if (processInjectedPsi) {
Boolean result = processInjectedFile(run, processor, searcher, progress, injectedLanguageManager);
if (result != null) {
return result.booleanValue() ? lastElement : null;
}
}
if (!processor.execute(run, start)) {
return null;
}
}
if (useTree) {
leafNode = leafNode.getTreeParent();
if (leafNode == null)
break;
} else {
leafElement = leafElement.getParent();
if (leafElement == null)
break;
}
}
assert run == scope : "Malbuilt PSI; scopeNode: " + scope + "; containingFile:" + PsiTreeUtil.getParentOfType(scope, PsiFile.class, false) + "; leafNode: " + run + "; isAncestor=" + PsiTreeUtil.isAncestor(scope, run, false) + "; in same file: " + (PsiTreeUtil.getParentOfType(scope, PsiFile.class, false) == PsiTreeUtil.getParentOfType(run, PsiFile.class, false));
return lastElement;
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class DeclarationMover method beforeMove.
@Override
public void beforeMove(@NotNull final Editor editor, @NotNull final MoveInfo info, final boolean down) {
super.beforeMove(editor, info, down);
if (myEnumToInsertSemicolonAfter != null) {
TreeElement semicolon = Factory.createSingleLeafElement(JavaTokenType.SEMICOLON, ";", 0, 1, null, myEnumToInsertSemicolonAfter.getManager());
try {
PsiElement inserted = myEnumToInsertSemicolonAfter.getParent().addAfter(semicolon.getPsi(), myEnumToInsertSemicolonAfter);
inserted = CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(inserted);
final LogicalPosition position = editor.offsetToLogicalPosition(inserted.getTextRange().getEndOffset());
info.toMove2 = new LineRange(position.line + 1, position.line + 1);
} catch (IncorrectOperationException e) {
LOG.error(e);
} finally {
myEnumToInsertSemicolonAfter = null;
}
}
}
use of com.intellij.psi.impl.source.tree.TreeElement in project intellij-community by JetBrains.
the class PsiModifierListImpl method setModifierProperty.
@Override
public void setModifierProperty(@NotNull String name, boolean value) throws IncorrectOperationException {
checkSetModifierProperty(name, value);
PsiElement parent = getParent();
PsiElement grandParent = parent != null ? parent.getParent() : null;
IElementType type = NAME_TO_KEYWORD_TYPE_MAP.get(name);
CompositeElement treeElement = (CompositeElement) getNode();
// changes horizontal position of parameters list start, hence, we need to reformat them in order to preserve alignment.
if (parent instanceof PsiMethod) {
PsiMethod method = (PsiMethod) parent;
CodeEditUtil.markToReformat(method.getParameterList().getNode(), true);
}
if (value) {
if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.PRIVATE_KEYWORD || type == JavaTokenType.PROTECTED_KEYWORD || type == null) /* package-private */
{
if (type != JavaTokenType.PUBLIC_KEYWORD) {
setModifierProperty(PUBLIC, false);
}
if (type != JavaTokenType.PRIVATE_KEYWORD) {
setModifierProperty(PRIVATE, false);
}
if (type != JavaTokenType.PROTECTED_KEYWORD) {
setModifierProperty(PROTECTED, false);
}
if (type == null)
return;
}
if (parent instanceof PsiField && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.STATIC_KEYWORD || type == JavaTokenType.FINAL_KEYWORD)
return;
} else if (parent instanceof PsiMethod && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD)
return;
} else if (parent instanceof PsiClass && grandParent instanceof PsiClass && ((PsiClass) grandParent).isInterface()) {
if (type == JavaTokenType.PUBLIC_KEYWORD)
return;
} else if (parent instanceof PsiAnnotationMethod && grandParent instanceof PsiClass && ((PsiClass) grandParent).isAnnotationType()) {
if (type == JavaTokenType.PUBLIC_KEYWORD || type == JavaTokenType.ABSTRACT_KEYWORD)
return;
}
if (treeElement.findChildByType(type) == null) {
TreeElement keyword = Factory.createSingleLeafElement(type, name, null, getManager());
treeElement.addInternal(keyword, keyword, null, null);
}
} else {
if (type == null) /* package-private */
{
//?
throw new IncorrectOperationException("Cannot reset package-private modifier.");
}
ASTNode child = treeElement.findChildByType(type);
if (child != null) {
SourceTreeToPsiMap.treeToPsiNotNull(child).delete();
}
}
}
Aggregations