tide
tide copied to clipboard
sort the edits in descending order
fixes #288
Applying the edit instructions in reverse to file will result in correctly reformatted text.
One of the comments in TypeScript repo says the edits should be applied in reverse order. But, I am not sure all the plugins follow this? Doing a manual sort does seem to fix the tslint issue. VS code applies the fixes correctly, but I can't able to find the logic. Atom[1] also manually sorts the edits.
1: https://github.com/TypeStrong/atom-typescript/blob/master/lib/main/pluginManager.ts#L349-L352
@jupl could you try this branch?
I can still replicate the issue unfortunately. :(
Could you share a minimal project where I can reproduce the issue.
On Sun, Nov 25, 2018, 12:35 AM jupl <[email protected] wrote:
I can still replicate the issue unfortunately. :(
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ananthakumaran/tide/pull/289#issuecomment-441388911, or mute the thread https://github.com/notifications/unsubscribe-auth/AAJG9sF0Ll4WmdTIZ_ZUtXEnvU-9i9VGks5uyZhggaJpZM4YwJWo .
https://github.com/jupl/tide-thingy
I can fix the var keyword with tide-fix, but attempting to fix curly spacing with s or d gets wacky. It works as expected in VSCode.
Thanks for the example, it was helpful in debugging. I have added two more fixes
- fixes for same file are sent as two different code-edits by the plugin, I have to do a group by filename to fix this
- we apply format-region on the region where the new code-edits are applied. This was a hack to prevent code-edit messing up the format, but in your case, tide-format was adding the spaces back. I have removed the tide-format.
I am kind of not sure about merging these changes. AFAIK, these things are not thoroughly documented anywhere, so I am of not sure what is the correct fix and who is at fault here.
/** Apply each textChange in order, updating future changes to account for the text offset of previous changes. */
function forEachTextChange(changes: ReadonlyArray<ts.TextChange>, cb: (change: ts.TextChange) => void): void {
// Copy this so we don't ruin someone else's copy
changes = JSON.parse(JSON.stringify(changes));
for (let i = 0; i < changes.length; i++) {
const change = changes[i];
cb(change);
const changeDelta = change.newText.length - change.span.length;
for (let j = i + 1; j < changes.length; j++) {
if (changes[j].span.start >= change.span.start) {
changes[j].span.start += changeDelta;
}
}
}
}
from this logic it looks like applying the changes in reverse order should work, as there won't be any more span that will be affected by the current one
@jupl could you test it once more