diff --git a/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py b/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py index 95f9665..c1dc8a2 100644 --- a/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py +++ b/src/VisualStudio/CodeTalk.LanguageService.Tests/Programs/PythonTests/pythonClasses.py @@ -4,6 +4,7 @@ class adder: def __init__(self, a, b): self.a=a self.b=b + self.list = [1,2,3,4,5,6] def getFirstValue(self): return self.a @@ -12,8 +13,24 @@ def getSecondValue(self): return self.b def add(self): - return self.a+self.b + a=0 + for i in self.list: + self.a = self.a+ i + return self.a + def addEvens(self): + l = len(self.list) + i=0 + sum=0 + while i in range(l): + if self.list[i]%2 == 0: + sum = sum + self.list[i] + return sum + + def returnOdds(self): + return [odds for num in self.list if num%2 != 0] + + #method that is not in any class: def outer_method(): return "bla" diff --git a/src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs b/src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs index 0becd50..c2051c0 100644 Binary files a/src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs and b/src/VisualStudio/CodeTalk.LanguageService.Tests/PythonTest.cs differ diff --git a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs index 9c478a7..dd24ce9 100644 --- a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs +++ b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCollector.cs @@ -8,10 +8,10 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using System.Diagnostics; -//using IronPython.Compiler.Ast; using IronPython.Compiler.Ast; -using System.Diagnostics; + namespace Microsoft.CodeTalk.LanguageService { @@ -97,21 +97,152 @@ public override void PostWalk(IronPython.Compiler.Ast.FunctionDefinition node) base.PostWalk(node); } - /// - /// Not realy sure of this override's purpose. may be used to override behavior when the current node being walked is an error. - /// - /// The python ast node being walked. - /// boolean value. not important for our implementation. this is handeled by a call to the base class. - public override bool Walk(ErrorExpression node) + public override bool Walk(IronPython.Compiler.Ast.ForStatement node) { + if (node == null) + { + return false; + } + var forBlock = PythonEntityCreationHelper.createForBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(forBlock); + forBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = forBlock; return true; } - public override bool Walk(Parameter node) + public override void PostWalk(IronPython.Compiler.Ast.ForStatement node) + { +if(node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + public override bool Walk(IronPython.Compiler.Ast.ComprehensionFor node) { + if (node == null) + { + return false; + } + var forBlock = PythonEntityCreationHelper.createComprehensionForBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(forBlock); + forBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = forBlock; return true; } + public override void PostWalk(IronPython.Compiler.Ast.ComprehensionFor node) + { + if (node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + public override bool Walk(IronPython.Compiler.Ast.WhileStatement node) + { + if(node == null) + { + return false; + } + var whileBlock = PythonEntityCreationHelper.createWhileBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(whileBlock); + whileBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = whileBlock; + return true; + + } + + public override void PostWalk(IronPython.Compiler.Ast.WhileStatement node) + { + if(node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + public override bool Walk(IronPython.Compiler.Ast.IfStatement node) + { + if (node == null) + { + return false; + } + var ifBlock = PythonEntityCreationHelper.createIfBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(ifBlock); + ifBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = ifBlock; + return true; + } + + public override void PostWalk(IronPython.Compiler.Ast.IfStatement node) + { + if(node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + public override bool Walk(IronPython.Compiler.Ast.ComprehensionIf node) + { + if (node == null) + { + return false; + } + var ifBlock = PythonEntityCreationHelper.createComprehensionIfBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(ifBlock); + ifBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = ifBlock; + return true; + } + + public override void PostWalk(IronPython.Compiler.Ast.ComprehensionIf node) + { + if (node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + public override bool Walk(IronPython.Compiler.Ast.TryStatement node) + { + if (node == null) + { + return false; + } + var tryBlock = PythonEntityCreationHelper.createTryBlock(node, m_currentCodeFile, m_currentParent); + m_currentParent.AddChild(tryBlock); + tryBlock.Parent = m_currentParent; + m_savedParents.Push(m_currentParent); + m_currentParent = tryBlock; + return true; + } + + public override void PostWalk(IronPython.Compiler.Ast.TryStatement node) + { + if (node == null) + { + return; + } + m_currentParent = m_savedParents.Pop(); + base.PostWalk(node); + } + + /// ///This method returns the populated CodeFile. /// diff --git a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs index a49212e..da0ea34 100644 --- a/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs +++ b/src/VisualStudio/CodeTalk.LanguageService/Languages/PythonEntityCreationHelper.cs @@ -36,8 +36,6 @@ internal static UserDefinedType createClassUDT(IronPython.Compiler.Ast.ClassDefi } /// - /// - /// /// The FunctionDefinition node /// The current CodeFile /// The parent of the current CodeFile @@ -59,5 +57,41 @@ internal static FunctionDefinition createFunction(IronPython.Compiler.Ast.Functi return fn; } + + internal static ForBlock createForBlock(IronPython.Compiler.Ast.ForStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + ForBlock forBlock = new ForBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return forBlock; + } + + internal static ForBlock createComprehensionForBlock(IronPython.Compiler.Ast.ComprehensionFor node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + ForBlock forBlock = new ForBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return forBlock; + } + + internal static WhileBlock createWhileBlock(IronPython.Compiler.Ast.WhileStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + WhileBlock whileBlock = new WhileBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return whileBlock; + } + + internal static IfBlock createIfBlock(IronPython.Compiler.Ast.IfStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + IfBlock ifBlock = new IfBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return ifBlock; + } + + internal static IfBlock createComprehensionIfBlock(IronPython.Compiler.Ast.ComprehensionIf node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + IfBlock ifBlock = new IfBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return ifBlock; + } + + internal static TryBlock createTryBlock(IronPython.Compiler.Ast.TryStatement node, CodeFile currentCodeFile, ISyntaxEntity parent) + { + TryBlock tryBlock = new TryBlock("", new FileSpan(node.Start.Line, node.Start.Column, node.End.Line, node.End.Column), parent, currentCodeFile); + return tryBlock; + } } }