Skip to content

Text format: Annotation Caret misplaced on lines with tabs #4018

@MichaReiser

Description

@MichaReiser

The TextEmitter misplaces the position caret ^ if the output string contains any tab characters.

/home/micha/astral/test/test.py:4:92: E501 Line too long (90 > 88 characters)
  |
4 |     def bind_keys(self):
5 | 		for key in self.operations:
6 | 			self.window.bind(key, lambda event, operator=key: self.append_operator_too_long(operator))
  |                                                                                         ^^ E501
  |

Found 1 error.

I assume this is because the rendered width of the tab character depends on the terminal.

Proposed solution

Replace tabs (and other invisible characters) before rendering. See https://github.com/rome/tools/blob/af2cfbe370f29baea0277c3c0814f108e22af54e/crates/rome_diagnostics/src/display/frame.rs#L394-L406 for inspiration (Rome also uses a heuristic to decide when to show invisible characters).

Reproduction

Input file

class Test:
    def bind_keys(self):
		for key in self.operations:
			self.window.bind(key, lambda event, operator=key: self.append_operator_too_long(operator))
cargo run --bin ruff -- check --no-cache --show-source --select E501 ../test/test.py --isolated

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions