Syntax Highlighting of an Embedded Language in Visual Studio 2012 - c#

I'm using C# and Visual Studio 2012 to implement syntax highlighting of a language embedded in html (much like ASP code inside <% %>). So far, I've found out that I need to use IProjectionBuffer to achieve this, but there are no examples of how to do this using MEF, apart from a somewhat vague description here: http://msdn.microsoft.com/en-us/library/dd885240.aspx#projection. I tried to assign spans of text from the ITextBuffer that is "imported" through MEF to an IProjectionBuffer, but it had no effect.
Does anybody have any ideas as to how to proceed?

There is a built in feature for this purpose I think. I used to deal with text highlighting in C# code with MEF with tags. I think you shold start with Text.Classification.
There is a lack of documentation sadly, but I think you should build a lexer or somthing similar for the lingual part MSDN:DLR (features) may can help you,
The DLR allows language implementers to avoid creating lexical
analyzers, parsers, semantic analyzers, code generators, and other
tools that they traditionally had to create themselves.
detect the delimiters, and color it through classification.
Here is an example project to customize C# code displaying.

Related

How / Why can visual studio give suggestions / hints while making a Regex string?

First, a little context as to what I mean. I've recently started learning some basics of Regex and wanted to try it in .NET. In Visual Studio 2019, while working in C#, I can make a Regex object. I was astonished and amazed when I saw that Visual Studio is actually giving me hints as to the syntax of the "Regex language" (so to speak), which it will also color appropriately. Here is a picture of what I mean:
With that in mind, my questions is: Does this happen just because it is built-in to Visual Studio, to show this when editing this particular argument of Regex? Or, if not, how and where exactly is it stated how it should give hints for the string? Is it some sort of advanced <Summary> like tagging?
Can I, for example, implement something like that for my classes so that custom hints are given out when making strings in my classes' methods?
Actually, what you saw is all the Intellisense that VS provides for Regex expressions.
And Intellisense that VS itself provides to the Regex expression is limited. And those are all.
Besides, Intellisense is a built-in tool of VS and we cannot get an easy way to change the build-in tool of VS unless you write your own vs extension that extends the Intellisense.
As a suggestion, you can search on the VS Marketplace to search if anyone has published such extension.
I have found that Resharper extension(It is a paid extension but the new user can use 30 days for free) has a powerful feature for Regex expressions.
See this document. It provides the better Intellisense and also has the feature to verify the regular expression.
In addition, if these still do not meet your requirements and you want to add some custom features, I suggest you could suggest a feature about your ideas to the DC Forum.
Also, you can share the link here and anyone including us will vote it so that it will get more Microsoft's attention.

Visual Studio intellisense and debug only in certain areas

I wrote a simple web-server that interprets c# inside HTML in real time (Like Razor, but it looks more like PHP style tags, just written in C#). Every C# code snippet is wrapped with <# and #>.
Now, I want to start writing a simple web application using it. Working with Notepad++ is painfully annoying, due to the fact it cannot really autocomplete C# well, and C# is a strict language (unlike PHP, Javascript or Python, in which developing in Atom or NP++ is extremely easy and natural).
So, the question:
I wanted to know if there is any way that I can edit those pages in Visual Studio, have all the autocomplete functionality, and in the same time let VS know that I want it to notice (colorize, syntax-check, etc) only code snippets wrapped in <# and #>.
How do I do that?
Visual studio 2015
Can be web developer or desktop version, I can any of them.
Thanks!
Visual Studio has a very powerful extension SDK, so you can achieve what you want by developing one or more extensions. If you're new to extension development, I'd suggest you take a look at this getting started guide first.
Microsoft has quite a few samples available on GitHub. At least the following two samples should be of interest to you.
Syntax coloring: Diff Classifier Sample
Custom programming language support: Ook Language Integration Sample
The Ook sample goes a bit further than what you actually need, but it has some elements that are usable to you.

how to use C# compiler to analyze text in a textbox? (possible lexical analysis)

Im writing a simple code editor just like visual studio code editor.
btw i have a textbox and wanna know how can i underline syntactical errors?
i`v searched the web, but did not find something usefull. any one got any refernce of documentation for C# compiler to help me in this topic?
thanks all and sorry for my bad english..
If you're doing this for a real-world scenario, there are good commercial options available. You need to look at using the CodeDom classes which can take your input text and convert it into an object model which can then be compiled into an assembly.
If there are compiler errors, they will be returned as a collection of CompilerError objects. These will tell you the line number, character number, and error message, so you can display the error.
There are significant drawbacks to using CodeDom, however. It is older technology and has not been updated to keep pace with the language changes so there are limitations in what it can parse. If you want to write your own parser, you need to look at language tools like lexx and yacc or Roslyn.
With a simple textbox control you certainly cannot do that. At least you need to use a RichTextBox control (comes in a variety of flavors, for WPF, WinForms, from Office...), to do some code formatting. As a first exercise, I would suggest you try to color keywords in your editor. That's a fairly easy task (and error recovery is not a problem) and may show you how to use the control.

ANTLR Syntax Highlighting DSL in Visual Studio

I have an ANTLR grammar that defines a DSL (domain specific language). This grammar is relatively simple. It is parsing the language and outputting C code to create a very basic translator.
This language is meant to be used in C# application (typed into some sort of control, whether it be RichTextBox or a custom control) and one requirement is to have syntax highlighting for this language. I have scoured the Internet in hopes of finding some sort of information on how to accomplish this, or find a tool to make this a little easier on myself.
After not finding too much information, my best assumption would be that I need to use the ANTLR generated lexer to look at the tokens and color them accordingly. Is this the correct path of action, or is there some other method/tool to provide syntax highlighting for custom domain specific languages? If this is the correct method, how do I go about recognizing specific tokens?
If I left out any important information, please ask! Thanks!
I successfully used AvalonEdit for a similar project of mine. I just created a small editor with the correct syntax highlighting.
It is very easy and quick to get it up and running in your project. You just have to provide it with a simple XML file to document the syntax of your DSL and you will have a colored output out-of-the-box as a WPF control.
It looks like they added completion facilities since I used it, I don't have experience on that part though, but I suspect it is also very well done if the quality is the same as the colouring.
This language is meant to be used in C# application (typed into some sort of control, whether it be RichTextBox or a custom control) and one requirement is to have syntax highlighting for this language.
Consider using Scintilla for your control. It's a text control for IDE-style text editing. Notepad++ uses it for its text control, as does the SciTE IDE from which it originates. I've used it in a small, custom IDE project written in C# using an unofficial .NET-specific version -- I think it was ScintillaNET.
Scintilla supports custom keyword highlighting and also a variety of programmable features like squiggly-line underlining and things like that.
If you have a control that you'd rather use, I think it's reasonable to use a small ANTLR lexer to produce tokens for you. Each token contains the line number, starting character position, source text, and token type -- everything you'd need to know what to highlight and how. The only hassle would be running the text through the lexer each time the text is changed. There are efficient ways to do that without re-lexing the entire document, but it's still something to keep in mind.

How can I create a Visual Studio syntax highligher for a C-like language

I want to have simple syntax highlighting when I am editing code files of a C like language. I know I can just pick C++ syntax highlighting but the language has a lot of keywords similar to HLSL and I want to color them too.
Do I need to write an VS extension for this? Or is this just a matter of proving a VS specific text file that lists keywords, operators, comments, etc?
Where can I find such examples to use as a base?
you can find a few examples on codeplex http://www.codeplex.com/site/search?query=highlight&ac=3. Specifically one of the plugins is for HSLS, GLSL, and CG (http://nshader.codeplex.com/)

Categories