views:

238

answers:

7

I am needing to make a program that can do syntax highlighting and the drop down autocomplete similar to visual studio. Id also like it to be able to collapse and expand code segments if possible.

I tried coding this before using rich text editor and using reg expressions to apply the coloring. This was very processor intensive and cause some lag with larger files. Then I thought maybe I was going about this entirely wrong.

So my question is

If you had to code a visual studio like program in VB.net or C#. How would you go about doing so?

+1  A: 

I'd build a plug-in or dsl for visual studio itself. Or if licensing/cost were an issue I'd use Notepad++. Why re-invent the wheel?

Joel Coehoorn
Well I need some custom functionality for this app. We currently use Textpad and a bunch of other command line tools for sorting data, ect. I just want a more custom wheel.
The Digital Ninja
What would prevent you from adding that functionality in Visual Studio?
John Saunders
A: 

Have you considered using a syntax highlighting component like Actipro's SyntaxHighlighter?

Praveen Angyan
I don't have a whole lot of money to trow at this project currently. Though id be interested in any free one you might know of.
The Digital Ninja
+6  A: 

Pretty much all tools that operate on source code (e.g. an IDE editor, a compiler), don't treat the code as text that is parsed via regexs, because this approach simply doesn't work in a real-world scale. These tools, operate on a model of the source code known as an Abstract Syntax Tree (AST), which models the code as a tree structure. In a program written in a lanuage such as C the root node of this tree is the main method where execution begins, the children of this root are the arguments that are passed to the main method, etc.

When operations are performed on the code, (e.g. highlight syntax, refactor), they are performed on the tree using something like a visitor pattern and the source files are kept in synch with the AST.

When code is updated it is parsed into tokens using a lexer, which are then analysed using a parser, and if the grammar rules of the language are obeyed, the AST is updated. To learn more about parsers, lexers, grammars and ASTs, I suggest checking out ANTLR.

Don
+1 for truth. Also, if the questioner is unfamiliar with *any* of the concepts discussed in this answer, they should consider picking up a compiler book. Perhaps the Dragon book or something similar.
Greg D
+1  A: 

If you want to go the Visual Studio language service route, the Managed Package Framework is a good place to start: http://msdn.microsoft.com/en-us/library/bb166360.aspx

It comes with the Visual Studio SDK. I believe there is a Regex based example that comes with it, but I'm not 100% sure.

Keep in mind that the Visual Studio shell is freely redistributable, it just might be a lot of overhead for your purposes.

Joel Day
+3  A: 
Robert Cartaino
A: 

I extend Visual Studio to support the languages I need. I have a blog talking about some of the techniques (in particular using ANTLR for the parsing services) (link).

Languages I've supported:

  • UnrealScript (syntax highlighting, almost all IntelliSense operations supported by Visual Studio)
  • ANTLR grammars (syntax highlighting, basic IntelliSense)
  • StringTemplate functional templating language (syntax highlighting, basic IntelliSense)
  • Chapel (syntax highlighting)

Places to go:

280Z28
A: 

Take a look at the Scintilla control.

jussij