Skip to content

In-place minify replaces stale offsets after the async request: editing the buffer meanwhile corrupts the text #1

Description

@dpc00

Summary

ToptalMinifierReplaceCommand captures the selection offsets (start, end) before the asynchronous API request and applies the result at those same offsets when the response arrives (toptal_minifier_apply_replace). If the buffer changes while the request is in flight (typing, another plugin, undo, an external reload), the minified text is written over whatever now occupies those offsets, corrupting the file, and the original selection stays untouched.

Repro (ToptalMinifier, Sublime Text 4200; network replaced by a stub that returns MIN after 1.5 s, using the package's own command class and its real toptal_minifier_apply_replace command)

Buffer alpha\nbeta\ngamma\n, select beta, run the in-place replace command; while the request is pending, insert a line at the top of the buffer.

Result after the response:

'// heaMINline added meanwhile\nalpha\nbeta\ngamma\n'

MIN replaced 4 characters at offsets 6-10 of the new text (inside the freshly typed header line) and beta was never replaced.

Suggested fix

Record view.change_count() before the request and, in apply_result, refuse to apply (show "buffer changed, minified result discarded/copied to clipboard") if it changed; or track the region with a view.add_regions key (regions follow edits) and replace that region instead of stored offsets.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions