vectorgraphics / asymptote

2D & 3D TeX-Aware Vector Graphics Language
https://asymptote.sourceforge.io/
GNU General Public License v3.0
549 stars 90 forks source link

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

Closed user202729 closed 3 months ago

user202729 commented 5 months ago

The error message in https://github.com/vectorgraphics/asymptote/issues/443 looks confusing because the tab is counted as 1 character by the arrow.

user202729 commented 3 months ago

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

johncbowman commented 3 months ago

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 commented 3 months ago

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.