This project is read-only.

SourceTextContainer vs. TextLoader

Topics: APIs
Jun 19, 2014 at 1:23 PM
Hi, I'm having a bit trouble understanding the Workspace API for editing SourceText (trying to make a "script editor" window to edit "plugins" in my application)

As far as I understand it, I need to associate a SourceTextContainer with a workspace document which then raises events whenever the contained text changed. So I implemented a subclass of CustomWorkspace, override OpenDocument and call OnDocumentOpened with my own SourceTextContainer.

The problem is that I receive the SourceText from the standard FileTextLoader, so the SourceText is not linked to my SourceTextContainer. Overriding FileTextLoader allows me to return my own SourceText, but I can't seem to construct a SourceText with auto-detected encoding AND my custom SourceTextConainer, so what am I supposed to do? I probably could make a wrapper SourceText which redirects to my own SourceTextContainer and forwards everything else to the TextLoader SourceText, but is that how it's supposed to work?
Jun 21, 2014 at 3:47 AM
The default SourceText created by FileTextLoader does not support the editing scenario. We have another implementation of SourceText that is a wrapper over Visual Studio ITextBuffer API. This is seeded into the workspace by the host when a document is opened. You should be able to implement your own SourceText and SourceTextContainer pair that supports editing. Wrapping the default SourceText would work, but not be very performant because it simply wraps a string.
Marked as answer by Zarat on 6/21/2014 at 3:19 AM
Jun 21, 2014 at 11:19 AM
Thanks, I get the idea. Maybe it would be useful to provide encoding detection as a service, so not everyone has to reimplement it.
Jun 23, 2014 at 12:10 AM
Shouldn't all C# soure code be in Unicode?
Jun 23, 2014 at 2:23 PM
Edited Jun 23, 2014 at 2:26 PM
Even Unicode has multiple file encodings you need to be able to autodetect. My main concern is not so much about the work involved implementing it myself, but more that my editor autodetection may differ from the Roslyn autodetection when invoked outside the editor.
Jun 23, 2014 at 9:53 PM
Edited Jun 23, 2014 at 9:54 PM
PauloMorgado wrote:
Shouldn't all C# soure code be in Unicode?
Unfortunately Visual Studio does not use Unicode encoding until it has to (and you need to have the Save documents as Unicode when data connect be saved in codepage in Environment | Documents checked). That is really annoying when different people on the team use systems with different encodings.