Hi,

I have been using rhino embedded in tomcat / jetty for a server web
framework that I am working on.

I started investigating the rhino debugger last week.  I had first
looked into options for debugging rhino with eclipse but didn't have
any success, but the rhino debugger is very easy to use and integrate.

I have modified the swing code a little to provide syntax hilighting
for code by using a JTextPane rather than JTextArea and doing some
simple parsing .

This list seems to be very quiet, but if there are any committers who
are interested, the diff is shown below.  I am happy to also submit
into bugzilla if that is proper process.

Joel

All the changes are in the
org.mozilla.javascript.tools.debugger.SwingGui.java file, but it would
be better to somehow make the TokenStream.isKeyword method visible
(maybe by putting a wrapper method in ScriptRuntime?) and use it
rather than the keyword list I have in the patch below.

70a71
> import java.util.HashSet;
80a82
> import java.util.Set;
496,497c498,499
<             int start = w.getPosition(lineNumber-1);
<             int end = w.getPosition(lineNumber)-1;
---
>             int start = w.getLineStartOffset(lineNumber-1);
>             int end = w.getLineStartOffset(lineNumber)-1;
529,537c531
<             try {
<                 w.setPosition(w.textArea.getLineStartOffset(line-1));
<             } catch (BadLocationException exc) {
<                 try {
<                     w.setPosition(w.textArea.getLineStartOffset(0));
<                 } catch (BadLocationException ee) {
<                     w.setPosition(-1);
<                 }
<             }
---
>             w.setPosition(w.getLineStartOffset(line-1));
581,594c575,583
<         JTextArea ta = w.textArea;
<         try {
<             if (line == -1) {
<                 w.setPosition(-1);
<                 if (currentWindow == w) {
<                     currentWindow = null;
<                 }
<             } else {
<                 int loc = ta.getLineStartOffset(line-1);
<                 if (currentWindow != null && currentWindow != w) {
<                     currentWindow.setPosition(-1);
<                 }
<                 w.setPosition(loc);
<                 currentWindow = w;
---
>         if (line == -1) {
>             w.setPosition(-1);
>             if (currentWindow == w) {
>                 currentWindow = null;
>             }
>         } else {
>             int loc = w.getLineStartOffset(line-1);
>             if (currentWindow != null && currentWindow != w) {
>                 currentWindow.setPosition(-1);
596,597c585,586
<         } catch (BadLocationException exc) {
<             // fix me
---
>             w.setPosition(loc);
>             currentWindow = w;
1392c1381
<
---
>
1425c1414
<     extends JTextArea
---
>     extends JTextPane
1461c1450
<                 int line = getLineOfOffset(pos);
---
>                 int line = w.getLineOfOffset(pos);
1468c1457
<                             modelToView(getLineStartOffset(line + 1));
---
>                             modelToView(w.getLineStartOffset(line + 1));
1570c1559
<             line = getLineOfOffset(pos);
---
>             line = w.getLineOfOffset(pos);
1960c1949
<         int lineCount = textArea.getLineCount() + 1;
---
>         int lineCount = fileWindow.getLineCount() + 1;
1987c1976
<         int lineCount = textArea.getLineCount() + 1;
---
>         int lineCount = fileWindow.getLineCount() + 1;
1999,2002c1988
<             try {
<                 pos = textArea.getLineStartOffset(i);
<             } catch (BadLocationException ignored) {
<             }
---
>             pos = fileWindow.getLineStartOffset(i);
2046c2032
<
---
>
2092a2079,2152
>     private static Set<String> KEYWORDS = new HashSet<String>(Arrays.asList(
>             new String[] {
>                 "abstract",
>                 "boolean",
>                 "break",
>                 "byte",
>                 "case",
>                 "catch",
>                 "char",
>                 "class",
>                 "const",
>                 "continue",
>                 "debugger",
>                 "default",
>                 "delete",
>                 "do",
>                 "double",
>                 "else",
>                 "enum",
>                 "export",
>                 "extends",
>                 "false",
>                 "final",
>                 "finally",
>                 "float",
>                 "for",
>                 "function",
>                 "goto",
>                 "if",
>                 "implements",
>                 "import",
>                 "in",
>                 "instanceof",
>                 "int",
>                 "interface",
>                 "long",
>                 "native",
>                 "new",
>                 "null",
>                 "package",
>                 "private",
>                 "protected",
>                 "public",
>                 "return",
>                 "short",
>                 "static",
>                 "super",
>                 "switch",
>                 "synchronized",
>                 "this",
>                 "throw",
>                 "throws",
>                 "transient",
>                 "true",
>                 "try",
>                 "typeof",
>                 "var",
>                 "void",
>                 "volatile",
>                 "while",
>                 "with"
>             }));
>     private static final SimpleAttributeSet KEYWORD = new 
> SimpleAttributeSet();
>     private static final SimpleAttributeSet JAVADOC = new 
> SimpleAttributeSet();
>     private static final SimpleAttributeSet COMMENT = new 
> SimpleAttributeSet();
>     private static final SimpleAttributeSet QUOTED = new SimpleAttributeSet();
>
>     static {
>         // use same color scheme as eclipse
>         StyleConstants.setForeground(KEYWORD, new Color(127, 0, 85));
>         StyleConstants.setForeground(JAVADOC, new Color(63, 95, 191));
>         StyleConstants.setForeground(COMMENT, new Color(63, 127, 125));
>         StyleConstants.setForeground(QUOTED, new Color(42, 0, 255));
>     }
2118c2178
<
---
>
2129a2190,2199
>      * Offsets for start of each line in {...@link #textArea}.
>      */
>     private int[] lineStartOffsets = new int[16];
>
>     /**
>      *  Number of lines.
>      */
>     private int lineCount = 0;
>
>     /**
2145,2149c2215,2219
<     public int getPosition(int line) {
<         int result = -1;
<         try {
<             result = textArea.getLineStartOffset(line);
<         } catch (javax.swing.text.BadLocationException exc) {
---
>     public int getLineStartOffset(int line) {
>         if (line >= 0 && line < lineStartOffsets.length) {
>             return lineStartOffsets[line];
>         } else {
>             return -1;
2151c2221,2228
<         return result;
---
>     }
>     public int getLineCount() {
>         return lineStartOffsets.length;
>     }
>     public int getLineOfOffset(int offset) {
>         int i = 0;
>         while (i < lineStartOffsets.length && offset < lineStartOffsets[i++]);
>         return i + 1;
2207,2208d2283
<         textArea.setRows(24);
<         textArea.setColumns(80);
2257a2333,2355
>
>             // re-calculate lineOffsets (must normalize linesep)
>             lineCount = 1;
>             int offset = 0;
>             for (int i = 0; i < newText.length(); i++) {
>                 char c = newText.charAt(i);
>                 // treat CRLF as single char
>                 if (c == '\n' && i > 1 && newText.charAt(i - 1) == '\r') {
>                     continue;
>                 }
>                 offset++;
>                 if (c == '\r' || c == '\n') {
>                     // resize if needed
>                     if (lineCount == lineStartOffsets.length) {
>                         int[] tmp = new int[lineStartOffsets.length * 2];
>                         System.arraycopy(lineStartOffsets, 0,
>                                 tmp, 0, lineStartOffsets.length);
>                         lineStartOffsets = tmp;
>                     }
>                     lineStartOffsets[lineCount++] = offset;
>                 }
>             }
>             hilight();
2263a2362,2421
>      * Apply syntax higlight to @link {...@link #textArea}
>      */
>     private void hilight() {
>         StyledDocument doc = textArea.getStyledDocument();
>         String text = "";
>         try {
>             text = doc.getText(0, doc.getLength());
>         } catch (BadLocationException e) {}
>         // parse JS and set colours
>         int pos = 0;
>         while (pos < text.length()) {
>             char c = text.charAt(pos);
>             SimpleAttributeSet style;
>             int start = pos;
>             if (Character.isJavaIdentifierStart(c)) {
>                 // read rest of identifier
>                 while (++pos < text.length()
>                         && Character.isJavaIdentifierPart(text.charAt(pos)));
>                 String word = text.substring(start, pos);
>                 if (!KEYWORDS.contains(word)) {
>                     continue;
>                 }
>                 style = KEYWORD;
>
>             // block comment (or javadoc)
>             } else if (text.regionMatches(pos, "/*", 0, 2)) {
>                 style = COMMENT;
>                 if (pos < text.length() - 3 && text.charAt(pos + 2) == '*'
>                         && text.charAt(pos + 3) != '/') {
>                     style = JAVADOC; // '/**' but not '/**/'
>                 }
>                 // move to end of '*/' or end of string if comment not closed
>                 pos = text.indexOf("*/", pos);
>                 pos = pos == -1 ? text.length() : pos + 2;
>
>             // line comment
>             } else if (text.regionMatches(pos, "//", 0, 2)) {
>                 style = COMMENT;
>                 // move to end of line or end of doc
>                 while (++pos < text.length() && text.charAt(pos) != '\r'
>                     && text.charAt(pos) != '\n');
>             } else if (c == '\"' || c == '\'') {
>                 style = QUOTED;
>                 int q = c; // match either single or double quote
>                 pos++; // skip first quote
>                 while (pos < text.length() && (c = text.charAt(pos++)) != q) {
>                     if (c == '\\') pos++; // skip any escaped quotes
>                 }
>             } else {
>                 pos++;
>                 continue;
>             }
>
>             // apply style
>             doc.setCharacterAttributes(start, pos - start, style, false);
>         }
>     }
>
>
>     /**
_______________________________________________
dev-tech-js-engine-rhino mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-js-engine-rhino

Reply via email to