stedolan/jq — pull request #135 by 13ren (2013-05-28 05:56:43+00:00)
Title: Locfile bugfix

SYMPTOM:
When there's a jq syntax error in a line other than the first line, an extra `\n` is printed before the line, and the carets that mark the error are offset to the right by one character.

Example of first line (no bug):
```
$ jq -n 'def aaa: aaa aaa";
def bbb: "bbb bbb"; .'
error: syntax error, unexpected IDENT
def aaa: aaa aaa";
^^^
1 compile error
```

Example of a later line (bug):
```
$ jq -n 'def aaa: "aaa aaa";
def bbb: bbb bbb"; .'
error: syntax error, unexpected IDENT

def bbb: bbb bbb"; .
^^^
1 compile error
```

CAUSE:
When `linemap` is initialized in `locfile.h`, later lines are recorded as starting at the `\n`, not where they actually start (though the first line is recorded at its actual start), resulting in latter lines being printed with the preceding `\n`, and the `\n` being counted when padding the caret markers.

CURE:
I've post facto assembled the solution into three commits because I'm not sure you will want them all. The first is a direct hack, making adjustments when printing. The second moves functions from `locfile.h` to `locfile.c` (for compilation speed - went from like 30sec to instant). The third is a proper solution. I've got brief explanatory comments here - I don't think you'll need them, but I found this bug strangely confusing, so I really did. Feel free to remove them - I think that would match the style of the codebase better. Actually, I'll add a fourth commit, without the comments.

Please let me know if having several commits like this is helpful. (I'm partly learning/playing with git rebase here). I thought the diff looked horrific with the `.h` -> `.c` change mixed in, totally obscuring the actual changes. (I had some lawyers try that on me, with Word's diff, without negotiating the substantive changes hidden within...)

BTW: I really like `git diff --color-words`, but I'm disappointed it doesn't pick up commonalities better. Pure LCS should be much better. I'm guessing they've detuned it so it's fast enough when operating per character (not per line like diff).