Пример #1
0
        private void FormatTrackingSpan(IBraceCompletionSession session, bool shouldHonorAutoFormattingOnCloseBraceOption)
        {
            if (!_optionsService.GeneralOptions.AutomaticallyFormatBlockOnCloseBrace && shouldHonorAutoFormattingOnCloseBraceOption)
            {
                return;
            }

            var snapshot      = session.SubjectBuffer.CurrentSnapshot;
            var startPoint    = session.OpeningPoint.GetPoint(snapshot);
            var endPoint      = session.ClosingPoint.GetPoint(snapshot);
            var startPosition = startPoint.Position;
            var endPosition   = endPoint.Position;

            if (HlslPackage.Instance.LanguagePreferences.IndentMode == vsIndentStyle.vsIndentStyleSmart)
            {
                // skip whitespace
                while (startPosition >= 0 && char.IsWhiteSpace(snapshot[startPosition]))
                {
                    startPosition--;
                }

                // skip token
                startPosition--;
                while (startPosition >= 0 && !char.IsWhiteSpace(snapshot[startPosition]))
                {
                    startPosition--;
                }
            }

            session.SubjectBuffer.Format(
                TextSpan.FromBounds(snapshot.ToSourceText(), Math.Max(startPosition, 0), endPosition),
                _optionsService);
        }
        // From https://github.com/rsdn/nemerle/blob/master/snippets/VS2010/Nemerle.VisualStudio/LanguageService/NemerleLanguageService.cs#L565
        private void GoToLocation(string filename, TextSpan textSpan, string caption, bool asReadonly)
        {
            uint           itemID;
            IVsUIHierarchy hierarchy;
            IVsWindowFrame docFrame;
            IVsTextView    textView;

            try
            {
                VsShellUtilities.OpenDocument(_serviceProvider, filename, VSConstants.LOGVIEWID_Code,
                                              out hierarchy, out itemID, out docFrame, out textView);
            }
            catch
            {
                // File might not exist, etc.
                return;
            }

            if (asReadonly)
            {
                IVsTextLines buffer;
                ErrorHandler.ThrowOnFailure(textView.GetBuffer(out buffer));
                IVsTextStream stream = (IVsTextStream)buffer;
                stream.SetStateFlags((uint)BUFFERSTATEFLAGS.BSF_USER_READONLY);
            }

            if (caption != null)
            {
                ErrorHandler.ThrowOnFailure(docFrame.SetProperty((int)__VSFPROPID.VSFPROPID_OwnerCaption, caption));
            }

            ErrorHandler.ThrowOnFailure(docFrame.Show());

            if (textView != null)
            {
                var wpfTextView = docFrame.GetWpfTextView();
                var line        = wpfTextView.TextBuffer.CurrentSnapshot.GetLineFromPosition(textSpan.Start);
                var span        = new Microsoft.VisualStudio.TextManager.Interop.TextSpan
                {
                    iStartLine  = line.LineNumber,
                    iStartIndex = textSpan.Start - line.Start.Position,
                    iEndLine    = line.LineNumber,
                    iEndIndex   = textSpan.Start - line.Start.Position
                };

                try
                {
                    ErrorHandler.ThrowOnFailure(textView.SetCaretPos(span.iStartLine, span.iStartIndex));
                    ErrorHandler.ThrowOnFailure(textView.EnsureSpanVisible(span));
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                }
            }
        }