eclipse-platform / eclipse.platform.text

8 stars 45 forks source link

ITextEditorAware API to inject ITextEditor. #108

Closed angelozerr closed 1 year ago

angelozerr commented 2 years ago

ITextEditorAware API to inject ITextEditor.

Signed-off-by: azerr azerr@redhat.com

angelozerr commented 2 years ago

ITextEditorAware API allows to inject an instance of ITextEditor.

I create this API because I need it for https://github.com/eclipse-platform/eclipse.platform.ui/issues/877

More I think this API can be very helpfull for generic editor. My idea is when generic editor creates a custom reconciler for instance, it check that the instanciated class implemented ITextEditorAware and in this case it set the editor to the contribution here https://github.com/eclipse-platform/eclipse.platform.text/blob/1900585152bdd052e3c38e2da2440b3405fb36ee/org.eclipse.ui.genericeditor/src/org/eclipse/ui/internal/genericeditor/GenericContentTypeRelatedExtension.java#L59

With this code:

@SuppressWarnings("unchecked")
public T createDelegate(ITextEditor editor) {
try {
    T delegateInstance = (T) extension.createExecutableExtension(CLASS_ATTRIBUTE);
    if (delegateInstance instanceof ITextEditorAware) {
        ((ITextEditorAware) delegateInstance).setEditor(editor);
    }
    return delegateInstance;
} catch (CoreException e) {
    GenericEditorPlugin.getDefault().getLog()
            .log(new Status(IStatus.ERROR, GenericEditorPlugin.BUNDLE_ID, e.getMessage(), e));
}
return null;
}

A good usecase is in LSP4e https://github.com/eclipse/lsp4e/blob/c6859853bd37c5ae70e9ee9ba43495ff28d668d3/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSContentAssistProcessor.java#L197

Here the required editor is get with a static method, by getting the current activate editor. It should be cleaner to do like this:

public class LSContentAssistProcessor implements IContentAssistProcessor, ITextEditorAware {

    private TextEditor editor;

    @Override
    public void setEditor(ITextEditor editor) {
       this.editor = editor;
   }
  ...
}

@mickaelistria do you like this idea?

angelozerr commented 2 years ago

Ok I have updated my PR to use ITextEditorAware with generic editor. It will opens the door to access to the generic editor in any contribution.

The main idea is to avoid getting the activated editor to get the editor in each contribution. I see that:

angelozerr commented 2 years ago

@mickaelistria here a concrete sample in the PR https://github.com/eclipse/tm4e/pull/461 which uses ITextEditorAware to have cleaner code in TM4e language configuration

mickaelistria commented 1 year ago

https://github.com/eclipse-pde/eclipse.pde/pull/402 should fix further issues.

vogella commented 1 year ago

Is this PR still relevant?

angelozerr commented 1 year ago

I think so, iy should avoid writing that https://github.com/eclipse/lsp4e/blob/c6859853bd37c5ae70e9ee9ba43495ff28d668d3/org.eclipse.lsp4e/src/org/eclipse/lsp4e/operations/completion/LSContentAssistProcessor.java#L197

laeubi commented 1 year ago

This repository was merged with https://github.com/eclipse-platform/eclipse.platform.ui if your pull-request is still relevant please rebase the code and push it to the new repository.