Java Code Examples for com.intellij.lang.LighterASTNode

The following are top voted examples for showing how to use com.intellij.lang.LighterASTNode. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: intellij-ce-playground   File: JavaNameValuePairType.java   Source Code and License 6 votes vote down vote up
@Override
public PsiNameValuePairStub createStub(LighterAST tree, LighterASTNode node, StubElement parentStub) {
  String name = null;
  String value = null;
  List<LighterASTNode> children = tree.getChildren(node);
  for (LighterASTNode child : children) {
    if (child.getTokenType() == JavaTokenType.IDENTIFIER) {
      name = RecordUtil.intern(tree.getCharTable(), child);
    }
    else if (child.getTokenType() == JavaElementType.LITERAL_EXPRESSION) {
      value = RecordUtil.intern(tree.getCharTable(), tree.getChildren(child).get(0));
      value = StringUtil.unquoteString(value);
    }
  }
  return new PsiNameValuePairStubImpl(parentStub, StringRef.fromString(name), StringRef.fromString(value));
}
 
Example 2
Project: intellij-ce-playground   File: JavaImportStatementElementType.java   Source Code and License 6 votes vote down vote up
@Override
public PsiImportStatementStub createStub(LighterAST tree, LighterASTNode node, StubElement parentStub) {
  boolean isOnDemand = false;
  String refText = null;

  for (LighterASTNode child : tree.getChildren(node)) {
    IElementType type = child.getTokenType();
    if (type == JavaElementType.JAVA_CODE_REFERENCE || type == JavaElementType.IMPORT_STATIC_REFERENCE) {
      refText = JavaSourceUtil.getReferenceText(tree, child);
    }
    else if (type == JavaTokenType.ASTERISK) {
      isOnDemand = true;
    }
  }

  byte flags = PsiImportStatementStubImpl.packFlags(isOnDemand, node.getTokenType() == JavaElementType.IMPORT_STATIC_STATEMENT);
  return new PsiImportStatementStubImpl(parentStub, refText, flags);
}
 
Example 3
Project: intellij-ce-playground   File: RecordUtil.java   Source Code and License 6 votes vote down vote up
public static boolean isDeprecatedByAnnotation(@NotNull LighterAST tree, @NotNull LighterASTNode modList) {
  for (final LighterASTNode child : tree.getChildren(modList)) {
    if (child.getTokenType() == JavaElementType.ANNOTATION) {
      final LighterASTNode ref = LightTreeUtil.firstChildOfType(tree, child, JavaElementType.JAVA_CODE_REFERENCE);
      if (ref != null) {
        final LighterASTNode id = LightTreeUtil.firstChildOfType(tree, ref, JavaTokenType.IDENTIFIER);
        if (id != null) {
          final String name = intern(tree.getCharTable(), id);
          if (DEPRECATED_ANNOTATION_NAME.equals(name)) return true;
        }
      }
    }
  }

  return false;
}
 
Example 4
Project: intellij-ce-playground   File: JavaLightStubBuilder.java   Source Code and License 6 votes vote down vote up
@NotNull
@Override
protected StubElement createStubForFile(@NotNull PsiFile file, @NotNull LighterAST tree) {
  if (!(file instanceof PsiJavaFile)) {
    return super.createStubForFile(file, tree);
  }

  String refText = "";
  LighterASTNode pkg = LightTreeUtil.firstChildOfType(tree, tree.getRoot(), JavaElementType.PACKAGE_STATEMENT);
  if (pkg != null) {
    LighterASTNode ref = LightTreeUtil.firstChildOfType(tree, pkg, JavaElementType.JAVA_CODE_REFERENCE);
    if (ref != null) {
      refText = JavaSourceUtil.getReferenceText(tree, ref);
    }
  }
  return new PsiJavaFileStubImpl((PsiJavaFile)file, StringRef.fromString(refText), false);
}
 
Example 5
Project: intellij-ce-playground   File: LightTreeUtil.java   Source Code and License 6 votes vote down vote up
public static void toBuffer(@NotNull LighterAST tree, @NotNull LighterASTNode node, @NotNull StringBuilder buffer, @Nullable TokenSet skipTypes) {
  if (skipTypes != null && skipTypes.contains(node.getTokenType())) {
    return;
  }

  if (node instanceof LighterASTTokenNode) {
    buffer.append(((LighterASTTokenNode)node).getText());
    return;
  }

  if (node instanceof LighterLazyParseableNode) {
    buffer.append(((LighterLazyParseableNode)node).getText());
    return;
  }

  List<LighterASTNode> children = tree.getChildren(node);
  for (int i = 0, size = children.size(); i < size; ++i) {
    toBuffer(tree, children.get(i), buffer, skipTypes);
  }
  tree.disposeChildren(children);
}
 
Example 6
Project: intellij-ce-playground   File: RecursiveLighterASTNodeWalkingVisitor.java   Source Code and License 6 votes vote down vote up
@Override
public IndexedLighterASTNode getFirstChild(@NotNull IndexedLighterASTNode element) {
  List<LighterASTNode> children = ast.getChildren(element.node);
  IndexedLighterASTNode[] indexedChildren = children.isEmpty() ? IndexedLighterASTNode.EMPTY_ARRAY : new IndexedLighterASTNode[children.size()];
  for (int i = 0; i < children.size(); i++) {
    LighterASTNode child = children.get(i);
    IndexedLighterASTNode indexedNode = new IndexedLighterASTNode(child, i == 0 ? null : indexedChildren[i - 1]);
    indexedChildren[i] = indexedNode;
    if (i != 0) {
      indexedChildren[i-1].next = indexedNode;
    }
  }
  childrenStack.push(indexedChildren);
  parentStack.push(element);
  return children.isEmpty() ? null : indexedChildren[0];
}
 
Example 7
Project: tools-idea   File: JavaNameValuePairType.java   Source Code and License 6 votes vote down vote up
@Override
public PsiNameValuePairStub createStub(LighterAST tree, LighterASTNode node, StubElement parentStub) {
  String name = null;
  String value = null;
  List<LighterASTNode> children = tree.getChildren(node);
  for (LighterASTNode child : children) {
    if (child.getTokenType() == JavaTokenType.IDENTIFIER) {
      name = RecordUtil.intern(tree.getCharTable(), child);
    }
    else if (child.getTokenType() == JavaElementType.LITERAL_EXPRESSION) {
      value = RecordUtil.intern(tree.getCharTable(), tree.getChildren(child).get(0));
      value = StringUtil.stripQuotesAroundValue(value);
    }
  }
  return new PsiNameValuePairStubImpl(parentStub, StringRef.fromString(name), StringRef.fromString(value));
}
 
Example 8
Project: tools-idea   File: JavaImportStatementElementType.java   Source Code and License 6 votes vote down vote up
@Override
public PsiImportStatementStub createStub(LighterAST tree, LighterASTNode node, StubElement parentStub) {
  boolean isOnDemand = false;
  String refText = null;

  for (LighterASTNode child : tree.getChildren(node)) {
    IElementType type = child.getTokenType();
    if (type == JavaElementType.JAVA_CODE_REFERENCE || type == JavaElementType.IMPORT_STATIC_REFERENCE) {
      refText = SourceUtil.getReferenceText(tree, child);
    }
    else if (type == JavaTokenType.ASTERISK) {
      isOnDemand = true;
    }
  }

  byte flags = PsiImportStatementStubImpl.packFlags(isOnDemand, node.getTokenType() == JavaElementType.IMPORT_STATIC_STATEMENT);
  return new PsiImportStatementStubImpl(parentStub, refText, flags);
}
 
Example 9
Project: tools-idea   File: RecordUtil.java   Source Code and License 6 votes vote down vote up
public static boolean isDeprecatedByAnnotation(@NotNull LighterAST tree, @NotNull LighterASTNode modList) {
  for (final LighterASTNode child : tree.getChildren(modList)) {
    if (child.getTokenType() == JavaElementType.ANNOTATION) {
      final LighterASTNode ref = LightTreeUtil.firstChildOfType(tree, child, JavaElementType.JAVA_CODE_REFERENCE);
      if (ref != null) {
        final LighterASTNode id = LightTreeUtil.firstChildOfType(tree, ref, JavaTokenType.IDENTIFIER);
        if (id != null) {
          final String name = intern(tree.getCharTable(), id);
          if (DEPRECATED_ANNOTATION_NAME.equals(name)) return true;
        }
      }
    }
  }

  return false;
}
 
Example 10
Project: tools-idea   File: JavaLightStubBuilder.java   Source Code and License 6 votes vote down vote up
@NotNull
@Override
protected StubElement createStubForFile(@NotNull PsiFile file, @NotNull LighterAST tree) {
  if (!(file instanceof PsiJavaFile)) {
    return super.createStubForFile(file, tree);
  }

  String refText = "";
  LighterASTNode pkg = LightTreeUtil.firstChildOfType(tree, tree.getRoot(), JavaElementType.PACKAGE_STATEMENT);
  if (pkg != null) {
    LighterASTNode ref = LightTreeUtil.firstChildOfType(tree, pkg, JavaElementType.JAVA_CODE_REFERENCE);
    if (ref != null) {
      refText = SourceUtil.getReferenceText(tree, ref);
    }
  }
  return new PsiJavaFileStubImpl((PsiJavaFile)file, StringRef.fromString(refText), false);
}
 
Example 11
Project: tools-idea   File: LightTreeUtil.java   Source Code and License 6 votes vote down vote up
public static void toBuffer(@NotNull LighterAST tree, @NotNull LighterASTNode node, @NotNull StringBuilder buffer, @Nullable TokenSet skipTypes) {
  if (skipTypes != null && skipTypes.contains(node.getTokenType())) return;

  if (node instanceof LighterASTTokenNode) {
    buffer.append(((LighterASTTokenNode)node).getText());
    return;
  }

  if (node instanceof LighterLazyParseableNode) {
    buffer.append(((LighterLazyParseableNode)node).getText());
    return;
  }

  List<LighterASTNode> children = tree.getChildren(node);
  for (int i = 0, size = children.size(); i < size; ++i) {
    toBuffer(tree, children.get(i), buffer, skipTypes);
  }
}
 
Example 12
Project: DeftIDEA   File: DylanParser.java   Source Code and License 6 votes vote down vote up
public static boolean definition_macro_call(PsiBuilder builder_, int level_) {
  if (!recursion_guard_(builder_, level_, "definition_macro_call")) return false;
  if (!nextTokenIs(builder_, DEFINE)) return false;
  boolean result_ = false;
  int start_ = builder_.getCurrentOffset();
  Marker marker_ = builder_.mark();
  result_ = definition_macro_call_0(builder_, level_ + 1);
  if (!result_) result_ = definition_macro_call_1(builder_, level_ + 1);
  LighterASTNode last_ = result_? builder_.getLatestDoneMarker() : null;
  if (last_ != null && last_.getStartOffset() == start_ && type_extends_(last_.getTokenType(), DEFINITION_MACRO_CALL)) {
    marker_.drop();
  }
  else if (result_) {
    marker_.done(DEFINITION_MACRO_CALL);
  }
  else {
    marker_.rollbackTo();
  }
  return result_;
}
 
Example 13
Project: consulo-java   File: JavaNullMethodArgumentIndex.java   Source Code and License 6 votes vote down vote up
@Nullable
private static IntArrayList getNullParameterIndices(LighterAST lighterAst, @NotNull LighterASTNode methodCall)
{
	final LighterASTNode node = LightTreeUtil.firstChildOfType(lighterAst, methodCall, EXPRESSION_LIST);
	if(node == null)
	{
		return null;
	}
	final List<LighterASTNode> parameters = JavaLightTreeUtil.getExpressionChildren(lighterAst, node);
	IntArrayList indices = new IntArrayList(1);
	for(int idx = 0; idx < parameters.size(); idx++)
	{
		if(isNullLiteral(lighterAst, parameters.get(idx)))
		{
			indices.add(idx);
		}
	}
	return indices;
}
 
Example 14
Project: DeftIDEA   File: DylanParser.java   Source Code and License 6 votes vote down vote up
public static boolean suite_component(PsiBuilder builder_, int level_) {
  if (!recursion_guard_(builder_, level_, "suite_component")) return false;
  if (!nextTokenIs(builder_, SUITE) && !nextTokenIs(builder_, TEST)
      && replaceVariants(builder_, 2, "<suite component>")) return false;
  boolean result_ = false;
  int start_ = builder_.getCurrentOffset();
  Marker marker_ = builder_.mark();
  enterErrorRecordingSection(builder_, level_, _SECTION_GENERAL_, "<suite component>");
  result_ = test_suite_component(builder_, level_ + 1);
  if (!result_) result_ = suite_suite_component(builder_, level_ + 1);
  LighterASTNode last_ = result_? builder_.getLatestDoneMarker() : null;
  if (last_ != null && last_.getStartOffset() == start_ && type_extends_(last_.getTokenType(), SUITE_COMPONENT)) {
    marker_.drop();
  }
  else if (result_) {
    marker_.done(SUITE_COMPONENT);
  }
  else {
    marker_.rollbackTo();
  }
  result_ = exitErrorRecordingSection(builder_, level_, result_, false, _SECTION_GENERAL_, null);
  return result_;
}
 
Example 15
Project: consulo   File: LightTreeUtil.java   Source Code and License 6 votes vote down vote up
public static void toBuffer(@Nonnull LighterAST tree, @Nonnull LighterASTNode node, @Nonnull StringBuilder buffer, @Nullable TokenSet skipTypes) {
  if (skipTypes != null && skipTypes.contains(node.getTokenType())) {
    return;
  }

  if (node instanceof LighterASTTokenNode) {
    buffer.append(((LighterASTTokenNode)node).getText());
    return;
  }

  if (node instanceof LighterLazyParseableNode) {
    buffer.append(((LighterLazyParseableNode)node).getText());
    return;
  }

  List<LighterASTNode> children = tree.getChildren(node);
  for (int i = 0, size = children.size(); i < size; ++i) {
    toBuffer(tree, children.get(i), buffer, skipTypes);
  }
}
 
Example 16
Project: consulo   File: RecursiveLighterASTNodeWalkingVisitor.java   Source Code and License 6 votes vote down vote up
@Override
public IndexedLighterASTNode getFirstChild(@Nonnull IndexedLighterASTNode element) {
  List<LighterASTNode> children = ast.getChildren(element.node);
  IndexedLighterASTNode[] indexedChildren = children.isEmpty() ? IndexedLighterASTNode.EMPTY_ARRAY : new IndexedLighterASTNode[children.size()];
  for (int i = 0; i < children.size(); i++) {
    LighterASTNode child = children.get(i);
    IndexedLighterASTNode indexedNode = new IndexedLighterASTNode(child, i == 0 ? null : indexedChildren[i - 1]);
    indexedChildren[i] = indexedNode;
    if (i != 0) {
      indexedChildren[i-1].next = indexedNode;
    }
  }
  childrenStack.push(indexedChildren);
  parentStack.push(element);
  return children.isEmpty() ? null : indexedChildren[0];
}
 
Example 17
Project: consulo-java   File: ContractInferenceIndexKt.java   Source Code and License 6 votes vote down vote up
@Nullable
private static MethodData calcData(LighterAST tree, LighterASTNode method)
{
	LighterASTNode body = LightTreeUtil.firstChildOfType(tree, method, JavaElementType.CODE_BLOCK);
	if(body == null)
	{
		return null;
	}
	List<LighterASTNode> statements = ContractInferenceInterpreter.getStatements(body, tree);
	List<PreContract> contracts = new ContractInferenceInterpreter(tree, method, body).inferContracts(statements);
	NullityInference.NullityInferenceVisitor nullityVisitor = new NullityInference.NullityInferenceVisitor(tree, body);
	PurityInference.PurityInferenceVisitor purityVisitor = new PurityInference.PurityInferenceVisitor(tree, body);

	for(LighterASTNode statement : statements)
	{
		walkMethodBody(tree, statement, it ->
		{
			nullityVisitor.visitNode(it);
			purityVisitor.visitNode(it);
		});
	}
	return createData(body, contracts, nullityVisitor.getResult(), purityVisitor.getResult());
}
 
Example 18
Project: consulo-xml   File: XmlParser.java   Source Code and License 6 votes vote down vote up
@Override
public ThreeState fun(ASTNode oldNode,
                      LighterASTNode newNode,
                      FlyweightCapableTreeStructure<LighterASTNode> structure) {
  if (oldNode instanceof XmlTag && newNode.getTokenType() == XmlElementType.XML_TAG) {
    String oldName = ((XmlTag)oldNode).getName();
    Ref<LighterASTNode[]> childrenRef = Ref.create(null);
    int count = structure.getChildren(newNode, childrenRef);
    if (count < 3) return ThreeState.UNSURE;
    LighterASTNode[] children = childrenRef.get();
    if (children[0].getTokenType() != XmlTokenType.XML_START_TAG_START) return ThreeState.UNSURE;
    if (children[1].getTokenType() != XmlTokenType.XML_NAME) return ThreeState.UNSURE;
    if (children[2].getTokenType() != XmlTokenType.XML_TAG_END) return ThreeState.UNSURE;
    LighterASTTokenNode name = (LighterASTTokenNode)children[1];
    CharSequence newName = name.getText();
    if (!oldName.equals(newName)) return ThreeState.NO;
  }

  return ThreeState.UNSURE;
}
 
Example 19
Project: consulo-xml   File: XmlBuilderDriver.java   Source Code and License 6 votes vote down vote up
public void build(XmlBuilder builder) {
  PsiBuilder b = createBuilderAndParse();

  FlyweightCapableTreeStructure<LighterASTNode> structure = b.getLightTree();

  LighterASTNode root = structure.getRoot();
  root = structure.prepareForGetChildren(root);

  final Ref<LighterASTNode[]> childrenRef = Ref.create(null);
  final int count = structure.getChildren(root, childrenRef);
  LighterASTNode[] children = childrenRef.get();

  for (int i = 0; i < count; i++) {
    LighterASTNode child = children[i];
    final IElementType tt = child.getTokenType();
    if (tt == XmlElementType.XML_TAG || tt == XmlElementType.HTML_TAG) {
      processTagNode(b, structure, child, builder);
    }
    else if (tt == XmlElementType.XML_PROLOG) {
      processPrologNode(b, builder, structure, child);
    }
  }

  structure.disposeChildren(children, count);
}
 
Example 20
Project: consulo-xml   File: XmlBuilderDriver.java   Source Code and License 6 votes vote down vote up
private void processPrologNode(PsiBuilder psiBuilder,
                               XmlBuilder builder,
                               FlyweightCapableTreeStructure<LighterASTNode> structure,
                               LighterASTNode prolog) {
  final Ref<LighterASTNode[]> prologChildren = new Ref<LighterASTNode[]>(null);
  final int prologChildrenCount = structure.getChildren(structure.prepareForGetChildren(prolog), prologChildren);
  for (int i = 0; i < prologChildrenCount; i++) {
    LighterASTNode node = prologChildren.get()[i];
    IElementType type = node.getTokenType();
    if (type == XmlElementType.XML_DOCTYPE) {
      processDoctypeNode(builder, structure, node);
      break;
    }
    if (type == TokenType.ERROR_ELEMENT) {
      processErrorNode(psiBuilder, node, builder);
    }
  }
}
 
Example 21
Project: consulo-java   File: ContractInferenceIndexKt.java   Source Code and License 6 votes vote down vote up
private static void walkMethodBody(LighterAST tree, LighterASTNode root, Consumer<LighterASTNode> processor)
{
	new RecursiveLighterASTNodeWalkingVisitor(tree)
	{
		@Override
		public void visitNode(@NotNull LighterASTNode element)
		{
			IElementType type = element.getTokenType();
			if(type == JavaElementType.CLASS || type == JavaElementType.FIELD || type == JavaElementType.METHOD || type == JavaElementType.ANNOTATION_METHOD || type == JavaElementType
					.LAMBDA_EXPRESSION)
			{
				return;
			}
			processor.accept(element);
			super.visitNode(element);
		}
	}.visitNode(root);
}
 
Example 22
Project: consulo-xml   File: XmlBuilderDriver.java   Source Code and License 6 votes vote down vote up
private CharSequence findTextByTokenType(LighterASTNode attrNode,
                                         FlyweightCapableTreeStructure<LighterASTNode> structure,
                                         IElementType tt) {
  attrNode = structure.prepareForGetChildren(attrNode);

  final Ref<LighterASTNode[]> childrenRef = Ref.create(null);
  final int count = structure.getChildren(attrNode, childrenRef);
  LighterASTNode[] children = childrenRef.get();

  CharSequence name = "";
  for (int i = 0; i < count; i++) {
    LighterASTNode child = children[i];
    if (child.getTokenType() == tt) {
      name = getTokenText(child);
      break;
    }
  }

  structure.disposeChildren(children, count);

  return name;
}
 
Example 23
Project: consulo-java   File: ContractInferenceInterpreter.java   Source Code and License 6 votes vote down vote up
@Nullable
private List<PreContract> handleDelegation(@Nullable LighterASTNode expression, boolean negated)
{
	if(expression == null)
	{
		return null;
	}
	if(expression.getTokenType() == PARENTH_EXPRESSION)
	{
		return handleDelegation(findExpressionChild(myTree, expression), negated);
	}

	if(isNegationExpression(expression))
	{
		return handleDelegation(findExpressionChild(myTree, expression), !negated);
	}

	if(expression.getTokenType() == METHOD_CALL_EXPRESSION)
	{
		return singletonList(new DelegationContract(ExpressionRange.create(expression, myBody.getStartOffset()), negated));
	}

	return null;
}
 
Example 24
Project: consulo-java   File: JavaNullMethodArgumentIndex.java   Source Code and License 6 votes vote down vote up
@NotNull
private static Set<LighterASTNode> findCallsWithNulls(LighterAST lighterAst, int[] nullOffsets)
{
	Set<LighterASTNode> calls = new HashSet<>();
	for(int offset : nullOffsets)
	{
		LighterASTNode leaf = LightTreeUtil.findLeafElementAt(lighterAst, offset);
		LighterASTNode literal = leaf == null ? null : lighterAst.getParent(leaf);
		if(isNullLiteral(lighterAst, literal))
		{
			LighterASTNode exprList = lighterAst.getParent(literal);
			if(exprList != null && exprList.getTokenType() == EXPRESSION_LIST)
			{
				ContainerUtil.addIfNotNull(calls, LightTreeUtil.getParentOfType(lighterAst, exprList, CALL_TYPES, ElementType.MEMBER_BIT_SET));
			}
		}
	}
	return calls;
}
 
Example 25
Project: consulo-java   File: ContractInferenceInterpreter.java   Source Code and License 6 votes vote down vote up
private int resolveParameter(@Nullable LighterASTNode expr)
{
	if(expr != null && expr.getTokenType() == REFERENCE_EXPRESSION && findExpressionChild(myTree, expr) == null)
	{
		String name = JavaLightTreeUtil.getNameIdentifierText(myTree, expr);
		if(name == null)
		{
			return -1;
		}

		List<LighterASTNode> parameters = getParameters();
		for(int i = 0; i < parameters.size(); i++)
		{
			if(name.equals(JavaLightTreeUtil.getNameIdentifierText(myTree, parameters.get(i))))
			{
				return i;
			}
		}
	}
	return -1;
}
 
Example 26
Project: consulo-java   File: ContractInferenceInterpreter.java   Source Code and License 6 votes vote down vote up
@NotNull
List<PreContract> inferContracts(List<LighterASTNode> statements)
{
	if(statements.isEmpty())
	{
		return emptyList();
	}

	if(statements.size() == 1)
	{
		List<PreContract> result = handleSingleStatement(statements.get(0));
		if(result != null)
		{
			return result;
		}
	}

	return visitStatements(singletonList(StandardMethodContract.createConstraintArray(getParameters().size())), statements);
}
 
Example 27
Project: consulo-java   File: JavaNameValuePairType.java   Source Code and License 6 votes vote down vote up
@Override
public PsiNameValuePairStub createStub(LighterAST tree, LighterASTNode node, StubElement parentStub) {
  String name = null;
  String value = null;
  List<LighterASTNode> children = tree.getChildren(node);
  for (LighterASTNode child : children) {
    if (child.getTokenType() == JavaTokenType.IDENTIFIER) {
      name = RecordUtil.intern(tree.getCharTable(), child);
    }
    else if (child.getTokenType() == JavaElementType.LITERAL_EXPRESSION) {
      value = RecordUtil.intern(tree.getCharTable(), tree.getChildren(child).get(0));
      value = StringUtil.stripQuotesAroundValue(value);
    }
  }
  return new PsiNameValuePairStubImpl(parentStub, StringRef.fromString(name), StringRef.fromString(value));
}
 
Example 28
Project: consulo-java   File: NullityInference.java   Source Code and License 6 votes vote down vote up
void visitNode(LighterASTNode element)
{
	IElementType type = element.getTokenType();

	if(type == TokenType.ERROR_ELEMENT)
	{
		hasErrors = true;
	}
	else if(type == RETURN_STATEMENT)
	{
		LighterASTNode value = JavaLightTreeUtil.findExpressionChild(tree, element);
		if(value == null)
		{
			hasErrors = true;
		}
		else
		{
			visitReturnedValue(value);
		}
	}
}
 
Example 29
Project: consulo-java   File: LambdaExpressionElementType.java   Source Code and License 6 votes vote down vote up
private static void formatTypeParameters(LighterAST tree, LighterASTNode typeParameters, StringBuilder buf)
{
	final List<LighterASTNode> children = LightTreeUtil.getChildrenOfType(tree, typeParameters, JavaElementType.TYPE);
	if(children.isEmpty())
	{
		return;
	}
	buf.append('<');
	for(int i = 0; i < children.size(); i++)
	{
		LighterASTNode child = children.get(i);
		formatType(tree, child, buf);
		if(i != children.size() - 1)
		{
			buf.append(", ");
		}
	}
	buf.append('>');
}
 
Example 30
Project: consulo-java   File: JavaNullMethodArgumentIndex.java   Source Code and License 6 votes vote down vote up
@Nullable
private static String getMethodName(LighterAST lighterAst, @NotNull LighterASTNode call, IElementType elementType)
{
	if(elementType == NEW_EXPRESSION || elementType == ANONYMOUS_CLASS)
	{
		final List<LighterASTNode> refs = LightTreeUtil.getChildrenOfType(lighterAst, call, JAVA_CODE_REFERENCE);
		if(refs.isEmpty())
		{
			return null;
		}
		final LighterASTNode lastRef = refs.get(refs.size() - 1);
		return JavaLightTreeUtil.getNameIdentifierText(lighterAst, lastRef);
	}

	LOG.assertTrue(elementType == METHOD_CALL_EXPRESSION);
	final LighterASTNode methodReference = lighterAst.getChildren(call).get(0);
	if(methodReference.getTokenType() == REFERENCE_EXPRESSION)
	{
		return JavaLightTreeUtil.getNameIdentifierText(lighterAst, methodReference);
	}
	return null;
}
 
Example 31
Project: consulo-java   File: PurityInference.java   Source Code and License 6 votes vote down vote up
void visitNode(LighterASTNode element)
{
	IElementType type = element.getTokenType();
	if(type == ASSIGNMENT_EXPRESSION)
	{
		mutatedRefs.add(tree.getChildren(element).get(0));
	}
	else if(type == RETURN_STATEMENT && JavaLightTreeUtil.findExpressionChild(tree, element) != null)
	{
		hasReturns = true;
	}
	else if((type == PREFIX_EXPRESSION || type == POSTFIX_EXPRESSION) && isMutatingOperation(element))
	{
		ContainerUtil.addIfNotNull(mutatedRefs, JavaLightTreeUtil.findExpressionChild(tree, element));
	}
	else if(isCall(element, type))
	{
		calls.add(element);
	}
}
 
Example 32
Project: consulo-java   File: RecordUtil.java   Source Code and License 6 votes vote down vote up
public static boolean isDeprecatedByDocComment(@NotNull LighterAST tree, @NotNull LighterASTNode comment)
{
	String text = LightTreeUtil.toFilteredString(tree, comment, null);
	if(text.contains(DEPRECATED_TAG))
	{
		JavaDocLexer lexer = new JavaDocLexer(LanguageLevel.HIGHEST);
		lexer.start(text);
		IElementType token;
		while((token = lexer.getTokenType()) != null)
		{
			if(token == JavaDocTokenType.DOC_TAG_NAME && DEPRECATED_TAG.equals(lexer.getTokenText()))
			{
				return true;
			}
			lexer.advance();
		}
	}

	return false;
}
 
Example 33
Project: consulo-java   File: JavaLightStubBuilder.java   Source Code and License 6 votes vote down vote up
@NotNull
@Override
protected StubElement createStubForFile(@NotNull PsiFile file, @NotNull LighterAST tree)
{
	if(!(file instanceof PsiJavaFile))
	{
		return super.createStubForFile(file, tree);
	}

	String refText = "";
	LighterASTNode pkg = LightTreeUtil.firstChildOfType(tree, tree.getRoot(), JavaElementType.PACKAGE_STATEMENT);
	if(pkg != null)
	{
		LighterASTNode ref = LightTreeUtil.firstChildOfType(tree, pkg, JavaElementType.JAVA_CODE_REFERENCE);
		if(ref != null)
		{
			refText = JavaSourceUtil.getReferenceText(tree, ref);
		}
	}
	return new PsiJavaFileStubImpl((PsiJavaFile) file, refText, null, false);
}
 
Example 34
Project: intellij-ce-playground   File: JavaFieldStubElementType.java   Source Code and License 5 votes vote down vote up
@Override
public PsiFieldStub createStub(final LighterAST tree, final LighterASTNode node, final StubElement parentStub) {
  final TypeInfo typeInfo = TypeInfo.create(tree, node, parentStub);

  boolean isDeprecatedByComment = false;
  boolean hasDeprecatedAnnotation = false;
  boolean hasDocComment = false;
  String name = null;
  String initializer = null;

  boolean expectingInit = false;
  for (final LighterASTNode child : tree.getChildren(node)) {
    final IElementType type = child.getTokenType();
    if (type == JavaDocElementType.DOC_COMMENT) {
      hasDocComment = true;
      isDeprecatedByComment = RecordUtil.isDeprecatedByDocComment(tree, child);
    }
    else if (type == JavaElementType.MODIFIER_LIST) {
      hasDeprecatedAnnotation = RecordUtil.isDeprecatedByAnnotation(tree, child);
    }
    else if (type == JavaTokenType.IDENTIFIER) {
      name = RecordUtil.intern(tree.getCharTable(), child);
    }
    else if (type == JavaTokenType.EQ) {
      expectingInit = true;
    }
    else if (expectingInit && !ElementType.JAVA_COMMENT_OR_WHITESPACE_BIT_SET.contains(type) && type != JavaTokenType.SEMICOLON) {
      initializer = encodeInitializer(tree, child);
      break;
    }
  }

  final boolean isEnumConst = node.getTokenType() == JavaElementType.ENUM_CONSTANT;
  final byte flags = PsiFieldStubImpl.packFlags(isEnumConst, isDeprecatedByComment, hasDeprecatedAnnotation, hasDocComment);

  return new PsiFieldStubImpl(parentStub, name, typeInfo, initializer, flags);
}
 
Example 35
Project: intellij-ce-playground   File: JavaFieldStubElementType.java   Source Code and License 5 votes vote down vote up
private static String encodeInitializer(final LighterAST tree, final LighterASTNode initializer) {
  final IElementType type = initializer.getTokenType();
  if (type == JavaElementType.NEW_EXPRESSION || type == JavaElementType.METHOD_CALL_EXPRESSION) {
    return PsiFieldStub.INITIALIZER_NOT_STORED;
  }

  if (initializer.getEndOffset() - initializer.getStartOffset() > INITIALIZER_LENGTH_LIMIT) {
    return PsiFieldStub.INITIALIZER_TOO_LONG;
  }

  return LightTreeUtil.toFilteredString(tree, initializer, null);
}
 
Example 36
Project: intellij-ce-playground   File: JavaClassReferenceListElementType.java   Source Code and License 5 votes vote down vote up
private static String[] getTexts(LighterAST tree, LighterASTNode node) {
  List<LighterASTNode> refs = LightTreeUtil.getChildrenOfType(tree, node, JavaElementType.JAVA_CODE_REFERENCE);
  String[] texts = ArrayUtil.newStringArray(refs.size());
  for (int i = 0; i < refs.size(); i++) {
    texts[i] = LightTreeUtil.toFilteredString(tree, refs.get(i), null);
  }
  return texts;
}
 
Example 37
Project: intellij-ce-playground   File: JavaParameterElementType.java   Source Code and License 5 votes vote down vote up
@Override
public PsiParameterStub createStub(LighterAST tree, LighterASTNode node, StubElement parentStub) {
  TypeInfo typeInfo = TypeInfo.create(tree, node, parentStub);
  LighterASTNode id = LightTreeUtil.requiredChildOfType(tree, node, JavaTokenType.IDENTIFIER);
  String name = RecordUtil.intern(tree.getCharTable(), id);
  return new PsiParameterStubImpl(parentStub, name, typeInfo, typeInfo.isEllipsis);
}
 
Example 38
Project: intellij-ce-playground   File: JavaLightStubBuilder.java   Source Code and License 5 votes vote down vote up
@Override
protected boolean skipChildProcessingWhenBuildingStubs(@NotNull LighterAST tree, @NotNull LighterASTNode parent, @NotNull LighterASTNode node) {
  IElementType parentType = parent.getTokenType();
  IElementType nodeType = node.getTokenType();

  if (checkByTypes(parentType, nodeType)) return true;

  if (nodeType == JavaElementType.CODE_BLOCK) {
    CodeBlockVisitor visitor = new CodeBlockVisitor();
    ((LighterLazyParseableNode)node).accept(visitor);
    return visitor.result;
  }

  return false;
}
 
Example 39
Project: intellij-ce-playground   File: DebugUtil.java   Source Code and License 5 votes vote down vote up
public static String lightTreeToString(@NotNull final FlyweightCapableTreeStructure<LighterASTNode> tree,
                                       final boolean skipWhitespaces) {
  final LengthBuilder ruler = new LengthBuilder();
  lightTreeToBuffer(tree, tree.getRoot(), ruler, 0, skipWhitespaces);
  final StringBuilder buffer = new StringBuilder(ruler.getLength());
  lightTreeToBuffer(tree, tree.getRoot(), buffer, 0, skipWhitespaces);
  return buffer.toString();
}
 
Example 40
Project: intellij-ce-playground   File: LightTreeUtil.java   Source Code and License 5 votes vote down vote up
@Nullable
public static LighterASTNode firstChildOfType(@NotNull List<LighterASTNode> children, @NotNull IElementType type) {
  for (int i = 0; i < children.size(); ++i) {
    LighterASTNode child = children.get(i);
    if (child.getTokenType() == type) return child;
  }

  return null;
}