/// <summary>
        /// Inserts a snippet based on a shortcut string.
        /// </summary>
        public int StartSnippetInsertion(out bool snippetInserted)
        {
            var hr = VSConstants.E_FAIL;

            snippetInserted = false;

            // Get the text at the current caret position and
            // determine if it is a snippet shortcut.
            if (!TextView.Caret.InVirtualSpace)
            {
                var textBuffer = GetTargetBuffer();
                var expansion  = textBuffer.GetBufferAdapter <IVsExpansion>(_services);

                var shortcut = TextView.GetItemBeforeCaret(out Span span, x => true);
                var exp      = _cache.GetExpansion(shortcut);

                var ts = TextSpanFromViewSpan(span);
                if (exp.HasValue && ts.HasValue)
                {
                    // Insert into R buffer
                    hr = expansion.InsertNamedExpansion(exp.Value.title, exp.Value.path, ts.Value, this, RGuidList.RLanguageServiceGuid, 0, out _expansionSession);
                    // If EndExpansion was called before InsertExpansion returned, so set _expansionSession
                    // to null to indicate that there is no active expansion session. This can occur when
                    // the snippet inserted doesn't have any expansion fields.
                    if (_expansionSession != null)
                    {
                        PositionCaretInField(0);
                    }
                    snippetInserted = ErrorHandler.Succeeded(hr);
                }
            }
            return(hr);
        }
Exemple #2
0
        public ExpansionsTest(IServiceContainer services)
        {
            _services         = services;
            _expansionManager = Substitute.For <IVsExpansionManager>();

            _cache = Substitute.For <IExpansionsCache>();
            _cache.GetExpansion("if").Returns(new VsExpansion {
                description = "if statement",
                path        = "path",
                shortcut    = "if",
                title       = "if statement"
            });
        }
Exemple #3
0
        public ExpansionsTest() {
            _expansionManager = Substitute.For<IVsExpansionManager>();

            _cache = Substitute.For<IExpansionsCache>();
            _cache.GetExpansion("if").Returns(new VsExpansion() {
                description = "if statement",
                path = "path",
                shortcut = "if",
                title = "if statement"
            });

            TextBufferUtilities.AdaptersFactoryService = new VsEditorAdaptersFactoryServiceMock();
        }
Exemple #4
0
        /// <summary>
        /// Inserts a snippet based on a shortcut string.
        /// </summary>
        public int StartSnippetInsertion(out bool snippetInserted)
        {
            int hr = VSConstants.E_FAIL;

            snippetInserted = false;

            // Get the text at the current caret position and
            // determine if it is a snippet shortcut.
            if (!TextView.Caret.InVirtualSpace)
            {
                SnapshotPoint caretPoint = TextView.Caret.Position.BufferPosition;

                var document = REditorDocument.FindInProjectedBuffers(TextView.TextBuffer);
                // Document may be null in tests
                var textBuffer = document != null ? document.TextBuffer : TextView.TextBuffer;
                var expansion  = textBuffer.GetBufferAdapter <IVsExpansion>();
                _earlyEndExpansionHappened = false;

                Span span;
                _shortcut = TextView.GetItemBeforeCaret(out span, x => true);
                VsExpansion?exp = _cache.GetExpansion(_shortcut);

                // Get view span
                var ts = span.Length > 0 ? TextSpanFromSpan(TextView, span) : TextSpanFromPoint(caretPoint);

                // Map it down to R buffer
                var start = TextView.MapDownToR(span.Start);
                var end   = TextView.MapDownToR(span.End);

                if (exp.HasValue && start.HasValue && end.HasValue)
                {
                    // Insert into R buffer
                    ts = TextSpanFromSpan(textBuffer, Span.FromBounds(start.Value, end.Value));
                    hr = expansion.InsertNamedExpansion(exp.Value.title, exp.Value.path, ts, this, RGuidList.RLanguageServiceGuid, 0, out _expansionSession);
                    if (_earlyEndExpansionHappened)
                    {
                        // EndExpansion was called before InsertExpansion returned, so set _expansionSession
                        // to null to indicate that there is no active expansion session. This can occur when
                        // the snippet inserted doesn't have any expansion fields.
                        _expansionSession          = null;
                        _earlyEndExpansionHappened = false;
                        _shortcut = null;
                        _title    = null;
                    }
                    ErrorHandler.ThrowOnFailure(hr);
                    snippetInserted = true;
                    return(hr);
                }
            }
            return(hr);
        }
Exemple #5
0
        public ExpansionsTest()
        {
            _expansionManager = Substitute.For <IVsExpansionManager>();

            _cache = Substitute.For <IExpansionsCache>();
            _cache.GetExpansion("if").Returns(new VsExpansion()
            {
                description = "if statement",
                path        = "path",
                shortcut    = "if",
                title       = "if statement"
            });

            TextBufferUtilities.AdaptersFactoryService = new VsEditorAdaptersFactoryServiceMock();
        }
Exemple #6
0
        /// <summary>
        /// Inserts a snippet based on a shortcut string.
        /// </summary>
        public int StartSnippetInsertion(out bool snippetInserted)
        {
            int hr = VSConstants.E_FAIL;

            snippetInserted = false;

            // Get the text at the current caret position and
            // determine if it is a snippet shortcut.
            if (!TextView.Caret.InVirtualSpace)
            {
                SnapshotPoint caretPoint = TextView.Caret.Position.BufferPosition;
                var           expansion  = TextBuffer.GetBufferAdapter <IVsExpansion>();

                _earlyEndExpansionHappened = false;
                Span span;
                _shortcut = TextView.GetItemBeforeCaret((x) => !char.IsWhiteSpace(x), out span);

                VsExpansion?exp = _cache.GetExpansion(_shortcut);
                var         ts  = span.Length > 0 ? TextSpanFromSpan(TextView, span) : TextSpanFromPoint(caretPoint);
                if (exp.HasValue)
                {
                    hr = expansion.InsertNamedExpansion(exp.Value.title, exp.Value.path, ts, this, RGuidList.RLanguageServiceGuid, 0, out _expansionSession);
                    if (_earlyEndExpansionHappened)
                    {
                        // EndExpansion was called before InsertExpansion returned, so set _expansionSession
                        // to null to indicate that there is no active expansion session. This can occur when
                        // the snippet inserted doesn't have any expansion fields.
                        _expansionSession          = null;
                        _earlyEndExpansionHappened = false;
                        _shortcut = null;
                        _title    = null;
                    }
                    ErrorHandler.ThrowOnFailure(hr);
                    snippetInserted = true;
                    return(hr);
                }
            }
            return(hr);
        }