AvalonEdit icon indicating copy to clipboard operation
AvalonEdit copied to clipboard

Highlighting rule not fully applied

Open sergiodebst opened this issue 4 years ago • 2 comments

Hello, I'm editing your xshd for C# https://github.com/icsharpcode/AvalonEdit/blob/master/ICSharpCode.AvalonEdit/Highlighting/Resources/CSharp-Mode.xshd in order to make it look like Visual Studio, and this is what I have by now: https://pastebin.com/xN9yx0Yj

And using this code to test it https://pastebin.com/J5CTpNCu

Currently it is displayed in this way: imagen

As I wrote in the image there are some things that I expected to be colorized and it doesn't.

  • The first one are the class inheritance or implementations when there are more than once. The rule I wrote to accomplish this is this one:
<!-- The type inheritions or implementations -->
<Rule color="ClassName">
	(?&lt;=(class|interface)\s+[\d\w_]*\s*\:[\d\w_\,\s|.]*)
	[\d\w_]+
	(?=\s*($|\,|\{))
</Rule>

I have tested this rule here https://bit.ly/2SYf9EG and seems to work as I expect, but in the editor it is like is stoping the highlighting in the first match (Attribute in this case).

  • The second one is a generic type parameter. The rule is this:
<!-- The type parameters of a generic class: List<MyClass> -->
<Rule color="ClassName">
	(?&lt;=&lt;[\d\w_\,\.\s&lt;&gt;]*)	#starts with &lt;
	[\d\w_]+
	(?=(\[\])?(&gt;|\s*\,[\s\.\d\w\,\[\]&lt;&gt;]*&gt;))		#ends with &gt;
</Rule>

And again in the test https://bit.ly/3a86xBm seems to be working as I would expect, and I thing the problem is the same than the previous one, it is stopping at the first occurrence.

sergiodebst avatar Feb 26 '20 13:02 sergiodebst

The fundamental problem you are hitting here is that you try to implement semantic highlighting through the use of regular expressions. This is generally not possible and Visual Studio and any other decent editor uses a parser and semantic analysis to detect whether an identifier in the editor refers to a type or member of a type. You will have to implement a custom DocumentColorizingTransformer which uses data provided by a parser etc. I recommend taking a look at Roslyn https://github.com/dotnet/roslyn, which provides a parser and type system. Hope this helps!

siegfriedpammer avatar Mar 08 '21 16:03 siegfriedpammer

Thank you for your advice, but without having read too much about Roslyn I always thought that in order to make it work I need to pass the entire project to evaluate or parse all types and methods, and that's probably a lot of processing work that I would prefer to avoid for the use I am going to give it (only display a piece of code from time to time), I don't need the highlighting to be perfect neither exactly equal as Visual Studio but I don't mind to spend some time thinking about some semantic rules that are projectable as regular expressions. When I wrote the issue I was wondering that since the regular expression seems to be correct (in the tester recognizes what I want), if maybe there is a bug or a property incorrectly set that it is preventing the highlight to work as I want. Thanks again.

sergiodebst avatar Mar 08 '21 17:03 sergiodebst