Ejemplo n.º 1
0
        public void StringCompletionFileEntries()
        {
            var cwd  = TestData.GetPath("TestData");
            var user = TestData.GetPath("TestData\\Databases");

            AssertUtil.ContainsAtLeast(
                StringLiteralCompletionList.GetEntryInfo(cwd + "\\AbsolutePaths", cwd, user),
                MakeEntryInfo(cwd, "AbsolutePath"),
                MakeEntryInfo(cwd, "AbsolutePath.sln"),
                MakeEntryInfo(cwd, "HelloWorld"),
                MakeEntryInfo(cwd, "HelloWorld.sln")
                );
            AssertUtil.ContainsAtLeast(
                StringLiteralCompletionList.GetEntryInfo(cwd + "\\AbsolutePath\\", cwd, user),
                MakeEntryInfo(cwd + "\\AbsolutePath", "AbsolutePath.pyproj")
                );
            AssertUtil.ContainsAtLeast(
                StringLiteralCompletionList.GetEntryInfo("./AbsolutePaths", cwd, user),
                MakeEntryInfo(cwd, "AbsolutePath", ".\\AbsolutePath\\"),
                MakeEntryInfo(cwd, "AbsolutePath.sln", ".\\AbsolutePath.sln"),
                MakeEntryInfo(cwd, "HelloWorld", ".\\HelloWorld\\"),
                MakeEntryInfo(cwd, "HelloWorld.sln", ".\\HelloWorld.sln")
                );
            AssertUtil.ContainsAtLeast(
                StringLiteralCompletionList.GetEntryInfo(".\\Ab", cwd, user),
                MakeEntryInfo(cwd, "AbsolutePath", ".\\AbsolutePath\\"),
                MakeEntryInfo(cwd, "AbsolutePath.sln", ".\\AbsolutePath.sln"),
                MakeEntryInfo(cwd, "HelloWorld", ".\\HelloWorld\\"),
                MakeEntryInfo(cwd, "HelloWorld.sln", ".\\HelloWorld.sln")
                );
            AssertUtil.ContainsAtLeast(
                StringLiteralCompletionList.GetEntryInfo("~/Ab", cwd, user),
                MakeEntryInfo(user, "V27", "~\\V27\\"),
                MakeEntryInfo(user, "Readme.txt", "~\\Readme.txt")
                );
            AssertUtil.ContainsAtLeast(
                StringLiteralCompletionList.GetEntryInfo("~\\Ab", cwd, user),
                MakeEntryInfo(user, "V27", "~\\V27\\"),
                MakeEntryInfo(user, "Readme.txt", "~\\Readme.txt")
                );
            AssertUtil.ContainsAtLeast(
                StringLiteralCompletionList.GetEntryInfo("~\\V27\\", cwd, user),
                MakeEntryInfo(user + "\\V27", "ntpath.idb", "~\\V27\\ntpath.idb"),
                MakeEntryInfo(user + "\\V27", "os.idb", "~\\V27\\os.idb")
                );
            AssertUtil.ContainsAtLeast(
                StringLiteralCompletionList.GetEntryInfo("Ab", cwd, user)
                );
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Returns the applicable span at the provided position.
        /// </summary>
        /// <returns>A tracking span, or null if there is no token at the
        /// provided position.</returns>
        internal static ITrackingSpan GetApplicableSpan(this ITextSnapshot snapshot, int position)
        {
            var classifier = snapshot.TextBuffer.GetPythonClassifier();
            var line       = snapshot.GetLineFromPosition(position);

            if (classifier == null || line == null)
            {
                return(null);
            }

            var spanLength = position - line.Start.Position;

            // Increase position by one to include 'fob' in: "abc.|fob"
            if (spanLength < line.Length)
            {
                spanLength += 1;
            }

            var classifications = classifier.GetClassificationSpans(new SnapshotSpan(line.Start, spanLength));

            // Handle "|"
            if (classifications == null || classifications.Count == 0)
            {
                return(null);
            }

            var lastToken = classifications[classifications.Count - 1];

            // Handle "fob |"
            if (lastToken == null || position > lastToken.Span.End)
            {
                return(null);
            }

            if (position > lastToken.Span.Start)
            {
                if (lastToken.ClassificationType.IsOfType(PredefinedClassificationTypeNames.String))
                {
                    // Handle "'contents of strin|g"
                    var text = lastToken.Span.GetText();
                    var span = StringLiteralCompletionList.GetStringContentSpan(text, lastToken.Span.Start) ?? lastToken.Span;

                    return(snapshot.CreateTrackingSpan(span, SpanTrackingMode.EdgeInclusive));
                }
                else if (lastToken.CanComplete())
                {
                    // Handle "fo|o"
                    return(snapshot.CreateTrackingSpan(lastToken.Span, SpanTrackingMode.EdgeInclusive));
                }
                else
                {
                    // Handle "<|="
                    return(null);
                }
            }

            var secondLastToken = classifications.Count >= 2 ? classifications[classifications.Count - 2] : null;

            if (lastToken.Span.Start == position && lastToken.CanComplete() &&
                (secondLastToken == null ||             // Handle "|fob"
                 position > secondLastToken.Span.End || // Handle "if |fob"
                 !secondLastToken.CanComplete()))       // Handle "abc.|fob"
            {
                return(snapshot.CreateTrackingSpan(lastToken.Span, SpanTrackingMode.EdgeInclusive));
            }

            // Handle "abc|."
            // ("ab|c." would have been treated as "ab|c")
            if (secondLastToken != null && secondLastToken.Span.End == position && secondLastToken.CanComplete())
            {
                return(snapshot.CreateTrackingSpan(secondLastToken.Span, SpanTrackingMode.EdgeInclusive));
            }

            return(null);
        }