async Task TriggerCompletionAsync(string line, int caretIndex)
        {
            cancellationTokenSource = new CancellationTokenSource(TabExpansionTimeout * 1000);

            SimpleExpansion simpleExpansion = await TryGetExpansionsAsync(
                line,
                caretIndex,
                cancellationTokenSource.Token);

            if (cancellationTokenSource == null || cancellationTokenSource.IsCancellationRequested)
            {
                return;
            }

            if (simpleExpansion?.Expansions == null)
            {
                return;
            }

            if (simpleExpansion.Expansions.Count == 1)
            {
                ReplaceTabExpansion(simpleExpansion);
                return;
            }

            CompletionDataList list = CreateCompletionList(simpleExpansion);

            var context = completionWidget.CreateCodeCompletionContext(simpleExpansion.Start);

            completionWindow.ShowListWindow('\0', list, completionWidget, context);
        }
        void ReplaceTabExpansion(SimpleExpansion expansion)
        {
            TextIter start = Buffer.GetIterAtOffset(InputLineBegin.Offset + expansion.Start);
            TextIter end   = Buffer.GetIterAtOffset(start.Offset + expansion.Length);

            Buffer.Delete(ref start, ref end);
            Buffer.Insert(ref start, expansion.Expansions [0]);
        }
Exemplo n.º 3
0
        private async Task TriggerCompletionAsync()
        {
            if (CommandExpansion == null)
            {
                return; // Host CommandExpansion service not available
            }

            if (IsCompletionSessionActive)
            {
                _completionSession.Dismiss();
                _completionSession = null;
            }

            string line       = WpfConsole.InputLineText;
            int    caretIndex = CaretPosition - WpfConsole.InputLineStart.Value;

            Debug.Assert(caretIndex >= 0);

            // Cancel tab expansion if it takes more than 'TabExpansionTimeout' secs (defaults to 3 secs) to get any results
            CancellationTokenSource ctSource        = new CancellationTokenSource(TabExpansionTimeout * 1000);
            SimpleExpansion         simpleExpansion = null;

            try
            {
                WpfConsole.Dispatcher.SetExecutingCommand(true);
                simpleExpansion = await CommandExpansion.GetExpansionsAsync(line, caretIndex, ctSource.Token);
            }
            catch (Exception x)
            {
                // Ignore exception from expansion, but write it to the activity log
                ExceptionHelper.WriteErrorToActivityLog(x);
            }
            finally
            {
                WpfConsole.Dispatcher.SetExecutingCommand(false);
            }

            if (simpleExpansion != null &&
                simpleExpansion.Expansions != null)
            {
                IList <string> expansions = simpleExpansion.Expansions;
                if (expansions.Count == 1) // Shortcut for 1 TabExpansion candidate
                {
                    ReplaceTabExpansion(simpleExpansion.Start, simpleExpansion.Length, expansions[0]);
                }
                else if (expansions.Count > 1) // Only start intellisense session for multiple expansion candidates
                {
                    _completionSession = CompletionBroker.CreateCompletionSession(
                        WpfTextView,
                        WpfTextView.TextSnapshot.CreateTrackingPoint(CaretPosition.Position, PointTrackingMode.Positive),
                        true);
                    _completionSession.Properties.AddProperty("TabExpansion", simpleExpansion);
                    _completionSession.Dismissed += CompletionSession_Dismissed;
                    _completionSession.Start();
                }
            }
        }
Exemplo n.º 4
0
        /* methods. */
        private void ThreadSim(object obj)
        {
            Console.WriteLine("[RandomSimulation] Thread started simulation.");
            object[] objArr = (object[])obj;
            CancellationTokenSource token = (CancellationTokenSource)(objArr[0]);
            Board b  = (Board)(objArr[1]);
            Node  cn = (Node)(objArr[2]);

            if (token.IsCancellationRequested == false)
            {
                /* choose simulation color. */
                Color rootColor = Color.black;
                if (cn.X != -1 && b.pieces[cn.X, cn.Y].owner.GetColor() == Color.black)
                {
                    rootColor = Color.white;
                }
                IExpansion expansion = new SimpleExpansion();
                int        win       = 0;
                int        sim       = 0;
                while (token.IsCancellationRequested == false)
                {
                    //Console.WriteLine("score: " + b.GetScore(1) + " : " + b.GetScore(2));
                    int res = Simulate(token, new Board(b), cn, expansion, rootColor);
                    if (res == -2)
                    {   /* received cancel request while simulating, discard this result. */
                        break;
                    }
                    else
                    {
                        win += res;
                        sim++;
                    }
                }
                //File.WriteAllText("output.txt", "Score " + b.GetScore(1) + ":" + b.GetScore(2) + "||" + win + " wins and " + sim + " simulations.");
                Console.WriteLine("Score " + b.GetScore(1) + ":" + b.GetScore(2) + "||" + win + " wins and " + sim + " simulations.");
                //using (StreamWriter sw = File.CreateText("output.txt"))
                //{
                //    sw.WriteLine(win + " wins and " + sim + " simulations.");
                //}
                //logger.WriteLine(win + " wins and " + sim + " simulations.");
                /* update node statistics. */
                mutex.WaitOne();
                wins   += win;
                visits += sim;
                mutex.ReleaseMutex();
                /* free memory. */
                b.FreePieces();
                b = null;
            }
            Console.WriteLine("[RandomSimulation] Thread exit.");
        }
        void TriggerCompletion()
        {
            if (CommandExpansion == null)
            {
                return; // Host CommandExpansion service not available
            }

            if (IsCompletionSessionActive)
            {
                _completionSession.Dismiss();
                _completionSession = null;
            }

            string line       = WpfConsole.InputLineText;
            int    caretIndex = CaretPosition - WpfConsole.InputLineStart.Value;

            Debug.Assert(caretIndex >= 0);

            SimpleExpansion simpleExpansion = null;

            try
            {
                simpleExpansion = CommandExpansion.GetExpansions(line, caretIndex);
            }
            catch (Exception x)
            {
                // Ignore exception from expansion, but write it to the activity log
                ExceptionHelper.WriteToActivityLog(x);
            }

            if (simpleExpansion != null && simpleExpansion.Expansions != null)
            {
                IList <string> expansions = simpleExpansion.Expansions;
                if (expansions.Count == 1) // Shortcut for 1 TabExpansion candidate
                {
                    ReplaceTabExpansion(simpleExpansion.Start, simpleExpansion.Length, expansions[0]);
                }
                else if (expansions.Count > 1) // Only start intellisense session for multiple expansion candidates
                {
                    _completionSession = CompletionBroker.CreateCompletionSession(
                        WpfTextView,
                        WpfTextView.TextSnapshot.CreateTrackingPoint(CaretPosition.Position, PointTrackingMode.Positive),
                        true);
                    _completionSession.Properties.AddProperty("TabExpansion", simpleExpansion);
                    _completionSession.Dismissed += CompletionSession_Dismissed;
                    _completionSession.Start();
                }
            }
        }
        static CompletionDataList CreateCompletionList(SimpleExpansion simpleExpansion)
        {
            var list = new CompletionDataList {
                AutoSelect = false
            };

            foreach (string expansion in simpleExpansion.Expansions)
            {
                list.Add(new CompletionData(expansion));
            }

            list.AddKeyHandler(new PackageConsoleCompletionKeyHandler());

            return(list);
        }
 public PSExpansionSession(SimpleExpansion expansion, string initialText)
 {
     _expansion   = expansion;
     _initialText = initialText;
 }