Skip to content

Commit

Permalink
Fix #186: Make auto-edit configurable.
Browse files Browse the repository at this point in the history
  • Loading branch information
vtst committed Apr 27, 2014
1 parent bbc2d9f commit e1d0854
Show file tree
Hide file tree
Showing 17 changed files with 356 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ Require-Bundle: org.eclipse.ui,
net.vtst.eclipse.easyxtext;bundle-version="1.0.12"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: net.vtst.eclipse.easyxtext.ui.folding,
Export-Package: net.vtst.eclipse.easyxtext.ui,
net.vtst.eclipse.easyxtext.ui.editor.autoedit,
net.vtst.eclipse.easyxtext.ui.folding,
net.vtst.eclipse.easyxtext.ui.launching,
net.vtst.eclipse.easyxtext.ui.launching.attributes,
net.vtst.eclipse.easyxtext.ui.launching.tab,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@ remove_project_nature_dialog_message = Also remove the XText nature?
# NatureAddingEditorCallback
add_nature_to_project_title = Add %s
add_nature_to_project_message = Do you want to add the %s to the project '%s'?
add_nature_to_project_error = Oups! An error occurred.
add_nature_to_project_error = Oups! An error occurred.

# DefaultEasyAutoEditStrategyProvider
DefaultEasyAutoEditStrategyProvider_compoundBracesBlocks = Compound Braces Blocks
DefaultEasyAutoEditStrategyProvider_indentationEditStrategy = Indentation
DefaultEasyAutoEditStrategyProvider_multilineComments = Multi-line comments
DefaultEasyAutoEditStrategyProvider_curlyBracesBlock = Curly braces {...}
DefaultEasyAutoEditStrategyProvider_squareBrackets = Square brackets [...]
DefaultEasyAutoEditStrategyProvider_parenthesis = Parenthesis (...)
DefaultEasyAutoEditStrategyProvider_stringLiteral = String literals '...' and "..."

# AutoEditPreferencePage
AutoEditPreferencePage_message = You need to re-open editors for the changes to take effect.
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());
}

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

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

}
3 changes: 2 additions & 1 deletion src/eclipse/net.vtst.eclipse.easyxtext/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ Require-Bundle: org.eclipse.ui,
org.apache.log4j;bundle-version="1.2.15"
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package: net.vtst.eclipse.easyxtext.guice,
Export-Package: net.vtst.eclipse.easyxtext,
net.vtst.eclipse.easyxtext.guice,
net.vtst.eclipse.easyxtext.nature,
net.vtst.eclipse.easyxtext.parser,
net.vtst.eclipse.easyxtext.resource,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
public abstract class EasyResourceBundle implements IEasyMessages {

private ResourceBundle bundle;
private EasyResourceBundle delegate = null;

public EasyResourceBundle() {
bundle = getBundle();
delegate = getDelegate();
}

public abstract ResourceBundle getBundle();
Expand All @@ -49,10 +47,10 @@ public String getString(String key) {
try {
return bundle.getString(key);
} catch (MissingResourceException e) {
if (delegate == null)
if (getDelegate() == null)
return "!" + key + "!";
else
return delegate.getString(key);
return getDelegate().getString(key);
}
}

Expand All @@ -69,10 +67,10 @@ public String format(String key, String... strings) {
try {
return String.format(bundle.getString(key), (Object[]) strings);
} catch (MissingResourceException e) {
if (delegate == null)
if (getDelegate() == null)
return "!" + key + "!";
else
return delegate.getString(key);
return getDelegate().getString(key);
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ less_file_pattern = *.less
less_validation_property_page = LESS
less_project_property_page = LESS
less_validation_preference_page = Validation
less_autoedit_preference_page = Auto-Edit
less_nature_add = Add LESS nature
less_nature_remove = Remove LESS nature
8 changes: 8 additions & 0 deletions src/eclipse/net.vtst.ow.eclipse.less.ui/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@
</page>
</extension>

<extension point="org.eclipse.ui.preferencePages">
<page
name="%less_autoedit_preference_page"
class="net.vtst.ow.eclipse.less.ui.LessExecutableExtensionFactory:net.vtst.eclipse.easyxtext.ui.editor.autoedit.AutoEditPreferencePage"
id="net.vtst.ow.eclipse.less.ui.properties.LessAutoEditPreferencePage"
category="net.vtst.ow.eclipse.less.Less">
</page>
</extension>

<!-- This is the part generated by XText. -->

Expand Down
Loading

0 comments on commit e1d0854

Please sign in to comment.