Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Convert tab to 4 [/8] spaces in error message reporting #444

Closed
user202729 opened this issue Apr 26, 2024 · 3 comments
Closed

Comments

@user202729
Copy link

The error message in #443 looks confusing because the tab is counted as 1 character by the arrow.

@user202729
Copy link
Author

@johncbowman Are you sure that change will work correctly? Because you're not adjusting the location of the ^ sign either.

@johncbowman
Copy link
Member

I did actually test the change with just 1 space, but at the last minute decided to change the tab to 2 spaces, which
in hindsight was a mistake, as you point out.

By the way, for reasons like this, it is generally recommended not to use tab characters in code. Tabs are useful for data/code entry but you should set your editor to automatically untabify (converted them to spaces).

@user202729
Copy link
Author

user202729 commented Jun 26, 2024

Re. tab versus space, it's opinion-based (https://stackoverflow.com/questions/7728829/what-is-the-difference-between-using-tab-and-space-when-we-do-source-code-format https://stackoverflow.com/questions/120926/why-does-python-pep-8-strongly-recommend-spaces-over-tabs-for-indentation ) so I wouldn't argue on that.

Anyway, I suppose you can do something like the following

auto part_before_cursor = s.substr(0, pos.column), part_after_cursor = s.substr(pos.column);
part_before_cursor = std::regex_replace(part_before_cursor,std::regex("\t"),"    ");
part_after_cursor = std::regex_replace(part_after_cursor,std::regex("\t"),"    ");
out << string(part_before_cursor.length(), ' ') << '^';

adapted as needed (I'm not sure if the two construction of std::regex is costly etc.)

For what it's worth, I think gcc (the compiler) replaces each tab with 8 spaces for the purpose of display.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants