# Code sample diff highlighting in custom rules

**URL:** <https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156>\
**Category:** Writing rules\
**Created:** [September 13, 2023, 10:49am UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156 "2023-09-13T10:49:29Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Alcaraz](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nicolas_alcaraz/32/48495_2.png) [@Nicolas\_Alcaraz](https://community.sonarsource.com/u/Nicolas_Alcaraz)\
**Post date:** [September 13, 2023, 10:49am UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/1 "2023-09-13T10:49:29Z")

</div>

Hi John,

I develop my own plugin and I’d like to do the same (multiple tabs in rule description, green/red highlighting). Could you please point to a code sample?

Thank you

Regards

Nicolas

---

<div class="post-metadata">

**Author:** ![john.clifton](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/john.clifton/32/47448_2.png) [@john.clifton](https://community.sonarsource.com/u/john.clifton)\
**Post date:** [September 15, 2023, 7:46am UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/2 "2023-09-15T07:46:20Z")

</div>

Hi @Nicolas_Alcaraz,

I’m talking to the team to see if we can get some more information for you.

Best regards

John

---

<div class="post-metadata">

**Author:** ![john.clifton](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/john.clifton/32/47448_2.png) [@john.clifton](https://community.sonarsource.com/u/john.clifton)\
**Post date:** [September 20, 2023, 8:38am UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/4 "2023-09-20T08:38:19Z")

</div>

Hi Nicolas,

I should have asked this before. Which programming language are you writing a plugin for?

Best regards

John

---

<div class="post-metadata">

**Author:** ![Nicolas\_Alcaraz](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nicolas_alcaraz/32/48495_2.png) [@Nicolas\_Alcaraz](https://community.sonarsource.com/u/Nicolas_Alcaraz)\
**Post date:** [September 21, 2023, 7:45am UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/6 "2023-09-21T07:45:07Z")

</div>

Hi John,

My boss told me that I cannot publicly tell what the language is and what it is about.  
I can just tell you that it is a simple programming language developed internally for our own usage.

Anyway, it should not have any impact on the way to write rule descriptions.

I’m on the latest version of the sonar-plugin-api:

```auto
<dependency>
  <groupId>org.sonarsource.api.plugin</groupId>
  <artifactId>sonar-plugin-api</artifactId>
  <version>10.1.0.809</version>
</dependency>

```

And on SonarQube 10.2.

Thanks for your help

Regards

Nicolas

---

<div class="post-metadata">

**Author:** ![john.clifton](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/john.clifton/32/47448_2.png) [@john.clifton](https://community.sonarsource.com/u/john.clifton)\
**Post date:** [September 21, 2023, 11:30am UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/7 "2023-09-21T11:30:00Z")

</div>

Hi @Nicolas_Alcaraz ,

Apparently, [https://github.com/SonarSource/sonar-php/tree/master/php-custom-rules](https://github.com/SonarSource/sonar-php/tree/master/php-custom-rules) is a good place to start. It is an example of a simple custom plugin for PHP. It was moved from [GitHub - SonarSource/sonar-custom-rules-examples: Shows how to bootstrap a project to write custom rules for PHP, Python, Cobol, RPG](https://github.com/SonarSource/sonar-custom-rules-examples). That repo contains more examples for different languages, but they can be outdated.

Good luck!

John

---

<div class="post-metadata">

**Author:** ![Nicolas\_Alcaraz](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nicolas_alcaraz/32/48495_2.png) [@Nicolas\_Alcaraz](https://community.sonarsource.com/u/Nicolas_Alcaraz)\
**Post date:** [September 21, 2023, 2:59pm UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/8 "2023-09-21T14:59:58Z")

</div>

Hi John,

Thanks for the link. But it looks like there is no advanced rule descriptions with multiple tabs, green/red highlighting, etc.

Regards

Nicolas

---

<div class="post-metadata">

**Author:** ![john.clifton](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/john.clifton/32/47448_2.png) [@john.clifton](https://community.sonarsource.com/u/john.clifton)\
**Post date:** [October 6, 2023, 2:47pm UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/10 "2023-10-06T14:47:30Z")

</div>

Hi Nicolas,

It might be worth trying copying/pasting the contents of an [existing rule](https://github.com/SonarSource/sonar-php/blob/master/php-checks/src/main/resources/org/sonar/l10n/php/rules/php/S6600.html) with the capabilities you want over the top of the sample rule in the PHP [example](https://github.com/SonarSource/sonar-php/blob/master/php-custom-rules/src/main/resources/org/sonar/l10n/php/rules/custom/S2.html) and see if that renders as you would expect. I don’t know if this will work though. This is how that rule looks [today](https://next.sonarqube.com/sonarqube/coding_rules?q=s6600&open=php%3AS6600).

The alternative is digging into the API repo and documentation:

- [GitHub - SonarSource/sonar-plugin-api: API to develop plugins for SonarQube, SonarCloud and SonarLint](https://github.com/SonarSource/sonar-plugin-api)
- [Plugin basics](https://docs.sonarqube.org/latest/extend/developing-plugin/)

Good luck

John

---

<div class="post-metadata">

**Author:** ![Nicolas\_Alcaraz](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nicolas_alcaraz/32/48495_2.png) [@Nicolas\_Alcaraz](https://community.sonarsource.com/u/Nicolas_Alcaraz)\
**Post date:** [October 6, 2023, 7:58pm UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/11 "2023-10-06T19:58:11Z")

</div>

Hi John,

Same as [Syntax highlighting of code examples - #16 by Nicolas\_Alcaraz](https://community.sonarsource.com/t/syntax-highlighting-of-code-examples/98626/16), there must be an html processing somewhere because here’s what I get with the exact same html:

- h2 are not converted into tabs
- There’s no red/green highlighting
- Etc.

 ![image](https://europe1.discourse-cdn.com/sonarsource/uploads/sonarcommunity/original/3X/d/4/d4951f80dd8a459b39de1af1d2ce97ac989d05cc.png)

I’ll have a look at the PHP plugin. It looks like there’s an EducationRuleLoader class in sonar-analyzer-commons that might do the job.

Regards

Nicolas

---

<div class="post-metadata">

**Author:** ![Nicolas\_Alcaraz](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/nicolas_alcaraz/32/48495_2.png) [@Nicolas\_Alcaraz](https://community.sonarsource.com/u/Nicolas_Alcaraz)\
**Post date:** [October 16, 2023, 8:24pm UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/12 "2023-10-16T20:24:34Z")

</div>

I managed to achieve what I wanted to by using [RuleMetadataLoader](https://github.com/SonarSource/sonar-analyzer-commons/blob/master/commons/src/main/java/org/sonarsource/analyzer/commons/RuleMetadataLoader.java) and its [EducationRuleLoader](https://github.com/SonarSource/sonar-analyzer-commons/blob/master/commons/src/main/java/org/sonarsource/analyzer/commons/EducationRuleLoader.java).

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/sonarsource/user_avatar/community.sonarsource.com/system/32/78532_2.png) [@system](https://community.sonarsource.com/u/system)\
**Post date:** [October 23, 2023, 8:25pm UTC](https://community.sonarsource.com/t/code-sample-diff-highlighting-in-custom-rules/102156/13 "2023-10-23T20:25:06Z")

</div>

Closed on October 23, 2023, 8:25pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
