Bug 457046 - Support .editorconfig (editorconfig.org)
Summary: Support .editorconfig (editorconfig.org)
Status: CLOSED MOVED
Alias: None
Product: Platform
Classification: Eclipse Project
Component: Text (show other bugs)
Version: 4.5   Edit
Hardware: All All
: P5 enhancement with 4 votes (vote)
Target Milestone: ---   Edit
Assignee: Platform-Text-Inbox CLA
QA Contact:
URL:
Whiteboard: RHT
Keywords:
Depends on: 494497
Blocks: 463071
  Show dependency tree
 
Reported: 2015-01-08 11:27 EST by Michael Scharf CLA
Modified: 2022-09-15 08:55 EDT (History)
12 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Scharf CLA 2015-01-08 11:27:22 EST
There is an emerging standard that is supported by many IDEs and editors:

http://editorconfig.org 

It would be great if eclipse would support .editorconfig files.
Comment 1 Martin Oberhuber CLA 2015-01-09 03:56:10 EST
It seems that the code to support most of the other IDE's is actually hosted on editorconfig.org (or various github projects). 

I don't know details about the text editor infrastructure at Eclipse, but I suspect that a support plugin for editorconfig.org would depend on the editorconfig core Java Library (which is hosted on github). Shouldn't thus a support plugin for Eclipse also be hosted on editorconfig.org / github, and not at Eclipse.org ?
Comment 2 Dani Megert CLA 2015-01-09 10:07:56 EST
(In reply to Michael Scharf from comment #0)
> There is an emerging standard that is supported by many IDEs and editors:
> 
> http://editorconfig.org 
> 
> It would be great if eclipse would support .editorconfig files.

Feel free to provide a high quality patch.
Comment 3 Michael Scharf CLA 2015-01-09 20:30:15 EST
Unfortunately, I don't have the time at the moment to provide a patch.

I thought this might be something the platform should support, because my guess is that this is hard to do as a plug-in (for all text editors).
Comment 4 Gautier de SAINT MARTIN LACAZE CLA 2015-04-01 05:37:49 EDT
Just for information, there is an existing plugin here https://github.com/ncjones/editorconfig-eclipse
Comment 5 Angelo ZERR CLA 2016-05-25 02:26:43 EDT
Hi,

I'm studying the capability to support .editorconfig inside Eclipse IDE by playing with https://github.com/ncjones/editorconfig-eclipse

This plugin apply the editorconfig when a text editor gain focus. When an editorgain focus:
 * it searchs the .editorconfig to use
 * it update for each preferences plugin (JDT, XML, Text, etc) the space, indent, etc preferences with global preferences

------------------------------------------
final IEclipsePreferences prefs = InstanceScope.INSTANCE.getNode("org.eclipse.ui.editors");
prefs.put("spacesForTabs", "true");
------------------------------------------

So it should NOT update the global preferences, but just updating the preferences for the editor instance.

To do that, see my comments at https://bugs.eclipse.org/bugs/show_bug.cgi?id=463071#c9

It should be cool if org.eclipse.ui.texteditor.AbstractTextEditor could host this code:

---------------------------------
AbstractTextEditor#handlePreferenceStoreChanged(PropertyChangeEvent)
	 */
	protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
...
}

@Override
public Object getAdapter(@SuppressWarnings("rawtypes") Class key) {
      if (key.equals(IEclipsePreferences.class)) {
        if (editorPreferences == null) {
		editorPreferences = new EclipsePreferences();
		editorPreferences.addPreferenceChangeListener(
				new org.eclipse.core.runtime.preferences.IEclipsePreferences.IPreferenceChangeListener() {

					@Override
					public void preferenceChange(PreferenceChangeEvent event) {
						handlePreferenceStoreChanged(new PropertyChangeEvent(event.getSource(), event.getKey(), event.getOldValue(), event.getNewValue()));
					}
				});
	}
      }
      return editorPreferences;
    }
        return super.getAdapter(key);
}

And after an external action can update the editor preferences like this:

---------------------------------
IEditorPart editor = ...
IEclipsePreferences p = (IEclipsePreferences) editor.getAdapter(IEclipsePreferences.class);
if (p != null) {
  p.put("spacesForTabs", "true")
}
---------------------------------

Hope you will like my idea.
Comment 6 Mickael Istria CLA 2016-05-25 02:29:48 EDT
Good point Angelo! I believe it's a good way to go. Can you please create another specific bug to request the adapter to access the Editor preferences and mark it as a dependency for this one?
The story of supporting editor config is too big for efficient tracking, we'll be more efficient splitting it into subtasks.
Comment 7 Angelo ZERR CLA 2016-05-25 02:38:30 EDT
> Good point Angelo! 

Thanks! Glad this idea please you:)

>I believe it's a good way to go. Can you please create another specific bug to request the adapter to access the Editor preferences and mark it as a dependency for this one?

doen at https://bugs.eclipse.org/bugs/show_bug.cgi?id=494497
Comment 8 Mickael Istria CLA 2016-12-07 04:19:57 EST
Would it be possible/sufficient to have editorconfig support be a listener on resource changes that would change the file on save? A bit like the save actions of JDT but in a more generic way for any file?
Comment 9 Angelo ZERR CLA 2016-12-07 04:36:32 EST
> Would it be possible/sufficient to have editorconfig support be a listener on resource changes that would change the file on save?

IMHO I think it's not very user frendly. VSCode, Jetbrains does that, so why Eclipse could not do that?

I think GenericEditor could be a good canditate to provide something which is able to  customize the editor without Eclipse preferences. See https://github.com/editorconfig/editorconfig-jetbrains/blob/master/src/org/editorconfig/configmanagement/CodeStyleManager.java for Jetbrains.
Comment 10 Mickael Istria CLA 2017-03-08 07:14:43 EST
Another approach which IMHO would work better: using the documentSetupParticipants extension, when a Document is open for which we can resolve an .editorconfig file, attach a change listener that applies formatting.
That has the huge advantage of being independent on the editor.
Comment 11 Christoph Obexer CLA 2017-03-09 02:26:23 EST
Visual Studio just shipped this included by default.
https://www.visualstudio.com/en-us/news/releasenotes/vs2017-relnotes#coding-convention-support-via-editorconfig

Which Eclipse version will this be included in?
Comment 12 Mickael Istria CLA 2017-03-09 03:40:28 EST
(In reply to Christoph Obexer from comment #11)
> Which Eclipse version will this be included in?

There is no commitment for a specific version, but this is a request that is getting higher and higher in priority ranking for some contributors. So thing should happen in the next months.
Note that although being reported to Platform/Text, it's totally doable to implement it in a separate project/bundle as a starting point; and it might even be preferable as there is no need nor gain to have the low-level Platform/Text put an adherence on the .editorconfig files.
By the way, did you try https://github.com/ncjones/editorconfig-eclipse ? Did it work for you? What do you think are the most critical issues in it to make it satisfying for your use-cases?
If you're interested in contributing code, and maybe even in setting up a project, we'd be glad to assist you from now on.
Comment 13 Angelo ZERR CLA 2017-03-09 03:53:49 EST
> By the way, did you try https://github.com/ncjones/editorconfig-eclipse ? Did it work for you? What do you think are the most critical issues in it to make it satisfying for your use-cases?

@Mickael, IMHO I think this plugin is not really usable since it updates the global preferences each time you open an editor. Eclipse editor should provide the capability to customize preferences for a given instance of an editor.

> using the documentSetupParticipants extension, when a Document is open for which we can resolve an .editorconfig file, attach a change listener that applies formatting.

This solution will perhaps working for formatting, but what about encoding and other properties? If I understand well, it means that editor should be opened. If I have a Format action which can be executed for files from directory (where editor are not opened), I'm not sure it will work.

I know it's not an easy task, but if Eclipse wishes support correctly .editorconfig, it should support the capability to customize preferences for a given editor instance or even a given IFile (when editor is not opened).

Today it exists the capability to set encoding for a given IFile (it stores in a preferences settings file the file path with the setted encoding), it should be cool if we could customize encoding with an other mean than eclipse preferences like a .editorconfig file.
Comment 14 Angelo ZERR CLA 2017-08-21 12:03:15 EDT
@Mickael, I have re-studied the editorconfig topic, and I think I have found a better clean solution.

It was a bad idea to customize the editor with an external action like I have suggested:

---------------------------------
IEditorPart editor = ...
IEclipsePreferences p = (IEclipsePreferences) editor.getAdapter(IEclipsePreferences.class);
if (p != null) {
  p.put("spacesForTabs", "true")
}
---------------------------------

Indead, the preferences value is re-read from the preference store and it doesn't work.

So my new idea is to consider the .editorconfig.file as an IPreferenceStore: it requires that editor create a ChainedPreferenceStore with EditorConfigPreferenceStore + the editor preference store.

Here my little POC, create a text editor like this:

-------------------------------------------------------
public class MyEditor extends TextEditor {

	public TextMateEditor() {
		setSourceViewerConfiguration(new TextSourceViewerConfiguration(new ChainedPreferenceStore(
				new IPreferenceStore[] { new EditorConfigPreferenceStore(this), super.getPreferenceStore() })));
	}
-------------------------------------------------------

Create EditorConfigPreferenceStore  like this:

-------------------------------------------------------
public class EditorConfigPreferenceStore extends PreferenceStore {

	private final ITextEditor textEditor;

	public EditorConfigPreferenceStore(ITextEditor textEditor) {
		this.textEditor = textEditor;
		IEditorInput input = textEditor.getEditorInput();
		IResource resource = input.getAdapter(IResource.class);
		// TODO: Search .editorconfig. file by using the editor file location
	}

	@Override
	public int getInt(String name) {
		if (AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH.equals(name)) {
			// indent_size or tab_width??
			// This value should comes from the .editorconfig. file
			return 10;
		}
		return super.getInt(name);
	}

	@Override
	public boolean getBoolean(String name) {
		if (AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS.equals(name)) {
			// indent_style: space
			return true;
		}
		return super.getBoolean(name);
	}

	@Override
	public boolean contains(String name) {
		if (AbstractDecoratedTextEditorPreferenceConstants.EDITOR_TAB_WIDTH.equals(name)) {
			return true;
		}
		if (AbstractDecoratedTextEditorPreferenceConstants.EDITOR_SPACES_FOR_TABS.equals(name)) {
			return true;
		}
		return super.contains(name);
	}
}
-------------------------------------------------------

It works for tab spaces and tab width. Encoding doesn't work because it doesn't use the preference store of the editor.

So I tell me if GenericEditor could provide a new extension point "preferenceStoreProvider" and I could use generic editor to support .editorconfig with clean mean.

@mickael what do you think about that?