src/Misc/EFTools/EntityDesignPackage/CustomCode/MicrosoftDataEntityDesignDocData.cs
/// <summary>
///     Creates an instance of a VSTextBuffer, sets the LanguageService SID, and uses IVsPersistDocData to fire loading of the doc data.
///     We also start broadcasting a FileNameChanged event if the buffer gets renamed. *note* this method also gets called on reload.
/// </summary>
Creates an instance of a VSTextBuffer, sets the LanguageService SID, and uses IVsPersistDocData to fire loading of the doc data.
We also start broadcasting a FileNameChanged event if the buffer gets renamed. *note* this method also gets called on reload.
Creates an instance of a VSTextBuffer sets the LanguageService SID and uses IVsPersistDocData to fire loading of the doc data . We also start broadcasting a FileNameChanged event if the buffer gets renamed . * note * this method also gets called on reload .
[SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Microsoft.VisualStudio.Shell.Interop.IVsPersistDocData.LoadDocData(System.String)")]
        [SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling")]
        public bool CreateAndLoadBuffer()
        {
            DestroyBuffer();
            var pkg = PackageManager.Package as Package;
            IServiceProvider serviceProvider = pkg;
            var textLinesType = typeof(VSTextManagerInterop.IVsTextLines);
            var riid = textLinesType.GUID;
            var clsid = typeof(VSTextManagerInterop.VsTextBufferClass).GUID;
            _underlyingBuffer = pkg.CreateInstance(ref clsid, ref riid, textLinesType);
            Debug.Assert(_underlyingBuffer != null, "Failure while creating buffer.");
            var buffer = _underlyingBuffer as VSTextManagerInterop.IVsTextLines;
            Debug.Assert(buffer != null, "Why does buffer not implement IVsTextLines?");
            var ows = buffer as IObjectWithSite;
            if (ows != null)
            {
                ows.SetSite(serviceProvider.GetService(typeof(IOleServiceProvider)));
            }
            var userData = buffer as VSTextManagerInterop.IVsUserData;
            if (userData != null)
            {
                var VsBufferDetectLangSID = new Guid("{17F375AC-C814-11d1-88AD-0000F87579D2}"); 
                VSErrorHandler.ThrowOnFailure(userData.SetData(ref VsBufferDetectLangSID, false));
            }
            var langSid = CommonPackageConstants.xmlEditorLanguageService;
            VSErrorHandler.ThrowOnFailure(buffer.SetLanguageServiceID(ref langSid));
            var persistDocData = buffer as IVsPersistDocData;
            if (persistDocData != null)
            {
                persistDocData.LoadDocData(FileName);
                var artifactUri = new Uri(FileName);
                var artifact = PackageManager.Package.ModelManager.GetArtifact(artifactUri);
                if (artifact != null
                    && artifact.IsCodeGenArtifact)
                {
                    var standaloneProvider = artifact.XmlModelProvider as StandaloneXmlModelProvider;
                    if (standaloneProvider.ExtensionErrors == null
                        || standaloneProvider.ExtensionErrors.Count == 0)
                    {
                        var projectItem = VsUtils.GetProjectItem(Hierarchy, ItemId);
                        if (projectItem != null)
                        {
                            if (VSHelpers.CheckOutFilesIfEditable(ServiceProvider, new[] { FileName }))
                            {
                                string artifactText = null;
                                using (var writer = new Utf8StringWriter())
                                {
                                    artifact.XDocument.Save(writer, SaveOptions.None);
                                    artifactText = writer.ToString();
                                }
                                if (!String.IsNullOrWhiteSpace(artifactText))
                                {
                                    VsUtils.SetTextForVsTextLines(VsBuffer, artifactText);
                                }
                            }
                            else
                            {
                                ErrorListHelper.LogExtensionErrors(
                                    new List<ExtensionError>
                                        {
                                            new ExtensionError(
                                                string.Format(
                                                    CultureInfo.CurrentCulture, Resources.ExtensionError_SourceControlLock,
                                                    Path.GetFileName(FileName)),
                                                ErrorCodes.ExtensionsError_BufferNotEditable,
                                                ExtensionErrorSeverity.Error)
                                        },
                                    projectItem);
                            }
                        }
                        PackageManager.Package.ModelManager.ClearArtifact(artifactUri);
                    }
                    else
                    {
                        PackageManager.Package.ModelManager.ClearArtifact(artifactUri);
                        DispatchLoadToExtensions();
                    }
                }
                else
                {
                    DispatchLoadToExtensions();
                }
            }
            FileNameChanged += OnFileNameChanged;
            RegisterUndoTracking();
            return true;
        }
[ SuppressMessage ( " " , " " , MessageId = " " ) ] [ SuppressMessage ( " " , " " ) ] public bool CreateAndLoadBuffer ( ) { DestroyBuffer ( ) ; var pkg = PackageManager . Package as Package ; IServiceProvider serviceProvider = pkg ; var textLinesType = typeof ( VSTextManagerInterop . IVsTextLines ) ; var riid = textLinesType . GUID ; var clsid = typeof ( VSTextManagerInterop . VsTextBufferClass ) . GUID ; _underlyingBuffer = pkg . CreateInstance ( ref clsid , ref riid , textLinesType ) ; Debug . Assert ( _underlyingBuffer != null , " " ) ; var buffer = _underlyingBuffer as VSTextManagerInterop . IVsTextLines ; Debug . Assert ( buffer != null , " " ) ; var ows = buffer as IObjectWithSite ; if ( ows != null ) { ows . SetSite ( serviceProvider . GetService ( typeof ( IOleServiceProvider ) ) ) ; } var userData = buffer as VSTextManagerInterop . IVsUserData ; if ( userData != null ) { var VsBufferDetectLangSID = new Guid ( " " ) ; VSErrorHandler . ThrowOnFailure ( userData . SetData ( ref VsBufferDetectLangSID , false ) ) ; } var langSid = CommonPackageConstants . xmlEditorLanguageService ; VSErrorHandler . ThrowOnFailure ( buffer . SetLanguageServiceID ( ref langSid ) ) ; var persistDocData = buffer as IVsPersistDocData ; if ( persistDocData != null ) { persistDocData . LoadDocData ( FileName ) ; var artifactUri = new Uri ( FileName ) ; var artifact = PackageManager . Package . ModelManager . GetArtifact ( artifactUri ) ; if ( artifact != null && artifact . IsCodeGenArtifact ) { var standaloneProvider = artifact . XmlModelProvider as StandaloneXmlModelProvider ; if ( standaloneProvider . ExtensionErrors == null || standaloneProvider . ExtensionErrors . Count == 0 ) { var projectItem = VsUtils . GetProjectItem ( Hierarchy , ItemId ) ; if ( projectItem != null ) { if ( VSHelpers . CheckOutFilesIfEditable ( ServiceProvider , new [ ] { FileName } ) ) { string artifactText = null ; using ( var writer = new Utf8StringWriter ( ) ) { artifact . XDocument . Save ( writer , SaveOptions . None ) ; artifactText = writer . ToString ( ) ; } if ( ! String . IsNullOrWhiteSpace ( artifactText ) ) { VsUtils . SetTextForVsTextLines ( VsBuffer , artifactText ) ; } } else { ErrorListHelper . LogExtensionErrors ( new List < ExtensionError > { new ExtensionError ( string . Format ( CultureInfo . CurrentCulture , Resources . ExtensionError_SourceControlLock , Path . GetFileName ( FileName ) ) , ErrorCodes . ExtensionsError_BufferNotEditable , ExtensionErrorSeverity . Error ) } , projectItem ) ; } } PackageManager . Package . ModelManager . ClearArtifact ( artifactUri ) ; } else { PackageManager . Package . ModelManager . ClearArtifact ( artifactUri ) ; DispatchLoadToExtensions ( ) ; } } else { DispatchLoadToExtensions ( ) ; } } FileNameChanged += OnFileNameChanged ; RegisterUndoTracking ( ) ; return true ; }
Creates an instance of a VSTextBuffer, sets the LanguageService SID, and uses IVsPersistDocData to fire loading of the doc data.
Creates an instance of a VSTextBuffer sets the LanguageService SID and uses IVsPersistDocData to fire loading of the doc data .
// We want to set the LanguageService SID explicitly to the XML Language Service. // We need turn off GUID_VsBufferDetectLangSID before calling LoadDocData so that the  // TextBuffer does not do the work to detect the LanguageService SID from the file extension. //GUID_VsBufferDetectLangSID; // If there is a cached code gen artifact, it will have loaded its text buffer using extensions already. // Therefore we can grab the text buffer from that artifact for our docdata buffer, and dispose the // code gen artifact since it's using a XmlProvider that is standalone and won't be supported by the // designer. // If the extensions ran into errors whilst loading, we'll need to re-run extensions anyway so we ignore the cache // DSL exposes the FileNameChanged event which we subscribe to so we can update the moniker inside // the text buffer, which is required to update our model as well as keep the XmlModel in sync.