Highlighting rule not fully applied #223

Open
opened 2020-02-26 07:52:13 -06:00 by sergiodebst · 2 comments
sergiodebst commented 2020-02-26 07:52:13 -06:00 (Migrated from github.com)

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.

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](https://user-images.githubusercontent.com/5172886/75348769-b9e61a80-58a3-11ea-94f5-c710ef8ee8f4.png) 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.
siegfriedpammer commented 2021-03-08 10:44:35 -06:00 (Migrated from github.com)

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!

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!
sergiodebst commented 2021-03-08 11:45:43 -06:00 (Migrated from github.com)

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.

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.
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-icsharpcode/AvalonEdit#223
No description provided.