use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class TagDeclarationRangeHandler method getDeclarationRange.
@Override
@NotNull
public TextRange getDeclarationRange(@NotNull final PsiElement container) {
XmlTag xmlTag = (XmlTag) container;
int endOffset = xmlTag.getTextRange().getStartOffset();
for (PsiElement child = xmlTag.getFirstChild(); child != null; child = child.getNextSibling()) {
endOffset = child.getTextRange().getEndOffset();
if (child instanceof XmlToken) {
XmlToken token = (XmlToken) child;
IElementType tokenType = token.getTokenType();
if (tokenType == XmlTokenType.XML_EMPTY_ELEMENT_END || tokenType == XmlTokenType.XML_TAG_END)
break;
}
}
return new TextRange(xmlTag.getTextRange().getStartOffset(), endOffset);
}
use of com.intellij.psi.tree.IElementType in project intellij-community by JetBrains.
the class XmlZenCodingGenerator method computeTemplateKey.
@Nullable
@Override
public String computeTemplateKey(@NotNull CustomTemplateCallback callback) {
Editor editor = callback.getEditor();
int currentOffset = editor.getCaretModel().getOffset();
int startOffset = Math.min(editor.getDocument().getLineStartOffset(editor.getDocument().getLineNumber(currentOffset)), currentOffset);
CharSequence documentText = editor.getDocument().getCharsSequence();
PsiElement prevVisibleLeaf = callback.getContext();
while (prevVisibleLeaf != null) {
TextRange textRange = prevVisibleLeaf.getTextRange();
final int endOffset = textRange.getEndOffset();
if (endOffset <= currentOffset) {
if (endOffset <= startOffset) {
break;
}
IElementType prevType = prevVisibleLeaf.getNode().getElementType();
if (prevType == XmlTokenType.XML_TAG_END || prevType == XmlTokenType.XML_EMPTY_ELEMENT_END) {
startOffset = endOffset;
break;
}
}
prevVisibleLeaf = PsiTreeUtil.prevVisibleLeaf(prevVisibleLeaf);
}
if (startOffset < 0 || currentOffset > documentText.length() || currentOffset < startOffset) {
Logger.getInstance(getClass()).error("Error while calculating emmet abbreviation. Offset: " + currentOffset + "; Start: " + startOffset, AttachmentFactory.createAttachment(editor.getDocument()));
return null;
}
String key = computeKey(documentText.subSequence(startOffset, currentOffset));
return !StringUtil.isEmpty(key) && ZenCodingTemplate.checkTemplateKey(key, callback, this) ? key : null;
}
use of com.intellij.psi.tree.IElementType in project kotlin by JetBrains.
the class DebugInfoUtil method markDebugAnnotations.
public static void markDebugAnnotations(@NotNull PsiElement root, @NotNull final BindingContext bindingContext, @NotNull final DebugInfoReporter debugInfoReporter) {
final Map<KtReferenceExpression, DiagnosticFactory<?>> markedWithErrorElements = Maps.newHashMap();
for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
DiagnosticFactory<?> factory = diagnostic.getFactory();
if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(diagnostic.getFactory())) {
markedWithErrorElements.put((KtReferenceExpression) diagnostic.getPsiElement(), factory);
} else if (factory == Errors.SUPER_IS_NOT_AN_EXPRESSION || factory == Errors.SUPER_NOT_AVAILABLE) {
KtSuperExpression superExpression = (KtSuperExpression) diagnostic.getPsiElement();
markedWithErrorElements.put(superExpression.getInstanceReference(), factory);
} else if (factory == Errors.EXPRESSION_EXPECTED_PACKAGE_FOUND) {
markedWithErrorElements.put((KtSimpleNameExpression) diagnostic.getPsiElement(), factory);
} else if (factory == Errors.UNSUPPORTED) {
for (KtReferenceExpression reference : PsiTreeUtil.findChildrenOfType(diagnostic.getPsiElement(), KtReferenceExpression.class)) {
markedWithErrorElements.put(reference, factory);
}
}
}
root.acceptChildren(new KtTreeVisitorVoid() {
@Override
public void visitForExpression(@NotNull KtForExpression expression) {
KtExpression range = expression.getLoopRange();
reportIfDynamicCall(range, range, LOOP_RANGE_ITERATOR_RESOLVED_CALL);
reportIfDynamicCall(range, range, LOOP_RANGE_HAS_NEXT_RESOLVED_CALL);
reportIfDynamicCall(range, range, LOOP_RANGE_NEXT_RESOLVED_CALL);
super.visitForExpression(expression);
}
@Override
public void visitDestructuringDeclaration(@NotNull KtDestructuringDeclaration destructuringDeclaration) {
for (KtDestructuringDeclarationEntry entry : destructuringDeclaration.getEntries()) {
reportIfDynamicCall(entry, entry, COMPONENT_RESOLVED_CALL);
}
super.visitDestructuringDeclaration(destructuringDeclaration);
}
@Override
public void visitProperty(@NotNull KtProperty property) {
VariableDescriptor descriptor = bindingContext.get(VARIABLE, property);
if (descriptor instanceof PropertyDescriptor && property.getDelegate() != null) {
PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor;
reportIfDynamicCall(property.getDelegate(), propertyDescriptor, PROVIDE_DELEGATE_RESOLVED_CALL);
reportIfDynamicCall(property.getDelegate(), propertyDescriptor.getGetter(), DELEGATED_PROPERTY_RESOLVED_CALL);
reportIfDynamicCall(property.getDelegate(), propertyDescriptor.getSetter(), DELEGATED_PROPERTY_RESOLVED_CALL);
}
super.visitProperty(property);
}
@Override
public void visitThisExpression(@NotNull KtThisExpression expression) {
ResolvedCall<? extends CallableDescriptor> resolvedCall = CallUtilKt.getResolvedCall(expression, bindingContext);
if (resolvedCall != null) {
reportIfDynamic(expression, resolvedCall.getResultingDescriptor(), debugInfoReporter);
}
super.visitThisExpression(expression);
}
@Override
public void visitReferenceExpression(@NotNull KtReferenceExpression expression) {
super.visitReferenceExpression(expression);
if (!BindingContextUtils.isExpressionWithValidReference(expression, bindingContext)) {
return;
}
IElementType referencedNameElementType = null;
if (expression instanceof KtSimpleNameExpression) {
KtSimpleNameExpression nameExpression = (KtSimpleNameExpression) expression;
IElementType elementType = expression.getNode().getElementType();
if (elementType == KtNodeTypes.OPERATION_REFERENCE) {
referencedNameElementType = nameExpression.getReferencedNameElementType();
if (EXCLUDED.contains(referencedNameElementType)) {
return;
}
}
if (elementType == KtNodeTypes.LABEL || nameExpression.getReferencedNameElementType() == KtTokens.THIS_KEYWORD) {
return;
}
}
debugInfoReporter.preProcessReference(expression);
String target = null;
DeclarationDescriptor declarationDescriptor = bindingContext.get(REFERENCE_TARGET, expression);
if (declarationDescriptor != null) {
target = declarationDescriptor.toString();
reportIfDynamic(expression, declarationDescriptor, debugInfoReporter);
}
if (target == null) {
PsiElement labelTarget = bindingContext.get(LABEL_TARGET, expression);
if (labelTarget != null) {
target = labelTarget.getText();
}
}
if (target == null) {
Collection<? extends DeclarationDescriptor> declarationDescriptors = bindingContext.get(AMBIGUOUS_REFERENCE_TARGET, expression);
if (declarationDescriptors != null) {
target = "[" + declarationDescriptors.size() + " descriptors]";
}
}
if (target == null) {
Collection<? extends PsiElement> labelTargets = bindingContext.get(AMBIGUOUS_LABEL_TARGET, expression);
if (labelTargets != null) {
target = "[" + labelTargets.size() + " elements]";
}
}
if (MAY_BE_UNRESOLVED.contains(referencedNameElementType)) {
return;
}
boolean resolved = target != null;
boolean markedWithError = markedWithErrorElements.containsKey(expression);
if (expression instanceof KtArrayAccessExpression && markedWithErrorElements.containsKey(((KtArrayAccessExpression) expression).getArrayExpression())) {
// if 'foo' in 'foo[i]' is unresolved it means 'foo[i]' is unresolved (otherwise 'foo[i]' is marked as 'missing unresolved')
markedWithError = true;
}
KotlinType expressionType = bindingContext.getType(expression);
DiagnosticFactory<?> factory = markedWithErrorElements.get(expression);
if (declarationDescriptor != null && (ErrorUtils.isError(declarationDescriptor) || ErrorUtils.containsErrorType(expressionType))) {
if (factory != Errors.EXPRESSION_EXPECTED_PACKAGE_FOUND) {
debugInfoReporter.reportElementWithErrorType(expression);
}
}
if (resolved && markedWithError) {
if (Errors.UNRESOLVED_REFERENCE_DIAGNOSTICS.contains(factory)) {
debugInfoReporter.reportUnresolvedWithTarget(expression, target);
}
} else if (!resolved && !markedWithError) {
debugInfoReporter.reportMissingUnresolved(expression);
}
}
private <E extends KtElement, K, D extends CallableDescriptor> boolean reportIfDynamicCall(E element, K key, WritableSlice<K, ResolvedCall<D>> slice) {
ResolvedCall<D> resolvedCall = bindingContext.get(slice, key);
if (resolvedCall != null) {
return reportIfDynamic(element, resolvedCall.getResultingDescriptor(), debugInfoReporter);
}
return false;
}
});
}
use of com.intellij.psi.tree.IElementType in project kotlin by JetBrains.
the class KtPsiUtil method areParenthesesNecessary.
public static boolean areParenthesesNecessary(@NotNull KtExpression innerExpression, @NotNull KtExpression currentInner, @NotNull KtElement parentElement) {
if (parentElement instanceof KtParenthesizedExpression || innerExpression instanceof KtParenthesizedExpression) {
return false;
}
if (parentElement instanceof KtPackageDirective)
return false;
if (parentElement instanceof KtWhenExpression || innerExpression instanceof KtWhenExpression) {
return false;
}
if (innerExpression instanceof KtIfExpression) {
if (parentElement instanceof KtQualifiedExpression)
return true;
PsiElement current = parentElement;
while (!(current instanceof KtBlockExpression || current instanceof KtDeclaration || current instanceof KtStatementExpression || current instanceof KtFile)) {
if (current.getTextRange().getEndOffset() != currentInner.getTextRange().getEndOffset()) {
// if current expression is "guarded" by parenthesis, no extra parenthesis is necessary
return !(current instanceof KtParenthesizedExpression) && !(current instanceof KtValueArgumentList);
}
current = current.getParent();
}
}
if (parentElement instanceof KtCallExpression && currentInner == ((KtCallExpression) parentElement).getCalleeExpression()) {
if (innerExpression instanceof KtSimpleNameExpression)
return false;
if (KtPsiUtilKt.getQualifiedExpressionForSelector(parentElement) != null)
return true;
return !(innerExpression instanceof KtThisExpression || innerExpression instanceof KtArrayAccessExpression || innerExpression instanceof KtConstantExpression || innerExpression instanceof KtStringTemplateExpression || innerExpression instanceof KtCallExpression);
}
if (parentElement instanceof KtValueArgument) {
// a(___, d > (e + f)) => a((b < c), d > (e + f)) to prevent parsing < c, d > as type argument list
KtValueArgument nextArg = PsiTreeUtil.getNextSiblingOfType(parentElement, KtValueArgument.class);
PsiElement nextExpression = nextArg != null ? nextArg.getArgumentExpression() : null;
if (innerExpression instanceof KtBinaryExpression && ((KtBinaryExpression) innerExpression).getOperationToken() == KtTokens.LT && nextExpression instanceof KtBinaryExpression && ((KtBinaryExpression) nextExpression).getOperationToken() == KtTokens.GT)
return true;
}
if (!(parentElement instanceof KtExpression))
return false;
IElementType innerOperation = getOperation(innerExpression);
IElementType parentOperation = getOperation((KtExpression) parentElement);
// 'return (@label{...})' case
if (parentElement instanceof KtReturnExpression && (innerExpression instanceof KtLabeledExpression || innerExpression instanceof KtAnnotatedExpression))
return true;
// '(x: Int) < y' case
if (innerExpression instanceof KtBinaryExpressionWithTypeRHS && parentOperation == KtTokens.LT) {
return true;
}
if (parentElement instanceof KtLabeledExpression)
return false;
// 'x ?: ...' case
if (parentElement instanceof KtBinaryExpression && parentOperation == KtTokens.ELVIS && !(innerExpression instanceof KtBinaryExpression) && currentInner == ((KtBinaryExpression) parentElement).getRight()) {
return false;
}
int innerPriority = getPriority(innerExpression);
int parentPriority = getPriority((KtExpression) parentElement);
if (innerPriority == parentPriority) {
if (parentElement instanceof KtBinaryExpression) {
if (innerOperation == KtTokens.ANDAND || innerOperation == KtTokens.OROR) {
return false;
}
return ((KtBinaryExpression) parentElement).getRight() == currentInner;
}
//'-(-x)' case
if (parentElement instanceof KtPrefixExpression && innerExpression instanceof KtPrefixExpression) {
return innerOperation == parentOperation && (innerOperation == KtTokens.PLUS || innerOperation == KtTokens.MINUS);
}
return false;
}
return innerPriority < parentPriority;
}
use of com.intellij.psi.tree.IElementType in project kotlin by JetBrains.
the class KtTypeProjection method getProjectionKind.
@NotNull
public KtProjectionKind getProjectionKind() {
KotlinTypeProjectionStub stub = getStub();
if (stub != null) {
return stub.getProjectionKind();
}
PsiElement projectionToken = getProjectionToken();
IElementType token = projectionToken != null ? projectionToken.getNode().getElementType() : null;
for (KtProjectionKind projectionKind : KtProjectionKind.values()) {
if (projectionKind.getToken() == token) {
return projectionKind;
}
}
throw new IllegalStateException(projectionToken.getText());
}
Aggregations