Skip to content

Add adapter factory for XtextDocument to support URI, IFile, and IPath - #3614

Closed
raghucssit wants to merge 1 commit into
eclipse-xtext:mainfrom
raghucssit:xtextdocument_adapter_factory
Closed

raghucssit wants to merge 1 commit into
eclipse-xtext:mainfrom
raghucssit:xtextdocument_adapter_factory

Conversation

@raghucssit

Copy link
Copy Markdown
Contributor

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

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 XtextDocumentAdapterFactory to provide adapter support for XtextDocument to commonly used Eclipse resource types
  • Extends XtextDocument.getAdapter() to handle java.net.URI type conversion by obtaining the file's location URI
  • Registers the adapter factory in plugin.xml for 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.

Comment on lines +42 to +65
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;
}

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +44 to +63

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);
}
}
}

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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;
}

Copilot uses AI. Check for mistakes.
private static final Class<?>[] ADAPTER_LIST = new Class<?>[] { URI.class, IFile.class, IPath.class };

@Override
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
public <T> T getAdapter(Object adaptableObject, Class<T> adapterType) {
if (!(adaptableObject instanceof XtextDocument)) {
return null;
}

Copilot uses AI. Check for mistakes.
Comment on lines +46 to +58
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)) {

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
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) {

Copilot uses AI. Check for mistakes.
Comment on lines +657 to +668
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;
}

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +38 to +71
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

Copilot AI Feb 18, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. The adapter factory correctly adapts XtextDocument to java.net.URI, IFile, and IPath
  2. The adapters work for both platform resources and non-platform resources (e.g., file:// URIs)
  3. The adapter returns null when adaptation is not possible
  4. 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.

Copilot uses AI. Check for mistakes.
@raghucssit

Copy link
Copy Markdown
Contributor Author

@iloveeclipse FYI

@iloveeclipse

Copy link
Copy Markdown
Contributor

See discussion on #3615.

@cdietrich

Copy link
Copy Markdown
Contributor

what is the state here? is the additional or as a replacement to the other pr that was merged

@iloveeclipse

Copy link
Copy Markdown
Contributor

This can be closed, #3615 is better.

@cdietrich cdietrich closed this Mar 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants