Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3561 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
23 changed files with 436 additions and 47 deletions
@ -0,0 +1,22 @@
@@ -0,0 +1,22 @@
|
||||
// <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.Generic; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
/// <summary>
|
||||
/// Returns member names or global names for the python console command line.
|
||||
/// </summary>
|
||||
public interface IMemberProvider |
||||
{ |
||||
IList<string> GetMemberNames(string name); |
||||
|
||||
IList<string> GetGlobals(string name); |
||||
} |
||||
} |
||||
@ -0,0 +1,75 @@
@@ -0,0 +1,75 @@
|
||||
// <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.Generic; |
||||
using System.Windows.Forms; |
||||
|
||||
using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor; |
||||
using ICSharpCode.SharpDevelop; |
||||
using ICSharpCode.TextEditor; |
||||
using ICSharpCode.TextEditor.Document; |
||||
using ICSharpCode.TextEditor.Gui.CompletionWindow; |
||||
|
||||
namespace ICSharpCode.PythonBinding |
||||
{ |
||||
/// <summary>
|
||||
/// Provides code completion for the Python Console window.
|
||||
/// </summary>
|
||||
public class PythonConsoleCompletionDataProvider : AbstractCompletionDataProvider |
||||
{ |
||||
IMemberProvider memberProvider; |
||||
|
||||
public PythonConsoleCompletionDataProvider(IMemberProvider memberProvider) |
||||
{ |
||||
this.memberProvider = memberProvider; |
||||
DefaultIndex = 0; |
||||
} |
||||
|
||||
public override ICompletionData[] GenerateCompletionData(string fileName, TextArea textArea, char charTyped) |
||||
{ |
||||
return GenerateCompletionData(GetLineText(textArea)); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Generates completion data for the specified text. The text should be everything before
|
||||
/// the dot character that triggered the completion. The text can contain the command line prompt
|
||||
/// '>>>' as this will be ignored.
|
||||
/// </summary>
|
||||
public ICompletionData[] GenerateCompletionData(string line) |
||||
{ |
||||
List<DefaultCompletionData> items = new List<DefaultCompletionData>(); |
||||
|
||||
string name = GetName(line); |
||||
if (!String.IsNullOrEmpty(name)) { |
||||
try { |
||||
foreach (string member in memberProvider.GetMemberNames(name)) { |
||||
items.Add(new DefaultCompletionData(member, String.Empty, ClassBrowserIconService.MethodIndex)); |
||||
} |
||||
} catch { |
||||
// Do nothing.
|
||||
} |
||||
} |
||||
return items.ToArray(); |
||||
} |
||||
|
||||
string GetName(string text) |
||||
{ |
||||
int startIndex = text.LastIndexOf(' '); |
||||
return text.Substring(startIndex + 1); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Gets the line of text up to the cursor position.
|
||||
/// </summary>
|
||||
string GetLineText(TextArea textArea) |
||||
{ |
||||
LineSegment lineSegment = textArea.Document.GetLineSegmentForOffset(textArea.Caret.Offset); |
||||
return textArea.Document.GetText(lineSegment); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,94 @@
@@ -0,0 +1,94 @@
|
||||
// <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.Generic; |
||||
using Microsoft.Scripting.Hosting; |
||||
|
||||
using ICSharpCode.PythonBinding; |
||||
using ICSharpCode.TextEditor; |
||||
using ICSharpCode.TextEditor.Gui.CompletionWindow; |
||||
using NUnit.Framework; |
||||
|
||||
namespace PythonBinding.Tests.Console |
||||
{ |
||||
/// <summary>
|
||||
/// Tests the code completion when the user has typed in __builtins__.
|
||||
/// </summary>
|
||||
[TestFixture] |
||||
public class BuiltinCodeCompletionTestFixture |
||||
{ |
||||
ICompletionData[] completionItems; |
||||
ICompletionData[] expectedCompletionItems; |
||||
PythonConsoleCompletionDataProvider provider; |
||||
MockMemberProvider memberProvider; |
||||
|
||||
[TestFixtureSetUp] |
||||
public void SetUpFixture() |
||||
{ |
||||
using (TextEditorControl textEditorControl = new TextEditorControl()) { |
||||
textEditorControl.Text = ">>> __builtins__"; |
||||
TextEditor textEditor = new TextEditor(textEditorControl); |
||||
|
||||
memberProvider = new MockMemberProvider(); |
||||
memberProvider.SetMemberNames(new string[] {"a", "b", "c"}); |
||||
expectedCompletionItems = CreateCompletionItems(memberProvider.GetMemberNames("__builtins__")); |
||||
|
||||
provider = new PythonConsoleCompletionDataProvider(memberProvider); |
||||
completionItems = provider.GenerateCompletionData(String.Empty, textEditorControl.ActiveTextAreaControl.TextArea, '.'); |
||||
|
||||
} |
||||
} |
||||
|
||||
[Test] |
||||
public void MoreThanOneCompletionItem() |
||||
{ |
||||
Assert.IsTrue(completionItems.Length > 0); |
||||
} |
||||
|
||||
[Test] |
||||
public void ExpectedCompletionItems() |
||||
{ |
||||
for (int i = 0; i < expectedCompletionItems.Length; ++i) { |
||||
Assert.AreEqual(expectedCompletionItems[i].Text, completionItems[i].Text); |
||||
} |
||||
} |
||||
|
||||
[Test] |
||||
public void ExpectedCompletionItemsCountEqualsActualCompletionItemsCount() |
||||
{ |
||||
Assert.AreEqual(expectedCompletionItems.Length, completionItems.Length); |
||||
} |
||||
|
||||
[Test] |
||||
public void PreSelectionIsNull() |
||||
{ |
||||
Assert.IsNull(provider.PreSelection); |
||||
} |
||||
|
||||
[Test] |
||||
public void DefaultIndexIsZero() |
||||
{ |
||||
Assert.AreEqual(0, provider.DefaultIndex); |
||||
} |
||||
|
||||
[Test] |
||||
public void BuiltinsPassedToMemberProvider() |
||||
{ |
||||
Assert.AreEqual("__builtins__", memberProvider.GetMemberNamesParameter); |
||||
} |
||||
|
||||
ICompletionData[] CreateCompletionItems(IList<string> memberNames) |
||||
{ |
||||
List<DefaultCompletionData> items = new List<DefaultCompletionData>(); |
||||
foreach (string memberName in memberNames) { |
||||
items.Add(new DefaultCompletionData(memberName, String.Empty, 0)); |
||||
} |
||||
return items.ToArray(); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,74 @@
@@ -0,0 +1,74 @@
|
||||
// <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; |
||||
using ICSharpCode.TextEditor.Gui.CompletionWindow; |
||||
using NUnit.Framework; |
||||
|
||||
namespace PythonBinding.Tests.Console |
||||
{ |
||||
[TestFixture] |
||||
public class EmptyStringCodeCompletionTestFixture |
||||
{ |
||||
MockMemberProvider memberProvider; |
||||
PythonConsoleCompletionDataProvider completionProvider; |
||||
|
||||
[SetUp] |
||||
public void Init() |
||||
{ |
||||
memberProvider = new MockMemberProvider(); |
||||
completionProvider = new PythonConsoleCompletionDataProvider(memberProvider); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// If the user presses the dot character without having any text in the command line then
|
||||
/// a SyntaxException occurs if the code calls IronPython's CommandLine.GetMemberNames. So this
|
||||
/// tests ensures that if the string is empty then this method is not called.
|
||||
/// </summary>
|
||||
[Test] |
||||
public void NoCompletionItemsGeneratedForEmptyString() |
||||
{ |
||||
memberProvider.SetMemberNames(new string[] {"a"}); |
||||
memberProvider.SetGlobals(new string[] {"a"}); |
||||
|
||||
Assert.AreEqual(0, completionProvider.GenerateCompletionData(">>> ").Length); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Checks that the GenerateCompletionData method catches any exceptions thrown by the
|
||||
/// IMemberProvider implementation. This can occur when an invalid name is passed to
|
||||
/// IronPython's CommandLine.GetMemberNames or GetGlobals. For example, an UnboundNameException is
|
||||
/// thrown if an unknown name is used.
|
||||
/// </summary>
|
||||
[Test] |
||||
public void NoCompletionItemsGeneratedWhenExceptionThrown() |
||||
{ |
||||
memberProvider.ExceptionToThrow = new ApplicationException("Should not be thrown"); |
||||
|
||||
Assert.AreEqual(0, completionProvider.GenerateCompletionData(">>> a").Length); |
||||
} |
||||
|
||||
[Test] |
||||
public void ImageIndexIsMethod() |
||||
{ |
||||
memberProvider.SetMemberNames(new string[] {"a"}); |
||||
|
||||
ICompletionData[] items = completionProvider.GenerateCompletionData(">>> a"); |
||||
Assert.AreEqual(ClassBrowserIconService.MethodIndex, items[0].ImageIndex); |
||||
} |
||||
|
||||
[Test] |
||||
public void UnderscoresPassedToGetMemberNames() |
||||
{ |
||||
completionProvider.GenerateCompletionData(">>> __builtins__"); |
||||
Assert.AreEqual("__builtins__", memberProvider.GetMemberNamesParameter); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,80 @@
@@ -0,0 +1,80 @@
|
||||
// <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.Generic; |
||||
using ICSharpCode.PythonBinding; |
||||
|
||||
namespace PythonBinding.Tests.Console |
||||
{ |
||||
public class MockMemberProvider : IMemberProvider |
||||
{ |
||||
List<string> memberNames = new List<string>(); |
||||
string getMemberNamesParameter; |
||||
List<string> globals = new List<string>(); |
||||
string getGlobalsParameter; |
||||
Exception exceptionToThrow; |
||||
|
||||
public MockMemberProvider() |
||||
{ |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Exception that will be thrown if the GetMemberNames method or GetGlobals method is called.
|
||||
/// </summary>
|
||||
public Exception ExceptionToThrow { |
||||
get { return exceptionToThrow; } |
||||
set { exceptionToThrow = value; } |
||||
} |
||||
|
||||
public void SetMemberNames(string[] names) |
||||
{ |
||||
memberNames.AddRange(names); |
||||
} |
||||
|
||||
public IList<string> GetMemberNames(string name) |
||||
{ |
||||
getMemberNamesParameter = name; |
||||
|
||||
if (exceptionToThrow != null) { |
||||
throw exceptionToThrow; |
||||
} |
||||
|
||||
return memberNames; |
||||
} |
||||
|
||||
public void SetGlobals(string[] names) |
||||
{ |
||||
globals.AddRange(names); |
||||
} |
||||
|
||||
public IList<string> GetGlobals(string name) |
||||
{ |
||||
getGlobalsParameter = name; |
||||
|
||||
if (exceptionToThrow != null) { |
||||
throw exceptionToThrow; |
||||
} |
||||
|
||||
return globals; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Returns the parameter passed to the GetGlobals method.
|
||||
/// </summary>
|
||||
public string GetGlobalsParameter { |
||||
get { return getGlobalsParameter; } |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Returns the parameter passed to the GetMemberNames method.
|
||||
/// </summary>
|
||||
public string GetMemberNamesParameter { |
||||
get { return getMemberNamesParameter; } |
||||
} |
||||
} |
||||
} |
||||
Loading…
Reference in new issue