Editor (@NotNull Editor hostEditor, @Nullable Caret hostCaret, final @Nullable PsiFile injectedFile) { if (injectedFile == null || !(hostEditor instanceof EditorImpl) || hostEditor.isDisposed()) return hostEditor; Project project = hostEditor.getProject(); if (project == null) project = injectedFile.getProject(); Document document = PsiDocumentManager.getInstance(project).getDocument(injectedFile); if (!(document instanceof DocumentWindowImpl documentWindow)) return hostEditor; if (hostCaret != null && hostCaret.hasSelection()) { int selstart = hostCaret.getSelectionStart(); if (selstart != -1) { int selend = Math.max(selstart, hostCaret.getSelectionEnd()); if (!documentWindow.containsRange(selstart, selend)) { // selection spreads out the injected editor range return hostEditor; } } } if (!documentWindow.isValid()) { return hostEditor; // since the moment we got hold of injectedFile and this moment call, document may have been dirtied } InjectedEditorWindowTrackerImpl tracker = (InjectedEditorWindowTrackerImpl)ApplicationManager.getApplication().getService(InjectedEditorWindowTracker.class); return tracker.createEditor(documentWindow, (EditorImpl)hostEditor, injectedFile); }