private void DispatchLoadToExtensions() { if (Hierarchy != null) { var projectItem = VsUtils.GetProjectItem(Hierarchy, ItemId); if (projectItem != null) { var fileContents = VSHelpers.GetTextFromVsTextLines(VsBuffer); string newBufferContents; List <ExtensionError> extensionErrors; if (StandaloneXmlModelProvider.TryGetBufferViaExtensions( projectItem, fileContents, out newBufferContents, out extensionErrors)) { if (VSHelpers.CheckOutFilesIfEditable(ServiceProvider, new[] { FileName })) { VsUtils.SetTextForVsTextLines(VsBuffer, newBufferContents); } else { ErrorListHelper.LogExtensionErrors( new List <ExtensionError> { new ExtensionError( string.Format( CultureInfo.CurrentCulture, Resources.ExtensionError_SourceControlLock, Path.GetFileName(FileName)), ErrorCodes.ExtensionsError_BufferNotEditable, ExtensionErrorSeverity.Error) }, projectItem); } } } } }
public override bool CanEditXmlModel() { string moniker = null; // first attempt to extract the file path of this XML Model try { if (Uri != null) { moniker = Uri.LocalPath; } } catch (Exception e) { Debug.Fail("Could not parse the URI of this XML model because of the exception: " + e.Message); // note we get out of here quickly if this happens as we don't want to allow any cases where somehow documents.Count > 0 return(false); } if (!String.IsNullOrWhiteSpace(moniker)) { return(VSHelpers.CheckOutFilesIfEditable(_serviceProvider, new[] { moniker })); } return(false); }
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))); } // 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. var userData = buffer as VSTextManagerInterop.IVsUserData; if (userData != null) { var VsBufferDetectLangSID = new Guid("{17F375AC-C814-11d1-88AD-0000F87579D2}"); //GUID_VsBufferDetectLangSID; 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) { // 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. 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 { // If the extensions ran into errors whilst loading, we'll need to re-run extensions anyway so we ignore the cache PackageManager.Package.ModelManager.ClearArtifact(artifactUri); DispatchLoadToExtensions(); } } else { DispatchLoadToExtensions(); } } // 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. FileNameChanged += OnFileNameChanged; RegisterUndoTracking(); return(true); }