-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #186: Make auto-edit configurable.
- Loading branch information
Showing
17 changed files
with
356 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...asyxtext.ui/src/net/vtst/eclipse/easyxtext/ui/editor/autoedit/AutoEditPreferencePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package net.vtst.eclipse.easyxtext.ui.editor.autoedit; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import net.vtst.eclipse.easyxtext.util.IEasyMessages; | ||
|
||
import org.eclipse.debug.internal.ui.SWTFactory; | ||
import org.eclipse.jface.preference.PreferencePage; | ||
import org.eclipse.swt.SWT; | ||
import org.eclipse.swt.layout.GridData; | ||
import org.eclipse.swt.widgets.Composite; | ||
import org.eclipse.swt.widgets.Control; | ||
import org.eclipse.swt.widgets.Table; | ||
import org.eclipse.swt.widgets.TableColumn; | ||
import org.eclipse.swt.widgets.TableItem; | ||
import org.eclipse.ui.IWorkbench; | ||
import org.eclipse.ui.IWorkbenchPreferencePage; | ||
import org.eclipse.xtext.ui.editor.autoedit.AbstractEditStrategyProvider; | ||
|
||
import com.google.inject.Inject; | ||
|
||
@SuppressWarnings("restriction") | ||
public class AutoEditPreferencePage extends PreferencePage implements IWorkbenchPreferencePage { | ||
|
||
@Inject | ||
AbstractEditStrategyProvider abstractEditStrategyProvider; | ||
|
||
EasyEditStrategyProvider editStrategyProvider; | ||
|
||
// ************************************************************************** | ||
// User interface | ||
|
||
private Table list; | ||
|
||
public Control createContents(Composite parent) { | ||
assert abstractEditStrategyProvider instanceof EasyEditStrategyProvider; | ||
editStrategyProvider = (EasyEditStrategyProvider) abstractEditStrategyProvider; | ||
Composite composite = SWTFactory.createComposite(parent, 1, 1, GridData.FILL_BOTH); | ||
list = new Table(composite, SWT.V_SCROLL | SWT.CHECK | SWT.BORDER); | ||
GridData gd = new GridData(GridData.FILL_BOTH); | ||
gd.horizontalSpan = 1; | ||
list.setLayoutData(gd); | ||
TableColumn column1 = new TableColumn(list, SWT.NONE); | ||
fillList(); | ||
column1.pack(); | ||
SWTFactory.createLabel(parent, messages.getString("AutoEditPreferencePage_message"), 1); | ||
return composite; | ||
} | ||
|
||
private void fillList() { | ||
for (Method method: editStrategyProvider.getConfigureMethods()) { | ||
TableItem item = new TableItem(list, SWT.NONE); | ||
item.setText(new String[] {getMethodLabel(method)}); | ||
item.setData(method); | ||
item.setChecked(editStrategyProvider.getMethodPreferenceValue(method)); | ||
} | ||
} | ||
|
||
@Inject(optional=true) | ||
private IEasyMessages messages; | ||
|
||
private String getMethodLabel(Method method) { | ||
String key = editStrategyProvider.getMessageKey(method); | ||
if (messages == null) return key; | ||
else return messages.getString(key); | ||
} | ||
|
||
@Override | ||
protected void performDefaults() { | ||
for (TableItem item: list.getItems()) { | ||
editStrategyProvider.resetPreferenceValue((Method) item.getData()); | ||
item.setChecked(editStrategyProvider.getMethodPreferenceValue((Method) item.getData())); | ||
} | ||
super.performDefaults(); | ||
} | ||
|
||
@Override | ||
public boolean performOk() { | ||
for (TableItem item: list.getItems()) { | ||
editStrategyProvider.setMethodPreferenceValue((Method) item.getData(), item.getChecked()); | ||
} | ||
return super.performOk(); | ||
} | ||
|
||
@SuppressWarnings("deprecation") | ||
public void init(IWorkbench workbench) { | ||
this.setPreferenceStore(workbench.getPreferenceStore()); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...pse.easyxtext.ui/src/net/vtst/eclipse/easyxtext/ui/editor/autoedit/ConfigureAutoEdit.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package net.vtst.eclipse.easyxtext.ui.editor.autoedit; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
@Target({ElementType.METHOD}) | ||
public @interface ConfigureAutoEdit { | ||
/** | ||
* @return true if the check is configurable. | ||
*/ | ||
boolean configurable() default true; | ||
|
||
/** | ||
* @return The default state of the check. | ||
*/ | ||
boolean defaultState() default true; | ||
} |
82 changes: 82 additions & 0 deletions
82
...rc/net/vtst/eclipse/easyxtext/ui/editor/autoedit/DefaultEasyAutoEditStrategyProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package net.vtst.eclipse.easyxtext.ui.editor.autoedit; | ||
|
||
import org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy; | ||
import org.eclipse.jface.text.IDocument; | ||
import org.eclipse.xtext.ui.editor.autoedit.CompoundMultiLineTerminalsEditStrategy; | ||
import org.eclipse.xtext.ui.editor.autoedit.MultiLineTerminalsEditStrategy; | ||
import org.eclipse.xtext.ui.editor.autoedit.PartitionDeletionEditStrategy; | ||
import org.eclipse.xtext.ui.editor.autoedit.PartitionEndSkippingEditStrategy; | ||
import org.eclipse.xtext.ui.editor.autoedit.PartitionInsertEditStrategy; | ||
import org.eclipse.xtext.ui.editor.autoedit.SingleLineTerminalsStrategy; | ||
import org.eclipse.xtext.ui.editor.autoedit.AbstractEditStrategyProvider.IEditStrategyAcceptor; | ||
import org.eclipse.xtext.ui.editor.model.TerminalsTokenTypeToPartitionMapper; | ||
|
||
import com.google.inject.Inject; | ||
import com.google.inject.Provider; | ||
|
||
public class DefaultEasyAutoEditStrategyProvider extends EasyEditStrategyProvider { | ||
@Inject | ||
protected Provider<DefaultIndentLineAutoEditStrategy> defaultIndentLineAutoEditStrategy; | ||
@Inject | ||
protected Provider<PartitionEndSkippingEditStrategy> partitionEndSkippingEditStrategy; | ||
@Inject | ||
protected PartitionInsertEditStrategy.Factory partitionInsert; | ||
@Inject | ||
protected PartitionDeletionEditStrategy.Factory partitionDeletion; | ||
@Inject | ||
protected SingleLineTerminalsStrategy.Factory singleLineTerminals; | ||
@Inject | ||
protected MultiLineTerminalsEditStrategy.Factory multiLineTerminals; | ||
|
||
@Inject | ||
protected CompoundMultiLineTerminalsEditStrategy.Factory compoundMultiLineTerminals; | ||
|
||
@ConfigureAutoEdit | ||
protected void compoundBracesBlocks(IEditStrategyAcceptor acceptor) { | ||
acceptor.accept(compoundMultiLineTerminals.newInstanceFor("{", "}").and("[", "]").and("(", ")"), IDocument.DEFAULT_CONTENT_TYPE); | ||
} | ||
|
||
@ConfigureAutoEdit | ||
protected void indentationEditStrategy(IEditStrategyAcceptor acceptor) { | ||
acceptor.accept(defaultIndentLineAutoEditStrategy.get(), IDocument.DEFAULT_CONTENT_TYPE); | ||
acceptor.accept(defaultIndentLineAutoEditStrategy.get(), TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION); | ||
acceptor.accept(defaultIndentLineAutoEditStrategy.get(), TerminalsTokenTypeToPartitionMapper.SL_COMMENT_PARTITION); | ||
} | ||
|
||
@ConfigureAutoEdit | ||
protected void multilineComments(IEditStrategyAcceptor acceptor) { | ||
acceptor.accept(singleLineTerminals.newInstance("/*", " */"),IDocument.DEFAULT_CONTENT_TYPE); | ||
acceptor.accept(multiLineTerminals.newInstance("/*"," * ", " */"),IDocument.DEFAULT_CONTENT_TYPE); | ||
acceptor.accept(multiLineTerminals.newInstance("/*"," * ", " */"),TerminalsTokenTypeToPartitionMapper.COMMENT_PARTITION); | ||
} | ||
|
||
@ConfigureAutoEdit | ||
protected void curlyBracesBlock(IEditStrategyAcceptor acceptor) { | ||
acceptor.accept(singleLineTerminals.newInstance("{", "}"),IDocument.DEFAULT_CONTENT_TYPE); | ||
} | ||
|
||
@ConfigureAutoEdit | ||
protected void squareBrackets(IEditStrategyAcceptor acceptor) { | ||
acceptor.accept(singleLineTerminals.newInstance("[", "]"),IDocument.DEFAULT_CONTENT_TYPE); | ||
} | ||
|
||
@ConfigureAutoEdit | ||
protected void parenthesis(IEditStrategyAcceptor acceptor) { | ||
acceptor.accept(singleLineTerminals.newInstance("(", ")"),IDocument.DEFAULT_CONTENT_TYPE); | ||
} | ||
|
||
@ConfigureAutoEdit | ||
protected void stringLiteral(IEditStrategyAcceptor acceptor) { | ||
acceptor.accept(partitionInsert.newInstance("\"","\""),IDocument.DEFAULT_CONTENT_TYPE); | ||
acceptor.accept(partitionInsert.newInstance("'","'"),IDocument.DEFAULT_CONTENT_TYPE); | ||
// The following two are registered for the default content type, because on deletion | ||
// the command.offset is cursor-1, which is outside the partition of terminals.length = 1. | ||
// How crude is that? | ||
// Note that in case you have two string literals following each other directly, the deletion strategy wouldn't apply. | ||
// One could add the same strategy for the STRING partition in addition to solve this | ||
acceptor.accept(partitionDeletion.newInstance("\"","\""),IDocument.DEFAULT_CONTENT_TYPE); | ||
acceptor.accept(partitionDeletion.newInstance("'","'"),IDocument.DEFAULT_CONTENT_TYPE); | ||
acceptor.accept(partitionEndSkippingEditStrategy.get(),TerminalsTokenTypeToPartitionMapper.STRING_LITERAL_PARTITION); | ||
} | ||
|
||
} |
84 changes: 84 additions & 0 deletions
84
...yxtext.ui/src/net/vtst/eclipse/easyxtext/ui/editor/autoedit/EasyEditStrategyProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package net.vtst.eclipse.easyxtext.ui.editor.autoedit; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.ArrayList; | ||
|
||
import org.eclipse.emf.ecore.EPackage; | ||
import org.eclipse.jface.preference.IPreferenceStore; | ||
import org.eclipse.ui.PlatformUI; | ||
import org.eclipse.xtext.ui.editor.autoedit.AbstractEditStrategyProvider; | ||
|
||
import com.google.inject.Inject; | ||
|
||
public class EasyEditStrategyProvider extends AbstractEditStrategyProvider { | ||
|
||
@Inject | ||
private EPackage ePackage; | ||
|
||
public String getPreferenceId(Method method) { | ||
return "EasyEditStrategyProvider:" + ePackage.getName() + ":" + method.getName(); | ||
} | ||
|
||
public String getMessageKey(Method method) { | ||
return method.getDeclaringClass().getSimpleName() + "_" + method.getName(); | ||
} | ||
|
||
IPreferenceStore store; | ||
|
||
@SuppressWarnings("deprecation") | ||
private IPreferenceStore getStore() { | ||
if (store == null) store = PlatformUI.getWorkbench().getPreferenceStore(); | ||
return store; | ||
} | ||
|
||
public boolean getMethodPreferenceValue(Method method) { | ||
String id = getPreferenceId(method); | ||
ConfigureAutoEdit annotation = method.getAnnotation(ConfigureAutoEdit.class); | ||
assert annotation != null; | ||
getStore().setDefault(id, annotation.defaultState()); | ||
return getStore().getBoolean(id); | ||
} | ||
|
||
public void setMethodPreferenceValue(Method method, boolean value) { | ||
getStore().setValue(getPreferenceId(method), value); | ||
} | ||
|
||
public void resetPreferenceValue(Method method) { | ||
getStore().setToDefault(getPreferenceId(method)); | ||
} | ||
|
||
public ArrayList<Method> getConfigureMethods() { | ||
ArrayList<Method> methods = new ArrayList<Method>(); | ||
Class<?> cls = this.getClass(); | ||
while (cls != null) { | ||
for (Method method: cls.getDeclaredMethods()) { | ||
ConfigureAutoEdit annotation = method.getAnnotation(ConfigureAutoEdit.class); | ||
if (annotation != null && annotation.configurable()) { | ||
methods.add(method); | ||
} | ||
} | ||
cls = cls.getSuperclass(); | ||
} | ||
return methods; | ||
} | ||
|
||
@Override | ||
protected void configure(IEditStrategyAcceptor acceptor) { | ||
for (Method method: getConfigureMethods()) { | ||
if (getMethodPreferenceValue(method)) { | ||
method.setAccessible(true); | ||
try { | ||
method.invoke(this, acceptor); | ||
} catch (IllegalAccessException e) { | ||
e.printStackTrace(); | ||
} catch (IllegalArgumentException e) { | ||
e.printStackTrace(); | ||
} catch (InvocationTargetException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
...t.vtst.eclipse.easyxtext/src/net/vtst/eclipse/easyxtext/validation/config/CheckState.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.