ryu1kn / vscode-partial-diff

Visual Studio Code Extension. Take a diff of 2 parts of text(s)
https://marketplace.visualstudio.com/items?itemName=ryu1kn.partial-diff
MIT License
186 stars 16 forks source link

[suggestion] use document provider API instead of temp file #2

Closed jrieken closed 8 years ago

jrieken commented 8 years ago

Instead of writing temp files you could use 'virtual' document. Create and register a TextDocumentContentProvider and use the uris of such documents for the diff command. A more complex sample with a content provider can be found here: https://github.com/Microsoft/vscode-extension-samples/tree/master/contentprovider-sample

ryu1kn commented 8 years ago

Thanks for the great input. I really appreciate it. Let me rewrite the extension with TextDocumentContentProvider ;)

ryu1kn commented 8 years ago

@jrieken

At first I thought I can embed editor.document.uri and editor.selection into my custom scheme, and from them I can retrieve the selected text inside TextDocumentContentProvider. But I actually want to capture the exact text rather than just the location of the text because, by the time a user chooses the second bit of text, they might have, with whatever reason, changed something before/in the 1st selected range.

So I embedded the selected text into uri by encodeURIComponenting, then pass 2 uris to vscode.diff command. It worked, but I'm not sure if I should go with this as I feel not right to put the entire selected text, which can be very very long depending on the kind of text a user selects.

Oh, well actually, I can introduce something like textRegistry which can store only 2 texts together with indexes, then inject it into textDocumentContentProvider. I also embed text index into my custom scheme so that textDocumentContentProvider can lookup texts from the textRegistry. This looks better to me.

Any thoughts??

jrieken commented 8 years ago

Correct - there is two ways to approach this: (1) either encode things into the uri or (2) keep an external data structure to which the uri points. I don't have clear favourite and do either or. In your case it might be easy to have two global'ish variables which keep the two strings from when the corresponding commands were run and make the content provider always use those two strings.

ryu1kn commented 8 years ago

Cool, I think I would go with the option (2). Thank you!

ryu1kn commented 8 years ago

Done! 8d51b9f949d853b696a0474037ba0f50d46007cd

Your sample is really good to help me understand how I can utilise ContentProvider. I now want to play with other providers ;)