-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get IPreferenceStore from AbstractTextEditor with adapt mechanism #88
Get IPreferenceStore from AbstractTextEditor with adapt mechanism #88
Conversation
This simple PR provides the capabaility to get IPreferenceStore from an AbstractTextEditor with adapt mechanism: ITextEditor = textEditor = ...
IPreferenceStore store = textEditor.getAdapter(IPreferenceStore.class); It avoids using Java Reflection to access it: Field field = AbstractTextEditor.class.getDeclaredField("fPreferenceStore");
field.setAccessible(true);
IPreferenceStore store = (IPreferenceStore) field.get(textEditor); I have 2 usecases where I will need to get IPreferenceStore from a ITextEditor:
ITextEditor textEditor = ...
IPreferenceStore store = textEditor.getAdapter(IPreferenceStore.class);
boolean spacesForTabs = (indent_style != tab)
if (oldSpacesForTabs != spacesForTabs) {
store.firePropertyChangeEvent(EDITOR_SPACES_FOR_TABS, oldSpacesForTabs, spacesForTabs);
} See:
See:
LSP4e hard code teh Editors UI preference store for format: IPreferenceStore store = EditorsUI.getPreferenceStore();
int tabWidth = store.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); With this PR we will able to write: ITextEditor textEditor = ...
IPreferenceStore store = textEditor.getAdapter(IPreferenceStore.class);
int tabWidth = store.getInt(AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH); The IPreferenceStore from editor could contains a custom IPreferenceStore for the XML language (XMLLanguagePreferenceStore which will return the value of custom spaces preferences for AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good approach here!
@angelozerr the build failure is not related to your change, I'll take a look into it! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still have concerns with the idea of reusing the editor preference store while most of the possible operations may not require an editor but could instead start from an IDocument (think about JDT-LS).
We're discussing possible alternatives (or lack of) with @angelozerr and possible increments towards the bigger goal. I'd rather wait for some former conclusions to be reached before merging this.
@mickaelistria here a concrete usecase for this PR. LSP4e formatter needs to get the preference store of the generic editor to get tabWidth / insert spaces for a given language |
@mickaelistria here an new concrete sample with TM4e language configuration to get insert spaces / tab width when on enter rule (when user type Enter,it indent correctly and should get the tab with / insert spaces from the editor preference store See * and access to the editor preference store https://github.com/eclipse/tm4e/pull/461/files#diff-2961979865d7499073f384bc8a71472fb1d5298dd54d3c4713c8f6ed94405801R295 |
Is there any chance to merge this PR? I tried to show you several usecases when IPreferenceStore must be get from an editor, and today we need to use Java reflection to access it, which is very bad. |
As it is about editor-config I think it is fair to adapt an editor but have no idea about the IDocument approach @mickaelistria maybe you can outline a bit more how this is supposed to work? I also checked the code-base a bit and I don't see that only one approach will work, for a full solution one would most probably need different approaches, e.g IDocument, Editors and even IFile as one can format source files also without any editor (and thus possibly any document). @angelozerr can you rebase the PR and make sure the build works? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the change is not harmful and do not add any special maintenance burden.
Even if there are better ways to archive this it could be a good first step.
It is not only for .editorconfig support. The main idea is that an ITextEditor is created with preference store and some external component need to access to those preferences. Take a sample without .editorconfig:. In WWD we have more and more users request who want to have the capability to customize spaces for XML (and don't use the global EditorUI preferences, like WTP XML editor). An external component created outside the WWD (like LSP4E, TM4E) needs to access to this preference to support format, auto editing strategies, etc, and they don't know WWD and which preferences store which is used to manages spaces. This PR gives the capability to get a preference store registered in the editor without using Java Reflection because AbstractTextEditor#getPreferenceStore is protected. I have several usescase to use this PR: |
dbf58fc
to
99a58e2
Compare
Signed-off-by: azerr <azerr@redhat.com>
rebased |
All checks are green, please @mickaelistria either adjust your review or give guidance how we can proceed here. My vote would be to merge this and if we later find better alternative it even is not harmful. Maybe also @vogella likes to give another review on this. |
LGTM |
What about making |
|
While I don't think those arguments are really strong; and that not publishing an API and instead relying on unspecified behavior is ugly; I looked at the code, and it really seems like there is a real effort to not expose such implementation details. |
Get IPreferenceStore from AbstractTextEditor with adapt mechanism
Signed-off-by: azerr azerr@redhat.com