Skip to content

[core] Make renderers have access to the context of violations #3904

@oowekyala

Description

@oowekyala

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    an:enhancementAn improvement on existing features / rules

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions