Exemple #1
0
        private static CompletionAnalysis TrySpecialCompletions(ReverseExpressionParser parser, Span loc)
        {
            if (parser.Tokens.Count > 0)
            {
                // Check for context-sensitive intellisense
                var lastClass = parser.Tokens[parser.Tokens.Count - 1];

                if (lastClass.ClassificationType == parser.Classifier.Provider.Comment)
                {
                    // No completions in comments
                    return(CompletionAnalysis.EmptyCompletionContext);
                }
                else if (lastClass.ClassificationType == parser.Classifier.Provider.StringLiteral)
                {
                    // String completion
                    return(new StringLiteralCompletionList(lastClass.Span.GetText(), loc.Start, parser.Span, parser.Buffer));
                }

                // Import completions
                var first = parser.Tokens[0];
                if (CompletionAnalysis.IsKeyword(first, "import"))
                {
                    return(ImportCompletionAnalysis.Make(first, lastClass, loc, parser.Snapshot, parser.Span, parser.Buffer, IsSpaceCompletion(parser, loc)));
                }
                else if (CompletionAnalysis.IsKeyword(first, "from"))
                {
                    return(FromImportCompletionAnalysis.Make(parser.Tokens, first, loc, parser.Snapshot, parser.Span, parser.Buffer, IsSpaceCompletion(parser, loc)));
                }
                return(null);
            }

            return(CompletionAnalysis.EmptyCompletionContext);
        }
        public static CompletionAnalysis Make(IList <ClassificationSpan> tokens, ITrackingSpan span, ITextBuffer textBuffer, CompletionOptions options)
        {
            Debug.Assert(tokens[0].Span.GetText() == "from");

            var  ns          = new List <string>();
            bool nsComplete  = false;
            bool seenImport  = false;
            bool seenName    = false;
            bool seenAs      = false;
            bool seenAlias   = false;
            bool includeStar = true;

            foreach (var token in tokens.Skip(1))
            {
                if (token == null || token.Span.End > span.GetEndPoint(textBuffer.CurrentSnapshot).Position)
                {
                    break;
                }

                if (!seenImport)
                {
                    if (token.ClassificationType.IsOfType(PredefinedClassificationTypeNames.Identifier))
                    {
                        ns.Add(token.Span.GetText());
                        nsComplete = true;
                    }
                    else if (token.ClassificationType.IsOfType(PythonPredefinedClassificationTypeNames.Dot))
                    {
                        nsComplete = false;
                    }
                    seenImport = IsKeyword(token, "import");
                }
                else if (token.ClassificationType.IsOfType(PythonPredefinedClassificationTypeNames.Comma))
                {
                    seenName    = false;
                    seenAs      = false;
                    seenAlias   = false;
                    includeStar = false;
                }
                else if (token.Span.GetText() == "*")
                {
                    // Nothing comes after a star
                    return(EmptyCompletionContext);
                }
                else if (IsKeyword(token, "as"))
                {
                    seenAs = true;
                }
                else if (token.ClassificationType.IsOfType(PredefinedClassificationTypeNames.Identifier))
                {
                    if (seenAlias)
                    {
                        return(EmptyCompletionContext);
                    }
                    else if (seenAs)
                    {
                        seenAlias = true;
                    }
                    else if (seenName)
                    {
                        return(EmptyCompletionContext);
                    }
                    else
                    {
                        seenName = true;
                    }
                }
                else
                {
                    includeStar = false;
                }
            }
            if (!seenImport)
            {
                if (nsComplete)
                {
                    return(new ImportKeywordCompletionAnalysis(span, textBuffer, options));
                }
                else
                {
                    return(ImportCompletionAnalysis.Make(tokens, span, textBuffer, options));
                }
            }

            if (!nsComplete || seenAlias || seenAs)
            {
                return(EmptyCompletionContext);
            }

            if (seenName)
            {
                return(new AsKeywordCompletionAnalysis(span, textBuffer, options));
            }

            return(new FromImportCompletionAnalysis(ns.ToArray(), includeStar, span, textBuffer, options));
        }