Closed jrieken closed 8 years ago
Thanks for the great input. I really appreciate it. Let me rewrite the extension with TextDocumentContentProvider
;)
@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 encodeURIComponent
ing, 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??
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.
Cool, I think I would go with the option (2). Thank you!
Done! 8d51b9f949d853b696a0474037ba0f50d46007cd
Your sample is really good to help me understand how I can utilise ContentProvider. I now want to play with other providers ;)
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