Highlighting styles in Options dialog

SyntaxEditor for WPF Forum

Posted 8 years ago by Kasper
Version: 11.1.0545
Avatar
Hi guys,

I have reached the point in the development of my application where I would like to provide an interface for editing the color and style of the various identifiers in the syntax languages used in my application. From the documentation and the provided quick start example, it would seem that this has been changed quite a bit in the WPF version, when compared to SyntaxEditor for WinForms. Despite having read the documentation on this at least a couple of times now, I'm not really sure that I understand this new Ambient registry, but it doesn't seem to be what I need. In your sample, there's just a big pile of "classification types", which appears to be from several languages, and yet it seems that they share some of the styles. This is not at all what I want. I would like to do things just like you did in the demo for the WinForms version, where I can select a language and THEN edit the styles for it. Unfortunately, after messing around with it for an hour or so, I'm still not sure how to do it.

I therefore hope that you can point me in the right direction here. I have a list of the syntax languages that I wish to support and a reference to their .langdef files. But how do I get their styles (besides manually parsing the XML files, which I don't suppose is the meaning)? From the documentation it seems that I need a highlighting style registry, but I'm not really sure how to get that? I would expect to be able to access this on the SyntaxLanguage instance that I initialize when loading a language, but that doesn't seem to be the case?

Also, I tried creating a test-UI for this, where I just bound a ListView to the AmbientHighlightingStyleRegistry.Instance.HighlightingStyles, since this is the only place where I know how to get a collection of styles for experimenting :). However, it doesn't seem that the HighlightingStyle instances I get, has a name or a description or any other identifier for my interface. Should I use classification types instead? I'm a bit confused on this, even after reading the documentation :)

Comments (7)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Kasper,

In the WPF design, we did it like Visual Studio where you define classification types. Classification types can be associated with tokens parsed by your language's lexer. Then the UI examines the classification type for each token and looks in the style registry (generally the AmbientHighlightingStyleRegistry instance) to see if there is a style for the classification type. If there is, then the text gets highlighted using that style.

We allow you to go either (or both) ways with how you set up your classification type to style mappings. Like in VS, we define a number of "common" classification types like Identifier, Comment, etc. and have a bunch of language reuse those. But you also can make separate classification types for each language too. If you load the Web Languages Add-on and then go to the QuickStart, you'll see a list of entries for XML like "XML Comment". It sounds like that's what you'd want to do for your languages. Then perhaps filter the items displayed so they only show the ones for your current language.

The ambient style registry is something that sits out there and is the default style registry used. It's fine to use it, even in your scenario. In the ListBox that shows the classification types from the IHighlightingStyleRegistry.ClassificationTypes property, you'd just want to filter the entries by the classification types for your selected language.

One final tip, for the "XML Comment" one for instance, we define the classification type like this so it doesn't conflict with the standard "Comment" one or any other language's comment classification type:
new ClassificationType("XmlComment", "XML Comment");


Actipro Software Support

Posted 8 years ago by Kasper
Avatar
Hi,

Thank you for the answer, but I'm still not quite sure how to do it. I don't want to limit the options to the "current language", but instead allow the user to configure all the languages that my application supports, although one language at a time :). So, I have a list of languages, but how do I access and modify each of the languages classification types?
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Kasper,

Then just do as I said and use the ambient style registry for everything but make each of your languages use their own classification types, like in my previous example where we have "XML Comment" for XML comments instead of the generic "Comment". So all the XML language classification type keys would start with "Xml" and their descriptions would all start with "XML ".

Our included Xml.langproj/langdef files are a perfect sample of this.


Actipro Software Support

Posted 8 years ago by Kasper
Avatar
Okay, I see. So I should simply filter the classification types based on their names?

Another question: I would like to show my styles much like you do in the Language designer, where I have a ListView showing each classification type along with the color, background, bold, italic and so on. This is unlike what you do in the quick start sample, where you will have to click each type to see its properties. However, because of the separation of classification and style, I'm not really sure how I can databind to it. Do you have an idea how I can show each classification type and the corresponding style within a single item? Will I have to write some wrapper code to translate back and forth or is it possible to do directly with databinding? :)
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Kasper,

For the Language Designer, we made a wrapper sort of item that we data bound to the list. The wrapper provided access to the classification type via one property and its registered style via another property.

Then in your XAML bindings, just hit the sub-properties of each property, like:
<GridViewColumn Header="Foreground" Width="100" DisplayMemberBinding="{Binding Style.Foreground}" />


Actipro Software Support

Posted 8 years ago by Kasper
Avatar
Hi,

Sorry for the late reply, but I've been focusing on other areas of my application. Now I'm looking into this again, and I have been able to achieve what I want by using a wrapper class. My next concern is saving the changes my users makes in this dialog that I'm building. With SyntaxEditor for WinForms, this was pretty easy, as far as I remember, where the changes was just saved back to the langdef file. However, it doesn't seem as obvious here, and since there doesn't seem to be anything about it in the documentation, nor is it used in the demo. So, what's the preferred way of doing this? Are there some methods on the AmbientHighlightingStyleRegistry for doing this that I just can't find? Should the changes still be saved to the langdef files or is it better to save it in my own files and format? If so, what's the best way to stuff the user defined settings into the AmbientHighlightingStyleRegistry again?

Don't get me wrong, I would be able to hack a solution that works for this, but I, and perhaps others, are looking for the preferred way of doing this, so perhaps a few lines in the documentation on it would be great? :)
Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Kasper,

Sorry but we have an import/export method still on the TODO list for WPF.

The ambient registry is really separate from the language definitions. The style data in the langdefs is more of a "default" settings sort of thing that are used to populate the ambient style registry if those styles haven't yet been initialized.

To do this on your own for now, just run through the items in the ambient style registry. Each item is a KeyValuePair<IClassificationType, IHighlightingStyle>. So serialize the classification key/description and the style options for each item. Then create new ClassificationType and HighlightingStyle objects when you load your app and call the Register method on the ambient style registry to register them.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.