Bug 463071 - JSDT should be able to customize code style with .editorconfig
Summary: JSDT should be able to customize code style with .editorconfig
Status: NEW
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: unspecified   Edit
Hardware: All All
: P3 enhancement with 1 vote (vote)
Target Milestone: ---   Edit
Assignee: Project Inbox CLA
QA Contact: Victor Rubezhny CLA
URL:
Whiteboard:
Keywords:
Depends on: 457046
Blocks:
  Show dependency tree
 
Reported: 2015-03-25 06:27 EDT by Angelo ZERR CLA
Modified: 2018-11-22 17:32 EST (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Angelo ZERR CLA 2015-03-25 06:27:11 EDT
Most of JavaScript project uses today an .editorconfig file http://editorconfig.org/ and most of IDE like Visual Studio, JetBrains is able to use this .editorconfig file for their code styles.

IMHO, I think JSDT should be able to use .editorconfig for code style. But perhaps it should be very good that it works too for Java and other Language editor, in other words Eclipse IDE should : 

 * provide an .editorconfig editor.
 * give a common preferences based on .editorconfig. After that each language could use this configuration for their code styles.
Comment 1 Angelo ZERR CLA 2015-03-25 12:34:13 EDT
I think this bug can be closed since there exists this bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=389139
Comment 2 Victor Rubezhny CLA 2015-03-25 12:52:27 EDT
No, it can't.

That bug #389139 is already closed (as won't fix). So, if you have a contribution ready to be incubated by eclipse then you could re-open that bug and propose your contribution if you would.

If you haven't, then this bug could be a source of new 'helpwanted' issue. So that such a contribution is still welcomed.

IMHO, such a configuration could be useful for any Eclipse's editor, but we can start at least with implementing it for JavaScript editor and, if suceeded, to propose it for the whole community.
Comment 3 Angelo ZERR CLA 2015-03-25 12:56:00 EDT
> No, it can't.

Ok Victor I understand.

I have no time today to develop this kind plugin since I have the intention to contribute to WTP with WTP JSON Editor https://github.com/angelozerr/eclipse-wtp-json (it's out of the topic, but completion based on JSON Schema starts working, now I'm working on bower dependencies completion).
Comment 4 Mickael Istria CLA 2015-07-17 03:13:06 EDT
See https://github.com/ncjones/editorconfig-eclipse
Comment 5 Angelo ZERR CLA 2015-08-06 12:12:10 EDT
The .editorconfig specification http://editorconfig.org/#file-location says : 

"When opening a file, EditorConfig plugins look for a file named .editorconfig in the directory of the opened file and in every parent directory. A search for .editorconfig files will stop if the root filepath is reached or an EditorConfig file with root=true is found."

It means that preferences of the editor must be setted according the file location. Today JSDT Editor doesn't provides this feature. It should be cool if JSDT could provide an extension point to customize preferences formatter for a given editor.

This extension point could be called here http://git.eclipse.org/c/jsdt/webtools.jsdt.core.git/tree/bundles/org.eclipse.wst.jsdt.ui/src/org/eclipse/wst/jsdt/internal/ui/javaeditor/CompilationUnitEditor.java#n215 to retrieve the options from the extension point.
Comment 6 Nathan Jones CLA 2015-11-17 07:14:02 EST
The way the editorconfig-eclipse plugin is implemented requires me adding explicit support for every type of editor, for example:

```
setPreference("org.eclipse.jdt.core", "org.eclipse.jdt.core.formatter.tabulation.char", "space");
setPreference("org.eclipse.wst.xml.core", "indentationChar", "space");
```

Currently only Text, Java and Xml editors are supported. I want to expand this support but it's clearly not a scalable solution and it's also quite a clunky hack since it modifies global settings whenever a file is opened.

Ideally each editor should natively support editorconfig by applying editorconfig settings in preference over project and workspace settings. This would require making the editorconfig plugin a dependency of all editors. This requires some pretty significant changes in the IDE. What are your thoughts?
Comment 7 Mickael Istria CLA 2015-11-17 07:18:47 EST
A possibility would be to have be able to plug editor config processors via Eclipse extension. So for example, JSDT would add a process that would take as input the editor config and apply the settings specific to JavaScript preferences and/or editors.
Comment 8 Gorkem Ercan CLA 2015-11-18 00:08:51 EST
I think this needs to go into org.eclipse.ui.texteditor.AbstractTextEditor. I feel that imitating the behavior via changing preferences is error prone as well. 

Has anyone looked if AbstractTextEditor is a good place to build this into?
Comment 9 Angelo ZERR CLA 2016-05-25 01:57:14 EDT
> I think this needs to go into org.eclipse.ui.texteditor.AbstractTextEditor. I feel that imitating the behavior via changing preferences is error prone as well. 

I have found a clean solution to customize editor instance (and not the whole editor of the project) with IEclipsePreferences. The basic idea is that the editor must returns her instance of IEclipsePreferences with getAdapter

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

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

@Override
public Object getAdapter(@SuppressWarnings("rawtypes") Class key) {
      if (key.equals(IEclipsePreferences.class)) {
      // support for .editorconfig
      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")
}
---------------------------------


Please read my comment at https://github.com/ncjones/editorconfig-eclipse/issues/9#issuecomment-221233859

> Has anyone looked if AbstractTextEditor is a good place to build this into?

I think the code with getAdapter could be hosted inside AbstractTextEditor, it should really cool!

What do you think about this idea?
Comment 10 Mickael Istria CLA 2016-05-25 02:01:58 EDT
@Angelo: I have the impression that this gets really into the right direction, thanks for sharing those thought? So you'd just like to be able to access editor preferences from a given AbstractTextEditor? If so, can you please another bugzilla for that, again Platform/Text to work on this specific change.
I believe such a request can easily be honored, maybe even from Neon.1.
Comment 11 Angelo ZERR CLA 2016-05-25 02:10:50 EDT
> @Angelo: I have the impression that this gets really into the right direction, thanks for sharing those thought?

Glad this idea please you:) I have tested with my TypeScript Editor and it works like a charm.

> So you'd just like to be able to access editor preferences from a given AbstractTextEditor?

Exactly, instead of each editor implements the same code woth getAdapter, AbstractTextEditor could do that.

> If so, can you please another bugzilla for that, again Platform/Text to work on this specific change.

> I believe such a request can easily be honored, maybe even from Neon.1.

Cool!

For your information, I'm improving .editorconfig support for Eclipse (like with my idea of getAdapter, but too I have implemented a EditorConfig editor with syntax coloration
https://github.com/ncjones/editorconfig-eclipse/issues/7#issuecomment-221390476

I'm waiting for answer (is https://github.com/ncjones/editorconfig-eclipse is in live?), otherwise I will create my own project.
Comment 12 Angelo ZERR CLA 2016-05-25 02:27:42 EDT
> If so, can you please another bugzilla for that, again Platform/Text to work on this specific change.


done at https://bugs.eclipse.org/bugs/show_bug.cgi?id=457046#c5