int IVsImmediateStatementCompletion2.SetCompletionContext(string filePath,
                                                                  IVsTextLines buffer,
                                                                  Microsoft.VisualStudio.TextManager.Interop.TextSpan[] currentStatementSpan,
                                                                  object punkContext,
                                                                  IVsTextView textView)
        {
            // The immediate window is always marked read-only and the language service is
            // responsible for asking the buffer to make itself writable. We'll have to do that for
            // commit, so we need to drag the IVsTextLines around, too.
            IVsTextLines debuggerBuffer;

            Marshal.ThrowExceptionForHR(textView.GetBuffer(out debuggerBuffer));

            var view = EditorAdaptersFactoryService.GetWpfTextView(textView);

            // Sometimes, they give us a null context buffer. In that case, there's probably not any
            // work to do.
            if (buffer != null)
            {
                var contextBuffer = EditorAdaptersFactoryService.GetDataBuffer(buffer);
                var context       = CreateContext(view, textView, debuggerBuffer, contextBuffer, currentStatementSpan);
                if (context.TryInitialize())
                {
                    this.filters[textView].SetContext(context);
                }
                else
                {
                    this.filters[textView].RemoveContext();
                }
            }

            return(VSConstants.S_OK);
        }
        public int UpdateLanguageContext(uint dwHint, IVsTextLines pBuffer, Microsoft.VisualStudio.TextManager.Interop.TextSpan[] ptsSelection, object pUC)
        {
            var textBuffer = EditorAdaptersFactoryService.GetDataBuffer(pBuffer);
            var context    = (IVsUserContext)pUC;

            if (textBuffer == null || context == null)
            {
                return(VSConstants.E_UNEXPECTED);
            }

            var document = textBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (document == null)
            {
                return(VSConstants.E_FAIL);
            }

            var start = textBuffer.CurrentSnapshot.GetLineFromLineNumber(ptsSelection[0].iStartLine).Start + ptsSelection[0].iStartIndex;
            var end   = textBuffer.CurrentSnapshot.GetLineFromLineNumber(ptsSelection[0].iEndLine).Start + ptsSelection[0].iEndIndex;
            var span  = Microsoft.CodeAnalysis.Text.TextSpan.FromBounds(start, end);

            var helpService = document.GetLanguageService <IHelpContextService>();

            if (helpService == null)
            {
                return(VSConstants.E_NOTIMPL);
            }

            // VS help is not cancellable.
            var cancellationToken = CancellationToken.None;
            var helpTermAsyncTask = helpService.GetHelpTermAsync(document, span, cancellationToken);

            helpTermAsyncTask.Wait(cancellationToken);
            var helpTerm = helpTermAsyncTask.Result;

            if (string.IsNullOrWhiteSpace(helpTerm))
            {
                return(VSConstants.S_FALSE);
            }

            context.RemoveAttribute("keyword", null);
            context.AddAttribute(VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, "devlang", helpService.Language);
            context.AddAttribute(VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, "product", helpService.Product);
            context.AddAttribute(VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_Filter, "product", "VS");
            context.AddAttribute(VSUSERCONTEXTATTRIBUTEUSAGE.VSUC_Usage_LookupF1_CaseSensitive, "keyword", helpTerm);

            return(VSConstants.S_OK);
        }
Exemple #3
0
        int IVsImmediateStatementCompletion2.SetCompletionContext(string filePath,
                                                                  IVsTextLines buffer,
                                                                  TextSpan[] currentStatementSpan,
                                                                  object punkContext,
                                                                  IVsTextView textView)
        {
            // The immediate window is always marked read-only and the language service is
            // responsible for asking the buffer to make itself writable. We'll have to do that for
            // commit, so we need to drag the IVsTextLines around, too.
            Marshal.ThrowExceptionForHR(textView.GetBuffer(out var debuggerBuffer));

            var view = EditorAdaptersFactoryService.GetWpfTextView(textView);

            // Sometimes, they give us a null context buffer. In that case, there's probably not any
            // work to do.
            if (buffer != null)
            {
                var contextBuffer = EditorAdaptersFactoryService.GetDataBuffer(buffer);

                if (!contextBuffer.ContentType.IsOfType(this.ContentTypeName))
                {
                    FatalError.ReportWithoutCrash(
                        new ArgumentException($"Expected content type {this.ContentTypeName} " +
                                              $"but got buffer of content type {contextBuffer.ContentType}"));

                    return(VSConstants.E_FAIL);
                }

                // Clean the old context in any case upfront:
                // even if we fail to initialize, the old context must be cleaned.
                this.filters[textView].RemoveContext();

                var context = CreateContext(view, textView, debuggerBuffer, contextBuffer, currentStatementSpan);
                if (context.TryInitialize())
                {
                    this.filters[textView].SetContext(context);
                }
            }

            return(VSConstants.S_OK);
        }
        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);
        }