Click here to Skip to main content
Email Password   helpLost your password?

Introduction

I needed a script-editor control for one of my projects, with syntax highlighting and intellisense for more comfort. I did a search on CodeProject and Google, but didn't find any - just separate solutions for one of the two functions... The one I found with both syntax highlighting and intellisense just wasn't flexible enough for me, because it could be used only for C#, and only could handle classes (intellisense popped up after pressing the . key). As I wanted to use this mostly for LUA, this simply wasn't enough. I decided to use this control as a sample, and write my own, flexible one... After a few days, I came out with this: I have a flexible syntax-highlighter, with intellisense. :) I can edit everything in design mode, for example, the intellisense hotkey, or the scope-operator's list... Yes, I can handle not just the point, but more like ::, or ->, with C++ language support in mind. :)

Using the code

I designed the CodeTextBox control to be easy to work with: you can edit all the necessary properties in design mode as well, under the CodeTextBox category.

CodeTextBox properties

There're some things that I couldn't do automatically, or put in Design mode...

Limitations, Ideas

I know that it's not perfect yet, but hey, it's useable... :) Let me tell you some ideas to make this better, and also some of its limitations that I haven't been able to solve until now...

Limitations:

As in most syntax highlighters, I cannot handle C-style comment blocks, and keywords like #region.

Ideas:

References

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralHelp [modified]
leosco
5:05 17 Dec '09  
First of all, this is extremely cool - I appreciate your efforts and this is fantastic for my project!

I have a small problem though - I want to highlight every instances of hexadecimal numbers from 00-FFFFFF (meaning every hex number from 00-FFFFFF which are 2 digit, 4 digit or 6 digit, NOT 1, 3 or 5 digit (like, highlight 01, 0001 and 000001 but NOT 1, 001 or 00001). If I write this manually, it will take be time-consuming, it would reduce the performance of the program too. What would be the best way to do this?

Oh, I forgot to mention. I want #$ to be written before the numbers. Like #$01, not just 01. I tried doing this manually but the # and $ won't get recognised. How can I fix this?

modified on Thursday, December 17, 2009 10:20 AM

Generalnot efficient
hsj5280
5:13 2 Feb '09  
when parsing a hundreds of lines file is very slow
GeneralRemove pinove call to run under moni, imporve speed
AndrusM
6:24 20 Oct '08  
Code contains one pinvoke call:

        [DllImport("user32.dll", SetLastError = true)]
public static extern bool PostMessage(
IntPtr hWnd,// handle to destination window
UInt32 Msg, // message
IntPtr wParam, // first message parameter
IntPtr lParam // second message parameter
);

so it will not run under MONO in Linux.
How to fix this so this can be used under MONO also ?

Also, some hunderd lines of code take 5-10 seconds to load at startuo.
How to speed it up ?

Andrus

GeneralRe: Remove pinove call to run under moni, imporve speed
The_Mega_ZZTer
17:05 23 Jun '09  
PostMessage is used to send a message to another window, usually a non-.NET one. However this code window shouldn't need to do that, so it should be possible to fix it so this call isn't needed. You'd have to look at the source code to figure out where this call is used and why, and then you can figure out an alternative.
GeneralCannot open with Visual Studio 2005 express
g.bude
2:02 17 Sep '08  
Can you provide an alternative solution (.sln) file?
GeneralOr Visual Studio 2005 Pro
eddiem9
18:10 14 Oct '08  
I would like to see a VS2005 solution file also.....looks like a great piece of code!

Thanks
GeneralRe: Or Visual Studio 2005 Pro
Tamas Honfi
23:52 14 Oct '08  
I don't have Visual Studio 2005 installed at the moment, but as I can remember there's no .NET 3.0 or .NET 3.5 specific code in the solution.
Just create a VS2005 solution, grab the files into, and it should work. Smile

Tamas

GeneralFlashing on paste
AndrusM
7:17 16 Sep '08  
When large block of code is pasted in to box, CodeTextBox blinks long time.
How to remove this blikning ?

Andrus

GeneralRe: Flashing on paste
Tamas Honfi
1:32 17 Sep '08  
Hi Andrus,

I know about that issue, but unfortunately haven't found a reasonable solution yet.
Maybe it can be solved with some Win32 API invokes (WM_SetRedraw) as I've seen in another solution, but that means, it won't work on Mono...

However,
if you have another working solution for this, please le me know!


Regards,

Tamas

GeneralRe: Flashing on paste
AndrusM
2:04 17 Sep '08  
Thank you for reply.
Mini-csarplab earlier version highlighter which I currently use pastes without flashing. It uses secondary invisible RichTextBox as buffer for this. This does not require pinvokes.
Maybe this method can be used.


I tested it in MONO SVN and found that sometimes MONO crashes
with expeption.

Also I found that colors are incorrect in MONO.
It may be worth to create simple code to reproduce those issues and post them to MONO buglist to they can be fixed.

I'm also interested to highlight SQL syntax. How to add this ? Manoli highlighter uses class below to define this. How to use this class or some other SQL syntax definition?

#region Copyright © 2001-2003 Jean-Claude Manoli [jc@manoli.net]
/* * Based on code submitted by Mitsugi Ogawa.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the author(s) be held liable for any damages arising from
* the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/

#endregion
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace Manoli.Utils.CSharpFormat
{
/// <summary> /// Generates color-coded T-SQL source code.
/// </summary> class TsqlFormat : CodeFormat
{
/// <summary> /// Regular expression string to match single line
/// comments (--).
/// </summary> protected override string CommentRegEx
{
get {
return @"(?:--\s).*?(?=\r|\n)";
}
}

/// <summary> /// Regular expression string to match string literals.
/// </summary> protected override string StringRegEx
{
get {
return @"''|'.*?'";
}
}

/// <summary> /// Returns <b>false</b>, since T-SQL is not case sensitive.
/// </summary> public override bool CaseSensitive
{
get { return false; }
}


/// <summary> /// The list of T-SQL keywords.
/// </summary> protected override string Keywords
{
get {
return "absolute action ada add admin after aggregate " + "alias all allocate alter and any are array as asc " + "assertion at authorization avg backup before begin " + "between binary bit bit_length blob boolean both breadth " + "break browse bulk by call cascade cascaded case cast " + "catalog char char_length character character_length " + "check checkpoint class clob close clustered coalesce " + "collate collation column commit completion compute " + "connect connection constraint constraints constructor " + "contains containstable continue convert corresponding " + "count create cross cube current current_date current_path " + "current_role current_time current_timestamp current_user " + "cursor cycle data database date day dbcc deallocate dec " + "decimal declare default deferrable deferred delete deny " + "depth deref desc describe descriptor destroy destructor " + "deterministic diagnostics dictionary disconnect disk " + "distinct distributed domain double drop dummy dump " + "dynamic each else end end-exec equals errlvl escape " + "every except exception exec execute exists exit external " + "extract false fetch file fillfactor first float for " + "foreign fortran found free freetext freetexttable from " + "full function general get global go goto grant group " + "grouping having holdlock host hour identity identity_insert " + "identitycol if ignore immediate in include index indicator " + "initialize initially inner inout input insensitive insert " + "int integer intersect interval into is isolation iterate " + "join key kill language large last lateral leading left " + "less level like limit lineno load local localtime localtimestamp " + "locator lower map match max min minute modifies modify " + "module month names national natural nchar nclob new next " + "no nocheck nonclustered none not null nullif numeric object " + "octet_length of off offsets old on only open opendatasource " + "openquery openrowset openxml operation option or order " + "ordinality out outer output over overlaps pad parameter " + "parameters partial pascal path percent plan position " + "postfix precision prefix preorder prepare preserve " + "primary print prior privileges proc procedure " + "public raiserror read reads readtext real reconfigure " + "recursive ref references referencing relative replication " + "restore restrict result return returns revoke right role " + "rollback rollup routine row rowcount rowguidcol rows rule " + "save savepoint schema scope scroll search second section " + "select sequence session session_user set sets setuser " + "shutdown size smallint some space specific specifictype " + "sql sqlca sqlcode sqlerror sqlexception sqlstate sqlwarning " + "start state statement static statistics structure substring " + "sum system_user table temporary terminate textsize than then " + "time timestamp timezone_hour timezone_minute to top trailing " + "tran transaction translate translation treat trigger trim " + "true truncate tsequal under union unique unknown unnest " + "update updatetext upper usage use user using value values " + "varchar variable varying view waitfor when whenever where " + "while with without work write writetext year zone";
}
}

/// <summary> /// Use the pre-processor color to mark keywords that start with @@.
/// </summary> protected override string Preprocessors
{
get
{
return @"@@CONNECTIONS @@CPU_BUSY @@CURSOR_ROWS @@DATEFIRST " + "@@DBTS @@ERROR @@FETCH_STATUS @@IDENTITY @@IDLE " + "@@IO_BUSY @@LANGID @@LANGUAGE @@LOCK_TIMEOUT " + "@@MAX_CONNECTIONS @@MAX_PRECISION @@NESTLEVEL @@OPTIONS " + "@@PACK_RECEIVED @@PACK_SENT @@PACKET_ERRORS @@PROCID " + "@@REMSERVER @@ROWCOUNT @@SERVERNAME @@SERVICENAME @@SPID " + "@@TEXTSIZE @@TIMETICKS @@TOTAL_ERRORS @@TOTAL_READ " + "@@TOTAL_WRITE @@TRANCOUNT @@VERSION";
}
}
}
}


Andrus

GeneralRe: Flashing on paste
Tamas Honfi
2:20 17 Sep '08  
Hi Andrus,

as I see, this class uses a space-separated list of strings for the words to highlight.
Splitting that strings for space, and addig the result arrays to the proper List collections of CodeTextbox should work.
Unfortunately, this version doesn't support string highlightning, but u can highlight preprocessors with the function list, and u can gain an additional color. Smile

The buffer-richTextbox solution is interesting, but I don't have any idea, how slow it could be because of the duplicate pastes, especially for larger texts...


Regards,

Tamas

GeneralOther solutions
RednaxelaFX
2:23 8 Sep '08  
SharpDevelop is LGPL'd open source. It's worth taking a look at.

Quite a few WinForm apps use #develop (SharpDevelop)'s TextEditor, which should give you pretty much of what you need as a syntax highlight editor. It not designed to work with the WinForm designer, though, but using it with just code isn't all that hard.
Other parts of SharpDevelop can be used to implement your own IntelliSense-like functionality.
GeneralRe: Other solutions
dave.dolan
10:14 8 Sep '08  
I'll also add that you can drop in your own kind of highlighting by implementing your own version of IHighlightingStrategy and plugging it in. If you have a parser you've created with some other parser generation tool, for example, gold parser builder, you can plug it in to the system by wrapping it in an IHighlightingStrategy class.
GeneralRe: Other solutions
AndrusM
11:39 15 Sep '08  
charpdevelop TextEditor uses p/invokes.
So it will not work under MONO.

This is the only control available which works well with MONO.

Andrus


Last Updated 8 Sep 2008 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010