예제 #1
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, DjangoAnalyzer 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._tags, 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._tags, 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",
                "Django Tags",
                applicableSpan,
                completions,
                options,
                CompletionComparer.UnderscoresLast);
        }
예제 #2
0
 public ProjectBlockCompletionContext(DjangoAnalyzer analyzer, ITextBuffer buffer)
     : base(analyzer, buffer, TemplateProjectionBuffer.GetFilePath(buffer)) {
     TemplateProjectionBuffer projBuffer;
     if (buffer.Properties.TryGetProperty(typeof(TemplateProjectionBuffer), out projBuffer)) {
         foreach (var span in projBuffer.Spans) {
             if (span.Block != null) {
                 foreach (var variable in span.Block.GetVariables()) {
                     AddLoopVariable(variable);
                 }
             }
         }
     }
 }
예제 #3
0
        public ProjectBlockCompletionContext(DjangoAnalyzer analyzer, ITextBuffer buffer)
            : base(analyzer, buffer, buffer.GetFileName()) {

            var doc = HtmlEditorDocument.FromTextBuffer(buffer);
            if (doc == null) {
                return;
            }

            var artifacts = doc.HtmlEditorTree.ArtifactCollection;
            foreach (var artifact in artifacts.OfType<TemplateBlockArtifact>()) {
                var artifactText = doc.HtmlEditorTree.ParseTree.Text.GetText(artifact.InnerRange);
                artifact.Parse(artifactText);
                if (artifact.Block != null) {
                    var varNames = artifact.Block.GetVariables();
                    foreach (var varName in varNames) {
                        AddLoopVariable(varName);
                    }
                }
            }
        }
예제 #4
0
        private DjangoAnalyzer AnalyzerTest(string path) {
            string djangoDbPath = TestData.GetPath("TestData\\DjangoDB");
            Assert.IsTrue(
                PythonTypeDatabase.IsDatabaseVersionCurrent(djangoDbPath),
                "TestData\\DjangoDB needs updating."
            );

            var testFact = InterpreterFactoryCreator.CreateAnalysisInterpreterFactory(
                new Version(2, 7),
                "Django Test Interpreter",
                TestData.GetPath("CompletionDB"),
                djangoDbPath
            );

            var serviceProvider = PythonToolsTestUtilities.CreateMockServiceProvider();
            PythonAnalyzer analyzer = PythonAnalyzer.CreateAsync(testFact).WaitAndUnwrapExceptions();
            DjangoAnalyzer djangoAnalyzer = new DjangoAnalyzer(serviceProvider);
            djangoAnalyzer.OnNewAnalyzer(analyzer);

            analyzer.AddAnalysisDirectory(path);

            List<IPythonProjectEntry> entries = new List<IPythonProjectEntry>();
            foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories)) {
                var entry = analyzer.AddModule(ModulePath.FromFullPath(file).ModuleName, file);
                var parser = Parser.CreateParser(
                    new FileStream(file, FileMode.Open, FileAccess.Read),
                    PythonLanguageVersion.V27
                );
                entry.UpdateTree(parser.ParseFile(), null);
                entries.Add(entry);
            }

            foreach (var entry in entries) {
                entry.Analyze(CancellationToken.None, false);
            }

            return djangoAnalyzer;
        }
 public ProjectBlockCompletionContextBase(DjangoAnalyzer analyzer, ITextBuffer buffer, string filename) {
     _analyzer = analyzer;
     _module = buffer.GetModuleContext(analyzer._serviceProvider);
     _filename = filename;
 }
예제 #6
0
 protected DjangoCompletionSourceBase(IGlyphService glyphService, DjangoAnalyzer analyzer, ITextBuffer textBuffer) {
     _glyphService = glyphService;
     _analyzer = analyzer;
     _buffer = textBuffer;
 }
예제 #7
0
 public GetTemplateAnalysisValue(DjangoAnalyzer analyzer, string name)
 {
     Analyzer     = analyzer;
     Filename     = name;
     RenderMethod = new TemplateRenderMethod(this);
 }
예제 #8
0
 public DeferredDecorator(DjangoAnalyzer analyzer, IAnalysisSet name, Dictionary <string, TagInfo> tags)
 {
     _analyzer = analyzer;
     _name     = name;
     _tags     = tags;
 }
예제 #9
0
 public DjangoCompletionSource(IGlyphService glyphService, DjangoAnalyzer analyzer, ITextBuffer textBuffer)
     : base(glyphService, analyzer, textBuffer) {
 }
예제 #10
0
 public GetTemplateAnalysisValue(DjangoAnalyzer analyzer, string name) {
     Analyzer = analyzer;
     Filename = name;
     RenderMethod = new TemplateRenderMethod(this);
 }
예제 #11
0
 public DeferredDecorator(DjangoAnalyzer analyzer, IAnalysisSet name, Dictionary<string, TagInfo> tags) {
     _analyzer = analyzer;
     _name = name;
     _tags = tags;
 }