Repository navigation
Add adapter factory for XtextDocument to support URI, IFile, and IPath - #3614
raghucssit wants to merge 1 commit into
Conversation
conversions. Clients like lsp4e and github copilot tries to derive URI and IFile from IDocument. XtextDocument does not implement IAdaptable so direct adaption is not possible. So we introduce adapter factory. see eclipse-xtext#3613
There was a problem hiding this comment.
Pull request overview
This pull request adds an adapter factory for XtextDocument to enable conversion to java.net.URI, IFile, and IPath types, allowing LSP4E and GitHub Copilot to properly derive URIs and files from Xtext documents.
Changes:
- Introduces
XtextDocumentAdapterFactoryto provide adapter support forXtextDocumentto commonly used Eclipse resource types - Extends
XtextDocument.getAdapter()to handlejava.net.URItype conversion by obtaining the file's location URI - Registers the adapter factory in
plugin.xmlfor the Eclipse adapter manager
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/model/XtextDocumentAdapterFactory.java | New adapter factory providing conversion from XtextDocument to URI, IFile, and IPath types |
| org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/model/XtextDocument.java | Adds java.net.URI adapter support to the getAdapter method |
| org.eclipse.xtext.ui/plugin.xml | Registers the XtextDocumentAdapterFactory with the Eclipse adapter manager |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) { | ||
| XtextDocument doc = (XtextDocument) adaptableObject; | ||
|
|
||
| URI uri = doc.getAdapter(URI.class); | ||
| if (uri != null && adapterType.isInstance(uri)) { | ||
| return adapterType.cast(uri); | ||
| } | ||
|
|
||
| IFile file = doc.getAdapter(IFile.class); | ||
| if (file != null && adapterType.isInstance(file)) { | ||
| return adapterType.cast(file); | ||
| } | ||
|
|
||
| if (IPath.class.equals(adapterType)) { | ||
| if (file != null) { | ||
| IPath fullPath = file.getFullPath(); | ||
| if (fullPath != null && adapterType.isInstance(fullPath)) { | ||
| return adapterType.cast(fullPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } |
There was a problem hiding this comment.
The adapter factory creates an infinite recursion when adapting to IFile or IPath for documents with non-platform resource URIs (e.g., file:// URIs).
When the adapter factory calls doc.getAdapter(IFile.class) (line 50), it eventually reaches IXtextDocument.getAdapter() which, for non-platform resources, falls through to Platform.getAdapterManager().getAdapter(this, adapterType) (line 48 in IXtextDocument.java). This calls the adapter factory again, creating infinite recursion.
The adapter factory should directly implement the adaptation logic instead of delegating back to the document's getAdapter method. It should extract the EMF URI from the document, check if it's a platform resource, and create the IFile/IPath accordingly.
|
|
||
| URI uri = doc.getAdapter(URI.class); | ||
| if (uri != null && adapterType.isInstance(uri)) { | ||
| return adapterType.cast(uri); | ||
| } | ||
|
|
||
| IFile file = doc.getAdapter(IFile.class); | ||
| if (file != null && adapterType.isInstance(file)) { | ||
| return adapterType.cast(file); | ||
| } | ||
|
|
||
| if (IPath.class.equals(adapterType)) { | ||
| if (file != null) { | ||
| IPath fullPath = file.getFullPath(); | ||
| if (fullPath != null && adapterType.isInstance(fullPath)) { | ||
| return adapterType.cast(fullPath); | ||
| } | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
The adapter factory logic is inefficient and performs redundant work. When adapting to IPath, it first calls doc.getAdapter(URI.class) which is never used, then calls doc.getAdapter(IFile.class) which performs all the work to get the IFile. For URI and IFile requests, both adapters are retrieved even though only one is needed.
The logic should be restructured to directly handle each adapter type without retrieving unnecessary adapters. For example, when IPath is requested, directly get the IFile (or compute it from the EMF URI) and derive the IPath from it.
| URI uri = doc.getAdapter(URI.class); | |
| if (uri != null && adapterType.isInstance(uri)) { | |
| return adapterType.cast(uri); | |
| } | |
| IFile file = doc.getAdapter(IFile.class); | |
| if (file != null && adapterType.isInstance(file)) { | |
| return adapterType.cast(file); | |
| } | |
| if (IPath.class.equals(adapterType)) { | |
| if (file != null) { | |
| IPath fullPath = file.getFullPath(); | |
| if (fullPath != null && adapterType.isInstance(fullPath)) { | |
| return adapterType.cast(fullPath); | |
| } | |
| } | |
| } | |
| if (URI.class.equals(adapterType)) { | |
| URI uri = doc.getAdapter(URI.class); | |
| if (uri != null) { | |
| return adapterType.cast(uri); | |
| } | |
| return null; | |
| } | |
| if (IFile.class.equals(adapterType)) { | |
| IFile file = doc.getAdapter(IFile.class); | |
| if (file != null) { | |
| return adapterType.cast(file); | |
| } | |
| return null; | |
| } | |
| if (IPath.class.equals(adapterType)) { | |
| IFile file = doc.getAdapter(IFile.class); | |
| if (file != null) { | |
| IPath fullPath = file.getFullPath(); | |
| if (fullPath != null) { | |
| return adapterType.cast(fullPath); | |
| } | |
| } | |
| return null; | |
| } |
| private static final Class<?>[] ADAPTER_LIST = new Class<?>[] { URI.class, IFile.class, IPath.class }; | ||
|
|
||
| @Override | ||
| public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) { |
There was a problem hiding this comment.
Missing null check for adaptableObject. The method casts adaptableObject to XtextDocument without checking if it's null or if it's actually an instance of XtextDocument. This can cause ClassCastException if the adapter manager passes an unexpected object type.
Add a type check: if (!(adaptableObject instanceof XtextDocument)) return null; before the cast.
| public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) { | |
| public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) { | |
| if (!(adaptableObject instanceof XtextDocument)) { | |
| return null; | |
| } |
| if (uri != null && adapterType.isInstance(uri)) { | ||
| return adapterType.cast(uri); | ||
| } | ||
|
|
||
| IFile file = doc.getAdapter(IFile.class); | ||
| if (file != null && adapterType.isInstance(file)) { | ||
| return adapterType.cast(file); | ||
| } | ||
|
|
||
| if (IPath.class.equals(adapterType)) { | ||
| if (file != null) { | ||
| IPath fullPath = file.getFullPath(); | ||
| if (fullPath != null && adapterType.isInstance(fullPath)) { |
There was a problem hiding this comment.
Redundant type checks in lines 46, 51, and 58. The condition adapterType.isInstance(uri) and similar checks are unnecessary because if uri/file/fullPath is not null and the types match what's expected (URI/IFile/IPath respectively), the isInstance check will always be true. The method already filters by adapterType through the switch logic, so these checks add no value and can be removed for clarity.
| if (uri != null && adapterType.isInstance(uri)) { | |
| return adapterType.cast(uri); | |
| } | |
| IFile file = doc.getAdapter(IFile.class); | |
| if (file != null && adapterType.isInstance(file)) { | |
| return adapterType.cast(file); | |
| } | |
| if (IPath.class.equals(adapterType)) { | |
| if (file != null) { | |
| IPath fullPath = file.getFullPath(); | |
| if (fullPath != null && adapterType.isInstance(fullPath)) { | |
| if (URI.class.equals(adapterType) && uri != null) { | |
| return adapterType.cast(uri); | |
| } | |
| IFile file = doc.getAdapter(IFile.class); | |
| if (IFile.class.equals(adapterType) && file != null) { | |
| return adapterType.cast(file); | |
| } | |
| if (IPath.class.equals(adapterType)) { | |
| if (file != null) { | |
| IPath fullPath = file.getFullPath(); | |
| if (fullPath != null) { |
| if (java.net.URI.class.equals(adapterType)) { | ||
| if (this.resource != null) { | ||
| IFile ifile = IXtextDocument.super.getAdapter(IFile.class); | ||
| if (ifile != null) { | ||
| java.net.URI uri = ifile.getLocationURI(); | ||
| if (uri != null) { | ||
| return adapterType.cast(uri); | ||
| } | ||
| } | ||
| } | ||
| return null; | ||
| } |
There was a problem hiding this comment.
The implementation creates a circular dependency that can lead to infinite recursion. When the document's EMF resource URI is not a platform resource (e.g., file:// URI), calling IXtextDocument.super.getAdapter(IFile.class) at line 659 will eventually reach Platform.getAdapterManager().getAdapter(this, IFile.class) (IXtextDocument.java line 48), which invokes the XtextDocumentAdapterFactory. The factory then calls back to doc.getAdapter(URI.class) (XtextDocumentAdapterFactory.java line 45), creating infinite recursion.
The java.net.URI adaptation logic should not rely on getting IFile through the adapter manager. Instead, it should directly check if the EMF resource URI is a platform resource and create the IFile without going through the adapter manager, similar to how IXtextDocument.getAdapter does it for IFile.
| public class XtextDocumentAdapterFactory implements IAdapterFactory { | ||
| private static final Class<?>[] ADAPTER_LIST = new Class<?>[] { URI.class, IFile.class, IPath.class }; | ||
|
|
||
| @Override | ||
| public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) { | ||
| XtextDocument doc = (XtextDocument) adaptableObject; | ||
|
|
||
| URI uri = doc.getAdapter(URI.class); | ||
| if (uri != null && adapterType.isInstance(uri)) { | ||
| return adapterType.cast(uri); | ||
| } | ||
|
|
||
| IFile file = doc.getAdapter(IFile.class); | ||
| if (file != null && adapterType.isInstance(file)) { | ||
| return adapterType.cast(file); | ||
| } | ||
|
|
||
| if (IPath.class.equals(adapterType)) { | ||
| if (file != null) { | ||
| IPath fullPath = file.getFullPath(); | ||
| if (fullPath != null && adapterType.isInstance(fullPath)) { | ||
| return adapterType.cast(fullPath); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<?>[] getAdapterList() { | ||
| return ADAPTER_LIST; | ||
| } | ||
| } No newline at end of file |
There was a problem hiding this comment.
Missing test coverage for the new adapter factory functionality. Given that the repository has comprehensive tests for XtextDocument (in org.eclipse.xtext.ui.tests/tests/org/eclipse/xtext/ui/tests/editor/model/), there should be tests verifying that:
- The adapter factory correctly adapts XtextDocument to java.net.URI, IFile, and IPath
- The adapters work for both platform resources and non-platform resources (e.g., file:// URIs)
- The adapter returns null when adaptation is not possible
- The XtextDocument.getAdapter method correctly handles java.net.URI requests
Tests should be added to ensure this functionality works as intended and to prevent regressions.
|
@iloveeclipse FYI |
|
See discussion on #3615. |
|
what is the state here? is the additional or as a replacement to the other pr that was merged |
|
This can be closed, #3615 is better. |
conversions.
Clients like lsp4e and github copilot tries to derive URI and IFile from IDocument. XtextDocument does not implement IAdaptable so direct adaption is not possible. So we introduce adapter factory.
see #3613