Browse Source
git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@5015 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61shortcuts
44 changed files with 1978 additions and 1263 deletions
@ -0,0 +1,76 @@ |
|||||||
|
// <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.TextEditor; |
||||||
|
using ICSharpCode.TextEditor.Actions; |
||||||
|
using ICSharpCode.TextEditor.Document; |
||||||
|
|
||||||
|
namespace ICSharpCode.PythonBinding |
||||||
|
{ |
||||||
|
public class PythonFormattingStrategy : DefaultFormattingStrategy |
||||||
|
{ |
||||||
|
public PythonFormattingStrategy() |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
protected override int SmartIndentLine(TextArea textArea, int line) |
||||||
|
{ |
||||||
|
IDocument document = textArea.Document; |
||||||
|
LineSegment previousLine = document.GetLineSegment(line - 1); |
||||||
|
string previousLineText = document.GetText(previousLine).Trim(); |
||||||
|
|
||||||
|
if (previousLineText.EndsWith(":")) { |
||||||
|
return IncreaseLineIndent(textArea, line); |
||||||
|
} else if (previousLineText == "pass") { |
||||||
|
return DecreaseLineIndent(textArea, line); |
||||||
|
} else if ((previousLineText == "return") || (previousLineText.StartsWith("return "))) { |
||||||
|
return DecreaseLineIndent(textArea, line); |
||||||
|
} else if ((previousLineText == "raise") || (previousLineText.StartsWith("raise "))) { |
||||||
|
return DecreaseLineIndent(textArea, line); |
||||||
|
} else if (previousLineText == "break") { |
||||||
|
return DecreaseLineIndent(textArea, line); |
||||||
|
} |
||||||
|
return base.SmartIndentLine(textArea, line); |
||||||
|
} |
||||||
|
|
||||||
|
int IncreaseLineIndent(TextArea textArea, int line) |
||||||
|
{ |
||||||
|
return ModifyLineIndent(textArea, line, true); |
||||||
|
} |
||||||
|
|
||||||
|
int DecreaseLineIndent(TextArea textArea, int line) |
||||||
|
{ |
||||||
|
return ModifyLineIndent(textArea, line, false); |
||||||
|
} |
||||||
|
|
||||||
|
int ModifyLineIndent(TextArea textArea, int line, bool increaseIndent) |
||||||
|
{ |
||||||
|
IDocument document = textArea.Document; |
||||||
|
LineSegment currentLine = document.GetLineSegment(line); |
||||||
|
string indentation = GetIndentation(textArea, line - 1); |
||||||
|
indentation = GetNewLineIndentation(indentation, Tab.GetIndentationString(document), increaseIndent); |
||||||
|
string newIndentedText = indentation + document.GetText(currentLine); |
||||||
|
SmartReplaceLine(document, currentLine, newIndentedText); |
||||||
|
return indentation.Length; |
||||||
|
} |
||||||
|
|
||||||
|
string GetNewLineIndentation(string previousLineIndentation, string singleIndent, bool increaseIndent) |
||||||
|
{ |
||||||
|
if (increaseIndent) { |
||||||
|
return previousLineIndentation + singleIndent; |
||||||
|
} |
||||||
|
|
||||||
|
// Decrease the new line indentation.
|
||||||
|
int decreaselength = previousLineIndentation.Length - singleIndent.Length; |
||||||
|
if (decreaselength < 0) { |
||||||
|
decreaselength = 0; |
||||||
|
} |
||||||
|
return previousLineIndentation.Substring(0, decreaselength); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,65 @@ |
|||||||
|
// <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.FormsDesigner; |
||||||
|
using ICSharpCode.SharpDevelop.Dom; |
||||||
|
using NUnit.Framework; |
||||||
|
using PythonBinding.Tests.Utils; |
||||||
|
|
||||||
|
namespace PythonBinding.Tests.Designer |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Tests the PythonDesignerGenerator uses the text editor properties for indentation when
|
||||||
|
/// inserting an event handler into the document.
|
||||||
|
/// </summary>
|
||||||
|
[TestFixture] |
||||||
|
public class InsertEventHandlerWithSpaceIndentTestFixture : InsertEventHandlerTestFixtureBase |
||||||
|
{ |
||||||
|
public override void AfterSetUpFixture() |
||||||
|
{ |
||||||
|
textEditorProperties.ConvertTabsToSpaces = true; |
||||||
|
textEditorProperties.IndentationSize = 4; |
||||||
|
MockEventDescriptor mockEventDescriptor = new MockEventDescriptor("Click"); |
||||||
|
insertedEventHandler = generator.InsertComponentEvent(null, mockEventDescriptor, "button1_click", String.Empty, out file, out position); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void ExpectedCodeAfterEventHandlerInserted() |
||||||
|
{ |
||||||
|
string expectedCode = GetTextEditorCode(); |
||||||
|
string eventHandler = " def button1_click(self, sender, e):\r\n" + |
||||||
|
" pass"; |
||||||
|
expectedCode = expectedCode + "\r\n" + eventHandler; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedCode, viewContent.DesignerCodeFileContent); |
||||||
|
} |
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Note that the text editor code already has the
|
||||||
|
/// statement:
|
||||||
|
///
|
||||||
|
/// "self._button1.Click += button1_click"
|
||||||
|
///
|
||||||
|
/// This is generated by the form designer framework and not
|
||||||
|
/// by the designer generator.
|
||||||
|
/// </summary>
|
||||||
|
protected override string GetTextEditorCode() |
||||||
|
{ |
||||||
|
return "from System.Windows.Forms import Form\r\n" + |
||||||
|
"\r\n" + |
||||||
|
"class MainForm(Form):\r\n" + |
||||||
|
" def __init__(self):\r\n" + |
||||||
|
" self.InitializeComponents()\r\n" + |
||||||
|
" \r\n" + |
||||||
|
" def InitializeComponents(self):\r\n" + |
||||||
|
" self._button1 = System.Windows.Forms.Button()\r\n" + |
||||||
|
" self.Controls.Add(self._button1)\r\n"; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,205 @@ |
|||||||
|
// <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.TextEditor; |
||||||
|
using ICSharpCode.TextEditor.Document; |
||||||
|
using ICSharpCode.TextEditor.Actions; |
||||||
|
using NUnit.Framework; |
||||||
|
using PythonBinding.Tests.Utils; |
||||||
|
|
||||||
|
namespace PythonBinding.Tests.Indentation |
||||||
|
{ |
||||||
|
/// <summary>
|
||||||
|
/// Tests that the PythonFormattingStrategy indents the new line added after pressing the ':' character.
|
||||||
|
/// </summary>
|
||||||
|
[TestFixture] |
||||||
|
public class PythonNewMethodIndentationTestFixture |
||||||
|
{ |
||||||
|
TextEditorControl textEditor; |
||||||
|
PythonFormattingStrategy formattingStrategy; |
||||||
|
|
||||||
|
[SetUp] |
||||||
|
public void Init() |
||||||
|
{ |
||||||
|
MockTextEditorProperties textEditorProperties = new MockTextEditorProperties(); |
||||||
|
textEditorProperties.IndentStyle = IndentStyle.Smart; |
||||||
|
textEditorProperties.TabIndent = 4; |
||||||
|
textEditor = new TextEditorControl(); |
||||||
|
textEditor.TextEditorProperties = textEditorProperties; |
||||||
|
formattingStrategy = new PythonFormattingStrategy(); |
||||||
|
} |
||||||
|
|
||||||
|
[TearDown] |
||||||
|
public void TearDown() |
||||||
|
{ |
||||||
|
textEditor.Dispose(); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void NewMethodDefinition() |
||||||
|
{ |
||||||
|
textEditor.Text = "def newMethod:\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 1); |
||||||
|
string expectedText = "def newMethod:\r\n" + |
||||||
|
"\t"; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
Assert.AreEqual(1, indentResult); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void NoExtraIndentationRequired() |
||||||
|
{ |
||||||
|
textEditor.Text = "\tprint 'abc'\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 1); |
||||||
|
string expectedText = "\tprint 'abc'\r\n" + |
||||||
|
"\t"; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
Assert.AreEqual(1, indentResult); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void PassStatementDecreasesIndentOnThirdLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "def method1:\r\n" + |
||||||
|
"\tpass\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 2); |
||||||
|
string expectedText = "def method1:\r\n" + |
||||||
|
"\tpass\r\n" + |
||||||
|
""; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void ReturnValueStatementDecreasesIndentOnThirdLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "def method1:\r\n" + |
||||||
|
"\treturn 0\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 2); |
||||||
|
string expectedText = "def method1:\r\n" + |
||||||
|
"\treturn 0\r\n" + |
||||||
|
""; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void ReturnStatementDecreasesIndentOnThirdLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "def method1:\r\n" + |
||||||
|
"\treturn\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 2); |
||||||
|
string expectedText = "def method1:\r\n" + |
||||||
|
"\treturn\r\n" + |
||||||
|
""; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
[Test] |
||||||
|
public void ReturnStatementWithNoIndentOnPreviousLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "return\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 1); |
||||||
|
string expectedText = "return\r\n" + |
||||||
|
""; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void StatementIsNotAReturnOnPreviousLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "\treturnValue\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 1); |
||||||
|
string expectedText = "\treturnValue\r\n" + |
||||||
|
"\t"; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void RaiseStatementWithObjectDecreasesIndentOnThirdLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "def method1:\r\n" + |
||||||
|
"\traise 'a'\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 2); |
||||||
|
string expectedText = "def method1:\r\n" + |
||||||
|
"\traise 'a'\r\n" + |
||||||
|
""; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void RaiseStatementDecreasesIndentOnThirdLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "def method1:\r\n" + |
||||||
|
"\traise\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 2); |
||||||
|
string expectedText = "def method1:\r\n" + |
||||||
|
"\traise\r\n" + |
||||||
|
""; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void StatementIsNotARaiseStatementOnPreviousLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "def method1:\r\n" + |
||||||
|
"\traiseThis\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 2); |
||||||
|
string expectedText = "def method1:\r\n" + |
||||||
|
"\traiseThis\r\n" + |
||||||
|
"\t"; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void BreakStatementDecreasesIndentOnThirdLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "def method1:\r\n" + |
||||||
|
"\tbreak\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 2); |
||||||
|
string expectedText = "def method1:\r\n" + |
||||||
|
"\tbreak\r\n" + |
||||||
|
""; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
|
||||||
|
[Test] |
||||||
|
public void StatementIsNotABreakStatementOnPreviousLine() |
||||||
|
{ |
||||||
|
textEditor.Text = "def method1:\r\n" + |
||||||
|
"\tbreakThis\r\n" + |
||||||
|
""; |
||||||
|
int indentResult = formattingStrategy.IndentLine(textEditor.ActiveTextAreaControl.TextArea, 2); |
||||||
|
string expectedText = "def method1:\r\n" + |
||||||
|
"\tbreakThis\r\n" + |
||||||
|
"\t"; |
||||||
|
|
||||||
|
Assert.AreEqual(expectedText, textEditor.Text); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue