locked
Is there a way to record the modifications to a file and then apply them to another copy? RRS feed

  • Question

  • I want to write a VSX for this purpose:

    It can record the changes of a file(the user may add/delete a sentence in a sourcefile),  and then ship the changes to another place ,where there is a copy of the sourcefile and the changes can be apply to this one

    I can now get the IVsTextView _textView, and after I getbuffer from it, the set in another place causes some exception, the code is like this:

                protected IVsTextView _textView ;   // the value is set in another place 
                IVsTextLines textLines;
                _textView.GetBuffer(out textLines);
                oldTextLines = textLines;
                _textView.SetBuffer(oldTextLines);  //set it right here is Ok, but if i do it in another method , the VS itself crashes

    how should i deal with the buffer so that i can get the result ? I need your help, please...

    • Edited by JerryNet_NJU Tuesday, May 18, 2010 2:06 AM clear
    Monday, May 17, 2010 5:45 PM

Answers

  • Hi,

    You code is not enough, so please post the essential information for us to reproduce your issue..

    Your code make the old text view and the new text view share the same buffer, so when your edit in one code view, another text view will also be changed. But you could not create another copy, because there is only one text buffer in the Memory.

    If you want the two view share the same buffer, you must make the both document opened. Here is the sample code that make the two textview share the same buffer.

                IVsTextManager txtManager = GetService(typeof(SVsTextManager)) as IVsTextManager;

                IVsTextView ActiveTxtView;

                IVsTextLines ActiveTxtLines;

                //Get the active view buffer.

                txtManager.GetActiveView(0, null, out ActiveTxtView);

                ActiveTxtView.GetBuffer(out ActiveTxtLines);

       

     

                IVsWindowFrame docFrame;

                IVsUIHierarchy pUIHier;

                uint itemID;

                //Get another opened document view

                if (VsShellUtilities.IsDocumentOpen(this, @" \Visual Studio 2008\Projects\FTPDownload\FTPDownload\Class1.vb",

                VSConstants.LOGVIEWID_Any, out pUIHier, out itemID, out docFrame))

                {

                    object obj;

                    docFrame.GetProperty((int)__VSFPROPID.VSFPROPID_DocView, out obj);

                    IVsCodeWindow codeWindow = obj as IVsCodeWindow;

                    IVsTextView textView;

                    codeWindow.GetPrimaryView(out textView);

                    //Set another document view with the active view buffer

                    textView.SetBuffer(ActiveTxtLines);

                }

    If you want to create another copy for your source code, I think a better approach is to get the code text using TextDocument Interface, and write it to a file using FileStream. 

    Thanks

    Chao

    • Edited by Chao Kuo Wednesday, May 19, 2010 12:28 PM More
    • Marked as answer by Chao Kuo Monday, May 24, 2010 2:41 AM
    Wednesday, May 19, 2010 12:22 PM