void loadFromEditor(@NotNull Editor editor) { assertDispatchThread(); LOG.assertTrue(!editor.isDisposed()); clear(); PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject); documentManager.commitDocument(editor.getDocument()); PsiFile file = documentManager.getPsiFile(editor.getDocument()); SmartPointerManager smartPointerManager = SmartPointerManager.getInstance(myProject); EditorFoldingInfo info = EditorFoldingInfo.get(editor); FoldRegion[] foldRegions = editor.getFoldingModel().getAllFoldRegions(); for (FoldRegion region : foldRegions) { if (!region.isValid()) continue; PsiElement element = info.getPsiElement(region); boolean expanded = region.isExpanded(); boolean collapseByDefault = element != null && FoldingPolicy.isCollapseByDefault(element) && !FoldingUtil.caretInsideRange(editor, TextRange.create(region)); if (collapseByDefault == expanded || element == null) { FoldingInfo fi = new FoldingInfo(region.getPlaceholderText(), expanded); if (element != null) { myPsiElements.add(smartPointerManager.createSmartPsiElementPointer(element, file)); element.putUserData(FOLDING_INFO_KEY, fi); } else if (region.isValid()) { myRangeMarkers.add(region); region.putUserData(FOLDING_INFO_KEY, fi); } } } }
@Override public void executeWriteAction(Editor editor, DataContext dataContext) { final Project project = editor.getProject(); final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); final Document document = editor.getDocument(); PsiFile file = getRoot(documentManager.getPsiFile(document), editor); final MoverWrapper mover = getSuitableMover(editor, file); if (mover != null) { mover.move(editor, file); } }
public static void updateDocumentIndentOptions( @NotNull Project project, @NotNull Document document) { if (!project.isDisposed()) { PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); if (documentManager != null) { PsiFile file = documentManager.getPsiFile(document); if (file != null) { CommonCodeStyleSettings.IndentOptions indentOptions = getSettings(project).getIndentOptionsByFile(file, true); indentOptions.associateWithDocument(document); } } } }
@Override public void mouseMoved(final EditorMouseEvent e) { if (e.isConsumed() || !myProject.isInitialized() || myProject.isDisposed()) { return; } MouseEvent mouseEvent = e.getMouseEvent(); if (isMouseOverTooltip(mouseEvent.getLocationOnScreen()) || ScreenUtil.isMovementTowards( myPrevMouseLocation, mouseEvent.getLocationOnScreen(), getHintBounds())) { myPrevMouseLocation = mouseEvent.getLocationOnScreen(); return; } myPrevMouseLocation = mouseEvent.getLocationOnScreen(); Editor editor = e.getEditor(); if (editor.getProject() != null && editor.getProject() != myProject) return; PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject); PsiFile psiFile = documentManager.getPsiFile(editor.getDocument()); Point point = new Point(mouseEvent.getPoint()); if (documentManager.isCommitted(editor.getDocument())) { // when document is committed, try to check injected stuff - it's fast int offset = editor.logicalPositionToOffset(editor.xyToLogicalPosition(point)); editor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(editor, psiFile, offset); } LogicalPosition pos = editor.xyToLogicalPosition(point); int offset = editor.logicalPositionToOffset(pos); int selStart = editor.getSelectionModel().getSelectionStart(); int selEnd = editor.getSelectionModel().getSelectionEnd(); myStoredModifiers = mouseEvent.getModifiers(); BrowseMode browseMode = getBrowseMode(myStoredModifiers); cancelPreviousTooltip(); if (browseMode == BrowseMode.None || offset >= selStart && offset < selEnd) { disposeHighlighter(); return; } myTooltipProvider = new TooltipProvider(editor, pos); myTooltipProvider.execute(browseMode); }
@NotNull public PsiFile getPsiFileForPsiDependentIndex() { Document document = FileDocumentManager.getInstance().getCachedDocument(getFile()); PsiFile psi = null; if (document != null) { PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(getProject()); if (psiDocumentManager.isUncommited(document)) { PsiFile existingPsi = psiDocumentManager.getPsiFile(document); if (existingPsi != null) { psi = existingPsi; } } } if (psi == null) { psi = getPsiFile(); } return psi; }
@Override public boolean isEnabled(Editor editor, DataContext dataContext) { if (editor.isViewer() || editor.isOneLineMode()) return false; final Project project = editor.getProject(); if (project == null || project.isDisposed()) return false; final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); final Document document = editor.getDocument(); documentManager.commitDocument(document); PsiFile psiFile = documentManager.getPsiFile(document); PsiFile file = getRoot(psiFile, editor); if (file == null) return false; final MoverWrapper mover = getSuitableMover(editor, file); if (mover == null || mover.getInfo().toMove2 == null) return false; final int maxLine = editor.offsetToLogicalPosition(editor.getDocument().getTextLength()).line; final LineRange range = mover.getInfo().toMove; if (range.startLine == 0 && !isDown) return false; return range.endLine <= maxLine || !isDown; }
static void indentBlock( Project project, Editor editor, final int startOffset, final int endOffset, int originalCaretCol) { final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project); documentManager.commitAllDocuments(); final Document document = editor.getDocument(); PsiFile file = documentManager.getPsiFile(document); if (file == null) { return; } if (LanguageFormatting.INSTANCE.forContext(file) != null) { indentBlockWithFormatter(project, document, startOffset, endOffset, file); } else { indentPlainTextBlock(document, startOffset, endOffset, originalCaretCol); } }
@Override @NotNull public List<TextEditorHighlightingPass> instantiatePasses( @NotNull final PsiFile psiFile, @NotNull final Editor editor, @NotNull final int[] passesToIgnore) { synchronized (this) { if (!checkedForCycles) { checkedForCycles = true; checkForCycles(); } } PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject); final Document document = editor.getDocument(); PsiFile fileFromDoc = documentManager.getPsiFile(document); if (!(fileFromDoc instanceof PsiCompiledElement)) { assert fileFromDoc == psiFile : "Files are different: " + psiFile + ";" + fileFromDoc; Document documentFromFile = documentManager.getDocument(psiFile); assert documentFromFile == document : "Documents are different. Doc: " + document + "; Doc from file: " + documentFromFile + "; File: " + psiFile + "; Virtual file: " + PsiUtilCore.getVirtualFile(psiFile); } final TIntObjectHashMap<TextEditorHighlightingPass> id2Pass = new TIntObjectHashMap<TextEditorHighlightingPass>(); final TIntArrayList passesRefusedToCreate = new TIntArrayList(); myRegisteredPassFactories.forEachKey( new TIntProcedure() { @Override public boolean execute(int passId) { if (ArrayUtil.find(passesToIgnore, passId) != -1) { return true; } PassConfig passConfig = myRegisteredPassFactories.get(passId); TextEditorHighlightingPassFactory factory = passConfig.passFactory; final TextEditorHighlightingPass pass = factory.createHighlightingPass(psiFile, editor); if (pass == null) { passesRefusedToCreate.add(passId); } else { // init with editor's colors scheme pass.setColorsScheme(editor.getColorsScheme()); TIntArrayList ids = new TIntArrayList(passConfig.completionPredecessorIds.length); for (int id : passConfig.completionPredecessorIds) { if (myRegisteredPassFactories.containsKey(id)) ids.add(id); } pass.setCompletionPredecessorIds( ids.isEmpty() ? ArrayUtil.EMPTY_INT_ARRAY : ids.toNativeArray()); ids = new TIntArrayList(passConfig.startingPredecessorIds.length); for (int id : passConfig.startingPredecessorIds) { if (myRegisteredPassFactories.containsKey(id)) ids.add(id); } pass.setStartingPredecessorIds( ids.isEmpty() ? ArrayUtil.EMPTY_INT_ARRAY : ids.toNativeArray()); pass.setId(passId); id2Pass.put(passId, pass); } return true; } }); DaemonCodeAnalyzerEx daemonCodeAnalyzer = DaemonCodeAnalyzerEx.getInstanceEx(myProject); final FileStatusMap statusMap = daemonCodeAnalyzer.getFileStatusMap(); passesRefusedToCreate.forEach( new TIntProcedure() { @Override public boolean execute(int passId) { statusMap.markFileUpToDate(document, passId); return true; } }); return (List) Arrays.asList(id2Pass.getValues()); }