Is your feature request related to a problem? Please describe.
Several Renderers want to show the context of a violation (couple of lines around the line where the violation is reported). This is really nice to help fix them (see #3349)
This is currently implemented ad-hoc, see eg
|
try (BufferedReader br = new BufferedReader(getReader(sourceFile))) { |
Here the TextColor renderer literally re-reads the file to fetch the relevant lines. The problems
- The file has already been read since it was parsed.
- The file will be read as many times as there are violations in the file
- The file name field of a RuleViolation may not be a file path at all, it could be a URL or a purely symbolic name. This would cause errors in the renderer
- Every renderer that wants access to a code snippet needs to implement its own logic to do this
Describe the solution you'd like
Find a way to make the TextDocument (#3784) available to renderers before it is closed and we move on to the next file.
Describe alternatives you've considered
#3354 is an attempt to address this issue, but I'm not happy with the implementation. I don't want the snippet to be part of the RuleViolation, as many renderers don't care about it. If it is part of the violation, we also have to write it into the violation cache, which seems wasteful.
Additional context
Is your feature request related to a problem? Please describe.
Several Renderers want to show the context of a violation (couple of lines around the line where the violation is reported). This is really nice to help fix them (see #3349)
This is currently implemented ad-hoc, see eg
pmd/pmd-core/src/main/java/net/sourceforge/pmd/renderers/TextColorRenderer.java
Line 247 in 05d5bea
Here the TextColor renderer literally re-reads the file to fetch the relevant lines. The problems
Describe the solution you'd like
Find a way to make the TextDocument (#3784) available to renderers before it is closed and we move on to the next file.
Describe alternatives you've considered
#3354 is an attempt to address this issue, but I'm not happy with the implementation. I don't want the snippet to be part of the RuleViolation, as many renderers don't care about it. If it is part of the violation, we also have to write it into the violation cache, which seems wasteful.
Additional context