/// <summary>
        /// Gets the description or null if none
        /// </summary>
        /// <param name="completion">Completion</param>
        /// <param name="cancellationToken">Cancellation token</param>
        /// <returns></returns>
        public Task <CompletionDescription> GetDescriptionAsync(RoslynCompletion completion, CancellationToken cancellationToken = default)
        {
            if (completion == null)
            {
                throw new ArgumentNullException(nameof(completion));
            }

            var info = CompletionInfo.Create(textView.TextSnapshot);

            if (info == null)
            {
                return(Task.FromResult <CompletionDescription>(null));
            }

            return(completionService.GetDescriptionAsync(info.Value.Document, completion.CompletionItem, cancellationToken));
        }
        public void Commit(RoslynCompletion completion)
        {
            if (completion == null)
            {
                throw new ArgumentNullException(nameof(completion));
            }

            mruCompletionService.AddText(completion.DisplayText);

            var info = CompletionInfo.Create(ApplicableTo.TextBuffer.CurrentSnapshot);

            Debug.Assert(info != null);
            if (info == null)
            {
                return;
            }

            var change          = completionService.GetChangeAsync(info.Value.Document, completion.CompletionItem, commitCharacter: null).GetAwaiter().GetResult();
            var buffer          = ApplicableTo.TextBuffer;
            var currentSnapshot = buffer.CurrentSnapshot;

            using (var ed = buffer.CreateEdit()) {
                var textChange = change.TextChange;
                Debug.Assert(textChange.Span.End <= originalSnapshot.Length);
                if (textChange.Span.End > originalSnapshot.Length)
                {
                    return;
                }
                var span = new SnapshotSpan(originalSnapshot, textChange.Span.ToSpan()).TranslateTo(currentSnapshot, SpanTrackingMode.EdgeInclusive);
                if (!ed.Replace(span.Span, textChange.NewText))
                {
                    return;
                }
                ed.Apply();
            }
            if (change.NewPosition != null)
            {
                var snapshot = buffer.CurrentSnapshot;
                Debug.Assert(change.NewPosition.Value <= snapshot.Length);
                if (change.NewPosition.Value <= snapshot.Length)
                {
                    textView.Caret.MoveTo(new SnapshotPoint(snapshot, change.NewPosition.Value));
                    textView.Caret.EnsureVisible();
                }
            }
        }
Beispiel #3
0
        public void AugmentCompletionSession(ICompletionSession session, IList <CompletionSet> completionSets)
        {
            var snapshot     = session.TextView.TextSnapshot;
            var triggerPoint = session.GetTriggerPoint(snapshot);

            if (triggerPoint == null)
            {
                return;
            }
            var info = CompletionInfo.Create(snapshot);

            if (info == null)
            {
                return;
            }

            // This helps a little to speed up the code
            ProfileOptimizationHelper.StartProfile("roslyn-completion-" + info.Value.CompletionService.Language);

            session.Properties.TryGetProperty(typeof(CompletionTrigger), out CompletionTrigger completionTrigger);

            var completionList = info.Value.CompletionService.GetCompletionsAsync(info.Value.Document, triggerPoint.Value.Position, completionTrigger).GetAwaiter().GetResult();

            if (completionList == null)
            {
                return;
            }
            Debug.Assert(completionList.Span.End <= snapshot.Length);
            if (completionList.Span.End > snapshot.Length)
            {
                return;
            }
            var trackingSpan  = snapshot.CreateTrackingSpan(completionList.Span.Start, completionList.Span.Length, SpanTrackingMode.EdgeInclusive, TrackingFidelityMode.Forward);
            var completionSet = RoslynCompletionSet.Create(imageMonikerService, mruCompletionService, completionList, info.Value.CompletionService, session.TextView, DefaultCompletionSetMoniker, dnSpy_Roslyn_Resources.CompletionSet_All, trackingSpan);

            completionSets.Add(completionSet);
        }