com.intellij.psi.impl.source.SourceTreeToPsiMap Java Examples
The following examples show how to use
com.intellij.psi.impl.source.SourceTreeToPsiMap.
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: CodeStyleManagerRunnable.java From consulo with Apache License 2.0 | 6 votes |
private static TextRange getSignificantRange(final PsiFile file, final int offset) { final ASTNode elementAtOffset = SourceTreeToPsiMap.psiElementToTree(CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset)); if (elementAtOffset == null) { int significantRangeStart = CharArrayUtil.shiftBackward(file.getText(), offset - 1, "\n\r\t "); return new TextRange(Math.max(significantRangeStart, 0), offset); } final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file); if (builder != null) { final TextRange textRange = builder.getRangeAffectingIndent(file, offset, elementAtOffset); if (textRange != null) { return textRange; } } final TextRange elementRange = elementAtOffset.getTextRange(); if (isWhiteSpace(elementAtOffset)) { return extendRangeAtStartOffset(file, elementRange); } return elementRange; }
Example #2
Source File: CodeStyleManagerImpl.java From consulo with Apache License 2.0 | 6 votes |
@Nonnull private static Pair<PsiElement, CharTable> doFindWhiteSpaceNode(@Nonnull PsiFile file, int offset) { ASTNode astNode = SourceTreeToPsiMap.psiElementToTree(file); if (!(astNode instanceof FileElement)) { return new Pair<>(null, null); } PsiElement elementAt = InjectedLanguageManager.getInstance(file.getProject()).findInjectedElementAt(file, offset); final CharTable charTable = ((FileElement)astNode).getCharTable(); if (elementAt == null) { elementAt = findElementInTreeWithFormatterEnabled(file, offset); } if (elementAt == null) { return new Pair<>(null, charTable); } ASTNode node = elementAt.getNode(); if (node == null || node.getElementType() != TokenType.WHITE_SPACE) { return new Pair<>(null, charTable); } return Pair.create(elementAt, charTable); }
Example #3
Source File: CodeStyleManagerImpl.java From consulo with Apache License 2.0 | 6 votes |
private static PsiElement reformatRangeImpl(final @Nonnull PsiElement element, final int startOffset, final int endOffset, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { LOG.assertTrue(element.isValid()); CheckUtil.checkWritable(element); if (!SourceTreeToPsiMap.hasTreeElement(element)) { return element; } ASTNode treeElement = element.getNode(); final PsiFile file = element.getContainingFile(); if (ExternalFormatProcessor.useExternalFormatter(file)) { return ExternalFormatProcessor.formatElement(element, TextRange.create(startOffset, endOffset), canChangeWhiteSpacesOnly); } final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(file), element.getLanguage()); final PsiElement formatted = codeFormatter.processRange(treeElement, startOffset, endOffset).getPsi(); return canChangeWhiteSpacesOnly ? formatted : postProcessElement(file, formatted); }
Example #4
Source File: BashSpacingProcessor.java From BashSupport with Apache License 2.0 | 6 votes |
private void init(final ASTNode child) { if (child == null) { return; } ASTNode treePrev = child.getTreePrev(); while (treePrev != null && SpacingUtil.isWhiteSpace(treePrev)) { treePrev = treePrev.getTreePrev(); } if (treePrev == null) { init(child.getTreeParent()); } else { myChild2 = child; myChild1 = treePrev; final CompositeElement parent = (CompositeElement) treePrev.getTreeParent(); myParent = SourceTreeToPsiMap.treeElementToPsi(parent); } }
Example #5
Source File: CodeStyleManagerImpl.java From consulo with Apache License 2.0 | 6 votes |
@Override @Nonnull public PsiElement reformat(@Nonnull PsiElement element, boolean canChangeWhiteSpacesOnly) throws IncorrectOperationException { CheckUtil.checkWritable(element); if (!SourceTreeToPsiMap.hasTreeElement(element)) { return element; } ASTNode treeElement = element.getNode(); final PsiFile file = element.getContainingFile(); if (ExternalFormatProcessor.useExternalFormatter(file)) { return ExternalFormatProcessor.formatElement(element, element.getTextRange(), canChangeWhiteSpacesOnly); } final PsiElement formatted = new CodeFormatterFacade(getSettings(file), element.getLanguage(), canChangeWhiteSpacesOnly).processElement(treeElement).getPsi(); if (!canChangeWhiteSpacesOnly) { return postProcessElement(file, formatted); } return formatted; }
Example #6
Source File: SharedImplUtil.java From consulo with Apache License 2.0 | 5 votes |
public static PsiElement addRange(PsiElement thisElement, PsiElement first, PsiElement last, ASTNode anchor, Boolean before) throws IncorrectOperationException { CheckUtil.checkWritable(thisElement); final CharTable table = findCharTableByTree(SourceTreeToPsiMap.psiElementToTree(thisElement)); TreeElement copyFirst = null; ASTNode copyLast = null; ASTNode next = SourceTreeToPsiMap.psiElementToTree(last).getTreeNext(); ASTNode parent = null; for (ASTNode element = SourceTreeToPsiMap.psiElementToTree(first); element != next; element = element.getTreeNext()) { TreeElement elementCopy = ChangeUtil.copyElement((TreeElement)element, table); if (element == first.getNode()) { copyFirst = elementCopy; } if (element == last.getNode()) { copyLast = elementCopy; } if (parent == null) { parent = elementCopy.getTreeParent(); } else { if(elementCopy.getElementType() == TokenType.WHITE_SPACE) CodeEditUtil.setNodeGenerated(elementCopy, true); parent.addChild(elementCopy, null); } } if (copyFirst == null) return null; copyFirst = ((CompositeElement)SourceTreeToPsiMap.psiElementToTree(thisElement)).addInternal(copyFirst, copyLast, anchor, before); for (TreeElement element = copyFirst; element != null; element = element.getTreeNext()) { element = ChangeUtil.decodeInformation(element); if (element.getTreePrev() == null) { copyFirst = element; } } return SourceTreeToPsiMap.treeElementToPsi(copyFirst); }
Example #7
Source File: SharedImplUtil.java From consulo with Apache License 2.0 | 5 votes |
public static PsiElement doReplace(PsiElement psiElement, TreeElement treeElement, PsiElement newElement) { CompositeElement treeParent = treeElement.getTreeParent(); LOG.assertTrue(treeParent != null); CheckUtil.checkWritable(psiElement); TreeElement elementCopy = ChangeUtil.copyToElement(newElement); treeParent.replaceChildInternal(treeElement, elementCopy); elementCopy = ChangeUtil.decodeInformation(elementCopy); final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy); treeElement.invalidate(); return result; }
Example #8
Source File: SqliteMagicLightMethodGenerator.java From sqlitemagic with Apache License 2.0 | 5 votes |
@Nullable @Override public TreeElement generateTreeFor(PsiElement original, CharTable table, PsiManager manager) { TreeElement result = null; if (original instanceof SqliteMagicLightMethodBuilder) { result = ChangeUtil.copyElement((TreeElement) SourceTreeToPsiMap.psiElementToTree(original), table); } return result; }
Example #9
Source File: LazyParseablePsiElement.java From consulo with Apache License 2.0 | 5 votes |
@Override public PsiElement addAfter(@Nonnull PsiElement element, PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE); return ChangeUtil.decodeInformation(treeElement).getPsi(); }
Example #10
Source File: LazyParseablePsiElement.java From consulo with Apache License 2.0 | 5 votes |
@Override public void deleteChildRange(PsiElement first, PsiElement last) throws IncorrectOperationException { CheckUtil.checkWritable(this); ASTNode firstElement = SourceTreeToPsiMap.psiElementToTree(first); ASTNode lastElement = SourceTreeToPsiMap.psiElementToTree(last); LOG.assertTrue(firstElement.getTreeParent() == this); LOG.assertTrue(lastElement.getTreeParent() == this); CodeEditUtil.removeChildren(this, firstElement, lastElement); }
Example #11
Source File: LazyParseablePsiElement.java From consulo with Apache License 2.0 | 5 votes |
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE); if (treeElement != null) return ChangeUtil.decodeInformation(treeElement).getPsi(); throw new IncorrectOperationException("Element cannot be added"); }
Example #12
Source File: CompositePsiElement.java From consulo with Apache License 2.0 | 5 votes |
@Override public PsiElement addAfter(@Nonnull PsiElement element, PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE); return ChangeUtil.decodeInformation(treeElement).getPsi(); }
Example #13
Source File: CompositePsiElement.java From consulo with Apache License 2.0 | 5 votes |
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); TreeElement treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE); if (treeElement != null) return ChangeUtil.decodeInformation(treeElement).getPsi(); throw new IncorrectOperationException("Element cannot be added"); }
Example #14
Source File: ChangeUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nullable public static TreeElement generateTreeElement(@Nullable PsiElement original, @Nonnull CharTable table, @Nonnull final PsiManager manager) { if (original == null) return null; PsiUtilCore.ensureValid(original); if (SourceTreeToPsiMap.hasTreeElement(original)) { return copyElement((TreeElement)SourceTreeToPsiMap.psiElementToTree(original), table); } else { for (TreeGenerator generator : TreeGenerator.EP_NAME.getExtensionList()) { final TreeElement element = generator.generateTreeFor(original, table, manager); if (element != null) return element; } return null; } }
Example #15
Source File: PsiDirectoryImpl.java From consulo with Apache License 2.0 | 5 votes |
private static void updateAddedFile(PsiFile copyPsi) throws IncorrectOperationException { final UpdateAddedFileProcessor processor = UpdateAddedFileProcessor.forElement(copyPsi); if (processor != null) { final TreeElement tree = (TreeElement)SourceTreeToPsiMap.psiElementToTree(copyPsi); if (tree != null) { ChangeUtil.encodeInformation(tree); } processor.update(copyPsi, null); if (tree != null) { ChangeUtil.decodeInformation(tree); } } }
Example #16
Source File: FormatterUtil.java From consulo with Apache License 2.0 | 5 votes |
@Nullable private static ASTNode findPreviousWhiteSpace(final ASTNode leafElement, final IElementType whiteSpaceTokenType) { final int offset = leafElement.getTextRange().getStartOffset() - 1; if (offset < 0) return null; final PsiElement psiElement = SourceTreeToPsiMap.treeElementToPsi(leafElement); if (psiElement == null) { return null; } final PsiElement found = psiElement.getContainingFile().findElementAt(offset); if (found == null) return null; final ASTNode treeElement = found.getNode(); if (treeElement != null && treeElement.getElementType() == whiteSpaceTokenType) return treeElement; return null; }
Example #17
Source File: OwnBufferLeafPsiElement.java From consulo with Apache License 2.0 | 5 votes |
@Override public PsiElement replace(@Nonnull PsiElement newElement) throws IncorrectOperationException { LOG.assertTrue(getTreeParent() != null); CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(newElement); getTreeParent().replaceChildInternal(this, elementCopy); elementCopy = ChangeUtil.decodeInformation(elementCopy); final PsiElement result = SourceTreeToPsiMap.treeElementToPsi(elementCopy); this.invalidate(); return result; }
Example #18
Source File: CodeStyleManagerImpl.java From consulo with Apache License 2.0 | 5 votes |
private void reformatText(@Nonnull PsiFile file, @Nonnull FormatTextRanges ranges, @Nullable Editor editor) throws IncorrectOperationException { if (ranges.isEmpty()) { return; } ApplicationManager.getApplication().assertWriteAccessAllowed(); PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); CheckUtil.checkWritable(file); if (!SourceTreeToPsiMap.hasTreeElement(file)) { return; } ASTNode treeElement = SourceTreeToPsiMap.psiElementToTree(file); transformAllChildren(treeElement); LOG.assertTrue(file.isValid(), "File name: " + file.getName() + " , class: " + file.getClass().getSimpleName()); if (editor == null) { editor = PsiUtilBase.findEditor(file); } CaretPositionKeeper caretKeeper = null; if (editor != null) { caretKeeper = new CaretPositionKeeper(editor, getSettings(file), file.getLanguage()); } if (FormatterUtil.isFormatterCalledExplicitly()) { removeEndingWhiteSpaceFromEachRange(file, ranges); } formatRanges(file, ranges, ExternalFormatProcessor.useExternalFormatter(file) ? null // do nothing, delegate the external formatting activity to post-processor : () -> { final CodeFormatterFacade codeFormatter = new CodeFormatterFacade(getSettings(file), file.getLanguage()); codeFormatter.processText(file, ranges, true); }); if (caretKeeper != null) { caretKeeper.restoreCaretPosition(); } }
Example #19
Source File: CodeStyleManagerImpl.java From consulo with Apache License 2.0 | 5 votes |
@Override @Deprecated public boolean isLineToBeIndented(@Nonnull PsiFile file, int offset) { if (!SourceTreeToPsiMap.hasTreeElement(file)) { return false; } CharSequence chars = file.getViewProvider().getContents(); int start = CharArrayUtil.shiftBackward(chars, offset - 1, " \t"); if (start > 0 && chars.charAt(start) != '\n' && chars.charAt(start) != '\r') { return false; } int end = CharArrayUtil.shiftForward(chars, offset, " \t"); if (end >= chars.length()) { return false; } ASTNode element = SourceTreeToPsiMap.psiElementToTree(findElementInTreeWithFormatterEnabled(file, end)); if (element == null) { return false; } if (element.getElementType() == TokenType.WHITE_SPACE) { return false; } if (element.getElementType() == PlainTextTokenTypes.PLAIN_TEXT) { return false; } /* if( element.getElementType() instanceof IJspElementType ) { return false; } */ if (getSettings(file).getCommonSettings(file.getLanguage()).KEEP_FIRST_COLUMN_COMMENT && isCommentToken(element)) { if (IndentHelper.getInstance().getIndent(myProject, file.getFileType(), element, true) == 0) { return false; } } return true; }
Example #20
Source File: ASTDelegatePsiElement.java From consulo with Apache License 2.0 | 5 votes |
@Override public PsiElement addAfter(@Nonnull PsiElement element, PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.FALSE); if (treeElement instanceof TreeElement) { return ChangeUtil.decodeInformation((TreeElement) treeElement).getPsi(); } return treeElement.getPsi(); }
Example #21
Source File: GraphQLNamedElementImpl.java From js-graphql-intellij-plugin with MIT License | 5 votes |
@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 #22
Source File: GraphQLIdentifierManipulator.java From js-graphql-intellij-plugin with MIT License | 5 votes |
@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 #23
Source File: JSGraphQLEndpointImportFileReferencePsiElement.java From js-graphql-intellij-plugin with MIT License | 5 votes |
@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 #24
Source File: JSGraphQLEndpointPsiUtil.java From js-graphql-intellij-plugin with MIT License | 5 votes |
/** * 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 #25
Source File: LombokLightMethodTreeGenerator.java From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License | 5 votes |
@Nullable public TreeElement generateTreeFor(PsiElement original, CharTable table, PsiManager manager) { TreeElement result = null; if (original instanceof LombokLightMethodBuilder) { result = ChangeUtil.copyElement((TreeElement) SourceTreeToPsiMap.psiElementToTree(original), table); } return result; }
Example #26
Source File: ANTLRv4FoldingBuilder.java From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License | 5 votes |
@SuppressWarnings("SimplifiableIfStatement") @Override protected boolean isRegionCollapsedByDefault(@NotNull ASTNode node) { final PsiElement element = SourceTreeToPsiMap.treeElementToPsi(node); if (element == null) return false; ANTLRv4FoldingSettings settings = ANTLRv4FoldingSettings.getInstance(); if (RULE_BLOCKS.contains(node.getElementType())) return settings.isCollapseRuleBlocks(); if (node.getElementType() == TOKENSSPEC) return settings.isCollapseTokens(); if (element instanceof AtAction) return settings.isCollapseActions(); if (element instanceof ANTLRv4FileRoot) { return settings.isCollapseFileHeader(); } if (node.getElementType() == DOC_COMMENT_TOKEN) { PsiElement parent = element.getParent(); if (parent instanceof ANTLRv4FileRoot) { PsiElement firstChild = parent.getFirstChild(); if (firstChild instanceof PsiWhiteSpace) { firstChild = firstChild.getNextSibling(); } if (element.equals(firstChild)) { return settings.isCollapseFileHeader(); } } return settings.isCollapseDocComments(); } if (isComment(element)) { return settings.isCollapseComments(); } return false; }
Example #27
Source File: StubBasedPsiElementBase.java From consulo with Apache License 2.0 | 5 votes |
/** * @return the parent of this element. Uses stub hierarchy if possible, but might cause an expensive switch to AST * if the parent stub doesn't correspond to the parent AST node. */ @Override public PsiElement getParent() { T stub = getGreenStub(); if (stub != null && !((ObjectStubBase<?>)stub).isDangling()) { return stub.getParentStub().getPsi(); } return SourceTreeToPsiMap.treeElementToPsi(getNode().getTreeParent()); }
Example #28
Source File: ASTDelegatePsiElement.java From consulo with Apache License 2.0 | 5 votes |
private PsiElement addInnerBefore(final PsiElement element, final PsiElement anchor) throws IncorrectOperationException { CheckUtil.checkWritable(this); TreeElement elementCopy = ChangeUtil.copyToElement(element); ASTNode treeElement = addInternal(elementCopy, elementCopy, SourceTreeToPsiMap.psiElementToTree(anchor), Boolean.TRUE); if (treeElement != null) { if (treeElement instanceof TreeElement) { return ChangeUtil.decodeInformation((TreeElement) treeElement).getPsi(); } return treeElement.getPsi(); } throw new IncorrectOperationException("Element cannot be added"); }
Example #29
Source File: PsiParserFacadeImpl.java From consulo with Apache License 2.0 | 5 votes |
private PsiComment findPsiCommentChild(PsiFile aFile) { PsiElement[] children = aFile.getChildren(); for (PsiElement aChildren : children) { if (aChildren instanceof PsiComment) { PsiComment comment = (PsiComment)aChildren; DummyHolderFactory.createHolder(myManager, (TreeElement)SourceTreeToPsiMap.psiElementToTree(comment), null); return comment; } } throw new IncorrectOperationException("Incorrect comment \"" + aFile.getText() + "\"."); }
Example #30
Source File: ASTDelegatePsiElement.java From consulo with Apache License 2.0 | 5 votes |
@Override public void deleteChildRange(final PsiElement first, final PsiElement last) throws IncorrectOperationException { CheckUtil.checkWritable(this); ASTNode firstElement = SourceTreeToPsiMap.psiElementToTree(first); ASTNode lastElement = SourceTreeToPsiMap.psiElementToTree(last); LOG.assertTrue(firstElement.getTreeParent() == getNode()); LOG.assertTrue(lastElement.getTreeParent() == getNode()); CodeEditUtil.removeChildren(getNode(), firstElement, lastElement); }