Skip to content

Commit

Permalink
[1] Support soft tabs in the route editor
Browse files Browse the repository at this point in the history
  • Loading branch information
erwan committed Apr 16, 2010
1 parent 6a851c4 commit acfa123
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 19 deletions.
29 changes: 28 additions & 1 deletion source/src/fr/zenexity/pdt/editors/Editor.java
Expand Up @@ -23,15 +23,19 @@
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TypedRegion;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewer;
import org.eclipse.jface.text.templates.Template;
import org.eclipse.swt.events.VerifyEvent;
import org.eclipse.swt.events.VerifyListener;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.editors.text.TextEditor;
import org.eclipse.ui.texteditor.MarkerUtilities;


public abstract class Editor extends TextEditor {
public abstract class Editor extends TextEditor implements VerifyListener {

ColorManager colorManager = new ColorManager();
DocumentProvider documentProvider;
Expand Down Expand Up @@ -270,6 +274,29 @@ private void clearMarkers() {
}
}

// Soft tabs

protected boolean useSoftTabs = false;
protected int softTabsWidth = 4;

@Override
protected ISourceViewer createSourceViewer(Composite parent,IVerticalRuler ruler, int styles) {
ISourceViewer viewer = super.createSourceViewer(parent, ruler, styles);
viewer.getTextWidget().addVerifyListener(this);
return viewer;
}

@Override
public void verifyText(VerifyEvent evt) {
String softTab = "";
if (useSoftTabs) {
for (int i = 0; i < softTabsWidth; i++) softTab = softTab + " ";
if (evt.text.equals("\t")) {
evt.text = softTab;
}
}
}

// Scanner

protected String content;
Expand Down
Expand Up @@ -20,7 +20,7 @@
import org.playframework.playclipse.editors.PlayEditor;

public class HTMLEditor extends PlayEditor {

public static final String DEFAULT_COLOR = "html_default_color";
public static final String DOCTYPE_COLOR = "html_doctype_color";
public static final String HTML_COLOR = "html_html_color";
Expand Down Expand Up @@ -112,9 +112,9 @@ public String autoClose(char pc, char c, char nc) {
}
return null;
};

// Template

public void templates(String contentType, String ctx) {
if(contentType == "default" || contentType == "html" || contentType == "string") {
template("$", "Insert dynamic expression", "$${${}}${cursor}");
Expand All @@ -135,15 +135,15 @@ public void templates(String contentType, String ctx) {
template(ctx, "Close the " + ctx + " HTML tag", "${cursor}"+closeTag);
}
}

// Hyperlink

Pattern extend_s = Pattern.compile("#\\{extends\\s+'([^']+)'");
Pattern include = Pattern.compile("#\\{include\\s+'([^']+)'");
Pattern action = Pattern.compile("@\\{([^}]+)\\}");
Pattern action_in_tag = Pattern.compile("#\\{.+(@.+[)])");
Pattern tag = Pattern.compile("#\\{([-a-zA-Z0-9.]+) ");

public IHyperlink detectHyperlink(ITextViewer textViewer, IRegion region) {
BestMatch match = findBestMatch(region.getOffset(), include, extend_s, action, action_in_tag, tag);
if(match != null) {
Expand All @@ -165,21 +165,21 @@ public IHyperlink detectHyperlink(ITextViewer textViewer, IRegion region) {
}
return null;
}

// Scanner

boolean consumeString = false;
char openedString = ' ';
String oldState = "default";
String oldStringState = "default";

@Override
protected void reset() {
super.reset();
consumeString = false;
oldState = "default";
}

@Override
public String scan() {
if(isNext("*{") && state != "skipped") {
Expand Down Expand Up @@ -286,8 +286,7 @@ public String scan() {
private Annotation[] oldAnnotations;
private ProjectionAnnotationModel annotationModel;

public void updateFoldingStructure(ArrayList<Position> positions)
{
public void updateFoldingStructure(ArrayList<Position> positions) {
Annotation[] annotations = new Annotation[positions.size()];

//this will hold the new annotations along
Expand All @@ -299,9 +298,9 @@ public void updateFoldingStructure(ArrayList<Position> positions)
newAnnotations.put(annotation, positions.get(i));
annotations[i] = annotation;
}

annotationModel.modifyAnnotations(oldAnnotations, newAnnotations, null);

oldAnnotations = annotations;
}

Expand All @@ -313,12 +312,12 @@ public void createPartControl(Composite parent)

projectionSupport = new ProjectionSupport(viewer, getAnnotationAccess(), getSharedColors());
projectionSupport.install();

//turn projection mode on
viewer.doOperation(ProjectionViewer.TOGGLE);

annotationModel = viewer.getProjectionAnnotationModel();

}

@Override
Expand All @@ -328,7 +327,7 @@ protected ISourceViewer createSourceViewer(Composite parent, IVerticalRuler rule

// ensure decoration support has been created and configured.
getSourceViewerDecorationSupport(viewer);

return viewer;
}

Expand Down
Expand Up @@ -5,10 +5,12 @@
import org.eclipse.core.resources.IMarker;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jface.preference.IPreferenceStore;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.hyperlink.IHyperlink;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.playframework.playclipse.ModelInspector;
import org.playframework.playclipse.PlayPlugin;
import org.playframework.playclipse.editors.PlayEditor;
Expand All @@ -25,6 +27,8 @@ public class RouteEditor extends PlayEditor {
* Can be: "error", "warning" or "ignore"
*/
public static final String MISSING_ROUTE = "route_missing_route";
public static final String SOFT_TABS = "route_soft_tabs";
public static final String SOFT_TABS_WIDTH = "route_soft_tabs_width";

String oldState = "default";
IJavaProject javaProject;
Expand All @@ -33,6 +37,9 @@ public class RouteEditor extends PlayEditor {
public RouteEditor() {
super();
setSourceViewerConfiguration(new RouteConfiguration(this));
IPreferenceStore store = PlayPlugin.getDefault().getPreferenceStore();
useSoftTabs = store.getBoolean(SOFT_TABS);
softTabsWidth = store.getInt(SOFT_TABS_WIDTH);
}

private IJavaProject getJavaProject() {
Expand Down Expand Up @@ -91,6 +98,18 @@ public String getStylePref(String type) {
return DEFAULT_COLOR;
}

@Override
protected void handlePreferenceStoreChanged(PropertyChangeEvent event) {
String key = event.getProperty();
System.out.println("handlePrefChanged: " + key);
if (key.equals(SOFT_TABS)) {
System.out.println("soft tabs!!");
useSoftTabs = ((Boolean)event.getNewValue()).booleanValue();
}

super.handlePreferenceStoreChanged(event);
}

private void addMarker(int start, int end, String text) throws BadLocationException {
String severityStr = PlayPlugin.getDefault().getPreferenceStore().getString(MISSING_ROUTE);
int severity = 0;
Expand Down
Expand Up @@ -30,6 +30,8 @@ public void initializeDefaultPreferences() {
PreferenceConverter.setDefault(store, RouteEditor.COMMENT_COLOR, new RGB(90, 90, 90));
PreferenceConverter.setDefault(store, RouteEditor.DEFAULT_COLOR, new RGB(0, 0, 0));
store.setDefault(RouteEditor.MISSING_ROUTE, "warning");
store.setDefault(RouteEditor.SOFT_TABS, false);
store.setDefault(RouteEditor.SOFT_TABS_WIDTH, 4);

PreferenceConverter.setDefault(store, ConfEditor.COMMENT_COLOR, new RGB(90, 90, 90));
PreferenceConverter.setDefault(store, ConfEditor.KEY_COLOR, new RGB(150, 0, 0));
Expand Down
Expand Up @@ -3,7 +3,9 @@
import java.util.LinkedHashMap;
import java.util.Map;

import org.eclipse.jface.preference.BooleanFieldEditor;
import org.eclipse.jface.preference.ComboFieldEditor;
import org.eclipse.jface.preference.IntegerFieldEditor;
import org.playframework.playclipse.editors.route.RouteEditor;

public class RoutesEditorPreferencePage extends PlayEditorPreferencePage {
Expand Down Expand Up @@ -33,6 +35,8 @@ public void createFieldEditors() {
{"Error", "error"}
};
addField(new ComboFieldEditor(RouteEditor.MISSING_ROUTE, "When a route is missing", missingRouteKeyValues, getFieldEditorParent()));
addField(new BooleanFieldEditor(RouteEditor.SOFT_TABS, "Use soft tabs", getFieldEditorParent()));
addField(new IntegerFieldEditor(RouteEditor.SOFT_TABS_WIDTH, "Soft tabs length", getFieldEditorParent()));
}

}

0 comments on commit acfa123

Please sign in to comment.