Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor syntax color

  • Loading branch information...
commit 893e736779079f5b78ae2c0e716f0aab256df0a3 1 parent 8665067
@0211hk authored
View
17 plugin.xml
@@ -13,5 +13,22 @@
name="IoLanguageEditor">
</editor>
</extension>
+ <extension
+ point="org.eclipse.ui.preferencePages">
+ <page
+ class="ilangplugin.preferences.SamplePreferencePage"
+ id="ilangplugin.preferences.SamplePreferencePage"
+ name="Sample Preferences">
+ </page>
+ </extension>
+ <extension
+ point="org.eclipse.core.runtime.preferences">
+ <initializer
+ class="ilangplugin.preferences.PreferenceInitializer">
+ </initializer>
+ </extension>
+ <extension
+ point="org.eclipse.core.runtime.preferences">
+ </extension>
</plugin>
View
16 src/ilangplugin/preferences/PreferenceConstants.java
@@ -0,0 +1,16 @@
+package ilangplugin.preferences;
+
+/**
+ * Constant definitions for plug-in preferences
+ */
+public class PreferenceConstants {
+
+ public static final String P_PATH = "pathPreference";
+
+ public static final String P_BOOLEAN = "booleanPreference";
+
+ public static final String P_CHOICE = "choicePreference";
+
+ public static final String P_STRING = "stringPreference";
+
+}
View
26 src/ilangplugin/preferences/PreferenceInitializer.java
@@ -0,0 +1,26 @@
+package ilangplugin.preferences;
+
+import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer;
+import org.eclipse.jface.preference.IPreferenceStore;
+
+import ilangplugin.Activator;
+
+/**
+ * Class used to initialize default preference values.
+ */
+public class PreferenceInitializer extends AbstractPreferenceInitializer {
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer#initializeDefaultPreferences()
+ */
+ public void initializeDefaultPreferences() {
+ IPreferenceStore store = Activator.getDefault().getPreferenceStore();
+ store.setDefault(PreferenceConstants.P_BOOLEAN, true);
+ store.setDefault(PreferenceConstants.P_CHOICE, "choice2");
+ store.setDefault(PreferenceConstants.P_STRING,
+ "Default value");
+ }
+
+}
View
64 src/ilangplugin/preferences/SamplePreferencePage.java
@@ -0,0 +1,64 @@
+package ilangplugin.preferences;
+
+import org.eclipse.jface.preference.*;
+import org.eclipse.ui.IWorkbenchPreferencePage;
+import org.eclipse.ui.IWorkbench;
+import ilangplugin.Activator;
+
+/**
+ * This class represents a preference page that
+ * is contributed to the Preferences dialog. By
+ * subclassing <samp>FieldEditorPreferencePage</samp>, we
+ * can use the field support built into JFace that allows
+ * us to create a page that is small and knows how to
+ * save, restore and apply itself.
+ * <p>
+ * This page is used to modify preferences only. They
+ * are stored in the preference store that belongs to
+ * the main plug-in class. That way, preferences can
+ * be accessed directly via the preference store.
+ */
+
+public class SamplePreferencePage
+ extends FieldEditorPreferencePage
+ implements IWorkbenchPreferencePage {
+
+ public SamplePreferencePage() {
+ super(GRID);
+ setPreferenceStore(Activator.getDefault().getPreferenceStore());
+ setDescription("A demonstration of a preference page implementation");
+ }
+
+ /**
+ * Creates the field editors. Field editors are abstractions of
+ * the common GUI blocks needed to manipulate various types
+ * of preferences. Each field editor knows how to save and
+ * restore itself.
+ */
+ public void createFieldEditors() {
+ addField(new DirectoryFieldEditor(PreferenceConstants.P_PATH,
+ "&Directory preference:", getFieldEditorParent()));
+ addField(
+ new BooleanFieldEditor(
+ PreferenceConstants.P_BOOLEAN,
+ "&An example of a boolean preference",
+ getFieldEditorParent()));
+
+ addField(new RadioGroupFieldEditor(
+ PreferenceConstants.P_CHOICE,
+ "An example of a multiple-choice preference",
+ 1,
+ new String[][] { { "&Choice 1", "choice1" }, {
+ "C&hoice 2", "choice2" }
+ }, getFieldEditorParent()));
+ addField(
+ new StringFieldEditor(PreferenceConstants.P_STRING, "A &text preference:", getFieldEditorParent()));
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
+ */
+ public void init(IWorkbench workbench) {
+ }
+
+}
View
375 src/org/hk/io/language/plugin/editors/CombinedWordRule.java
@@ -0,0 +1,375 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2011 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.hk.io.language.plugin.editors;
+
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.eclipse.core.runtime.Assert;
+
+import org.eclipse.jface.text.rules.ICharacterScanner;
+import org.eclipse.jface.text.rules.IRule;
+import org.eclipse.jface.text.rules.IToken;
+import org.eclipse.jface.text.rules.IWordDetector;
+import org.eclipse.jface.text.rules.Token;
+
+
+/**
+ * An implementation of <code>IRule</code> capable of detecting words.
+ * <p>
+ * Word rules also allow for the association of tokens with specific words.
+ * That is, not only can the rule be used to provide tokens for exact matches,
+ * but also for the generalized notion of a word in the context in which it is used.
+ * A word rules uses a word detector to determine what a word is.</p>
+ * <p>
+ * This word rule allows a word detector to be shared among different word matchers.
+ * Its up to the word matchers to decide if a word matches and, in this a case, which
+ * token is associated with that word.
+ * </p>
+ *
+ * @see IWordDetector
+ * @since 3.0
+ */
+public class CombinedWordRule implements IRule {
+
+ /**
+ * Word matcher, that associates matched words with tokens.
+ */
+ public static class WordMatcher {
+
+ /** The table of predefined words and token for this matcher */
+ private Map<CharacterBuffer, IToken> fWords= new HashMap<CharacterBuffer, IToken>();
+
+ /**
+ * Adds a word and the token to be returned if it is detected.
+ *
+ * @param word the word this rule will search for, may not be <code>null</code>
+ * @param token the token to be returned if the word has been found, may not be <code>null</code>
+ */
+ public void addWord(String word, IToken token) {
+ Assert.isNotNull(word);
+ Assert.isNotNull(token);
+
+ fWords.put(new CharacterBuffer(word), token);
+ }
+
+ /**
+ * Returns the token associated to the given word and the scanner state.
+ *
+ * @param scanner the scanner
+ * @param word the word
+ * @return the token or <code>null</code> if none is associated by this matcher
+ */
+ public IToken evaluate(ICharacterScanner scanner, CharacterBuffer word) {
+ IToken token= fWords.get(word);
+ if (token != null)
+ return token;
+ return Token.UNDEFINED;
+ }
+
+ /**
+ * Removes all words.
+ */
+ public void clearWords() {
+ fWords.clear();
+ }
+ }
+
+ /**
+ * Character buffer, mutable <b>or</b> suitable for use as key in hash maps.
+ */
+ public static class CharacterBuffer {
+
+ /** Buffer content */
+ private char[] fContent;
+ /** Buffer content size */
+ private int fLength= 0;
+
+ /** Is hash code cached? */
+ private boolean fIsHashCached= false;
+ /** The hash code */
+ private int fHashCode;
+
+ /**
+ * Initialize with the given capacity.
+ *
+ * @param capacity the initial capacity
+ */
+ public CharacterBuffer(int capacity) {
+ fContent= new char[capacity];
+ }
+
+ /**
+ * Initialize with the given content.
+ *
+ * @param content the initial content
+ */
+ public CharacterBuffer(String content) {
+ fContent= content.toCharArray();
+ fLength= content.length();
+ }
+
+ /**
+ * Empties this buffer.
+ */
+ public void clear() {
+ fIsHashCached= false;
+ fLength= 0;
+ }
+
+ /**
+ * Appends the given character to the buffer.
+ *
+ * @param c the character
+ */
+ public void append(char c) {
+ fIsHashCached= false;
+ if (fLength == fContent.length) {
+ char[] old= fContent;
+ fContent= new char[old.length << 1];
+ System.arraycopy(old, 0, fContent, 0, old.length);
+ }
+ fContent[fLength++]= c;
+ }
+
+ /**
+ * Returns the length of the content.
+ *
+ * @return the length
+ */
+ public int length() {
+ return fLength;
+ }
+
+ /**
+ * Returns the content as string.
+ *
+ * @return the content
+ */
+ @Override
+ public String toString() {
+ return new String(fContent, 0, fLength);
+ }
+
+ /**
+ * Returns the character at the given position.
+ *
+ * @param i the position
+ * @return the character at position <code>i</code>
+ */
+ public char charAt(int i) {
+ return fContent[i];
+ }
+
+ /*
+ * @see java.lang.Object#hashCode()
+ */
+ @Override
+ public int hashCode() {
+ if (fIsHashCached)
+ return fHashCode;
+
+ int hash= 0;
+ for (int i= 0, n= fLength; i < n; i++)
+ hash= 29*hash + fContent[i];
+ fHashCode= hash;
+ fIsHashCached= true;
+ return hash;
+ }
+
+
+ /*
+ * @see java.lang.Object#equals(java.lang.Object)
+ */
+ @Override
+ public boolean equals(Object obj) {
+ if (obj == this)
+ return true;
+ if (!(obj instanceof CharacterBuffer))
+ return false;
+ CharacterBuffer buffer= (CharacterBuffer) obj;
+ int length= buffer.length();
+ if (length != fLength)
+ return false;
+ for (int i= 0; i < length; i++)
+ if (buffer.charAt(i) != fContent[i])
+ return false;
+ return true;
+ }
+
+ /**
+ * Is the content equal to the given string?
+ *
+ * @param string the string
+ * @return <code>true</code> iff the content is the same character sequence as in the string
+ */
+ public boolean equals(String string) {
+ int length= string.length();
+ if (length != fLength)
+ return false;
+ for (int i= 0; i < length; i++)
+ if (string.charAt(i) != fContent[i])
+ return false;
+ return true;
+ }
+ }
+
+ /** Internal setting for the uninitialized column constraint */
+ private static final int UNDEFINED= -1;
+
+ /** The word detector used by this rule */
+ private IWordDetector fDetector;
+ /** The default token to be returned on success and if nothing else has been specified. */
+ private IToken fDefaultToken;
+ /** The column constraint */
+ private int fColumn= UNDEFINED;
+ /** Buffer used for pattern detection */
+ private CharacterBuffer fBuffer= new CharacterBuffer(16);
+
+ /** List of word matchers */
+ private List<WordMatcher> fMatchers= new ArrayList<WordMatcher>();
+
+ /**
+ * Creates a rule which, with the help of an word detector, will return the token
+ * associated with the detected word. If no token has been associated, the scanner
+ * will be rolled back and an undefined token will be returned in order to allow
+ * any subsequent rules to analyze the characters.
+ *
+ * @param detector the word detector to be used by this rule, may not be <code>null</code>
+ *
+ * @see WordMatcher#addWord(String, IToken)
+ */
+ public CombinedWordRule(IWordDetector detector) {
+ this(detector, null, Token.UNDEFINED);
+ }
+
+ /**
+ * Creates a rule which, with the help of an word detector, will return the token
+ * associated with the detected word. If no token has been associated, the
+ * specified default token will be returned.
+ *
+ * @param detector the word detector to be used by this rule, may not be <code>null</code>
+ * @param defaultToken the default token to be returned on success
+ * if nothing else is specified, may not be <code>null</code>
+ *
+ * @see WordMatcher#addWord(String, IToken)
+ */
+ public CombinedWordRule(IWordDetector detector, IToken defaultToken) {
+ this(detector, null, defaultToken);
+ }
+
+ /**
+ * Creates a rule which, with the help of an word detector, will return the token
+ * associated with the detected word. If no token has been associated, the scanner
+ * will be rolled back and an undefined token will be returned in order to allow
+ * any subsequent rules to analyze the characters.
+ *
+ * @param detector the word detector to be used by this rule, may not be <code>null</code>
+ * @param matcher the initial word matcher
+ *
+ * @see WordMatcher#addWord(String, IToken)
+ */
+ public CombinedWordRule(IWordDetector detector, WordMatcher matcher) {
+ this(detector, matcher, Token.UNDEFINED);
+ }
+
+ /**
+ * Creates a rule which, with the help of an word detector, will return the token
+ * associated with the detected word. If no token has been associated, the
+ * specified default token will be returned.
+ *
+ * @param detector the word detector to be used by this rule, may not be <code>null</code>
+ * @param matcher the initial word matcher
+ * @param defaultToken the default token to be returned on success
+ * if nothing else is specified, may not be <code>null</code>
+ *
+ * @see WordMatcher#addWord(String, IToken)
+ */
+ public CombinedWordRule(IWordDetector detector, WordMatcher matcher, IToken defaultToken) {
+
+ Assert.isNotNull(detector);
+ Assert.isNotNull(defaultToken);
+
+ fDetector= detector;
+ fDefaultToken= defaultToken;
+ if (matcher != null)
+ addWordMatcher(matcher);
+ }
+
+
+ /**
+ * Adds the given matcher.
+ *
+ * @param matcher the matcher
+ */
+ public void addWordMatcher(WordMatcher matcher) {
+ fMatchers.add(matcher);
+ }
+
+ /**
+ * Sets a column constraint for this rule. If set, the rule's token
+ * will only be returned if the pattern is detected starting at the
+ * specified column. If the column is smaller then 0, the column
+ * constraint is considered removed.
+ *
+ * @param column the column in which the pattern starts
+ */
+ public void setColumnConstraint(int column) {
+ if (column < 0)
+ column= UNDEFINED;
+ fColumn= column;
+ }
+
+ /*
+ * @see IRule#evaluate(ICharacterScanner)
+ */
+ public IToken evaluate(ICharacterScanner scanner) {
+ int c= scanner.read();
+ if (fDetector.isWordStart((char) c)) {
+ if (fColumn == UNDEFINED || (fColumn == scanner.getColumn() - 1)) {
+
+ fBuffer.clear();
+ do {
+ fBuffer.append((char) c);
+ c= scanner.read();
+ } while (c != ICharacterScanner.EOF && fDetector.isWordPart((char) c));
+ scanner.unread();
+
+ for (int i= 0, n= fMatchers.size(); i < n; i++) {
+ IToken token= fMatchers.get(i).evaluate(scanner, fBuffer);
+ if (!token.isUndefined())
+ return token;
+ }
+
+ if (fDefaultToken.isUndefined())
+ unreadBuffer(scanner);
+
+ return fDefaultToken;
+ }
+ }
+
+ scanner.unread();
+ return Token.UNDEFINED;
+ }
+
+ /**
+ * Returns the characters in the buffer to the scanner.
+ *
+ * @param scanner the scanner to be used
+ */
+ private void unreadBuffer(ICharacterScanner scanner) {
+ for (int i= fBuffer.length() - 1; i >= 0; i--)
+ scanner.unread();
+ }
+}
View
4 src/org/hk/io/language/plugin/editors/IIoColorConstants.java
@@ -4,8 +4,8 @@
public interface IIoColorConstants {
RGB COMMENT = new RGB(128, 0, 0);
- RGB KEYWORD = new RGB(128, 128, 128);
RGB STRING = new RGB(0, 128, 0);
- RGB TYPE = new RGB(0, 0, 0);
RGB OPERATOR = new RGB(0, 0, 128);
+ RGB KEYWORD = new RGB(128, 128, 128);
+ RGB DEFAULT = new RGB(0, 0, 0);
}
View
2  src/org/hk/io/language/plugin/editors/IoConfiguration.java
@@ -37,7 +37,7 @@ protected IoScanner getIoScanner() {
if (ioScanner == null) {
ioScanner = new IoScanner(colorManager);
ioScanner.setDefaultReturnToken(new Token(new TextAttribute(
- colorManager.getColor(IIoColorConstants.TYPE))));
+ colorManager.getColor(IIoColorConstants.KEYWORD))));
}
return ioScanner;
}
View
56 src/org/hk/io/language/plugin/editors/IoScanner.java
@@ -6,7 +6,6 @@
import org.eclipse.jface.text.rules.IWordDetector;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.Token;
-import org.eclipse.jface.text.rules.WordRule;
public class IoScanner extends RuleBasedScanner {
@@ -24,22 +23,23 @@
"super", "system", "then", "thisBlock", "thisContext", "call",
"try", "type", "uniqueId", "updateSlot", "wait", "while", "write",
"yield" };
- private final static String TYPES[] = new String[] { "Array", "AudioDevice",
- "AudioMixer", "Block", "Box", "Buffer", "CFunction", "CGI",
- "Color", "Curses", "DBM", "DNSResolver", "DOConnection", "DOProxy",
- "DOServer", "Date", "Directory", "Duration", "DynLib", "Error",
- "Exception", "FFT", "File", "Fnmatch", "Font", "Future", "GL",
- "GLE", "GLScissor", "GLU", "GLUCylinder", "GLUQuadric",
- "GLUSphere", "GLUT", "Host", "Image", "Importer", "LinkList",
- "List", "Lobby", "Locals", "MD5", "MP3Decoder", "MP3Encoder",
- "Map", "Message", "Movie", "Notification", "Number", "Object",
- "OpenGL", "Point", "Protos", "Regex", "SGML", "SGMLElement",
- "SGMLParser", "SQLite", "Server", "Sequence", "ShowMessage",
- "SleepyCat", "SleepyCatCursor", "Socket", "SocketManager", "Sound",
- "Soup", "Store", "String", "Tree", "UDPSender", "UPDReceiver",
- "URL", "User", "Warning", "WeakLink", "true", "false", "nil",
- "Random", "BigNum", "Sequence" };
-
+
+ private final static String TYPES[] = new String[] { "Array",
+ "AudioDevice", "AudioMixer", "Block", "Box", "Buffer", "CFunction",
+ "CGI", "Color", "Curses", "DBM", "DNSResolver", "DOConnection",
+ "DOProxy", "DOServer", "Date", "Directory", "Duration", "DynLib",
+ "Error", "Exception", "FFT", "File", "Fnmatch", "Font", "Future",
+ "GL", "GLE", "GLScissor", "GLU", "GLUCylinder", "GLUQuadric",
+ "GLUSphere", "GLUT", "Host", "Image", "Importer", "LinkList",
+ "List", "Lobby", "Locals", "MD5", "MP3Decoder", "MP3Encoder",
+ "Map", "Message", "Movie", "Notification", "Number", "Object",
+ "OpenGL", "Point", "Protos", "Regex", "SGML", "SGMLElement",
+ "SGMLParser", "SQLite", "Server", "Sequence", "ShowMessage",
+ "SleepyCat", "SleepyCatCursor", "Socket", "SocketManager", "Sound",
+ "Soup", "Store", "String", "Tree", "UDPSender", "UPDReceiver",
+ "URL", "User", "Warning", "WeakLink", "true", "false", "nil",
+ "Random", "BigNum", "Sequence" };
+
static class WordDetector implements IWordDetector {
public final boolean isWordPart(char c) {
@@ -50,24 +50,26 @@ public final boolean isWordStart(char c) {
return Character.isJavaIdentifierStart(c);
}
}
-
+
public IoScanner(ColorManager manager) {
- IToken string = new Token(new TextAttribute(
+ IToken token = new Token(new TextAttribute(
manager.getColor(IIoColorConstants.KEYWORD)));
+ IToken defaultToken = new Token(new TextAttribute(
+ manager.getColor(IIoColorConstants.DEFAULT)));
- final WordRule w = new WordRule(new WordDetector(), string);
+ CombinedWordRule.WordMatcher wordRule = new CombinedWordRule.WordMatcher();
for (final String k : KEY_WORDS) {
- w.addWord(k, string);
- w.addWord(k.toUpperCase(), string);
+ wordRule.addWord(k, token);
}
-
+
for (final String k : TYPES) {
- w.addWord(k, string);
- w.addWord(k.toUpperCase(), string);
+ wordRule.addWord(k, token);
}
-
+ CombinedWordRule combinedWordRule = new CombinedWordRule(
+ new WordDetector(), defaultToken);
+ combinedWordRule.addWordMatcher(wordRule);
IRule[] rules = new IRule[1];
- rules[0] = w;
+ rules[0] = combinedWordRule;
setRules(rules);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.