This repository has been archived by the owner on Oct 16, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolve IronPython local variables up to the current expression line …
…and not beyond.
- Loading branch information
Showing
8 changed files
with
228 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
src/AddIns/BackendBindings/Scripting/Project/Src/ScriptingLocalMethod.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) | ||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) | ||
|
||
using System; | ||
|
||
namespace ICSharpCode.Scripting | ||
{ | ||
/// <summary> | ||
/// Used to extract the code for a method based on the range of lines. | ||
/// </summary> | ||
public class ScriptingLocalMethod | ||
{ | ||
string code = String.Empty; | ||
|
||
public ScriptingLocalMethod(string code) | ||
{ | ||
if (code != null) { | ||
this.code = code; | ||
} | ||
} | ||
|
||
public string GetCode(int endLine) | ||
{ | ||
int endIndex = FindIndexForEndOfLine(endLine); | ||
if (endIndex > 0) { | ||
return code.Substring(0, endIndex); | ||
} | ||
return code; | ||
} | ||
|
||
int FindIndexForEndOfLine(int line) | ||
{ | ||
int index = 0; | ||
for (int i = 0; i <= line; ++i) { | ||
index = code.IndexOf('\n', index) + 1; | ||
} | ||
return index; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
src/AddIns/BackendBindings/Scripting/Test/Resolver/ScriptingLocalMethodTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) | ||
// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) | ||
|
||
using System; | ||
using NUnit.Framework; | ||
using ICSharpCode.Scripting; | ||
|
||
namespace ICSharpCode.Scripting.Tests.Resolver | ||
{ | ||
[TestFixture] | ||
public class ScriptingLocalMethodTests | ||
{ | ||
ScriptingLocalMethod method; | ||
|
||
void CreateLocalMethod(string code) | ||
{ | ||
method = new ScriptingLocalMethod(code); | ||
} | ||
|
||
[Test] | ||
public void GetCode_EndLineIsZeroAndTwoLinesOfCode_ReturnsFirstLineOfCode() | ||
{ | ||
string fullCode = | ||
"first\r\n" + | ||
"second"; | ||
|
||
CreateLocalMethod(fullCode); | ||
|
||
int endLine = 0; | ||
string code = method.GetCode(endLine); | ||
|
||
string expectedCode = "first\r\n"; | ||
|
||
Assert.AreEqual(expectedCode, code); | ||
} | ||
|
||
[Test] | ||
public void GetCode_EndLineIsOneAndThreeLinesOfCode_ReturnsFirstTwoLinesOfCode() | ||
{ | ||
string fullCode = | ||
"first\r\n" + | ||
"second\r\n" + | ||
"third"; | ||
|
||
CreateLocalMethod(fullCode); | ||
|
||
int endLine = 1; | ||
string code = method.GetCode(endLine); | ||
|
||
string expectedCode = | ||
"first\r\n" + | ||
"second\r\n"; | ||
|
||
Assert.AreEqual(expectedCode, code); | ||
} | ||
|
||
[Test] | ||
public void GetCode_EndLineIsOneAndTwoLinesOfCode_ReturnsFirstTwoLinesOfCode() | ||
{ | ||
string fullCode = | ||
"first\r\n" + | ||
"second"; | ||
|
||
CreateLocalMethod(fullCode); | ||
|
||
int endLine = 1; | ||
string code = method.GetCode(endLine); | ||
|
||
string expectedCode = | ||
"first\r\n" + | ||
"second"; | ||
|
||
Assert.AreEqual(expectedCode, code); | ||
} | ||
|
||
[Test] | ||
public void GetCode_EndLineIsOneAndCodeIsNull_ReturnsEmptyString() | ||
{ | ||
string fullCode = null; | ||
|
||
CreateLocalMethod(fullCode); | ||
|
||
int endLine = 1; | ||
string code = method.GetCode(endLine); | ||
|
||
string expectedCode = String.Empty; | ||
|
||
Assert.AreEqual(expectedCode, code); | ||
} | ||
} | ||
} |