Esempio n. 1
0
        /// <summary>
        /// Activates a text view for a text buffer, and sets the cursor to a specific location
        /// </summary>
        public static bool NavigateToTextBuffer(ITextBuffer textBuffer, int start, int length)
        {
            IProjectionSnapshot projectionSnapshot = textBuffer.CurrentSnapshot as IProjectionSnapshot;

            if (projectionSnapshot != null)
            {
                // Find the main buffer for the view

                SnapshotPoint sourcePoint = new SnapshotPoint();
                bool          success     = true;

                try {
                    sourcePoint = projectionSnapshot.MapToSourceSnapshot(start, PositionAffinity.Successor);
                } catch (ArgumentOutOfRangeException) {
                    success = false;
                } catch (InvalidOperationException) {
                    success = false;
                }

                if (success)
                {
                    return(NavigateToTextBuffer(sourcePoint.Snapshot.TextBuffer, sourcePoint.Position, length));
                }
            }
            else
            {
                // This is the main buffer for the view

                IVsTextManager textManager  = VsAppShell.Current.GetGlobalService <IVsTextManager>(typeof(SVsTextManager));
                IVsTextBuffer  vsTextBuffer = textBuffer.GetBufferAdapter <IVsTextBuffer>();
                Guid           viewType     = VSConstants.LOGVIEWID_TextView;

                if (vsTextBuffer != null &&
                    ErrorHandler.Succeeded(textManager.NavigateToPosition(vsTextBuffer, ref viewType, start, length)))
                {
                    return(true);
                }
            }

            return(false);
        }