Java Code Examples for com.intellij.psi.util.PsiUtilCore#ensureValid()
The following examples show how to use
com.intellij.psi.util.PsiUtilCore#ensureValid() .
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: DocumentationManager.java From consulo with Apache License 2.0 | 6 votes |
@Nullable public PsiElement getElementFromLookup(Editor editor, @Nullable PsiFile file) { Lookup activeLookup = LookupManager.getInstance(myProject).getActiveLookup(); if (activeLookup != null) { LookupElement item = activeLookup.getCurrentItem(); if (item != null) { int offset = editor.getCaretModel().getOffset(); if (offset > 0 && offset == editor.getDocument().getTextLength()) offset--; PsiReference ref = TargetElementUtil.findReference(editor, offset); PsiElement contextElement = file == null ? null : ObjectUtils.coalesce(file.findElementAt(offset), file); PsiElement targetElement = ref != null ? ref.getElement() : contextElement; if (targetElement != null) { PsiUtilCore.ensureValid(targetElement); } DocumentationProvider documentationProvider = getProviderFromElement(file); PsiManager psiManager = PsiManager.getInstance(myProject); PsiElement fromProvider = targetElement == null ? null : documentationProvider.getDocumentationElementForLookupItem(psiManager, item.getObject(), targetElement); return fromProvider != null ? fromProvider : CompletionUtil.getTargetElement(item); } } return null; }
Example 2
Source File: GeneralHighlightingPass.java From consulo with Apache License 2.0 | 6 votes |
public GeneralHighlightingPass(@Nonnull Project project, @Nonnull PsiFile file, @Nonnull Document document, int startOffset, int endOffset, boolean updateAll, @Nonnull ProperTextRange priorityRange, @Nullable Editor editor, @Nonnull HighlightInfoProcessor highlightInfoProcessor) { super(project, document, PRESENTABLE_NAME, file, editor, TextRange.create(startOffset, endOffset), true, highlightInfoProcessor); myUpdateAll = updateAll; myPriorityRange = priorityRange; PsiUtilCore.ensureValid(file); boolean wholeFileHighlighting = isWholeFileHighlighting(); myHasErrorElement = !wholeFileHighlighting && Boolean.TRUE.equals(getFile().getUserData(HAS_ERROR_ELEMENT)); final DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject); FileStatusMap fileStatusMap = daemonCodeAnalyzer.getFileStatusMap(); myErrorFound = !wholeFileHighlighting && fileStatusMap.wasErrorFound(getDocument()); // initial guess to show correct progress in the traffic light icon setProgressLimit(document.getTextLength() / 2); // approx number of PSI elements = file length/2 myGlobalScheme = editor != null ? editor.getColorsScheme() : EditorColorsManager.getInstance().getGlobalScheme(); }
Example 3
Source File: PsiDocumentManagerBase.java From consulo with Apache License 2.0 | 5 votes |
@Override public Document getDocument(@Nonnull PsiFile file) { Document document = getCachedDocument(file); if (document != null) { if (!file.getViewProvider().isPhysical()) { PsiUtilCore.ensureValid(file); associatePsi(document, file); } return document; } FileViewProvider viewProvider = file.getViewProvider(); if (!viewProvider.isEventSystemEnabled()) return null; document = FileDocumentManager.getInstance().getDocument(viewProvider.getVirtualFile()); if (document != null) { if (document.getTextLength() != file.getTextLength()) { String message = "Document/PSI mismatch: " + file + " (" + file.getClass() + "); physical=" + viewProvider.isPhysical(); if (document.getTextLength() + file.getTextLength() < 8096) { message += "\n=== document ===\n" + document.getText() + "\n=== PSI ===\n" + file.getText(); } throw new AssertionError(message); } if (!viewProvider.isPhysical()) { PsiUtilCore.ensureValid(file); associatePsi(document, file); file.putUserData(HARD_REF_TO_DOCUMENT, document); } } return document; }
Example 4
Source File: EditorWindowImpl.java From consulo with Apache License 2.0 | 5 votes |
private void checkValid() { PsiUtilCore.ensureValid(myInjectedFile); if (!isValid()) { StringBuilder reason = new StringBuilder("Not valid"); if (myDisposed) reason.append("; editorWindow: disposed"); if (!myDocumentWindow.isValid()) reason.append("; documentWindow: invalid"); if (myDelegate.isDisposed()) reason.append("; editor: disposed"); if (myInjectedFile.getProject().isDisposed()) reason.append("; project: disposed"); throw new AssertionError(reason.toString()); } }
Example 5
Source File: CompositeElement.java From consulo with Apache License 2.0 | 5 votes |
private static void diagnoseNullParent(TreeElement cur) { PsiElement psi = cur.getPsi(); if (psi != null) { PsiUtilCore.ensureValid(psi); } throw new IllegalStateException("Null parent of " + cur + " " + cur.getClass()); }
Example 6
Source File: PsiFileImpl.java From consulo with Apache License 2.0 | 5 votes |
@RequiredReadAction @Override public int getTextLength() { final ASTNode tree = derefTreeElement(); if (tree != null) return tree.getTextLength(); PsiUtilCore.ensureValid(this); return getViewProvider().getContents().length(); }
Example 7
Source File: Identikit.java From consulo with Apache License 2.0 | 5 votes |
@Nullable static Pair<ByAnchor, PsiElement> withAnchor(@Nonnull PsiElement element, @Nonnull Language fileLanguage) { PsiUtilCore.ensureValid(element); if (element.isPhysical()) { for (SmartPointerAnchorProvider provider : SmartPointerAnchorProvider.EP_NAME.getExtensionList()) { PsiElement anchor = provider.getAnchor(element); if (anchor != null && anchor.isPhysical() && provider.restoreElement(anchor) == element) { ByAnchor anchorKit = new ByAnchor(fromPsi(element, fileLanguage), fromPsi(anchor, fileLanguage), provider); return Pair.create(ourAnchorInterner.intern(anchorKit), anchor); } } } return null; }
Example 8
Source File: SmartPointerManagerImpl.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull public SmartPsiFileRange createSmartPsiFileRangePointer(@Nonnull PsiFile file, @Nonnull TextRange range, boolean forInjected) { PsiUtilCore.ensureValid(file); SmartPointerTracker.processQueue(); SmartPsiFileRangePointerImpl pointer = new SmartPsiFileRangePointerImpl(this, file, ProperTextRange.create(range), forInjected); trackPointer(pointer, file.getViewProvider().getVirtualFile()); return pointer; }
Example 9
Source File: SmartPointerManagerImpl.java From consulo with Apache License 2.0 | 5 votes |
private static void ensureValid(@Nonnull PsiElement element, @Nullable PsiFile containingFile) { boolean valid = containingFile != null ? containingFile.isValid() : element.isValid(); if (!valid) { PsiUtilCore.ensureValid(element); if (containingFile != null && !containingFile.isValid()) { throw new PsiInvalidElementAccessException(containingFile, "Element " + element.getClass() + "(" + element.getLanguage() + ")" + " claims to be valid but returns invalid containing file "); } } }
Example 10
Source File: PsiAnchor.java From consulo with Apache License 2.0 | 5 votes |
@Nonnull public static PsiAnchor create(@Nonnull final PsiElement element) { PsiUtilCore.ensureValid(element); PsiAnchor anchor = doCreateAnchor(element); if (ApplicationManager.getApplication().isUnitTestMode() && !ApplicationInfoImpl.isInPerformanceTest()) { PsiElement restored = anchor.retrieve(); if (!element.equals(restored)) { LOG.error("Cannot restore element " + element + " of " + element.getClass() + " from anchor " + anchor + ", getting " + restored + " instead"); } } return anchor; }
Example 11
Source File: StubTextInconsistencyException.java From consulo with Apache License 2.0 | 5 votes |
public static void checkStubTextConsistency(@Nonnull PsiFile file) throws StubTextInconsistencyException { PsiUtilCore.ensureValid(file); FileViewProvider viewProvider = file.getViewProvider(); if (viewProvider instanceof FreeThreadedFileViewProvider || viewProvider.getVirtualFile() instanceof LightVirtualFile) return; PsiFile bindingRoot = viewProvider.getStubBindingRoot(); if (!(bindingRoot instanceof PsiFileImpl)) return; IStubFileElementType fileElementType = ((PsiFileImpl)bindingRoot).getElementTypeForStubBuilder(); if (fileElementType == null || !fileElementType.shouldBuildStubFor(viewProvider.getVirtualFile())) return; List<PsiFileStub> fromText = restoreStubsFromText(viewProvider); List<PsiFileStub> fromPsi = ContainerUtil.map(StubTreeBuilder.getStubbedRoots(viewProvider), p -> ((PsiFileImpl)p.getSecond()).calcStubTree().getRoot()); if (fromPsi.size() != fromText.size()) { throw new StubTextInconsistencyException( "Inconsistent stub roots: " + "PSI says it's " + ContainerUtil.map(fromPsi, s -> s.getType()) + " but re-parsing the text gives " + ContainerUtil.map(fromText, s -> s.getType()), file, fromText, fromPsi); } for (int i = 0; i < fromPsi.size(); i++) { PsiFileStub psiStub = fromPsi.get(i); if (!DebugUtil.stubTreeToString(psiStub).equals(DebugUtil.stubTreeToString(fromText.get(i)))) { throw new StubTextInconsistencyException("Stub is inconsistent with text in " + file.getLanguage(), file, fromText, fromPsi); } } }
Example 12
Source File: AbstractFileViewProvider.java From consulo with Apache License 2.0 | 5 votes |
private void checkLengthConsistency() { Document document = getCachedDocument(); if (document instanceof DocumentWindow) { return; } if (document != null && ((PsiDocumentManagerBase)PsiDocumentManager.getInstance(myManager.getProject())).getSynchronizer().isInSynchronization(document)) { return; } List<FileElement> knownTreeRoots = getKnownTreeRoots(); if (knownTreeRoots.isEmpty()) return; int fileLength = myContent.getTextLength(); for (FileElement fileElement : knownTreeRoots) { int nodeLength = fileElement.getTextLength(); if (!isDocumentConsistentWithPsi(fileLength, fileElement, nodeLength)) { PsiUtilCore.ensureValid(fileElement.getPsi()); List<Attachment> attachments = ContainerUtil .newArrayList(new Attachment(myVirtualFile.getName(), myContent.getText().toString()), new Attachment(myVirtualFile.getNameWithoutExtension() + ".tree.txt", fileElement.getText())); if (document != null) { attachments.add(new Attachment(myVirtualFile.getNameWithoutExtension() + ".document.txt", document.getText())); } // exceptions here should be assigned to peter LOG.error("Inconsistent " + fileElement.getElementType() + " tree in " + this + "; nodeLength=" + nodeLength + "; fileLength=" + fileLength, attachments.toArray(Attachment.EMPTY_ARRAY)); } } }
Example 13
Source File: PomModelImpl.java From consulo with Apache License 2.0 | 5 votes |
private void startTransaction(@Nonnull PomTransaction transaction) { final PsiDocumentManagerBase manager = (PsiDocumentManagerBase)PsiDocumentManager.getInstance(myProject); final PsiToDocumentSynchronizer synchronizer = manager.getSynchronizer(); final PsiElement changeScope = transaction.getChangeScope(); final PsiFile containingFileByTree = getContainingFileByTree(changeScope); if (containingFileByTree != null && !(containingFileByTree instanceof DummyHolder) && !manager.isCommitInProgress()) { PsiUtilCore.ensureValid(containingFileByTree); } boolean physical = changeScope.isPhysical(); if (synchronizer.toProcessPsiEvent()) { // fail-fast to prevent any psi modifications that would cause psi/document text mismatch // PsiToDocumentSynchronizer assertions happen inside event processing and are logged by PsiManagerImpl.fireEvent instead of being rethrown // so it's important to throw something outside event processing if (isDocumentUncommitted(containingFileByTree)) { throw new IllegalStateException("Attempt to modify PSI for non-committed Document!"); } CommandProcessor commandProcessor = CommandProcessor.getInstance(); if (physical && !commandProcessor.isUndoTransparentActionInProgress() && commandProcessor.getCurrentCommand() == null) { throw new IncorrectOperationException( "Must not change PSI outside command or undo-transparent action. See com.intellij.openapi.command.WriteCommandAction or com.intellij.openapi.command.CommandProcessor"); } } if (containingFileByTree != null) { ((SmartPointerManagerImpl)SmartPointerManager.getInstance(myProject)).fastenBelts(containingFileByTree.getViewProvider().getVirtualFile()); if (containingFileByTree instanceof PsiFileImpl) { ((PsiFileImpl)containingFileByTree).beforeAstChange(); } } BlockSupportImpl.sendBeforeChildrenChangeEvent((PsiManagerImpl)PsiManager.getInstance(myProject), changeScope, true); Document document = containingFileByTree == null ? null : physical ? manager.getDocument(containingFileByTree) : manager.getCachedDocument(containingFileByTree); if (document != null) { synchronizer.startTransaction(myProject, document, changeScope); } }
Example 14
Source File: IdIndex.java From consulo with Apache License 2.0 | 5 votes |
public static boolean hasIdentifierInFile(@Nonnull PsiFile file, @Nonnull String name) { PsiUtilCore.ensureValid(file); if (file.getVirtualFile() == null || DumbService.isDumb(file.getProject())) { return StringUtil.contains(file.getViewProvider().getContents(), name); } GlobalSearchScope scope = GlobalSearchScope.fileScope(file); return !FileBasedIndex.getInstance().getContainingFiles(NAME, new IdIndexEntry(name, true), scope).isEmpty(); }
Example 15
Source File: CompletionAssertions.java From consulo with Apache License 2.0 | 4 votes |
static void assertHostInfo(PsiFile hostCopy, OffsetMap hostMap) { PsiUtilCore.ensureValid(hostCopy); if (hostMap.getOffset(CompletionInitializationContext.START_OFFSET) > hostCopy.getTextLength()) { throw new AssertionError("startOffset outside the host file: " + hostMap.getOffset(CompletionInitializationContext.START_OFFSET) + "; " + hostCopy); } }
Example 16
Source File: LookupElementBuilder.java From consulo with Apache License 2.0 | 4 votes |
public static LookupElementBuilder create(@Nonnull Object lookupObject, @Nonnull String lookupString) { if (lookupObject instanceof PsiElement) { PsiUtilCore.ensureValid((PsiElement)lookupObject); } return new LookupElementBuilder(lookupString, lookupObject); }
Example 17
Source File: TemplateState.java From consulo with Apache License 2.0 | 4 votes |
private void recalcSegment(int segmentNumber, boolean isQuick, Expression expressionNode, Expression defaultValue) { String oldValue = getExpressionString(segmentNumber); int start = mySegments.getSegmentStart(segmentNumber); int end = mySegments.getSegmentEnd(segmentNumber); PsiDocumentManager.getInstance(myProject).commitDocument(myDocument); PsiFile psiFile = getPsiFile(); PsiElement element = psiFile.findElementAt(start); if (element != null) { PsiUtilCore.ensureValid(element); } ExpressionContext context = createExpressionContext(start); Result result = isQuick ? expressionNode.calculateQuickResult(context) : expressionNode.calculateResult(context); if (isQuick && result == null) { if (!oldValue.isEmpty()) { return; } } final boolean resultIsNullOrEmpty = result == null || result.equalsToText("", element); // do not update default value of neighbour segment if (resultIsNullOrEmpty && myCurrentSegmentNumber >= 0 && (mySegments.getSegmentStart(segmentNumber) == mySegments.getSegmentEnd(myCurrentSegmentNumber) || mySegments.getSegmentEnd(segmentNumber) == mySegments.getSegmentStart(myCurrentSegmentNumber))) { return; } if (defaultValue != null && resultIsNullOrEmpty) { result = defaultValue.calculateResult(context); } if (element != null) { PsiUtilCore.ensureValid(element); } if (result == null || result.equalsToText(oldValue, element)) return; replaceString(StringUtil.notNullize(result.toString()), start, end, segmentNumber); if (result instanceof RecalculatableResult) { IntArrayList indices = initEmptyVariables(); shortenReferences(); PsiDocumentManager.getInstance(myProject).commitDocument(myDocument); ((RecalculatableResult)result) .handleRecalc(psiFile, myDocument, mySegments.getSegmentStart(segmentNumber), mySegments.getSegmentEnd(segmentNumber)); restoreEmptyVariables(indices); } }
Example 18
Source File: LookupElementBuilder.java From consulo with Apache License 2.0 | 4 votes |
public static LookupElementBuilder createWithIcon(@Nonnull PsiNamedElement element) { PsiUtilCore.ensureValid(element); return create(element).withIcon(IconDescriptorUpdaters.getIcon(element, 0)); }
Example 19
Source File: LookupElementBuilder.java From consulo with Apache License 2.0 | 4 votes |
public static LookupElementBuilder create(@Nonnull PsiNamedElement element) { PsiUtilCore.ensureValid(element); return new LookupElementBuilder(StringUtil.notNullize(element.getName()), element); }
Example 20
Source File: LookupElementBuilder.java From consulo with Apache License 2.0 | 4 votes |
public static LookupElementBuilder createWithSmartPointer(@Nonnull String lookupString, @Nonnull PsiElement element) { PsiUtilCore.ensureValid(element); return new LookupElementBuilder(lookupString, SmartPointerManager.getInstance(element.getProject()).createSmartPsiElementPointer(element)); }