Exemplo n.º 1
0
        private bool InsertAnyExpansion(string shortcut, string title, string path)
        {
            //first get the location of the caret, and set up a TextSpan
            int endColumn, startLine;

            //get the column number from  the IVsTextView, not the ITextView
            m_vsTextView.GetCaretPos(out startLine, out endColumn);

            TextSpan addSpan = new TextSpan();

            addSpan.iStartIndex = endColumn;
            addSpan.iEndIndex   = endColumn;
            addSpan.iStartLine  = startLine;
            addSpan.iEndLine    = startLine;

            if (shortcut != null) //get the expansion from the shortcut
            {
                //reset the TextSpan to the width of the shortcut,
                //because we're going to replace the shortcut with the expansion
                addSpan.iStartIndex = addSpan.iEndIndex - shortcut.Length;

                m_exManager.GetExpansionByShortcut(
                    this,
                    Guids.LuaLanguageService,
                    shortcut,
                    m_vsTextView,
                    new TextSpan[] { addSpan },
                    0,
                    out path,
                    out title);
            }
            if (title != null && path != null)
            {
                IVsTextLines textLines;
                m_vsTextView.GetBuffer(out textLines);
                IVsExpansion bufferExpansion = (IVsExpansion)textLines;

                if (bufferExpansion != null)
                {
                    int hr = bufferExpansion.InsertNamedExpansion(
                        title,
                        path,
                        addSpan,
                        this,
                        Guids.LuaLanguageService,
                        0,
                        out m_exSession);
                    if (VSConstants.S_OK == hr)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        private bool InsertAnyExpansion(string shortcut, string title, string path)
        {
            _vsTextView.GetCaretPos(out int startLine, out int endColumn);

            var addSpan = new TextSpan()
            {
                iStartIndex = endColumn,
                iEndIndex   = endColumn,
                iStartLine  = startLine,
                iEndLine    = startLine
            };

            if (shortcut != null)
            {
                addSpan.iStartIndex = addSpan.iEndIndex - shortcut.Length;

                _manager.GetExpansionByShortcut(
                    this,
                    new Guid(_guid),
                    shortcut,
                    _vsTextView,
                    new TextSpan[] { addSpan },
                    0,
                    out path,
                    out title);
            }
            if (title != null && path != null)
            {
                _vsTextView.GetBuffer(out IVsTextLines textLines);
                var bufferExpansion = (IVsExpansion)textLines;

                if (bufferExpansion != null)
                {
                    int hr = bufferExpansion.InsertNamedExpansion(
                        title,
                        path,
                        addSpan,
                        this,
                        new Guid(_guid),
                        0,
                        out _session);

                    if (VSConstants.S_OK == hr)
                    {
                        Telemetry.TrackUserTask("SnippetInserted");
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 3
0
        private bool TryTriggerExpansion()
        {
            if (_expansionMgr != null)
            {
                var snapshot       = _textView.TextBuffer.CurrentSnapshot;
                var span           = new SnapshotSpan(snapshot, new Span(_textView.Caret.Position.BufferPosition.Position - 1, 1));
                var classification = _textView.TextBuffer.GetPythonClassifier().GetClassificationSpans(span);
                if (classification.Count == 1)
                {
                    var clsSpan = classification.First().Span;
                    var text    = classification.First().Span.GetText();

                    TextSpan[] textSpan = new TextSpan[1];
                    textSpan[0].iStartLine  = clsSpan.Start.GetContainingLine().LineNumber;
                    textSpan[0].iStartIndex = clsSpan.Start.Position - clsSpan.Start.GetContainingLine().Start;
                    textSpan[0].iEndLine    = clsSpan.End.GetContainingLine().LineNumber;
                    textSpan[0].iEndIndex   = clsSpan.End.Position - clsSpan.End.GetContainingLine().Start;

                    string expansionPath, title;
                    int    hr = _expansionMgr.GetExpansionByShortcut(
                        _expansionClient,
                        GuidList.guidPythonLanguageServiceGuid,
                        text,
                        GetViewAdapter(),
                        textSpan,
                        1,
                        out expansionPath,
                        out title
                        );
                    if (ErrorHandler.Succeeded(hr))
                    {
                        // hr may be S_FALSE if there are multiple expansions,
                        // so we don't want to InsertNamedExpansion yet. VS will
                        // pop up a selection dialog in this case.
                        if (hr == VSConstants.S_OK)
                        {
                            return(ErrorHandler.Succeeded(_expansionClient.InsertNamedExpansion(title, expansionPath, textSpan[0])));
                        }
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        private bool InsertAnyExpansion(string shortcut, string title, string path)
        {
            if (_exManager == null)
            {
                return(false);
            }

            int startLine, endCol;

            _viewAdapter.GetCaretPos(out startLine, out endCol);

            var addSpan = new TextSpan();

            addSpan.iStartIndex = endCol;
            addSpan.iEndIndex   = endCol;
            addSpan.iStartLine  = startLine;
            addSpan.iEndLine    = startLine;

            if (shortcut != null)
            {
                addSpan.iStartIndex = addSpan.iEndIndex - shortcut.Length;                      // To the start of the word typed

                _exManager.GetExpansionByShortcut(this, GuidList.guidSnippets, shortcut, _viewAdapter, new TextSpan[] { addSpan }, 0, out path, out title);
            }

            if (title != null && path != null)
            {
                IVsTextLines textLines;
                _viewAdapter.GetBuffer(out textLines);

                var bufferExpansion = textLines as IVsExpansion;
                if (bufferExpansion != null)
                {
                    var result = bufferExpansion.InsertNamedExpansion(title, path, addSpan, this, GuidList.guidSnippets, 0, out _exSession);
                    if (result == VSConstants.S_OK)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
        private bool TryTriggerExpansion()
        {
            if (_expansionMgr != null)
            {
                var snapshot       = _textView.TextBuffer.CurrentSnapshot;
                var span           = new SnapshotSpan(snapshot, new Span(_textView.Caret.Position.BufferPosition.Position - 1, 1));
                var classification = _textView.TextBuffer.GetPythonClassifier().GetClassificationSpans(span);
                if (classification.Count == 1)
                {
                    var clsSpan = classification.First().Span;
                    var text    = classification.First().Span.GetText();

                    TextSpan[] textSpan = new TextSpan[1];
                    textSpan[0].iStartLine  = clsSpan.Start.GetContainingLine().LineNumber;
                    textSpan[0].iStartIndex = clsSpan.Start.Position - clsSpan.Start.GetContainingLine().Start;
                    textSpan[0].iEndLine    = clsSpan.End.GetContainingLine().LineNumber;
                    textSpan[0].iEndIndex   = clsSpan.End.Position - clsSpan.End.GetContainingLine().Start;

                    string expansionPath, title;
                    int    hr = _expansionMgr.GetExpansionByShortcut(
                        _expansionClient,
                        GuidList.guidPythonLanguageServiceGuid,
                        text,
                        GetViewAdapter(),
                        textSpan,
                        1,
                        out expansionPath,
                        out title
                        );
                    if (ErrorHandler.Succeeded(hr) &&
                        ErrorHandler.Succeeded(_expansionClient.InsertNamedExpansion(title, expansionPath, textSpan[0])))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Exemplo n.º 6
0
        /// <summary>Returns S_OK if match found, S_FALSE if expansion UI is shown, and error otherwise</summary>
        internal virtual int FindExpansionByShortcut(IVsTextView view, string shortcut, TextSpan span, bool showDisambiguationUI, out string title, out string path)
        {
            if (this.expansionActive)
            {
                this.EndTemplateEditing(true);
            }
            this.view = view;
            title     = path = null;

            LanguageService     svc = this.source.LanguageService;
            IVsExpansionManager mgr = svc.Site.GetService(typeof(SVsExpansionManager)) as IVsExpansionManager;

            if (mgr == null)
            {
                return(NativeMethods.E_FAIL);
            }
            Guid guidLanguage = svc.GetLanguageServiceGuid();

            TextSpan[] pts = new TextSpan[1];
            pts[0] = span;
            int hr = mgr.GetExpansionByShortcut(this, guidLanguage, shortcut, this.TextView, pts, showDisambiguationUI ? 1 : 0, out path, out title);

            return(hr);
        }
Exemplo n.º 7
0
        public bool TryTriggerExpansion(ITextView textView)
        {
            if (textView == null)
            {
                throw new ArgumentNullException(nameof(textView));
            }

            if (_vsExpansionMgr == null)
            {
                return(false);
            }

            if (!textView.Selection.IsEmpty || textView.Caret.Position.BufferPosition <= 0)
            {
                return(false);
            }

            var snapshot  = textView.TextBuffer.CurrentSnapshot;
            var caretSpan = new SnapshotSpan(snapshot, new Span(textView.Caret.Position.BufferPosition.Position - 1, 1));

            var bufferInfo = PythonTextBufferInfo.ForBuffer(_site, textView.TextBuffer);
            var tokens     = bufferInfo.GetTrackingTokens(caretSpan);

            if (!tokens.Any())
            {
                return(false);
            }

            var token     = tokens.First();
            var tokenSpan = token.ToSnapshotSpan(snapshot);

            if (tokenSpan.End.Position != caretSpan.End.Position)
            {
                // Match C# behavior and only trigger snippet
                // if caret is at the end of an identifier. Otherwise,
                // a TAB should be inserted even if the token matches
                // a snippet shortcut.
                return(false);
            }

            var text = tokenSpan.GetText();

            var textSpan = new TextSpan[1];

            textSpan[0].iStartLine  = tokenSpan.Start.GetContainingLine().LineNumber;
            textSpan[0].iStartIndex = tokenSpan.Start.Position - tokenSpan.Start.GetContainingLine().Start;
            textSpan[0].iEndLine    = tokenSpan.End.GetContainingLine().LineNumber;
            textSpan[0].iEndIndex   = tokenSpan.End.Position - tokenSpan.End.GetContainingLine().Start;

            var client = GetOrCreateExpansionClient(textView);
            int hr     = _vsExpansionMgr.GetExpansionByShortcut(
                client,
                CommonGuidList.guidPythonLanguageServiceGuid,
                text,
                _editorAdaptersFactoryService.GetViewAdapter(textView),
                textSpan,
                1,
                out string expansionPath,
                out string title
                );

            if (ErrorHandler.Succeeded(hr))
            {
                // hr may be S_FALSE if there are multiple expansions,
                // so we don't want to InsertNamedExpansion yet. VS will
                // pop up a selection dialog in this case.
                if (hr == VSConstants.S_OK)
                {
                    return(ErrorHandler.Succeeded(client.InsertNamedExpansion(title, expansionPath, textSpan[0])));
                }
                return(true);
            }

            return(false);
        }