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?
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.