Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@5434 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61pull/1/head
24 changed files with 613 additions and 138 deletions
@ -0,0 +1,26 @@
@@ -0,0 +1,26 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
public class PythonBuiltInModuleMemberName : MemberName |
||||
{ |
||||
public const string PythonBuiltInModuleName = "__builtin__"; |
||||
|
||||
public PythonBuiltInModuleMemberName(string memberName) |
||||
: base(PythonBuiltInModuleName, memberName) |
||||
{ |
||||
} |
||||
|
||||
public static bool IsBuiltInModule(string name) |
||||
{ |
||||
return name == PythonBuiltInModuleName; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,32 @@
@@ -0,0 +1,32 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
public class PythonDotNetMethodResolver |
||||
{ |
||||
PythonClassResolver classResolver; |
||||
|
||||
public PythonDotNetMethodResolver(PythonClassResolver classResolver) |
||||
{ |
||||
this.classResolver = classResolver; |
||||
} |
||||
|
||||
public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult) |
||||
{ |
||||
MemberName memberName = new MemberName(expressionResult.Expression); |
||||
IClass matchingClass = classResolver.GetClass(resolverContext, memberName.Type); |
||||
if (matchingClass != null) { |
||||
return new PythonMethodGroupResolveResult(matchingClass, memberName.Name); |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,20 @@
@@ -0,0 +1,20 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
public class PythonMethodGroupResolveResult : MethodGroupResolveResult |
||||
{ |
||||
public PythonMethodGroupResolveResult(IClass containingClass, string methodName) |
||||
: base(null, null, containingClass.DefaultReturnType, methodName) |
||||
{ |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
public class PythonStandardModuleMethodGroupResolveResult : MethodGroupResolveResult |
||||
{ |
||||
public PythonStandardModuleMethodGroupResolveResult(IReturnType containingType, string methodName, MethodGroup[] methodGroups) |
||||
: base(null, null, containingType, methodName, methodGroups) |
||||
{ |
||||
} |
||||
|
||||
public static PythonStandardModuleMethodGroupResolveResult Create(PythonStandardModuleType type, string methodName) |
||||
{ |
||||
PythonModuleCompletionItems completionItems = PythonModuleCompletionItemsFactory.Create(type); |
||||
MethodGroup methods = completionItems.GetMethods(methodName); |
||||
if (methods.Count > 0) { |
||||
return Create(methods); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
static PythonStandardModuleMethodGroupResolveResult Create(MethodGroup methods) |
||||
{ |
||||
MethodGroup[] methodGroups = new MethodGroup[] { methods }; |
||||
IMethod method = methods[0]; |
||||
IReturnType returnType = new DefaultReturnType(method.DeclaringType); |
||||
return new PythonStandardModuleMethodGroupResolveResult(returnType, method.Name, methodGroups); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,84 @@
@@ -0,0 +1,84 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
public class PythonStandardModuleMethodResolver |
||||
{ |
||||
PythonStandardModuleResolver standardModuleResolver; |
||||
|
||||
public PythonStandardModuleMethodResolver(PythonStandardModuleResolver standardModuleResolver) |
||||
{ |
||||
this.standardModuleResolver = standardModuleResolver; |
||||
} |
||||
|
||||
public ResolveResult Resolve(PythonResolverContext resolverContext, ExpressionResult expressionResult) |
||||
{ |
||||
MemberName memberName = new MemberName(expressionResult.Expression); |
||||
MethodGroupResolveResult result = ResolveMethodFromImportedNames(resolverContext, memberName); |
||||
if (result != null) { |
||||
return result; |
||||
} |
||||
result = ResolveIfMethodIsImported(resolverContext, memberName); |
||||
if (result != null) { |
||||
return result; |
||||
} |
||||
return ResolveMethodFromModulesThatImportEverything(resolverContext, memberName); |
||||
} |
||||
|
||||
MethodGroupResolveResult ResolveMethodFromImportedNames(PythonResolverContext resolverContext, MemberName memberName) |
||||
{ |
||||
if (!memberName.HasName) { |
||||
string name = memberName.Type; |
||||
string moduleName = resolverContext.GetModuleForImportedName(name); |
||||
if (moduleName != null) { |
||||
PythonStandardModuleType type = standardModuleResolver.GetStandardModuleType(moduleName); |
||||
if (type != null) { |
||||
name = resolverContext.UnaliasImportedName(name); |
||||
return PythonStandardModuleMethodGroupResolveResult.Create(type, name); |
||||
} |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
MethodGroupResolveResult ResolveIfMethodIsImported(PythonResolverContext resolverContext, MemberName memberName) |
||||
{ |
||||
if (!memberName.HasName) { |
||||
memberName = new PythonBuiltInModuleMemberName(memberName.Type); |
||||
} |
||||
|
||||
PythonStandardModuleType type = standardModuleResolver.GetStandardModuleTypeIfImported(resolverContext, memberName.Type); |
||||
if (type != null) { |
||||
return CreateResolveResult(type, memberName.Name); |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
MethodGroupResolveResult ResolveMethodFromModulesThatImportEverything(PythonResolverContext resolverContext, MemberName memberName) |
||||
{ |
||||
foreach (string module in resolverContext.GetModulesThatImportEverything()) { |
||||
PythonStandardModuleType type = standardModuleResolver.GetStandardModuleType(module); |
||||
if (type != null) { |
||||
MethodGroupResolveResult resolveResult = CreateResolveResult(type, memberName.Type); |
||||
if (resolveResult != null) { |
||||
return resolveResult; |
||||
} |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
MethodGroupResolveResult CreateResolveResult(PythonStandardModuleType type, string name) |
||||
{ |
||||
return PythonStandardModuleMethodGroupResolveResult.Create(type, name); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using ICSharpCode.PythonBinding; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using NUnit.Framework; |
||||
|
||||
namespace PythonBinding.Tests.Parsing |
||||
{ |
||||
[TestFixture] |
||||
public class ParseFromMathImportAllTestFixture |
||||
{ |
||||
ICompilationUnit compilationUnit; |
||||
PythonImport import; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
string python = "from math import *"; |
||||
|
||||
DefaultProjectContent projectContent = new DefaultProjectContent(); |
||||
PythonParser parser = new PythonParser(); |
||||
compilationUnit = parser.Parse(projectContent, @"C:\test.py", python); |
||||
import = compilationUnit.UsingScope.Usings[0] as PythonImport; |
||||
} |
||||
|
||||
[Test] |
||||
public void PythonImportContainsMathModuleName() |
||||
{ |
||||
Assert.AreEqual("math", import.Module); |
||||
} |
||||
|
||||
[Test] |
||||
public void PythonImportImportsEverythingReturnsTrue() |
||||
{ |
||||
Assert.IsTrue(import.ImportsEverything); |
||||
} |
||||
|
||||
[Test] |
||||
public void PythonImportGetIdentifierForAliasDoesNotThrowNullReferenceException() |
||||
{ |
||||
string identifier = String.Empty; |
||||
Assert.DoesNotThrow(delegate { identifier = import.GetOriginalNameForAlias("unknown"); }); |
||||
Assert.IsNull(identifier); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,41 @@
@@ -0,0 +1,41 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.PythonBinding; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using ICSharpCode.SharpDevelop.Dom.CSharp; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Resolver |
||||
{ |
||||
[TestFixture] |
||||
public class ResolveMethodFromUnknownImportAllTestFixture : ResolveTestFixtureBase |
||||
{ |
||||
protected override ExpressionResult GetExpressionResult() |
||||
{ |
||||
return new ExpressionResult("methodcall", ExpressionContext.Default); |
||||
} |
||||
|
||||
protected override string GetPythonScript() |
||||
{ |
||||
return |
||||
"from unknown import *\r\n" + |
||||
"methodcall\r\n" + |
||||
"\r\n"; |
||||
} |
||||
|
||||
[Test] |
||||
public void ResolveResultIsNull() |
||||
{ |
||||
Assert.IsNull(resolveResult); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.PythonBinding; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Resolver |
||||
{ |
||||
[TestFixture] |
||||
public class ResolveMethodWhenFromImportIsUnknownTestFixture : ResolveTestFixtureBase |
||||
{ |
||||
protected override ExpressionResult GetExpressionResult() |
||||
{ |
||||
return new ExpressionResult("methodcall", ExpressionContext.Default); |
||||
} |
||||
|
||||
protected override string GetPythonScript() |
||||
{ |
||||
return |
||||
"from unknown import methodcall\r\n" + |
||||
"methodcall\r\n" + |
||||
"\r\n"; |
||||
} |
||||
|
||||
[Test] |
||||
public void ResolveResultIsNull() |
||||
{ |
||||
Assert.IsNull(resolveResult); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,40 @@
@@ -0,0 +1,40 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.PythonBinding; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Resolver |
||||
{ |
||||
[TestFixture] |
||||
public class ResolveMethodWhenImportIsUnknownTestFixture : ResolveTestFixtureBase |
||||
{ |
||||
protected override ExpressionResult GetExpressionResult() |
||||
{ |
||||
return new ExpressionResult("unknown.methodcall", ExpressionContext.Default); |
||||
} |
||||
|
||||
protected override string GetPythonScript() |
||||
{ |
||||
return |
||||
"from unknown import methodcall\r\n" + |
||||
"unknown.methodcall\r\n" + |
||||
"\r\n"; |
||||
} |
||||
|
||||
[Test] |
||||
public void ResolveResultIsNull() |
||||
{ |
||||
Assert.IsNull(resolveResult); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,67 @@
@@ -0,0 +1,67 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using System.Collections; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.PythonBinding; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using ICSharpCode.SharpDevelop.Dom.CSharp; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Resolver |
||||
{ |
||||
[TestFixture] |
||||
public class ResolveTanMethodFromMathImportAllTestFixture : ResolveTestFixtureBase |
||||
{ |
||||
protected override ExpressionResult GetExpressionResult() |
||||
{ |
||||
return new ExpressionResult("tan", ExpressionContext.Default); |
||||
} |
||||
|
||||
protected override string GetPythonScript() |
||||
{ |
||||
return |
||||
"from sys import *\r\n" + |
||||
"from math import *\r\n" + |
||||
"from socket import *\r\n" + |
||||
"\r\n" + |
||||
"tan\r\n" + |
||||
"\r\n"; |
||||
} |
||||
|
||||
[Test] |
||||
public void ResolveResultIsMethodGroupResolveResult() |
||||
{ |
||||
Assert.IsTrue(resolveResult is MethodGroupResolveResult); |
||||
} |
||||
|
||||
[Test] |
||||
public void ResolveResultMethodNameIsTan() |
||||
{ |
||||
Assert.AreEqual("tan", MethodResolveResult.Name); |
||||
} |
||||
|
||||
MethodGroupResolveResult MethodResolveResult { |
||||
get { return (MethodGroupResolveResult)resolveResult; } |
||||
} |
||||
|
||||
[Test] |
||||
public void ResolveResultContainingTypeHasOneTanMethods() |
||||
{ |
||||
List<IMethod> tanMethods = GetTanMethods(); |
||||
Assert.AreEqual(1, tanMethods.Count); |
||||
} |
||||
|
||||
List<IMethod> GetTanMethods() |
||||
{ |
||||
List<IMethod> methods = MethodResolveResult.ContainingType.GetMethods(); |
||||
return PythonCompletionItemsHelper.FindAllMethodsFromCollection("tan", -1, methods.ToArray()); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,73 @@
@@ -0,0 +1,73 @@
|
||||
// <file>
|
||||
// <copyright see="prj:///doc/copyright.txt"/>
|
||||
// <license see="prj:///doc/license.txt"/>
|
||||
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
|
||||
// <version>$Revision$</version>
|
||||
// </file>
|
||||
|
||||
using System; |
||||
using ICSharpCode.SharpDevelop.Dom; |
||||
using ICSharpCode.PythonBinding; |
||||
using NUnit.Framework; |
||||
using PythonBinding.Tests.Utils; |
||||
|
||||
namespace PythonBinding.Tests.Resolver |
||||
{ |
||||
[TestFixture] |
||||
public class ResolverContextGetImportAllModulesTests |
||||
{ |
||||
[Test] |
||||
public void GetModulesThatImportEverythingReturnsEmptyCollectionIfNotImportAll() |
||||
{ |
||||
string code = "from math import tan"; |
||||
ParseInformation parseInfo = PythonParserHelper.CreateParseInfo(code); |
||||
|
||||
PythonResolverContext resolverContext = new PythonResolverContext(parseInfo); |
||||
|
||||
string[] expectedModules = new string[0]; |
||||
Assert.AreEqual(expectedModules, resolverContext.GetModulesThatImportEverything()); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetModulesThatImportEverythingReturnsSysForFromSysImportAllStatement() |
||||
{ |
||||
string code = "from sys import *"; |
||||
ParseInformation parseInfo = PythonParserHelper.CreateParseInfo(code); |
||||
|
||||
PythonResolverContext resolverContext = new PythonResolverContext(parseInfo); |
||||
|
||||
string[] expectedModules = new string[] { "sys" }; |
||||
Assert.AreEqual(expectedModules, resolverContext.GetModulesThatImportEverything()); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetModulesThatImportEverythingReturnsSysAndMathForFromSysImportAllStatement() |
||||
{ |
||||
string code = |
||||
"from sys import *\r\n" + |
||||
"from math import *"; |
||||
|
||||
ParseInformation parseInfo = PythonParserHelper.CreateParseInfo(code); |
||||
|
||||
PythonResolverContext resolverContext = new PythonResolverContext(parseInfo); |
||||
|
||||
string[] expectedModules = new string[] { "sys", "math" }; |
||||
Assert.AreEqual(expectedModules, resolverContext.GetModulesThatImportEverything()); |
||||
} |
||||
|
||||
[Test] |
||||
public void GetModulesThatImportEverythingIgnoresNonFromImportStatement() |
||||
{ |
||||
string code = |
||||
"import math\r\n" + |
||||
"from sys import *"; |
||||
ParseInformation parseInfo = PythonParserHelper.CreateParseInfo(code); |
||||
|
||||
PythonResolverContext resolverContext = new PythonResolverContext(parseInfo); |
||||
|
||||
string[] expectedModules = new string[] { "sys" }; |
||||
Assert.AreEqual(expectedModules, resolverContext.GetModulesThatImportEverything()); |
||||
} |
||||
|
||||
} |
||||
} |
||||
Loading…
Reference in new issue