9

मैं वीएस2017 (सी # में) के लिए एक विस्तार VSPackage बनाने की कोशिश कर रहा हूं जो बाइनरी डेटा को एक्सएमएल में परिवर्तित कर देगा, डिफ़ॉल्ट रूप से डिफ़ॉल्ट वीएस एक्सएमएल संपादक और एक्सएमएल भाषा सेवा में खुलता है, और फिर इसे सहेजने पर बाइनरी में बदल देता है।वीएसएक्स: एक्सएमएल में परिवर्तित बाइनरी फाइलों को संभालने के लिए मैं मौजूदा एक्सएमएल संपादक का पुन: उपयोग कैसे कर सकता हूं?

हालांकि, मुझे यह पता लगाने में परेशानी है कि इसके लिए कौन से कदमों की आवश्यकता होगी।

  • बनाएं नया पाठ बफर
  • परिवर्तित XML डेटा
  • कोर संपादक बनाएं साथ यह फीड
  • पाठ के साथ यह फ़ीड: मैं अब के लिए निम्नलिखित जब संपादक कारखाने में एक नए संपादक बनाने के बारे में सोचा बफ़र

अभी मेरा प्रयास इस तरह दिखता है:

private MyPackage _package; // Filled via constructor 
private IServiceProvider _serviceProvider; // Filled via SetSite 

public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, 
    IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, 
    out IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW) 
{ 
    // Initialize and validate parameters. 
    ppunkDocView = IntPtr.Zero; 
    ppunkDocData = IntPtr.Zero; 
    pbstrEditorCaption = String.Empty; 
    pguidCmdUI = Guid.Empty; 
    pgrfCDW = 0; 
    VSConstants.CEF createDocFlags = (VSConstants.CEF)grfCreateDoc; 
    if (!createDocFlags.HasFlag(VSConstants.CEF.OpenFile) && !createDocFlags.HasFlag(VSConstants.CEF.Silent)) 
     return VSConstants.E_INVALIDARG; 
    if (punkDocDataExisting != IntPtr.Zero) 
     return VSConstants.VS_E_INCOMPATIBLEDOCDATA; 

    // Create a sited IVsTextBuffer storing the converted data with the XML data and language service set. 
    IVsTextLines textLines = _package.CreateComInstance<VsTextBufferClass, IVsTextLines>(); 
    SiteObject(textLines); 
    string xmlText = BinaryXmlData.GetXmlString(pszMkDocument); 
    textLines.InitializeContent(xmlText, xmlText.Length); 
    ErrorHandler.ThrowOnFailure(textLines.SetLanguageServiceID(ref Guids.XmlLanguageServiceGuid)); 

    // Instantiate a sited IVsCodeWindow and feed it with the text buffer. 
    IVsCodeWindow codeWindow = _package.CreateComInstance<VsCodeWindowClass, IVsCodeWindow>(); 
    SiteObject(codeWindow); 
    codeWindow.SetBuffer(textLines); 

    // Return the created instances to the caller. 
    ppunkDocView = Marshal.GetIUnknownForObject(codeWindow); 
    ppunkDocData = Marshal.GetIUnknownForObject(textLines); 

    return VSConstants.S_OK; 
} 

private void SiteObject(object obj) 
{ 
    (obj as IObjectWithSite)?.SetSite(_serviceProvider); 
} 

// --- CreateComInstance is a method on my package ---- 
internal TInterface CreateComInstance<TClass, TInterface>() 
{ 
    Guid guidT = typeof(TClass).GUID; 
    Guid guidInterface = typeof(TInterface).GUID; 

    TInterface instance = (TInterface)CreateInstance(ref guidT, ref guidInterface, typeof(TInterface)); 
    if (instance == null) 
     throw new COMException($"Could not instantiate {typeof(TClass).Name}/{typeof(TInterface).Name}."); 

    return instance; 
} 

जब मैं अपने संपादक के साथ फ़ाइल को स्पष्ट रूप से खोलने का प्रयास करता हूं, तो यह "" चयनित संपादक के साथ फ़ाइल खोला नहीं जा सकता है। कृपया एक और संपादक चुनें। "संदेश मुझे समझ में नहीं आता है, मैंने XML संपादक के साथ XML डेटा खोलने का प्रयास किया है, लेकिन यह अभी भी बाइनरी डेटा के साथ एक टेक्स्ट एडिटर खोलने का प्रयास करता है।

मैं यहां फंस गया हूं, मैंने किया सब मैं के बारे में सोच सकता है यह डेटा परिवर्तित को खिलाने के लिए। जाहिर है इस तरह से सही नहीं है।

  • मैं बाइनरी डेटा लाने के लिए, जल्दी से यह XML में बदलने का है, तो एक्सएमएल करने के लिए इसे फ़ीड inbetween चरणों कैसे जोड़ सकता है संपादक?
  • एक्सएमएल संपादक फ़ाइल सहेजता है जब मैं इसे बाइनरी के रूप में वापस कैसे स्टोर करूं?
  • क्या एक्सएमएल का पुन: उपयोग करना संभव है इसके लिए संपादक और भाषा सेवाएं?

मुझे खेद है कि इन प्रश्नों के लिए लंबे उत्तरों की आवश्यकता है; मैं पहले से ही खुश हूं अगर मैं सही दिशा में या कुछ पहले से ही खुले-सोर्स किए गए एक्सटेंशन को कुछ समान कर सकता हूं (इसे एक वीएस कोड संपादक में प्रदर्शित करने से पहले फ़ाइल डेटा परिवर्तित करना)।

+0

सुनिश्चित नहीं हैं, लेकिन मैं एक अस्थायी निर्देशिका में एक xml फ़ाइल के रूप में सहेजने से उसके चारों ओर हैक संपादक में खोलें, और के लिए फ़ाइल को देखने चाहते हैं परिवर्तन। सुरुचिपूर्ण नहीं, लेकिन जल्दी से करने योग्य। हो सकता है कि कोई जानता है कि सुरुचिपूर्ण मार्ग कैसे काम कर रहा है ... – Will

+0

हाँ, मैंने इसके बारे में सोचा कि वीएसएक्स द्वारा पीटा महसूस किया जा रहा है, लेकिन फिर मैं अपने मौजूदा बाहरी प्रोग्राम को फ़ाइलों को संशोधित करने के लिए रख सकता हूं; प्लस मुझे यकीन नहीं है कि उन हैक्स को अंततः कार्यान्वित करना आसान है ... –

उत्तर

5

सामान्य विचार यह है कि एक्सएमएल संपादक को आमतौर पर ऐसा करने दें: दस्तावेज़ खोलें।

अपने मामले में, यदि मैं इसे सही ढंग से समझता हूं, तो आपके पास भौतिक एक्सएमएल दस्तावेज़ नहीं है, इसलिए आपको एक बनाना होगा। एक दस्तावेज़ कुछ है (यह एक भौतिक फ़ाइल नहीं है) जो विजुअल स्टूडियो के Running Object Table में पंजीकृत है।

एक बार आपके पास एक दस्तावेज़ होने के बाद आप खोल खोलने के लिए केवल पूछ सकते हैं। आप BeforeSave and AfterSave events को संभालने के लिए फिर से आरओटी का उपयोग कर सकते हैं। यहां कुछ नमूना कोड है कि यह सब क्या करना चाहिए है:

public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW) 
{ 
    ppunkDocView = IntPtr.Zero; 
    ppunkDocData = IntPtr.Zero; 
    pbstrEditorCaption = null; 
    pguidCmdUI = Guid.Empty; 
    pgrfCDW = 0; 

    // create your virtual Xml buffer 
    var data = Package.CreateComInstance<VsTextBufferClass, IVsTextLines>(); 
    SiteObject(data); 

    // this is where you're supposed to build your virtual Xml content from your binary data 
    string myXml = "<root>blah</root>"; 
    data.InitializeContent(myXml, myXml.Length); 
    var dataPtr = Marshal.GetIUnknownForObject(data); 

    // build a document and register it in the Running Object Table 
    // this document has no hierarchy (it will be handled by the 'Miscellaneous Files' fallback project) 
    var rotFlags = _VSRDTFLAGS.RDT_ReadLock | _VSRDTFLAGS.RDT_VirtualDocument; 

    // come up with a moniker (which will be used as the caption also by the Xml editor) 
    // Note I presume the moniker is a file path, wich may not always be ok depending on your context 
    var virtualMk = Path.ChangeExtension(pszMkDocument, ".xml"); 
    var rot = (IVsRunningDocumentTable)_sp.GetService(typeof(SVsRunningDocumentTable)); 
    int hr = rot.RegisterAndLockDocument((uint)rotFlags, virtualMk, null, VSConstants.VSITEMID_NIL, dataPtr, out uint docCookie); 
    if (hr != 0) 
     return hr; 

    try 
    { 
     // ask Visual Studio to open that document 
     var opener = (IVsUIShellOpenDocument)_sp.GetService(typeof(SVsUIShellOpenDocument)); 
     var view = VSConstants.LOGVIEWID_Primary; 
     opener.OpenDocumentViaProject(virtualMk, ref view, 
      out Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp, 
      out IVsUIHierarchy uiHier, 
      out uint id, 
      out IVsWindowFrame frame); 
     if (frame != null) 
     { 
      // Hmm.. the dirty bit (the star after the caption) is not updated by the Xml Editor... 
      // If you close the document (or close VS), it does update it, but it does not react when we type in the editor. 
      // This is unexpected, so, let's do the "dirty" work ourselves 
      // hook on text line events from the buffer 
      var textLineEvents = new TextLineEvents((IConnectionPointContainer)data); 

      // we want to know when to unadvise, to hook frame events too 
      ((IVsWindowFrame2)frame).Advise(textLineEvents, out uint frameCookie); 

      textLineEvents.LineTextChanged += (sender, e) => 
      { 
       // get the dirty bit and override the frame's dirty state 
       ((IVsPersistDocData)data).IsDocDataDirty(out int dirty); 
       frame.SetProperty((int)__VSFPROPID2.VSFPROPID_OverrideDirtyState, dirty != 0 ? true : false); 
      }; 

      // now handle save events using the rot 
      var docEventHandler = new RotDocumentEvents(docCookie); 
      docEventHandler.Saving += (sender, e) => 
      { 
       // this is where you can get the content of the data and save your binary data back 
       // you can use Saved or Saving 

      }; 

      docEventHandler.Saved += (sender, e) => 
      { 
       // manual reset of dirty bit... 
       frame.SetProperty((int)__VSFPROPID2.VSFPROPID_OverrideDirtyState, false); 
      }; 
      rot.AdviseRunningDocTableEvents(docEventHandler, out uint rootCookie); 

      frame.Show(); 
     } 
    } 
    finally 
    { 
     rot.UnlockDocument((uint)_VSRDTFLAGS.RDT_ReadLock, docCookie); 
    } 
    return VSConstants.S_OK; 
} 

private class TextLineEvents : IVsTextLinesEvents, IVsWindowFrameNotify, IVsWindowFrameNotify2 
{ 
    public event EventHandler LineTextChanged; 
    private uint _cookie; 
    private IConnectionPoint _cp; 

    public TextLineEvents(IConnectionPointContainer cpc) 
    { 
     var textLineEventsGuid = typeof(IVsTextLinesEvents).GUID; 
     cpc.FindConnectionPoint(ref textLineEventsGuid, out _cp); 
     _cp.Advise(this, out _cookie); 
    } 

    public void OnChangeLineText(TextLineChange[] pTextLineChange, int fLast) => LineTextChanged?.Invoke(this, EventArgs.Empty); 

    public int OnClose(ref uint pgrfSaveOptions) 
    { 
     _cp.Unadvise(_cookie); 
     return VSConstants.S_OK; 
    } 

    public void OnChangeLineAttributes(int iFirstLine, int iLastLine) { } 
    public int OnShow(int fShow) => VSConstants.S_OK; 
    public int OnMove() => VSConstants.S_OK; 
    public int OnSize() => VSConstants.S_OK; 
    public int OnDockableChange(int fDockable) => VSConstants.S_OK; 
} 

private class RotDocumentEvents : IVsRunningDocTableEvents3 
{ 
    public event EventHandler Saved; 
    public event EventHandler Saving; 

    public RotDocumentEvents(uint docCookie) 
    { 
     DocCookie = docCookie; 
    } 

    public uint DocCookie { get; } 

    public int OnBeforeSave(uint docCookie) 
    { 
     if (docCookie == DocCookie) 
     { 
      Saving?.Invoke(this, EventArgs.Empty); 
     } 
     return VSConstants.S_OK; 
    } 

    public int OnAfterSave(uint docCookie) 
    { 
     if (docCookie == DocCookie) 
     { 
      Saved?.Invoke(this, EventArgs.Empty); 
     } 
     return VSConstants.S_OK; 
    } 

    public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining) => VSConstants.S_OK; 
    public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining) => VSConstants.S_OK; 
    public int OnAfterAttributeChange(uint docCookie, uint grfAttribs) => VSConstants.S_OK; 
    public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame) => VSConstants.S_OK; 
    public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame) => VSConstants.S_OK; 
    public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew) => VSConstants.S_OK; 
} 
+0

वाह, यह पूरी तरह से काम करता है!सिर्फ एक छोटा प्रश्न बचा है; संपादक शीर्षक में काम करने के लिए "सहेजे गए परिवर्तन" तारांकन प्राप्त करना संभव है? और: आपको यह सब कैसे पता चला, मुझे कोई साहित्य पढ़ना चाहिए? : डी –

+0

आप सही हैं। यह अप्रत्याशित है, शायद क्योंकि यह वास्तविक फ़ाइल नहीं है? बफर में गंदा बिट सेट होता है, लेकिन जब हम टेक्स्ट बदलते हैं तो XmlEditor इसका उपयोग नहीं करता है (जब हम फ़ाइल/प्रोजेक्ट/समाधान बंद करते हैं तो यह काम करता है, हमें चेतावनी मिलती है और * सेट हो जाता है)। मैंने एक वर्कअराउंड जोड़ा ... वीएस विकास के बारे में, officiel एसडीके (जो पूल है) से परे कोई विशिष्ट साहित्य नहीं है। बस अनुभव करें (और रिफ्लेक्टर, आईएलएसपी इत्यादि जैसे टूल का उपयोग करके विजुअल स्टूडियो असेंबली में बहुत सारे स्पेलंकिंग :-) ध्यान दें कि यह "पुराना" इंटरफेस है। नया WPF एट अल। बेहतर हैं। –

+0

यह निश्चित रूप से अजीब व्यवहार है, मैं हर मिनट आपके समाधान का प्रयास करता हूं। इस बीच मैंने एक बग का शिकार करने की कोशिश की जहां वीएस, कुछ रहस्यमय कारणों से, सहेजने पर एक्सएमएल सामग्री के साथ Temp.txt लिखना चाहता था जो कि वर्तमान कार्यशील निर्देशिका में जो कुछ भी है, ऐसा लगता है - एक संदेश बॉक्स पॉप अप करना यह कुछ स्टोर नहीं कर सका सी में: \ विंडोज \ system32 \ Temp.txt या वीएस devenv.exe पथ। मैंने निश्चित रूप से ऐसा कोड नहीं जोड़ा है, मैं स्मृति में रूपांतरित करता हूं, और ऐसा लगता है कि सहेजने और सहेजे गए ... शायद मैं वर्तमान निर्देशिका को वास्तविक अस्थायी पथ में बदलने की कोशिश करता हूं ...: ओ बाउंटी को जल्द ही सम्मानित किया जाता है दूर! –

संबंधित मुद्दे

 संबंधित मुद्दे