diff options
| author | Jonah Graham | 2019-01-06 16:43:42 +0000 |
|---|---|---|
| committer | Jonah Graham | 2019-01-24 11:46:12 +0000 |
| commit | fda12d10396d27d017aa8f2d442c832e297d8fd9 (patch) | |
| tree | 9f20f82e1108c161e8c7e462b4dc3adbde123efb | |
| parent | 2a5c4f666c6cbe1db8082d9193cef516c53ed866 (diff) | |
| download | lsp4e-fda12d10396d27d017aa8f2d442c832e297d8fd9.tar.gz lsp4e-fda12d10396d27d017aa8f2d442c832e297d8fd9.tar.xz lsp4e-fda12d10396d27d017aa8f2d442c832e297d8fd9.zip | |
Bug 543281: Support more source lookup/director options
In particular add support for IFile's being located, and
try to open the correct editor (instead of hardcoding generic
editor).
Change-Id: I86c432f28a47d8c2da1928f186ebfbe522816943
3 files changed, 217 insertions, 2 deletions
diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/DSPDebugModelPresentation.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/DSPDebugModelPresentation.java index 263ceeab..87418436 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/DSPDebugModelPresentation.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/DSPDebugModelPresentation.java @@ -8,18 +8,23 @@ *******************************************************************************/ package org.eclipse.lsp4e.debug.presentation; +import java.net.URI; + import org.eclipse.core.filesystem.EFS; import org.eclipse.core.filesystem.IFileStore; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IWorkspaceRoot; import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.Path; +import org.eclipse.core.runtime.Platform; import org.eclipse.debug.core.model.IDisconnect; import org.eclipse.debug.core.model.ILineBreakpoint; import org.eclipse.debug.core.model.ITerminate; import org.eclipse.debug.core.model.IValue; import org.eclipse.debug.internal.ui.DebugUIPlugin; import org.eclipse.debug.ui.IDebugModelPresentation; +import org.eclipse.debug.ui.ISourcePresentation; import org.eclipse.debug.ui.IValueDetailListener; import org.eclipse.jface.resource.JFaceResources; import org.eclipse.jface.viewers.IFontProvider; @@ -31,8 +36,14 @@ import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Font; import org.eclipse.swt.graphics.FontData; import org.eclipse.swt.widgets.Display; +import org.eclipse.ui.IEditorDescriptor; import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorRegistry; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.IURIEditorInput; +import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.FileStoreEditorInput; +import org.eclipse.ui.ide.IDE; import org.eclipse.ui.part.FileEditorInput; public class DSPDebugModelPresentation extends LabelProvider implements IDebugModelPresentation, IFontProvider { @@ -111,6 +122,10 @@ public class DSPDebugModelPresentation extends LabelProvider implements IDebugMo if (element instanceof ILineBreakpoint) { return new FileEditorInput((IFile) ((ILineBreakpoint) element).getMarker().getResource()); } + if (element instanceof IFile) { + return new FileEditorInput((IFile) element); + } + IFileStore fileStore = EFS.getLocalFileSystem().getStore(new Path(element.toString())); IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot(); IFile[] files = root.findFilesForLocationURI(fileStore.toURI()); @@ -126,8 +141,49 @@ public class DSPDebugModelPresentation extends LabelProvider implements IDebugMo @Override public String getEditorId(IEditorInput input, Object element) { - // return EditorsUI.DEFAULT_TEXT_EDITOR_ID; - return "org.eclipse.ui.genericeditor.GenericEditor"; + String id = null; + if (input != null) { + IEditorDescriptor descriptor = null; + if (input instanceof IFileEditorInput) { + IFileEditorInput fileEditorInput = (IFileEditorInput) input; + IFile file = fileEditorInput.getFile(); + descriptor = IDE.getDefaultEditor(file); + } else if (input instanceof IURIEditorInput) { + IURIEditorInput uriEditorInput = (IURIEditorInput) input; + URI uri = uriEditorInput.getURI(); + try { + IFileStore fileStore = EFS.getStore(uri); + id = WorkaroundForBug516470.getEditorId(fileStore, false); + } catch (CoreException e) { + // fallback to default case + } + } + if (id == null) { + if (descriptor == null) { + IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry(); + descriptor = registry.getDefaultEditor(input.getName()); + } + + id = "org.eclipse.ui.genericeditor.GenericEditor"; + if (descriptor != null) { + id = descriptor.getId(); + } + } + + if (id == null && element instanceof ILineBreakpoint) { + // There is no associated editor ID for this breakpoint, see if an alternative + // can be supplied from an adapter. + ISourcePresentation sourcePres = Platform.getAdapterManager().getAdapter(element, + ISourcePresentation.class); + if (sourcePres != null) { + String lid = sourcePres.getEditorId(input, element); + if (lid != null) { + id = lid; + } + } + } + } + return id; } @Override diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/WorkaroundForBug516470.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/WorkaroundForBug516470.java new file mode 100644 index 00000000..811429b2 --- /dev/null +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/presentation/WorkaroundForBug516470.java @@ -0,0 +1,157 @@ +package org.eclipse.lsp4e.debug.presentation; + +import java.io.IOException; +import java.io.InputStream; + +import org.eclipse.core.filesystem.EFS; +import org.eclipse.core.filesystem.IFileStore; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.Platform; +import org.eclipse.core.runtime.content.IContentType; +import org.eclipse.ui.IEditorDescriptor; +import org.eclipse.ui.IEditorRegistry; +import org.eclipse.ui.PartInitException; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.ide.FileStoreEditorInput; +import org.eclipse.ui.ide.IDE; +import org.eclipse.ui.ide.IUnassociatedEditorStrategy; +import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; +import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; +import org.eclipse.ui.internal.ide.registry.SystemEditorOrTextEditorStrategy; +import org.eclipse.ui.internal.ide.registry.UnassociatedEditorStrategyRegistry; + +/** + * This class contains copy of code that is currently private in {@link IDE}. + * Bug 516470 is to workaround this issue. + */ +public class WorkaroundForBug516470 { + + /** + * Returns an editor id appropriate for opening the given file store. + * <p> + * The editor descriptor is determined using a multi-step process. This method + * will attempt to resolve the editor based on content-type bindings as well as + * traditional name/extension bindings. + * </p> + * <ol> + * <li>The workbench editor registry is consulted to determine if an editor + * extension has been registered for the file type. If so, an instance of the + * editor extension is opened on the file. See + * <code>IEditorRegistry.getDefaultEditor(String)</code>.</li> + * <li>The operating system is consulted to determine if an in-place component + * editor is available (e.g. OLE editor on Win32 platforms).</li> + * <li>The operating system is consulted to determine if an external editor is + * available.</li> + * <li>The workbench editor registry is consulted to determine if the default + * text editor is available.</li> + * </ol> + * </p> + * + * @param fileStore the file store + * @return the id of an editor, appropriate for opening the file + * @throws PartInitException if no editor can be found + * @todo The IDE class has this method as a private, copied here so that it can + * be exposed. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=516470 + * @deprecated Deprecated on creation as this is waiting for Bug 516470 to be + * resolved + */ + @Deprecated + public static String getEditorId(IFileStore fileStore, boolean allowInteractive) throws PartInitException { + String name = fileStore.fetchInfo().getName(); + if (name == null) { + throw new IllegalArgumentException(); + } + + IContentType contentType = null; + try { + InputStream is = null; + try { + is = fileStore.openInputStream(EFS.NONE, null); + contentType = Platform.getContentTypeManager().findContentTypeFor(is, name); + } finally { + if (is != null) { + is.close(); + } + } + } catch (CoreException ex) { + // continue without content type + } catch (IOException ex) { + // continue without content type + } + + IEditorRegistry editorReg = PlatformUI.getWorkbench().getEditorRegistry(); + + IEditorDescriptor defaultEditor = editorReg.getDefaultEditor(name, contentType); + defaultEditor = IDE.overrideDefaultEditorAssociation(new FileStoreEditorInput(fileStore), contentType, + defaultEditor); + return getEditorDescriptor(name, editorReg, defaultEditor, allowInteractive).getId(); + } + + /** + * Get the editor descriptor for a given name using the editorDescriptor passed + * in as a default as a starting point. + * + * @param name The name of the element to open. + * @param editorReg The editor registry to do the lookups from. + * @param defaultDescriptor IEditorDescriptor or <code>null</code> + * @return IEditorDescriptor + * @throws PartInitException if no valid editor can be found + * + * @todo The IDE class has this method as a private, copied here so that it can + * be exposed via getEditorId. See + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=516470 + * @deprecated Deprecated on creation as this is waiting for Bug 516470 to be + * resolved + */ + @Deprecated + private static IEditorDescriptor getEditorDescriptor(String name, IEditorRegistry editorReg, + IEditorDescriptor defaultDescriptor, boolean allowInteractive) throws PartInitException { + + if (defaultDescriptor != null) { + return defaultDescriptor; + } + + IUnassociatedEditorStrategy strategy = getUnassociatedEditorStrategy(allowInteractive); + IEditorDescriptor editorDesc; + try { + editorDesc = strategy.getEditorDescriptor(name, editorReg); + } catch (CoreException e) { + throw new PartInitException(IDEWorkbenchMessages.IDE_noFileEditorFound, e); + } + + // if no valid editor found, bail out + if (editorDesc == null) { + throw new PartInitException(IDEWorkbenchMessages.IDE_noFileEditorFound); + } + + return editorDesc; + } + + /** + * @param allowInteractive Whether interactive strategies are considered + * @return The strategy to use in order to open unknown file. Either as set by + * preference, or a {@link SystemEditorOrTextEditorStrategy} if none is + * explicitly configured. Never returns {@code null}. + * + * @todo The IDE class has this method as a private, copied here so that it can + * be exposed via getEditorId. See + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=516470 + * @deprecated Deprecated on creation as this is waiting for Bug 516470 to be + * resolved + */ + @Deprecated + private static IUnassociatedEditorStrategy getUnassociatedEditorStrategy(boolean allowInteractive) { + String preferedStrategy = IDEWorkbenchPlugin.getDefault().getPreferenceStore() + .getString(IDE.UNASSOCIATED_EDITOR_STRATEGY_PREFERENCE_KEY); + IUnassociatedEditorStrategy res = null; + UnassociatedEditorStrategyRegistry registry = IDEWorkbenchPlugin.getDefault() + .getUnassociatedEditorStrategyRegistry(); + if (allowInteractive || !registry.isInteractive(preferedStrategy)) { + res = registry.getStrategy(preferedStrategy); + } + if (res == null) { + res = new SystemEditorOrTextEditorStrategy(); + } + return res; + } +} diff --git a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/sourcelookup/DSPSourceLookupDirector.java b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/sourcelookup/DSPSourceLookupDirector.java index 8d668e7d..b4a4ef6f 100644 --- a/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/sourcelookup/DSPSourceLookupDirector.java +++ b/org.eclipse.lsp4e.debug/src/org/eclipse/lsp4e/debug/sourcelookup/DSPSourceLookupDirector.java @@ -13,6 +13,8 @@ import org.eclipse.debug.core.sourcelookup.ISourceLookupParticipant; public class DSPSourceLookupDirector extends AbstractSourceLookupDirector { + public static final String ID = "org.eclipse.lsp4e.debug.sourceLocator"; + @Override public void initializeParticipants() { addParticipants(new ISourceLookupParticipant[] { new DSPSourceLookupParticipant() }); |
