/// <summary>
 /// Push completion list result back to client
 /// </summary>
 /// <param name="completionResultList">Completion list got from intellisense service</param>
 public void PushCompletionResult(CompletionResultList completionResultList, int requestWindowId)
 {
     if (CompletionListUpdated != null)
     {
         CompletionListUpdated(this, new EventArgs <CompletionResultList, int>(completionResultList, requestWindowId));
     }
 }
        public void GetCompletionResultsDashTriggerTest()
        {
            var mre = new ManualResetEvent(false);

            CompletionResultList result = null;

            ((IntelliSenseEventsHandlerProxy)_context).CompletionListUpdated += (sender, args) => { result = args.Value1; mre.Set(); };

            _service.RequestCompletionResults("Write-", 6, 0, DateTime.UtcNow.Ticks);

            mre.WaitOne();

            Assert.AreEqual <int>(0, result.ReplacementIndex);
            Assert.AreEqual <int>(6, result.ReplacementLength);
        }
        private void ProcessCompletion(string script, int caretPosition, int requestWindowId, long triggerTag)
        {
            lock (_syncLock)
            {
                _requestTrigger = triggerTag;
            }

            if (_callback == null)
            {
                _callback = OperationContext.Current.GetCallbackChannel <IIntelliSenseServiceCallback>();
            }

            // Start process the existing waiting request, should only be one
            Task.Run(() =>
            {
                try
                {
                    CommandCompletion commandCompletion = null;

                    lock (ServiceCommon.RunspaceLock)
                    {
                        if (_runspace.RunspaceAvailability == RunspaceAvailability.Available)
                        {
                            commandCompletion = CommandCompletionHelper.GetCommandCompletionList(script, caretPosition, _runspace);
                        }
                        else
                        {
                            // we'll handle it when we work on giving intellisense for debugging command
                            // for now we just simply return with null for this request to complete.
                        }
                    }

                    ServiceCommon.LogCallbackEvent("Callback intellisense at position {0}", caretPosition);
                    _callback.PushCompletionResult(CompletionResultList.FromCommandCompletion(commandCompletion), requestWindowId);

                    // Reset trigger
                    lock (_syncLock)
                    {
                        _requestTrigger = 0;
                    }
                }
                catch (Exception ex)
                {
                    ServiceCommon.Log("Failed to retrieve the completion list per request due to exception: {0}", ex.Message);
                }
            });
        }
        public void GetCompletionResultsDollarTriggerTest()
        {
            var mre = new ManualResetEvent(false);

            CompletionResultList result = null;

            ((IntelliSenseEventsHandlerProxy)_context).CompletionListUpdated += (sender, args) => { result = args.Value1; mre.Set(); };

            string script = @"$myVar = 2; $myStrVar = 'String variable'; Write-Host $";

            _service.RequestCompletionResults(script, 55, 0, DateTime.UtcNow.Ticks);

            mre.WaitOne();

            Assert.AreEqual <string>("$myVar", result.CompletionMatches[0].CompletionText);
            Assert.AreEqual <string>("$myStrVar", result.CompletionMatches[1].CompletionText);
            Assert.AreEqual <int>(54, result.ReplacementIndex);
            Assert.AreEqual <int>(1, result.ReplacementLength);
        }