Esempio n. 1
0
 public TemplateRegion(string text, TemplateTokenKind kind, DjangoBlock block, int start)
 {
     Text  = text;
     Kind  = kind;
     Start = start;
     Block = block;
 }
Esempio n. 2
0
        private static void ValidateForBlock(DjangoBlock expected, DjangoBlock got)
        {
            var forExpected = (DjangoForBlock)expected;
            var forGot      = (DjangoForBlock)got;

            Assert.AreEqual(forExpected.ParseInfo.Start, forGot.ParseInfo.Start);
            Assert.AreEqual(forExpected.InStart, forGot.InStart);
        }
Esempio n. 3
0
        private static void ValidateArgumentless(DjangoBlock expected, DjangoBlock got)
        {
            var aeExpected = (DjangoArgumentlessBlock)expected;
            var aeGot      = (DjangoArgumentlessBlock)got;

            Assert.AreEqual(aeExpected.ParseInfo.Start, aeGot.ParseInfo.Start);
            Assert.AreEqual(aeExpected.ParseInfo.Command, aeGot.ParseInfo.Command);
            Assert.AreEqual(aeExpected.ParseInfo.Args, aeGot.ParseInfo.Args);
        }
Esempio n. 4
0
        private static void ValidateFilter(DjangoBlock expected, DjangoBlock got)
        {
            var filterExpected = (DjangoFilterBlock)expected;
            var filterGot      = (DjangoFilterBlock)got;

            Assert.AreEqual(filterExpected.ParseInfo.Start, filterGot.ParseInfo.Start);
            Assert.AreEqual(filterExpected.ParseInfo.Command, filterGot.ParseInfo.Command);
            Assert.AreEqual(filterExpected.ParseInfo.Args, filterGot.ParseInfo.Args);
        }
Esempio n. 5
0
        private static void ValidateIfOrIfNotEqualBlock(DjangoBlock expected, DjangoBlock got)
        {
            var ifExpected = (DjangoIfOrIfNotEqualBlock)expected;
            var ifGot      = (DjangoIfOrIfNotEqualBlock)got;

            Assert.AreEqual(ifExpected.ParseInfo.Start, ifGot.ParseInfo.Start);
            Assert.AreEqual(ifExpected.ParseInfo.Command, ifGot.ParseInfo.Command);
            Assert.AreEqual(ifExpected.ParseInfo.Args, ifGot.ParseInfo.Args);
        }
Esempio n. 6
0
        private static void ValidateMultiArgumentBlock(DjangoBlock expected, DjangoBlock got)
        {
            var maExpected = (DjangoMultiVariableArgumentBlock)expected;
            var maGot      = (DjangoMultiVariableArgumentBlock)got;

            Assert.AreEqual(maExpected.ParseInfo.Start, maGot.ParseInfo.Start);
            Assert.AreEqual(maExpected.ParseInfo.Command, maGot.ParseInfo.Command);
            Assert.AreEqual(maExpected.ParseInfo.Args, maGot.ParseInfo.Args);
        }
Esempio n. 7
0
        private static void ValidateSpacelessBlock(DjangoBlock expected, DjangoBlock got)
        {
            var spacelessExpected = (DjangoSpacelessBlock)expected;
            var spacelessGot      = (DjangoSpacelessBlock)got;

            Assert.AreEqual(spacelessExpected.ParseInfo.Start, spacelessGot.ParseInfo.Start);
            Assert.AreEqual(spacelessExpected.ParseInfo.Command, spacelessGot.ParseInfo.Command);
            Assert.AreEqual(spacelessExpected.ParseInfo.Args, spacelessGot.ParseInfo.Args);
        }
Esempio n. 8
0
        private static void ValidateTemplateTagBlock(DjangoBlock expected, DjangoBlock got)
        {
            var tempTagExpected = (DjangoTemplateTagBlock)expected;
            var tempTagGot      = (DjangoTemplateTagBlock)got;

            Assert.AreEqual(tempTagExpected.ParseInfo.Start, tempTagGot.ParseInfo.Start);
            Assert.AreEqual(tempTagExpected.ParseInfo.Command, tempTagGot.ParseInfo.Command);
            Assert.AreEqual(tempTagExpected.ParseInfo.Args, tempTagGot.ParseInfo.Args);
        }
Esempio n. 9
0
        private static void ValidateWidthRatioBlock(DjangoBlock expected, DjangoBlock got)
        {
            var withExpected = (DjangoWidthRatioBlock)expected;
            var withGot      = (DjangoWidthRatioBlock)got;

            Assert.AreEqual(withExpected.ParseInfo.Start, withGot.ParseInfo.Start);
            Assert.AreEqual(withExpected.ParseInfo.Command, withGot.ParseInfo.Command);
            Assert.AreEqual(withExpected.ParseInfo.Args, withGot.ParseInfo.Args);
        }
Esempio n. 10
0
        protected void ClassifyTemplateBody(ITextSnapshot snapshot, List <ClassificationSpan> spans, TemplateRegion region, int prefixLength, int suffixLength)
        {
            switch (region.Kind)
            {
            case TemplateTokenKind.Comment:
                spans.Add(
                    new ClassificationSpan(
                        new SnapshotSpan(
                            snapshot,
                            new Span(region.Start + prefixLength, region.Text.Length - (prefixLength + suffixLength))
                            ),
                        _classifierProvider._commentClassType
                        )
                    );
                break;

            case TemplateTokenKind.Variable:
                var filterInfo = DjangoVariable.Parse(region.Text);

                if (filterInfo != null)
                {
                    foreach (var curSpan in filterInfo.GetSpans())
                    {
                        spans.Add(ToClassificationSpan(curSpan, snapshot, region.Start));
                    }
                }
                break;

            case TemplateTokenKind.Block:
                var blockInfo = region.Block ?? DjangoBlock.Parse(region.Text);
                if (blockInfo != null)
                {
                    foreach (var curSpan in blockInfo.GetSpans())
                    {
                        spans.Add(ToClassificationSpan(curSpan, snapshot, region.Start));
                    }
                }
                else if (region.Text.Length > (prefixLength + suffixLength))            // unterminated block at end of file
                {
                    spans.Add(
                        new ClassificationSpan(
                            new SnapshotSpan(
                                snapshot,
                                new Span(region.Start + prefixLength, region.Text.Length - (prefixLength + suffixLength))
                                ),
                            _classifierProvider._classType
                            )
                        );
                }
                break;
            }
        }
Esempio n. 11
0
        private static void ValidateIfBlock(DjangoBlock expected, DjangoBlock got)
        {
            var ifExpected = (DjangoIfBlock)expected;
            var ifGot      = (DjangoIfBlock)got;

            Assert.AreEqual(ifExpected.ParseInfo.Start, ifGot.ParseInfo.Start);
            Assert.AreEqual(ifExpected.ParseInfo.Command, ifGot.ParseInfo.Command);
            Assert.AreEqual(ifExpected.ParseInfo.Args, ifGot.ParseInfo.Args);
            Assert.AreEqual(ifExpected.Args.Length, ifGot.Args.Length);
            for (int i = 0; i < ifExpected.Args.Length; i++)
            {
                Assert.AreEqual(ifExpected.Args[i], ifGot.Args[i]);
            }
        }
        private static void ValidateUrlBlock(DjangoBlock expected, DjangoBlock got)
        {
            var urlExpected = (DjangoUrlBlock)expected;
            var urlGot      = (DjangoUrlBlock)got;

            Assert.AreEqual(urlExpected.ParseInfo.Start, urlGot.ParseInfo.Start);
            Assert.AreEqual(urlExpected.ParseInfo.Command, urlGot.ParseInfo.Command);
            Assert.AreEqual(urlExpected.ParseInfo.Args, urlGot.ParseInfo.Args);
            Assert.AreEqual(urlExpected.Args.Length, urlGot.Args.Length);
            for (int i = 0; i < urlExpected.Args.Length; i++)
            {
                Assert.AreEqual(urlExpected.Args[i], urlGot.Args[i]);
            }
        }
Esempio n. 13
0
        /// <param name="kind">The type of template tag we are processing</param>
        /// <param name="templateText">The text of the template tag which we are offering a completion in</param>
        /// <param name="templateStart">The offset in the buffer where the template starts</param>
        /// <param name="triggerPoint">The point in the buffer where the completion was triggered</param>
        internal CompletionSet GetCompletionSet(CompletionOptions options, VsProjectAnalyzer analyzer, TemplateTokenKind kind, string templateText, int templateStart, SnapshotPoint triggerPoint, out ITrackingSpan applicableSpan)
        {
            int position = triggerPoint.Position - templateStart;
            IEnumerable <CompletionInfo> tags;
            IDjangoCompletionContext     context;

            applicableSpan = GetWordSpan(templateText, templateStart, triggerPoint);

            switch (kind)
            {
            case TemplateTokenKind.Block:
                var block = DjangoBlock.Parse(templateText);
                if (block != null)
                {
                    if (position <= block.ParseInfo.Start + block.ParseInfo.Command.Length)
                    {
                        // we are completing before the command
                        // TODO: Return a new set of tags?  Do nothing?  Do this based upon ctrl-space?
                        tags = FilterBlocks(CompletionInfo.ToCompletionInfo(analyzer.GetTags(), StandardGlyphGroup.GlyphKeyword), triggerPoint);
                    }
                    else
                    {
                        // we are in the arguments, let the block handle the completions
                        context = new ProjectBlockCompletionContext(analyzer, _buffer);
                        tags    = block.GetCompletions(context, position);
                    }
                }
                else
                {
                    // no tag entered yet, provide the known list of tags.
                    tags = FilterBlocks(CompletionInfo.ToCompletionInfo(analyzer.GetTags(), StandardGlyphGroup.GlyphKeyword), triggerPoint);
                }
                break;

            case TemplateTokenKind.Variable:
                var variable = DjangoVariable.Parse(templateText);
                context = new ProjectBlockCompletionContext(analyzer, _buffer);
                if (variable != null)
                {
                    tags = variable.GetCompletions(context, position);
                }
                else
                {
                    // show variable names
                    tags = CompletionInfo.ToCompletionInfo(context.Variables, StandardGlyphGroup.GlyphGroupVariable);
                }

                break;

            default:
                throw new InvalidOperationException();
            }

            var completions = tags
                              .OrderBy(tag => tag.DisplayText, StringComparer.OrdinalIgnoreCase)
                              .Select(tag => new DynamicallyVisibleCompletion(
                                          tag.DisplayText,
                                          tag.InsertionText,
                                          StripDocumentation(tag.Documentation),
                                          _glyphService.GetGlyph(tag.Glyph, StandardGlyphItem.GlyphItemPublic),
                                          "tag"));

            return(new FuzzyCompletionSet(
                       "PythonDjangoTags",
                       Resources.DjangoTagsCompletionSetDisplayName,
                       applicableSpan,
                       completions,
                       options,
                       CompletionComparer.UnderscoresLast));
        }
Esempio n. 14
0
        private void ValidateBlock(DjangoBlock expected, DjangoBlock got)
        {
            Assert.AreEqual(expected.GetType(), got.GetType());

            _blockValidators[expected.GetType()](expected, got);
        }
Esempio n. 15
0
        public void BlockParserTests()
        {
            var testCases = new[] {
                new {
                    Got         = ("for x in "),
                    Expected    = (DjangoBlock) new DjangoForBlock(new BlockParseInfo("for", "x in ", 0), 6, null, 9, -1, new[] { new Tuple <string, int>("x", 4) }),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 9,
                            Expected = new[] { "fob", "oar" }
                        },
                        new {
                            Position = 4,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("for x in oar"),
                    Expected    = (DjangoBlock) new DjangoForBlock(new BlockParseInfo("for", "x in oar", 0), 6, DjangoVariable.Variable("oar", 9), 12, -1, new[] { new Tuple <string, int>("x", 4) }),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 9,
                            Expected = new[] { "fob", "oar" }
                        },
                        new {
                            Position = 4,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("for x in b"),
                    Expected    = (DjangoBlock) new DjangoForBlock(new BlockParseInfo("for", "x in b", 0), 6, DjangoVariable.Variable("b", 9), 10, -1, new[] { new Tuple <string, int>("x", 4) }),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 10,
                            Expected = new [] { "fob", "oar" }
                        },
                        new {
                            Position = 4,
                            Expected = new string[0]
                        }
                    }
                },

                new {
                    Got         = ("autoescape"),
                    Expected    = (DjangoBlock) new DjangoAutoEscapeBlock(new BlockParseInfo("autoescape", "", 0), -1, -1),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 10,
                            Expected = new[] { "on", "off" }
                        }
                    }
                },
                new {
                    Got         = ("autoescape on"),
                    Expected    = (DjangoBlock) new DjangoAutoEscapeBlock(new BlockParseInfo("autoescape", " on", 0), 11, 2),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 10,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("comment"),
                    Expected    = (DjangoBlock) new DjangoArgumentlessBlock(new BlockParseInfo("comment", "", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 0,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("spaceless"),
                    Expected    = (DjangoBlock) new DjangoSpacelessBlock(new BlockParseInfo("spaceless", "", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 0,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("filter "),
                    Expected    = (DjangoBlock) new DjangoFilterBlock(new BlockParseInfo("filter", " ", 0), DjangoVariable.Variable("", 7)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 7,
                            Expected = new [] { "cut", "lower" }
                        }
                    }
                },
                new {
                    Got         = ("ifequal "),
                    Expected    = (DjangoBlock) new DjangoIfOrIfNotEqualBlock(new BlockParseInfo("ifequal", " ", 0), DjangoVariable.Variable("", 8)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 8,
                            Expected = new [] { "fob", "oar" }
                        }
                    }
                },
                new {
                    Got         = ("ifequal fob "),
                    Expected    = (DjangoBlock) new DjangoIfOrIfNotEqualBlock(new BlockParseInfo("ifequal", " fob ", 0), DjangoVariable.Variable("fob", 8)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 12,
                            Expected = new [] { "fob", "oar" }
                        }
                    }
                },
                new {
                    Got         = ("if "),
                    Expected    = (DjangoBlock) new DjangoIfBlock(new BlockParseInfo("if", " ", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 3,
                            Expected = new [] { "fob", "oar", "not" }
                        }
                    }
                },
                new {
                    Got         = ("if fob "),
                    Expected    = (DjangoBlock) new DjangoIfBlock(new BlockParseInfo("if", " fob ", 0), new BlockClassification(new Span(3, 3), Classification.Identifier)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 7,
                            Expected = new [] { "and", "or" }
                        }
                    }
                },
                new {
                    Got         = ("if fob and "),
                    Expected    = (DjangoBlock) new DjangoIfBlock(new BlockParseInfo("if", " fob and ", 0), new BlockClassification(new Span(3, 3), Classification.Identifier), new BlockClassification(new Span(7, 3), Classification.Keyword)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 11,
                            Expected = new [] { "fob", "oar", "not" }
                        }
                    }
                },
                new {
                    Got         = ("firstof "),
                    Expected    = (DjangoBlock) new DjangoMultiVariableArgumentBlock(new BlockParseInfo("firstof", " ", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 8,
                            Expected = new [] { "fob", "oar" }
                        }
                    }
                },
                new {
                    Got         = ("firstof fob|"),
                    Expected    = (DjangoBlock) new DjangoMultiVariableArgumentBlock(new BlockParseInfo("firstof", " fob|", 0), DjangoVariable.Variable("fob", 8)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 12,
                            Expected = new [] { "cut", "lower" }
                        }
                    }
                },
                new {
                    Got         = ("spaceless "),
                    Expected    = (DjangoBlock) new DjangoSpacelessBlock(new BlockParseInfo("spaceless", " ", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 10,
                            Expected = new string[0]
                        }
                    }
                },
                new {
                    Got         = ("widthratio "),
                    Expected    = (DjangoBlock) new DjangoWidthRatioBlock(new BlockParseInfo("widthratio", " ", 0)),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 11,
                            Expected = new [] { "fob", "oar" }
                        }
                    }
                },
                new {
                    Got         = ("templatetag "),
                    Expected    = (DjangoBlock) new DjangoTemplateTagBlock(new BlockParseInfo("templatetag", " ", 0), 11, null),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 11,
                            Expected = new [] { "openblock", "closeblock", "openvariable", "closevariable", "openbrace", "closebrace", "opencomment", "closecomment" }
                        }
                    }
                },
                new {
                    Got         = ("templatetag open"),
                    Expected    = (DjangoBlock) new DjangoTemplateTagBlock(new BlockParseInfo("templatetag", " open", 0), 11, null),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 15,
                            Expected = new [] { "openblock", "openvariable", "openbrace", "opencomment" }
                        }
                    }
                },
                new {
                    Got         = ("templatetag openblock "),
                    Expected    = (DjangoBlock) new DjangoTemplateTagBlock(new BlockParseInfo("templatetag", " openblock ", 0), 11, "openblock"),
                    Context     = TestCompletionContext.Simple,
                    Completions = new[] {
                        new {
                            Position = 22,
                            Expected = new string[0]
                        }
                    }
                },
            };

            foreach (var testCase in testCases)
            {
                Console.WriteLine(testCase.Got);

                var got = DjangoBlock.Parse(testCase.Got);

                ValidateBlock(testCase.Expected, got);

                foreach (var completionCase in testCase.Completions)
                {
                    var completions = new HashSet <string>(got.GetCompletions(testCase.Context, completionCase.Position).Select(x => x.DisplayText));

                    Assert.AreEqual(completionCase.Expected.Length, completions.Count);
                    var expected = new HashSet <string>(completionCase.Expected);
                    foreach (var value in completions)
                    {
                        Assert.IsTrue(expected.Contains(value));
                    }
                }
            }
        }
Esempio n. 16
0
 public TemplateRegion(string text, TemplateTokenKind kind, DjangoBlock block, int start) {
     Text = text;
     Kind = kind;
     Start = start;
     Block = block;
 }