/// <summary>
        /// If there was no $end$ token, place it at the end of the snippet code. Otherwise, it
        /// defaults to the beginning of the snippet code.
        /// </summary>
        private static bool SetEndPositionIfNoneSpecified(IExpansionSession pSession)
        {
            if (pSession.GetSnippetNode() is not XElement snippetNode)
            {
                return(false);
            }

            var ns       = snippetNode.Name.NamespaceName;
            var codeNode = snippetNode.Element(XName.Get("Code", ns));

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

            var delimiterAttribute = codeNode.Attribute("Delimiter");
            var delimiter          = delimiterAttribute != null ? delimiterAttribute.Value : "$";

            if (codeNode.Value.IndexOf(string.Format("{0}end{0}", delimiter), StringComparison.OrdinalIgnoreCase) != -1)
            {
                return(false);
            }

            var snippetSpan = pSession.GetSnippetSpan();

            var newEndSpan = new SnapshotSpan(snippetSpan.End, 0);

            pSession.EndSpan = newEndSpan;
            return(true);
        }
Exemplo n.º 2
0
        public void OnItemChosen(string title, string pszPath)
        {
            var textViewModel = TextView.TextViewModel;

            if (textViewModel == null)
            {
                Debug.Assert(TextView.IsClosed);
                return;
            }

            var textSpan = TextView.Caret.Position.BufferPosition;

            var expansion = ExpansionServiceProvider.GetExpansionService(TextView);

            earlyEndExpansionHappened = false;
            ExpansionSession          = expansion.InsertNamedExpansion(title, pszPath, new SnapshotSpan(textSpan, 0), this, LanguageServiceGuid, false);

            if (earlyEndExpansionHappened)
            {
                // EndExpansion was called before InsertNamedExpansion 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;
            }
        }
Exemplo n.º 3
0
        public virtual bool TryInsertExpansion(int startPositionInSubjectBuffer, int endPositionInSubjectBuffer)
        {
            var textViewModel = TextView.TextViewModel;

            if (textViewModel == null)
            {
                Debug.Assert(TextView.IsClosed);
                return(false);
            }

            // The expansion itself needs to be created in the data buffer, so map everything up
            _ = SubjectBuffer.CurrentSnapshot.GetPoint(startPositionInSubjectBuffer);
            _ = SubjectBuffer.CurrentSnapshot.GetPoint(endPositionInSubjectBuffer);
            if (!TryGetSpanOnHigherBuffer(
                    SubjectBuffer.CurrentSnapshot.GetSpan(startPositionInSubjectBuffer, endPositionInSubjectBuffer - startPositionInSubjectBuffer),
                    textViewModel.DataBuffer,
                    out var dataBufferSpan))
            {
                return(false);
            }

            var expansion = ExpansionServiceProvider.GetExpansionService(TextView);

            ExpansionSession = expansion.InsertExpansion(dataBufferSpan, this, LanguageServiceGuid);
            if (ExpansionSession == null)
            {
                return(false);
            }
            return(true);
        }
Exemplo n.º 4
0
        public void EndExpansion()
        {
            if (ExpansionSession == null)
            {
                earlyEndExpansionHappened = true;
            }

            ExpansionSession    = null;
            indentCaretOnCommit = false;
        }
Exemplo n.º 5
0
        public virtual bool TryHandleEscape()
        {
            if (ExpansionSession != null)
            {
                ExpansionSession.EndCurrentExpansion(leaveCaret: true);
                ExpansionSession = null;
                return(true);
            }

            return(false);
        }
Exemplo n.º 6
0
        public virtual bool TryHandleReturn()
        {
            if (ExpansionSession != null)
            {
                // Only move the caret if the enter was hit within the snippet fields.
                var hitWithinField = ExpansionSession.GoToNextExpansionField(commitIfLast: false);
                ExpansionSession.EndCurrentExpansion(leaveCaret: !hitWithinField);
                ExpansionSession = null;

                return(hitWithinField);
            }

            return(false);
        }
Exemplo n.º 7
0
        public virtual bool TryHandleBackTab()
        {
            if (ExpansionSession != null)
            {
                var tabbedInsideSnippetField = ExpansionSession.GoToPreviousExpansionField();

                if (!tabbedInsideSnippetField)
                {
                    ExpansionSession.EndCurrentExpansion(leaveCaret: true);
                    ExpansionSession = null;
                }

                return(tabbedInsideSnippetField);
            }

            return(false);
        }
Exemplo n.º 8
0
        private void AddReferencesAndImports(
            IExpansionSession pSession,
            int position,
            CancellationToken cancellationToken)
        {
            if (!(pSession.GetSnippetNode() is XElement snippetNode))
            {
                return;
            }

            var documentWithImports = this.SubjectBuffer.CurrentSnapshot.GetOpenDocumentInCurrentContextWithChanges();

            if (documentWithImports == null)
            {
                return;
            }

            var documentOptions           = documentWithImports.GetOptionsAsync(cancellationToken).WaitAndGetResult(cancellationToken);
            var placeSystemNamespaceFirst = documentOptions.GetOption(GenerationOptions.PlaceSystemNamespaceFirst);

            documentWithImports = AddImports(documentWithImports, position, snippetNode, placeSystemNamespaceFirst, cancellationToken);
            AddReferences(documentWithImports.Project, snippetNode);
        }
 public void OnBeforeInsertion(IExpansionSession pSession)
 {
     Logger.Log(FunctionId.Snippet_OnBeforeInsertion);
     this.ExpansionSession = pSession;
 }
Exemplo n.º 10
0
 public void OnAfterInsertion(IExpansionSession pSession)
 {
     Logger.Log(FunctionId.Snippet_OnAfterInsertion);
 }