views:

1378

answers:

4

Last year I wrote a Language Service for Visual Studio which added syntax highlighting for NHaml files: http://github.com/snappycode/hamleditor.

To clarify, NHaml is a html template language that can mix in code elements like an aspx file can. This plugin adds support to the IDE for editing NHaml files, but basically only adds syntax highlighting.

I was wondering if anyone knows how to add inline c# intellisense to the service like you get now in an aspx file. I'm hoping that would be possible without doing the whole c# grammar myself specific for the plugin.

Has anyone written a language service that mixes languages?

UPDATE: It looks like the spark view engine guys have made some inroads here, I am investigating their implementation

A: 

You can easily add keywords by creating or modifying a usertype.dat file. Check here for some directions on attaching to specific file extentions. That might get you at least part of the way, without redoing the complete c# syntax.

(In fact, I'm not sure what you mean exactly by 'syntax highlighting' in this context. I'm sure, for instance, you get brace-match highlighting for free in the editor).

Ofek Shilon
The context is he has developed a plugin for Visual Studio that provides syntax highlighting (pretty colours), to provide brace-matching you have to parse the code, and tell visually studio where the brace pair/tupples are.
Simeon Pilgrim
+1  A: 

this looks like it might help

http://www.codeproject.com/KB/recipes/VSLanguageService.aspx

Simon
+2  A: 

I checked the Spark View Engine, and they seem to have made a generic ATL stuff (called SparkLanguagePackageLib), that in fact seems to be not containiag anything Spark specific. It seems to be just a generic C# intellisense library that needs the following:

  • The original code
  • The C# source that gets generated from the original code
  • The position mappings between the two (for example the code on line 2 pos 5 gets mapped in the output to line 4 pos 10, etc.)
  • Some other things, like Paintings(?)

And after that you can call:

events.OnGenerated(
    primaryText, // original source code
    entry.SourceCode, // generated sourcecode
    cMappings, // mappings between the two
    ref mappings[0], // ?
    cPaints, // ?
    ref paints[0]); // ?

I've tried to find Spark-specific stuff in that C++ library, but I couldn't find anything: everythig spark-related is split to a separate C# code file. I think this is good, because:

  • You don't need to edit the C++ files
  • If the spark view engine's intellisense support is installed it can be used by other view engines too
  • You only need to create a class, that maps between the original nhaml file and it's generated C# counterpart.

Btw. Are you still working on this NHaml Intellisense library? If not I'll try to patch their implementation in hope it can be converted to NHaml easily.

SztupY
Any updates on this? has anyone successfully ported the spark class to nhaml ?
Martijn Laarman
cMapping is the size argument for the mappings array, the ref [0] is address-of-array to interop. Same with cPaints and paints. Mapping is exactly right - it's an array of substring offset pairs for each little window where code in the template show through as code in the generated file.Paint is an array of substring offsets and color types in the original source. That information is used to colorize the rest of the non-code text.To be honest the biggest pain was reworking the parse/gen code to carry forward and capture all of the input/output text offsets.
loudej
@loudej: Thanks for the clarification! @Martijn: I already managed to rewrite the stuff to NHaml, but unfortunately NHaml is using an alternative parsing/code generation method than Spark, so I have to learn how it works. I don't want to bloat it with too much stuff.
SztupY
+1  A: 

I finally managed to modify the code to support NHaml. It wasn't that hard at all. Unfortunately the original NHaml library doesn't support everything that was needed, so I had to create a new parser for NHaml. It doesn't support all of the constructs, but it supports most of them (enough to make NHaml programming easier)

Download: http://github.com/sztupy/nhamlsense

Screencast: http://www.youtube.com/watch?v=8jTZ2zC9eYc

SztupY
Awesome work SztupY!
Dave Newman