Code editor control with syntax highlighting for Silverlight available

by Vlad 1. November 2010 14:12

This is an update to my Syntax Highlighting TextBox in Silverlight using MVVM post.

First of all, i'd like to thank Jeff Willcox for his port to Silverlight of Drew Miller's syntax highlighting engine called ColorCode and Morten Nielsen for putting it together with my overlaying TextBox idea.

Morten's SyntaxHighlightingTextBox control is just for demo (he mentions that a polished version will be included in their SDK), and the performance goes down when the text is bigger than just a few lines of code.

I rewrote the control in order to improve the performance - the goal was to get instant update when typing.

Changes:

Used a RichTextBox in the background instead of a TextBox. They both use Inlines for the content so the change is trivial, but from my observations the RichTextBox is faster, especially for scrolling.

Fixed a couple of bugs in the Colorizer.

Problem:

Syntax highlighting is done on the UI thread at every key press; refreshing the syntax caused the whole content do be cleared and rebuilt.

Solution:

Use in memory entities for formatting instead of updating the Inlines collection directly. This decouples the syntax highlighting code from the UI and also enables background processing. Also, it enables partial updating - basically updating only the Inlines that changed instead of clearing and reloading all the content for the RichTextBox. Even more, it is possible to update only the style (color) for existing Inlines if the text is the same.

Problem:

With the syntax highlighting in the background, the UI continues responsive, but typing takes place in the invisible overlaying TextBox and it is not visible until the syntax highlighting updates the background RichTextBox, and it is confusing for the user. (For big texts, the syntax highlighting can take seconds).

Solution:

Use a DiffTextBox as overlay. A DiffTextBox handles all key presses and updates a list of text transforms (insert, remove, move caret). Using that information, I can update rapidly the affected RichTextBox Inlines and get instant update for the background. The syntax highlighting can now be delayed so that it takes place after the user stops typing (configurable HighlightDelay).

There is still a limit of text size that the text box can handle - if the text is very big (dozens of pages), the Silverlight TextBox and RichTextBox slow down considerably. I'm thinking this could be fixed by managing "virtual pages" and loading only the current context (several pages) in the control so that it keeps being responsive.

You can see the control in action in this code sharing site I built: CodeReturn.com. You can upload code that you want to share and / or embed in your pages. The best part is that you can edit the code (and soon: post replies to other people's code) directly in the page where it is embedded! You just need to login with any OpenID account!

Like this:

Also, you can grab the source code here: SyntaxHighlighting.zip (99.63 kb).

Filter candidates early by interviewing them online

by Vlad 9. April 2010 14:59

This happened to me lots of times:

The candidate arrives for the interview. We spend a few minutes for the introductions and small talk. Then, the first question: "What is a class and what is an object?"... <crickets>...

At this moment I normally try a few other questions, just to be polite, but my mind is made up: I just wasted my time!

It can be even worse - I get an answer that is sufficiently vague and uses some buzzwords to be considered correct. So I spend let's say 30 minutes trying to determine if the candidate knows stuff and has a problem expressing himself or he's just taking long shots with his answers.

And then, the practical part: get him or her to write some simple code -  a class with a property and a method (C#). As a programmer you probably write dozens of those each day. And that's when I have my confirmation: the candidate doesn't even know the syntax for a property or a method. He's looking for the door, thinking if it would be less shameful if he just got up and ran. And I just wasted even more of my time!

Now, on top of that, imagine that the company had to arrange transportation and accommodation for the candidate, because he/she was from another city. Or, the company has a recruiting process that requires the candidate to pass an interview with HR or to take some personality tests first. What a waste!

And it is a global problem - Coding Horror, The Non-Programming Programmer.

The solution? Interview candidates by phone or online first. Coder Proof is a free interview tool that allows you to see the candidate's skills at programming in real time. You can see how he writes code before dedicating more time and resources to bring him to the office for a face-to-face interview.

And the candidate can run at any time - there is no shame in "I just lost my internet connection". Wink