com.intellij.psi.impl.source.tree.Factory Java Examples

The following examples show how to use com.intellij.psi.impl.source.tree.Factory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PsiBuilderImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public ASTNode convert(final Node n) {
  if (n instanceof Token) {
    final Token token = (Token)n;
    return token.myBuilder.createLeaf(token.getTokenType(), token.myTokenStart, token.myTokenEnd);
  }
  else if (n instanceof ErrorItem) {
    return Factory.createErrorElement(((ErrorItem)n).myMessage);
  }
  else {
    final StartMarker startMarker = (StartMarker)n;
    final CompositeElement composite = n == myRoot ? (CompositeElement)myRoot.myBuilder.createRootAST(myRoot) : createComposite(startMarker);
    startMarker.myBuilder.bind(startMarker, composite);
    return composite;
  }
}
 
Example #2
Source File: GraphQLNamedElementImpl.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public PsiElement setName(@NotNull String newName) throws IncorrectOperationException {
    final GraphQLIdentifier nameIdentifier = getNameIdentifier();
    if (nameIdentifier != null) {
        final LeafElement renamedLeaf = Factory.createSingleLeafElement(GraphQLElementTypes.NAME, newName, null, nameIdentifier.getManager());
        final PsiElement renamedPsiElement = SourceTreeToPsiMap.treeElementToPsi(renamedLeaf);
        if (renamedPsiElement != null) {
            nameIdentifier.getFirstChild().replace(renamedPsiElement);
        }
    }
    return this;
}
 
Example #3
Source File: GraphQLIdentifierManipulator.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public GraphQLIdentifierImpl handleContentChange(@NotNull GraphQLIdentifierImpl element, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
    // replace the NAME leaf element inside the identifier
    final LeafElement renamedLeaf = Factory.createSingleLeafElement(GraphQLElementTypes.NAME, newContent, null, element.getManager());
    final PsiElement renamedPsiElement = SourceTreeToPsiMap.treeElementToPsi(renamedLeaf);
    if (renamedPsiElement != null) {
        element.getFirstChild().replace(renamedPsiElement);
    }
    return element;

}
 
Example #4
Source File: JSGraphQLEndpointImportFileReferencePsiElement.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@Override
public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException {
	final PsiElement nameIdentifier = getNameIdentifier();
	if(nameIdentifier != null) {
		final LeafElement renamedLeaf = Factory.createSingleLeafElement(JSGraphQLEndpointTokenTypes.STRING_BODY, name, null, getManager());
		final PsiElement renamedPsiElement = SourceTreeToPsiMap.treeElementToPsi(renamedLeaf);
		if (renamedPsiElement != null) {
			nameIdentifier.replace(renamedPsiElement);
		}
	}
	return this;
}
 
Example #5
Source File: JSGraphQLEndpointPsiUtil.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
/**
 * Renames an identifier, e.g. during a refactoring
 */
public static PsiElement renameIdentifier(PsiNameIdentifierOwner owner, String name) throws IncorrectOperationException {
	final PsiElement identifier = owner.getNameIdentifier();
	if (identifier == null) {
		throw new IncorrectOperationException();
	}
	final LeafElement renamedLeaf = Factory.createSingleLeafElement(JSGraphQLEndpointTokenTypes.IDENTIFIER, name, null, identifier.getManager());
	final PsiElement renamedPsiElement = SourceTreeToPsiMap.treeElementToPsi(renamedLeaf);
	if (renamedPsiElement != null) {
		identifier.replace(renamedPsiElement);
	}
	return owner;
}
 
Example #6
Source File: CSharpDeclarationMover.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void beforeMove(@Nonnull final Editor editor, @Nonnull final MoveInfo info, final boolean down)
{
	super.beforeMove(editor, info, down);

	if(myEnumToInsertSemicolonAfter != null)
	{
		TreeElement semicolon = Factory.createSingleLeafElement(CSharpTokens.SEMICOLON, ";", 0, 1, null, myEnumToInsertSemicolonAfter.getManager());

		try
		{
			PsiElement inserted = myEnumToInsertSemicolonAfter.getParent().addAfter(semicolon.getPsi(), myEnumToInsertSemicolonAfter);
			inserted = CodeInsightUtilBase.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;
		}
	}
}
 
Example #7
Source File: LanguageTokenSeparatorGenerators.java    From consulo with Apache License 2.0 5 votes vote down vote up
private LanguageTokenSeparatorGenerators() {
  super("com.intellij.lang.tokenSeparatorGenerator", new TokenSeparatorGenerator() {
    @Override
    public ASTNode generateWhitespaceBetweenTokens(ASTNode left, ASTNode right) {
      Language l = PsiUtilCore.getNotAnyLanguage(left);
      Language rightLang = PsiUtilCore.getNotAnyLanguage(right);
      if (rightLang.isKindOf(l)) {
        l = rightLang; // get more precise lexer
      }
      final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(l);
      if (parserDefinition != null) {
        PsiManager manager = right.getTreeParent().getPsi().getManager();
        ASTNode generatedWhitespace;
        switch(parserDefinition.spaceExistenceTypeBetweenTokens(left, right)){
          case MUST:
            generatedWhitespace = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, " ", 0, 1, null, manager);
            break;
          case MUST_LINE_BREAK:
            generatedWhitespace = Factory.createSingleLeafElement(TokenType.WHITE_SPACE, "\n", 0, 1, null, manager);
            break;
          default:
            generatedWhitespace = null;
        }
        return generatedWhitespace;
      }
      return null;
    }
  });
}
 
Example #8
Source File: PsiBuilderImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void bind(@Nonnull StartMarker rootMarker, @Nonnull CompositeElement rootNode) {
  StartMarker curMarker = rootMarker;
  CompositeElement curNode = rootNode;

  int lexIndex = rootMarker.myLexemeIndex;
  ProductionMarker item = rootMarker.myFirstChild != null ? rootMarker.myFirstChild : rootMarker.myDoneMarker;
  while (true) {
    lexIndex = insertLeaves(lexIndex, item.myLexemeIndex, curNode);

    if (item == rootMarker.myDoneMarker) break;

    if (item instanceof StartMarker) {
      final StartMarker marker = (StartMarker)item;
      if (!marker.myDoneMarker.myCollapse) {
        curMarker = marker;

        final CompositeElement childNode = createComposite(marker);
        curNode.rawAddChildrenWithoutNotifications(childNode);
        curNode = childNode;

        item = marker.myFirstChild != null ? marker.myFirstChild : marker.myDoneMarker;
        continue;
      }
      else {
        lexIndex = collapseLeaves(curNode, marker);
      }
    }
    else if (item instanceof ErrorItem) {
      final CompositeElement errorElement = Factory.createErrorElement(((ErrorItem)item).myMessage);
      curNode.rawAddChildrenWithoutNotifications(errorElement);
    }
    else if (item instanceof DoneMarker) {
      curMarker = (StartMarker)((DoneMarker)item).myStart.myParent;
      curNode = curNode.getTreeParent();
      item = ((DoneMarker)item).myStart;
    }

    item = item.myNext != null ? item.myNext : curMarker.myDoneMarker;
  }
}
 
Example #9
Source File: PsiBuilderImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private static CompositeElement createComposite(@Nonnull StartMarker marker) {
  final IElementType type = marker.myType;
  if (type == TokenType.ERROR_ELEMENT) {
    String message = marker.myDoneMarker instanceof DoneWithErrorMarker ? ((DoneWithErrorMarker)marker.myDoneMarker).myMessage : null;
    return Factory.createErrorElement(message);
  }

  if (type == null) {
    throw new RuntimeException(UNBALANCED_MESSAGE);
  }

  return ASTFactory.composite(type);
}
 
Example #10
Source File: CodeEditUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
public static PsiElement createLineFeed(final PsiManager manager) {
  return Factory.createSingleLeafElement(TokenType.WHITE_SPACE, "\n", 0, 1, null, manager).getPsi();
}