Ejemplo n.º 1
0
        public IWpfTextViewHost CreateProjectionEditor(string filePath, int start, int length, bool isReadonly = true)
        {
            //IVsInvisibleEditors are in-memory represenations of typical Visual Studio editors.
            //Language services, highlighting and error squiggles are hooked up to these editors
            //for us once we convert them to WpfTextViews.
            var invisibleEditor = GetInvisibleEditor(filePath);

            var  docDataPointer   = IntPtr.Zero;
            Guid guidIVsTextLines = typeof(IVsTextLines).GUID;

            ErrorHandler.ThrowOnFailure(invisibleEditor.GetDocData(
                                            fEnsureWritable: 1
                                            , riid: ref guidIVsTextLines
                                            , ppDocData: out docDataPointer));

            IVsTextLines docData = (IVsTextLines)Marshal.GetObjectForIUnknown(docDataPointer);

            // This will actually be defined as _codewindowbehaviorflags2.CWB_DISABLEDIFF once the latest version of
            // Microsoft.VisualStudio.TextManager.Interop.16.0.DesignTime is published. Setting the flag will have no effect
            // on releases prior to d16.0.
            const _codewindowbehaviorflags CWB_DISABLEDIFF = (_codewindowbehaviorflags)0x04;

            //Create a code window adapter
            var codeWindow = EditorAdaptersFactoryService.CreateVsCodeWindowAdapter(OLEServiceProvider);

            // You need to disable the dropdown, splitter and -- for d16.0 -- diff since you are extracting the code window's TextViewHost and using it.
            ((IVsCodeWindowEx)codeWindow).Initialize((uint)_codewindowbehaviorflags.CWB_DISABLESPLITTER | (uint)_codewindowbehaviorflags.CWB_DISABLEDROPDOWNBAR | (uint)CWB_DISABLEDIFF,
                                                     VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter,
                                                     string.Empty,
                                                     string.Empty,
                                                     0,
                                                     new INITVIEW[1]);

            ErrorHandler.ThrowOnFailure(codeWindow.SetBuffer(docData));

            //Get a text view for our editor which we will then use to get the WPF control for that editor.
            IVsTextView textView;

            ErrorHandler.ThrowOnFailure(codeWindow.GetPrimaryView(out textView));

            //We add our own role to this text view. Later this will allow us to selectively modify
            //this editor without getting in the way of Visual Studio's normal editors.
            var roles = TextEditorFactoryService.DefaultRoles.Concat(new string[] { DCI_BABY_IDE });

            var vsTextBuffer = docData as IVsTextBuffer;
            var textBuffer   = EditorAdaptersFactoryService.GetDataBuffer(vsTextBuffer);

            if (textBuffer.Properties.ContainsProperty("StartPosition"))
            {
                textBuffer.Properties.RemoveProperty("StartPosition");
            }
            if (textBuffer.Properties.ContainsProperty("EndPosition"))
            {
                textBuffer.Properties.RemoveProperty("EndPosition");
            }

            textBuffer.Properties.AddProperty("StartPosition", start);
            textBuffer.Properties.AddProperty("EndPosition", start + length);
            var guid = VSConstants.VsTextBufferUserDataGuid.VsTextViewRoles_guid;

            ((IVsUserData)codeWindow).SetData(ref guid, TextEditorFactoryService.CreateTextViewRoleSet(roles).ToString());

            var textViewHost = EditorService.EditorAdaptersFactoryService.GetWpfTextViewHost(textView);

            return(textViewHost);
        }