Exemple #1
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);
                    }
                }
            }
        }
Exemple #2
0
        private DjangoAnalyzer AnalyzerTest(string path, out PythonLanguageVersion languageVersion)
        {
            var version = PythonPaths.Versions.LastOrDefault(v => Directory.Exists(Path.Combine(v.PrefixPath, "Lib", "site-packages", "django")));

            version.AssertInstalled();

            var testFact = InterpreterFactoryCreator.CreateInterpreterFactory(version.Configuration, new InterpreterFactoryCreationOptions {
                DatabasePath     = TestData.GetTempPath(),
                UseExistingCache = false,
                TraceLevel       = TraceLevel.Verbose,
                WatchFileSystem  = false
            });

            Debug.WriteLine("Testing with {0}".FormatInvariant(version.InterpreterPath));
            languageVersion = testFact.GetLanguageVersion();

            var analyzer       = PythonAnalyzer.CreateAsync(testFact).WaitAndUnwrapExceptions();
            var djangoAnalyzer = new DjangoAnalyzer();

            djangoAnalyzer.Register(analyzer);

            var entries = new List <IPythonProjectEntry>();

            foreach (string file in Directory.EnumerateFiles(path, "*.py", SearchOption.AllDirectories))
            {
                if (!ModulePath.FromBasePathAndFile_NoThrow(path, file, out var mp))
                {
                    Debug.WriteLine("Not parsing {0}".FormatInvariant(file));
                    continue;
                }
                Debug.WriteLine("Parsing {0} ({1})".FormatInvariant(mp.FullName, file));
                var entry  = analyzer.AddModule(mp.ModuleName, file);
                var parser = Parser.CreateParser(
                    new FileStream(file, FileMode.Open, FileAccess.Read),
                    testFact.GetLanguageVersion()
                    );
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                entries.Add(entry);
            }

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

            Debug.WriteLine((testFact as IPythonInterpreterFactoryWithLog)?.GetAnalysisLogContent(CultureInfo.CurrentUICulture) ?? "(no logs)");

            return(djangoAnalyzer);
        }
Exemple #3
0
        private DjangoAnalyzer AnalyzerTest(string path)
        {
            string djangoDbPath = TestData.GetPath("TestData\\DjangoDB");

            Assert.IsTrue(
                PythonTypeDatabase.IsDatabaseVersionCurrent(djangoDbPath),
                "TestData\\DjangoDB needs updating."
                );

            var testFact = PythonInterpreterFactoryWithDatabase.CreateFromDatabase(
                new Version(2, 7),
                TestData.GetPath("CompletionDB"),
                djangoDbPath
                );

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

            djangoAnalyzer.Register(analyzer);

            analyzer.SetSearchPaths(new[] { 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
                    );
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                entries.Add(entry);
            }

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

            return(djangoAnalyzer);
        }
Exemple #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        = new PythonAnalyzer(testFact);
            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);
        }
Exemple #5
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);
                        }
                    }
                }
            }
        }
Exemple #6
0
        private DjangoAnalyzer AnalyzerTest(string path)
        {
            var version  = new Version(2, 7);
            var testFact = new AstPythonInterpreterFactory(
                new InterpreterConfiguration($"AnalysisOnly|{version}", $"Analysis Only {version}", version: version, uiMode: InterpreterUIMode.Normal),
                new InterpreterFactoryCreationOptions {
                WatchFileSystem = false
            }
                );

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

            djangoAnalyzer.Register(analyzer);

            analyzer.SetSearchPaths(new[] { 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
                    );
                using (var p = entry.BeginParse()) {
                    p.Tree = parser.ParseFile();
                    p.Complete();
                }
                entries.Add(entry);
            }

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

            return(djangoAnalyzer);
        }
Exemple #7
0
 public DjangoCompletionSource(IGlyphService glyphService, DjangoAnalyzer analyzer, ITextBuffer textBuffer)
     : base(glyphService, analyzer, textBuffer)
 {
 }
 public ProjectBlockCompletionContextBase(DjangoAnalyzer analyzer, ITextBuffer buffer, string filename)
 {
     _analyzer = analyzer;
     _module   = buffer.GetModuleContext(analyzer._serviceProvider);
     _filename = filename;
 }
Exemple #9
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();
            djangoAnalyzer.Register(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;
        }
        /// <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));
        }
 protected DjangoCompletionSourceBase(IGlyphService glyphService, DjangoAnalyzer analyzer, ITextBuffer textBuffer)
 {
     _glyphService = glyphService;
     _analyzer     = analyzer;
     _buffer       = textBuffer;
 }