Exemplo n.º 1
0
 private static IEnumerable <string> GetActiveInputs(IWpfTextView activeView, PythonReplEvaluator eval)
 {
     return(eval.JoinCode(activeView.Selection.SelectedSpans.SelectMany(s => eval.SplitCode(s.GetText()))));
 }
Exemplo n.º 2
0
        private static void PasteReplCode(PythonReplEvaluator eval, string pasting) {
            // there's some text in the buffer...
            var view = eval.Window.TextView;
            var caret = view.Caret;

            if (view.Selection.IsActive && !view.Selection.IsEmpty) {
                foreach (var span in view.Selection.SelectedSpans) {
                    foreach (var normalizedSpan in view.BufferGraph.MapDownToBuffer(span, SpanTrackingMode.EdgeInclusive, eval.Window.CurrentLanguageBuffer)) {
                        normalizedSpan.Snapshot.TextBuffer.Delete(normalizedSpan);
                    }
                }
            }

            var curBuffer = eval.Window.CurrentLanguageBuffer;
            var inputPoint = view.BufferGraph.MapDownToBuffer(
                caret.Position.BufferPosition,
                PointTrackingMode.Positive,
                curBuffer,
                PositionAffinity.Successor
            );


            // if we didn't find a location then see if we're in a prompt, and if so, then we want
            // to insert after the prompt.
            if (caret.Position.BufferPosition != eval.Window.TextView.TextBuffer.CurrentSnapshot.Length) {
                for (int i = caret.Position.BufferPosition + 1;
                    inputPoint == null && i <= eval.Window.TextView.TextBuffer.CurrentSnapshot.Length;
                    i++) {
                    inputPoint = view.BufferGraph.MapDownToBuffer(
                        new SnapshotPoint(eval.Window.TextView.TextBuffer.CurrentSnapshot, i),
                        PointTrackingMode.Positive,
                        curBuffer,
                        PositionAffinity.Successor
                    );
                }
            }

            if (inputPoint == null) {
                // we didn't find a point to insert, insert at the beginning.
                inputPoint = new SnapshotPoint(curBuffer.CurrentSnapshot, 0);
            }
            
            // we want to insert the pasted code at the caret, but we also want to
            // respect the stepping.  So first grab the code before and after the caret.
            string startText = curBuffer.CurrentSnapshot.GetText(0, inputPoint.Value);

            string endText = curBuffer.CurrentSnapshot.GetText(
                inputPoint.Value,
                curBuffer.CurrentSnapshot.Length - inputPoint.Value);


            var splitCode = eval.JoinCode(eval.SplitCode(startText + pasting + endText)).ToList();
            curBuffer.Delete(new Span(0, curBuffer.CurrentSnapshot.Length));

            if (splitCode.Count == 1) {
                curBuffer.Insert(0, splitCode[0]);
                var viewPoint = view.BufferGraph.MapUpToBuffer(
                    new SnapshotPoint(curBuffer.CurrentSnapshot, Math.Min(inputPoint.Value.Position + pasting.Length, curBuffer.CurrentSnapshot.Length)),
                    PointTrackingMode.Positive,
                    PositionAffinity.Successor,
                    view.TextBuffer
                );

                if (viewPoint != null) {
                    view.Caret.MoveTo(viewPoint.Value);
                }
            } else if (splitCode.Count != 0) {
                var lastCode = splitCode[splitCode.Count - 1];
                splitCode.RemoveAt(splitCode.Count - 1);

                eval.Window.ReadyForInput += new PendLastSplitCode(eval.Window, lastCode).AppendCode;
                eval.Window.Submit(splitCode);
            } else {
                eval.Window.CurrentLanguageBuffer.Insert(0, startText + pasting + endText);
            }
        }
Exemplo n.º 3
0
 private static void TestOutput(MockReplWindow window, PythonReplEvaluator evaluator, string code, bool success, params string[] expectedOutput)
 {
     TestOutput(window, evaluator, code, success, null, true, 3000, expectedOutput);
 }
Exemplo n.º 4
0
        private static void TestOutput(MockReplWindow window, PythonReplEvaluator evaluator, string code, bool success, Action <bool> afterExecute, bool equalOutput, int timeout = 3000, params string[] expectedOutput)
        {
            window.ClearScreen();

            bool completed = false;
            var  task      = evaluator.ExecuteText(code).ContinueWith(completedTask => {
                Assert.AreEqual(success, completedTask.Result.IsSuccessful);

                var output = success ? window.Output : window.Error;
                if (equalOutput)
                {
                    if (output.Length == 0)
                    {
                        Assert.IsTrue(expectedOutput.Length == 0);
                    }
                    else
                    {
                        // don't count ending \n as new empty line
                        output = output.Replace("\r\n", "\n");
                        if (output[output.Length - 1] == '\n')
                        {
                            output = output.Remove(output.Length - 1, 1);
                        }

                        var lines = output.Split('\n');
                        if (lines.Length != expectedOutput.Length)
                        {
                            for (int i = 0; i < lines.Length; i++)
                            {
                                Console.WriteLine("{0}: {1}", i, lines[i].ToString());
                            }
                        }

                        Assert.AreEqual(lines.Length, expectedOutput.Length);
                        for (int i = 0; i < expectedOutput.Length; i++)
                        {
                            Assert.AreEqual(lines[i], expectedOutput[i]);
                        }
                    }
                }
                else
                {
                    foreach (var line in expectedOutput)
                    {
                        Assert.IsTrue(output.Contains(line), string.Format("'{0}' does not contain '{1}'", output, line));
                    }
                }

                completed = true;
            });

            if (afterExecute != null)
            {
                afterExecute(completed);
            }

            try {
                task.Wait(timeout);
            } catch (AggregateException ex) {
                if (ex.InnerException != null)
                {
                    throw ex.InnerException;
                }
                throw;
            }

            if (!completed)
            {
                Assert.Fail(string.Format("command didn't complete in {0} seconds", timeout / 1000.0));
            }
        }
Exemplo n.º 5
0
        public override CompletionSet GetCompletions(IGlyphService glyphService)
        {
            var start1 = _stopwatch.ElapsedMilliseconds;

            MemberResult[] members;

            IReplEvaluator      eval;
            PythonReplEvaluator dlrEval = null;

            if (_snapshot.TextBuffer.Properties.TryGetProperty <IReplEvaluator>(typeof(IReplEvaluator), out eval))
            {
                dlrEval = eval as PythonReplEvaluator;
            }

            var analysis = GetAnalysisEntry();

            if (analysis != null)
            {
                members = analysis.GetMembers(
                    Text,
                    _snapshot.GetLineNumberFromPosition(_pos) + 1,
                    _intersectMembers).ToArray();
            }
            else
            {
                members = new MemberResult[0];
            }

            if (dlrEval != null && _snapshot.TextBuffer.GetAnalyzer().ShouldEvaluateForCompletion(Text))
            {
                string text = Text;
                if (Text.EndsWith("."))
                {
                    text = Text.Substring(0, Text.Length - 1);
                }

                if (members.Length == 0)
                {
                    members = dlrEval.GetMemberNames(TextBuffer.GetAnalyzer(), text);
                    if (members == null)
                    {
                        members = new MemberResult[0];
                    }
                }
                else
                {
                    // prefer analysis members over live members but merge the two together.
                    Dictionary <string, MemberResult> memberDict = new Dictionary <string, MemberResult>();
                    var replMembers = dlrEval.GetMemberNames(TextBuffer.GetAnalyzer(), text);
                    if (replMembers != null)
                    {
                        foreach (var member in replMembers)
                        {
                            memberDict[member.Name] = member;
                        }

                        foreach (var member in members)
                        {
                            memberDict[member.Name] = member;
                        }

                        members = memberDict.Values.ToArray();
                    }
                }
            }

            members = DoFilterCompletions(members);
            Array.Sort(members, ModuleSort);

            var end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime)
            {
                Trace.WriteLine(String.Format("{0} lookup time {1} for {2} members", this, end - start1, members.Length));
            }

            var start = _stopwatch.ElapsedMilliseconds;

            var result = new PythonCompletionSet(
                Text,
                Text,
                _snapshot.CreateTrackingSpan(_pos, 0, SpanTrackingMode.EdgeInclusive),
                TransformMembers(glyphService, members),
                new Completion[0]);

            end = _stopwatch.ElapsedMilliseconds;

            if (/*Logging &&*/ (end - start1) > TooMuchTime)
            {
                Trace.WriteLine(String.Format("{0} completion set time {1} total time {2}", this, end - start, end - start1));
            }

            return(result);
        }