public override void Commit()
        {
            var completion = CurrentCompletion.Completion as RoslynCompletion;

            if (completion == null)
            {
                base.Commit();
                return;
            }

            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()) {
                foreach (var c in change.TextChanges)
                {
                    Debug.Assert(c.Span.End <= originalSnapshot.Length);
                    if (c.Span.End > originalSnapshot.Length)
                    {
                        return;
                    }
                    var span = new SnapshotSpan(originalSnapshot, c.Span.ToSpan()).TranslateTo(currentSnapshot, SpanTrackingMode.EdgeInclusive);
                    if (!ed.Replace(span.Span, c.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();
                }
            }
        }
Example #2
0
		public void AugmentCompletionSession(ICompletionSession session, IList<CompletionCollection> completionCollections) {
			var snapshot = session.TextView.TextSnapshot;
			var triggerPoint = session.GetTriggerPoint(snapshot);
			if (triggerPoint == null)
				return;
			var info = CompletionInfo.Create(snapshot);
			if (info == null)
				return;

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

			var completionList = info.Value.CompletionService.GetCompletionsAsync(info.Value.Document, triggerPoint.Value.Position, completionTrigger).GetAwaiter().GetResult();
			if (completionList == null)
				return;
			Debug.Assert(completionList.DefaultSpan.End <= snapshot.Length);
			if (completionList.DefaultSpan.End > snapshot.Length)
				return;
			var trackingSpan = snapshot.CreateTrackingSpan(completionList.DefaultSpan.Start, completionList.DefaultSpan.Length, SpanTrackingMode.EdgeInclusive, TrackingFidelityMode.Forward);
			var completionCollection = RoslynCompletionCollection.Create(completionList, info.Value.CompletionService, session.TextView, trackingSpan);
			completionCollections.Add(completionCollection);
		}
        bool TryStartSession(char c, bool isDelete)
        {
            if (HasSession)
            {
                return(false);
            }

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

            if (info == null)
            {
                return(false);
            }
            int pos = textView.Caret.Position.BufferPosition.Position;
            var completionTrigger = isDelete ? CompletionTrigger.CreateDeletionTrigger(c) : CompletionTrigger.CreateInsertionTrigger(c);

            if (!info.Value.CompletionService.ShouldTriggerCompletion(info.Value.SourceText, pos, completionTrigger))
            {
                return(false);
            }

            StartSession(info, completionTrigger);
            return(HasSession);
        }
 CompletionService TryGetRoslynCompletionService() => CompletionInfo.Create(textView.TextSnapshot)?.CompletionService;