New feature: serverside syntax highlighting

I got tired of the buggy, CPU hogging Javascript syntax highlighter.

Hence I wrote some horrific PHP that spits out:


/*
 * multi-line comment
 */

public class Test {
   public static final double WAY_TOO_MUCH = 25.4f;
   public static final long[] DISARRAY = {-1, 1, 2L, 0xf3L};

   public volatile Map<String,
               Map<Integer,double>> lastNameToAgeToCreditRating;

   /**
    * Javadoc
    */

   @Annotation(14)
   @Annotator("fourteen")
   public static void main(String[] args) throws IOException {

      // single line comment

      try {
         char secret = 'X';
         int requested = 5;
         int offered = Integer.parseInt(args[0]);

@@         if(offered > WAY_TOO_MUCH) { // I prefixed the line with @@ to trigger the highlight
            System.out.println("get off my "+args[(int)DISARRAY[offered]]+"!");
         }
         else {
            System.out.println("hm. kay."); 
         }
      }
      finally {
         System.out.println   ("thx."); 
      }
   }
}

it’s far from flawless, and you guys will easily break it, if only due to failing to comply with java naming conventions :slight_smile:

Ooh let’s see what we can do to it…


class $x {
   volatile strictfp double \u2603 = 0.;
   @_(@__() Void.class) foo(Object ... o) { foo<Integer>(123,o) }
}


Damn, not bad at all… ;D

Thanks for your test-case, just fixed a bunch of screwups and your typical off-by-one bugs.

Is that valid Java code? o_O

/* ******************* / ********************
A old test function

public int testComment(String cmt)
{
  System.out.println("*/");
  System.out.println(cmt);
  System.out.println("/*");
}

// Next the new function, " // " change

Funny no ?

* * * *  **/


public int testComment(String cmt)
{
  System.out.println("/*");
  System.out.println(cmt);
  System.out.println("*/");
}

Commenting a full code block with /* */ doesn’t work :wink:


public class Util {
	
	public static final String fourCC(int fourCC) {
		byte[] fourCCString = new byte[4];
		fourCCString[0] = (byte)fourCC;
		fourCCString[1] = (byte)(fourCC >> 8);
		fourCCString[2] = (byte)(fourCC >> 16);
		fourCCString[3] = (byte)(fourCC >> 24);
		return new String(fourCCString);
	}
	
	public static final int fourCC(String fourCC) {
		if (fourCC.length() > 4)
			throw new IllegalArgumentException("FourCC String must have 4 or less characters.");
		
		// fill fourCC with whitespaces if length is shorter than 4
		if (fourCC.length() < 4) {
			for (int i=0; i<(4-fourCC.length()); i++) {
				fourCC.concat(" ");
			}
		}
		
		int fourCCInt = 0;		
		fourCCInt = (((byte)fourCC.charAt(0) & 0xFFFFFFFF) |
						((byte)fourCC.charAt(1) & 0xFFFFFFFF) << 8 |
						((byte)fourCC.charAt(2) & 0xFFFFFFFF) << 16 |
						((byte)fourCC.charAt(3) & 0xFFFFFFFF) << 24);
		return fourCCInt;
	}
}

Looks good except for the byte array in line 4.

There seem to be quite a lot off-by-one bugs left. I will fix it tonight.

Nice! reminds me to my first editors, editplus and notepad++

Riven, can you also modify the size of a tab? Seems like it’s using the “standard” of 8 spaces, but for code snippets it should probably be set to something smaller like 4, to minimize the chance of much horizontal scrolling.

Ditched the entire thing and wrote a state-machine. It can distinguish variables and fields from methods now. I added support for nested generics, but the numbers are still split on the periods.

@Bonbon-Chan: the example you gave might look incorrect now, but this is how it’s supposed to look.

@BoBear2681: converted tabs to 3 spaces

Nice! Looks good., my only suggestion is to make the font a little bigger. It just looks a bit small to me, maybe it’s my fault or maybe I’m weird ;D. Also is there by chance an xml file or, something for that syntax highlighting color scheme, I like it. :persecutioncomplex:

The font is perfectly fine for me, in fact it’s a bit on the large side. I can’t see where it’s set in CSS, so I think it’s dependent on the monospace font you have set in your browser.

Great stuff. it’s such a pity that there’s no easy to rip off code out there from netbeans or eclipse. The best I could find when I needed this in swing was jsyntaxpane but it was very buggy when I used it back in 2009 http://code.google.com/p/jsyntaxpane/

Yep, it’s using my browser’s (Firefox 8’s) monospace font. Thanks :slight_smile:


		span.javaLineComment { color: #080; }
		span.javaMultiComment { color: #080; }
		span.javaDocComment { color: #088; }
		span.javaKeyword { color: #808; }
		span.javaClass   { color: #800; }
		span.javaAnnotation { color: #888; }
		span.javaField   { color: #00c; }
		span.javaStatic  { color: #404; }
		span.javaMethod  { color: #008; }
		span.javaNumber  { color: #c00; }
		span.javaString  { color: #488; }
		span.javaChar    { color: #f00; }

I kinda picked random colors, mostly for debugging purposes. My home workspace looks nothing like it. I’ll probably adjust it further to make it match the rest of the layout. I think a bunch of you just threw up in your mouth, screaming ‘no more stale blue!’, but I guess you can’t please everyone :slight_smile:

Use Droid Sans Mono for the font, it’s pretty!

Thanks :slight_smile: I kinda like it’s quirkiness, and it differentiates well between different things.

JL235: That does look pretty! Good suggestion :slight_smile:

You can’t beat this. You really can’t.


http://indiespot.net/files/eclipse-code-style.png

That’s nice as well. It’s very unobtrusive to the eye, not to bright and not to dark, and it also differentiates well. I simply use Netbean’s color scheme (in Eclipse 8) )with a few modifications.

Derailing my own thread…

  1. fourCC will not be modified (strings are immutable), you’re basically doing nothing.
  2. if they were, it still wouldn’t work, so adding the assignment wouldn’t help (much)
    You can see that if you pass an empty string, it will stop once it reaches a length of 2, instead of 4.

Trivial fix:


      // fill fourCC with whitespaces if length is shorter than 4
      while (fourCC.length() < 4) {
         fourCC = fourCC.concat(" ");
      }

Besides that, shoving arbitrary bytes in a byte[4] into a String(…) is extremely dangerous, as you get a conversation to a char[] using the default system dependent charset which is not at all guaranteed to result into a char[4], and even when it’s a char[4] within the String, that doesn’t mean each char holds the value each original byte had.

Spooky nicely colored code :slight_smile: