Custom language : how to detect code duplication

Hello,
I made a plugin for a custom language on SonarQube 7.9, starting from only a grammar and a lexer.
It is working fine, the custom rules I made are correctly detected.
However, I can’t find a way to report the code duplication. Reports always show 0 duplicated lines even if I copy paste the same big classes.

From the documentation it looks like it is something standard where the number of similar tokens, lines of codes, … can be parametrized. But how does it work for custom languages ? What should be implemented in the sensor to support code duplication detection ?

Thanks in advance and kind regards,

Julian

Hi,

For CPD to work, you have to provide the list of tokens for each files that should be considered when looking for duplications. Use the method newCpdTokens() for that.
Don’t forget to replace literals by the same placeholder if you want to be able to detect duplications in similar code snippets that only differs by constant values.

See for example: