C# Code Highlighter (BETA)

by Moridin8 24. February 2007 19:53

What self respecting developer site wouldn't have a code-highlighter? Well... several really, but this isn't one of them and unlike most of the others that 'borrow' other peoples efforts I have created by own.

The code behind the scenes for this Beta is not all new.  The lexer engine is pretty old and was developed while I was creating bespoke content management engines that needed things like CSS, XML and other data/script parsing/interpretation.  Fortunatly as I created my lexer engine in my own time, I was able to retain copyright - thus I rooted it out of my archive and update it to .NET 2.0.  However the add-on engines that translated the lexical analysis tree produced by the lexer engine were not my own, so I can't bring those to you.  My personal translation objects I won't release yet as I am using them for other things. 

Except of course my c# translater/highlighter... once it's fully working and the Lexer has been tidied up some.

So.  Please have a play and let me know of any issues (via usual channels).

C# Code Highlighter (opens new window).

To aid in this I have purposefully allowed full error messages to be displayed if anything nasty does occur.  This is a privelage, please don't abuse it. 

P.s.  There is a glaring bug in the highlighter that I do know about, but I am opting to see if any of you spot it!  Evil ain't I?

   Update!:   It took a while, but a '##csharp' user finally found the problem.  The last token is
                  never parsed!  Congratulations to '
IRBMe'!... no prizes though!  sorry.  ;)

Spotted bugs:

  1. Enter standard text that contains brackets produces an 'Index was Out of Range' exception in the HighLighter on line 78. - OmniMarko
    • MRW: Thanks for this, I'm suprised I never picked this up.  (>.<)
  2. ASP.NET does not highlight very nicely, even though structure is maintained. - Anon
    • MRW: I am strangely not suprised considering this is a C# highlighter.  Structure would be maintained as the highlighter retains white-spacing.  I do not yet have plans for an ASP.NET highlighter/translater.
  3. (Not a bug) Can you get your highlighter to colourise sql statements in strings? - z0mb13Grrrl
    • MRW: I do plan a SQL highlighter as a large percentage of of my work involves SQL-Server.  However I have no plans to incorporate this into the C# highlighter as I refuse to use inline SQL.  I suggest you use Stored Procedures.
    • z0mb13Grrrl: I have to use Access so I can not use Stored Procedures, so what about people like myself who have no option?
    • MRW: I will take this into consideration, but no promises.

Tags: ,

Articles

That nasty DesignMode issue… again… and again...

by Moridin8 1. February 2007 19:46

For all those I know who keep asking this question... too often (^_^).

Microsoft is not all seeing. They are not omnipotent, despite what they may blag about in their marketing and sales propaganda. Being a hardcore user of their technologies for a long time has given me the ability to say that with some conviction. For instance, some of the bugs features that occur in their work astound me. Like the one where under certain circumstances, when the moon is high you can create a ctor (in c# 2.0) that needs to access a property within the same class (due to functionality encompassed there-in) and the debugger will simply skip over it entirely (although the property will be run anyway) simply because of a missed ‘nop’ opcode at compile-time. You would have thought that would have been picked up early.

Another obvious one is the subject of this missive… the DesignMode property (on the ISite interface, courtesy system.componentmodel.component).
There are times when it is necessary to create custom WinForm controls, sometimes inheriting other controls (i.e. Panel) or sometimes inheriting directly off Control, or even inheriting other custom controls. Sometimes control logic decisions need to be made within the component that say what happens at Design-Time or Run-Time. These things are not always the same – in-fact rarely so. So would it surprise you to know that the DesignMode property does not work for the child controls of a form? It always returns false.

This has caused quite some consternation on the internet, and Microsoft do not have an official work around for this quite common experience. The workaround out there on the internet for this error is usually something like this:

///
/// Indicates if the current view is being utilized in the VS.NET IDE or not.
/// The similarly-named .Net framework property for a UserControl will only
/// show that its related object in is DesignMode only if the immediate parent
/// is viewed in the IDE. For instance, if UserControl A has UserControlB placed
/// on it, and UserControl B has UserControlC placed on it, and UserControl A
/// is being viewed in the IDE, UserControl C will normally register its
/// DesignMode flag as false. This overridden implmentation of DesignMode will
/// utilized a different method in determining this.
///
public new bool DesignMode
{
      get
      {
            return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv");
      }
}


(Above snippet courtesy of Mark Jordan – chosen as it’s the best presented)

However the above solution has a nasty tendency to fail quite horribly if you are working in a different IDE, or even version of .NET. It is not desirable, and I’d say too ambiguous. Some of the other workarounds are so dire that I won’t let them grace this blog.

YET! All is not lost. (How did you guess I was going to have a better solution?)

When this horrid little problem first reared it’s postuled little head a couple of years ago, I took it upon myself to try and find out why DesignMode was failing. Microsoft was coming up short with an answer, and no-one out there seemed to know either. So I whipped out Lutz Roeder’s Reflector and went for a snoop. It may surprised you to know that while the ISite interface (And all it entails) are available at the time a control is added to it’s parent, the actual ISiteDesignMode value is not assigned. This is a little odd considering that only Controls can be added anyway. Also that the DesignMode property itself is protected (not Public as it could have been to help here); so any chance you think right now you may have of navigating the parent hierarchy until you find the parent Form and retrieving the DesignMode that way… your horribly mistaken. The only OTHER route is a nasty reflection hack (and YES, this form of hack was a suggested work around by another individual – yeuck!). Something that seems a little OTT when there has to be a much better solution to this issue.

Well. There is. Within the System.ComponentModel namespace is a nice little class called ‘LicenseManager’. It has a handy static function within it called ‘UsageMode’. It returns an enumerated value from ‘LicenseModeUsage’ with two options. Guess what they are!?

  • Designtime
  • Runtime.

No. I’m not joking.
Yes, they work under all tested circumstances (so long as the licensing model of the framework is used correctly – which most of you out their will not even think about).

Why doesn’t Microsoft tell everyone complaining about the DesignMode issue?

No idea. Upon asking someone I know on the C# development team on IRC, they told me that they didn’t know either. He informed me that he will chase it up for me. I look forward to his reply to this quirky issue.

Either way… here is a quick usage example:

if(LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
// design time only stuff
}
else
{
// runtime only stuff.
}


Not rocket science is it?

If Licensing is an interest or a worry to you, here is the info: How to: License Components and Controls. But I doubt it will be an issue unless you want to ban design time access to your component anyway.  

Tags: ,

Articles

Powered by BlogEngine.NET 1.5.0.7

About Matt R.Warren

MeMy name is Matt and I am the current tenant of this small corner of the internet. I mostly architect, design and prototype applications that use .NET with C# and a little C++/CLI for Enterprise although I am aware of and enjoy fully embracing Java based solutions and alternatives such as Mono/Linux.  

I have worked on projects ranging from small tools to large distributed real-time Enterprise systems ranging from EPOS and real-time/JIT stock management systems, to distributed applications for National/International Utility, Healthcare, Insurance and Finance  in the private sector in both the USA and the EU.

My LinkedIn Profile (Opens new window/tab)

“Matt is one of the brightest people I've worked with. His in-depth knowledge of the .NET frameworks has been a tremendous benefit to nVISIA and our clients. His knowledge of software architecture in general allows him to architect systems for the best fit to his client's needs.” 
Dan Christopherson , Technical Director , nVISIA

“I had the distinct pleasure of working with Matt at nVisia. Matt's understanding of the Microsoft Technical space is outstanding. He is constantly working on improving his technical skills and rapidly masters any new technology that he encounters. He is an excellent teacher and a wonderful asset for any size team.” 
Jim Harnden , Senior Technical Architect , nVISIA

“Matt Warren is a very talented developer with great capacity for self study, investigation and adapts to new languages and frameworks with ease. He has an excellent grasp of software architecture and modern development principles. He has proven himself time and time again to be a hard worker and someone who can get the job done when you're in a tight spot.” 
Andrew Jump , Partner, C# Developer , Contegra

This website represents some of my spare time.  My small presence on the web between my family and my career.  I hope over time you find many useful things here.