Skip to content

Commit 4862bc5

Browse files
committed
feat: extend Editors
1 parent 3d11a26 commit 4862bc5

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

  • plugin/src/main/java/de/sebthom/eclipse/commons/ui

plugin/src/main/java/de/sebthom/eclipse/commons/ui/Editors.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.eclipse.ui.IPathEditorInput;
2525
import org.eclipse.ui.IURIEditorInput;
2626
import org.eclipse.ui.IWorkbenchPage;
27+
import org.eclipse.ui.IWorkbenchPart;
2728
import org.eclipse.ui.part.MultiPageEditorPart;
2829
import org.eclipse.ui.texteditor.ITextEditor;
2930

@@ -43,7 +44,7 @@ public abstract class Editors {
4344
return getActiveEditor(UI.getActiveWorkbenchPage());
4445
}
4546

46-
public static @Nullable IEditorPart getActiveEditor(@Nullable final IWorkbenchPage workbenchPage) {
47+
public static @Nullable IEditorPart getActiveEditor(final @Nullable IWorkbenchPage workbenchPage) {
4748
if (workbenchPage == null)
4849
return null;
4950
return workbenchPage.getActiveEditor();
@@ -54,20 +55,11 @@ public abstract class Editors {
5455
}
5556

5657
public static @Nullable ITextEditor getActiveTextEditor() {
57-
final var editor = getActiveEditor();
58-
if (editor == null)
59-
return null;
60-
61-
if (editor instanceof final ITextEditor textEditor)
62-
return textEditor;
63-
64-
if (editor instanceof final MultiPageEditorPart pagePart) {
65-
final var page = pagePart.getSelectedPage();
66-
if (page instanceof final ITextEditor textEditor)
67-
return textEditor;
68-
}
58+
return getActiveTextEditor(UI.getActiveWorkbenchPage());
59+
}
6960

70-
return Adapters.adapt(editor, ITextEditor.class);
61+
public static @Nullable ITextEditor getActiveTextEditor(final @Nullable IWorkbenchPage workbenchPage) {
62+
return getTextEditor(getActiveEditor(workbenchPage));
7163
}
7264

7365
public static @Nullable String getActiveTextSelection() {
@@ -146,6 +138,28 @@ public static String getText(final @Nullable ITextEditor editor) {
146138
return doc.get();
147139
}
148140

141+
public static @Nullable ITextEditor getTextEditor(final @Nullable IEditorPart editorPart) {
142+
if (editorPart == null)
143+
return null;
144+
145+
if (editorPart instanceof final ITextEditor textEditor)
146+
return textEditor;
147+
148+
if (editorPart instanceof final MultiPageEditorPart multiPagePart) {
149+
final var page = multiPagePart.getSelectedPage();
150+
if (page instanceof final ITextEditor textEditor)
151+
return textEditor;
152+
}
153+
154+
return Adapters.adapt(editorPart, ITextEditor.class);
155+
}
156+
157+
public static @Nullable ITextEditor getTextEditor(final @Nullable IWorkbenchPart part) {
158+
if (part instanceof final IEditorPart editorPart)
159+
return getTextEditor(editorPart);
160+
return null;
161+
}
162+
149163
public static boolean replaceCurrentSelection(final String replacement, final boolean selectReplacement) {
150164
final var editor = getActiveTextEditor();
151165
if (editor == null)

0 commit comments

Comments
 (0)