tide icon indicating copy to clipboard operation
tide copied to clipboard

sort the edits in descending order

Open ananthakumaran opened this issue 6 years ago • 7 comments

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

ananthakumaran avatar Nov 23 '18 06:11 ananthakumaran

@jupl could you try this branch?

ananthakumaran avatar Nov 23 '18 06:11 ananthakumaran

I can still replicate the issue unfortunately. :(

jupl avatar Nov 24 '18 19:11 jupl

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 .

ananthakumaran avatar Nov 25 '18 03:11 ananthakumaran

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.

jupl avatar Nov 25 '18 08:11 jupl

Thanks for the example, it was helpful in debugging. I have added two more fixes

  1. 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
  2. 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.

ananthakumaran avatar Nov 25 '18 14:11 ananthakumaran

    /** 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

ananthakumaran avatar Nov 25 '18 14:11 ananthakumaran

@jupl could you test it once more

ananthakumaran avatar Nov 25 '18 14:11 ananthakumaran