Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update F# script package * compute F# kernel completions
- Loading branch information
Showing
7 changed files
with
182 additions
and
57 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
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,54 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using Microsoft.DotNet.Interactive.Utility; | ||
using Xunit; | ||
|
||
namespace Microsoft.DotNet.Interactive.Tests | ||
{ | ||
public class UtilityTests | ||
{ | ||
[Fact] | ||
public void replacement_position_can_be_found_at_the_end_of_a_string() | ||
{ | ||
var code = "System.Linq.Enumerable."; | ||
// finding this ^ | ||
var pos = SourceUtilities.ComputeReplacementStartPosition(code, code.Length); | ||
Assert.Equal(code.Length, pos); | ||
} | ||
|
||
[Fact] | ||
public void replacement_position_can_be_found_not_at_the_end_of_a_string() | ||
{ | ||
var code = "System.Linq.Enumerable.Ran"; | ||
// finding this ^ | ||
var lastDotPos = code.LastIndexOf('.') + 1; | ||
var pos = SourceUtilities.ComputeReplacementStartPosition(code, lastDotPos); | ||
Assert.Equal(lastDotPos, pos); | ||
} | ||
|
||
[Fact] | ||
public void replacement_position_can_be_found_at_the_end_of_a_multiline_string() | ||
{ | ||
var code = @" | ||
using System.Linq; | ||
Enumerable. | ||
// ^ finding this"; | ||
var lastDotPos = code.LastIndexOf('.') + 1; | ||
var pos = SourceUtilities.ComputeReplacementStartPosition(code, lastDotPos); | ||
Assert.Equal(lastDotPos, pos); | ||
} | ||
|
||
[Fact] | ||
public void replacement_position_can_be_found_not_at_the_end_of_a_multiline_string() | ||
{ | ||
var code = @" | ||
using System.Linq; | ||
Enumerable.Ran | ||
// ^ finding this"; | ||
var lastDotPos = code.LastIndexOf('.') + 1; | ||
var pos = SourceUtilities.ComputeReplacementStartPosition(code, lastDotPos); | ||
Assert.Equal(lastDotPos, pos); | ||
} | ||
} | ||
} |
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,46 @@ | ||
// Copyright (c) .NET Foundation and contributors. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Text.RegularExpressions; | ||
|
||
namespace Microsoft.DotNet.Interactive.Utility | ||
{ | ||
public static class SourceUtilities | ||
{ | ||
private static readonly Regex _lastToken = new Regex(@"(?<lastToken>\S+)$", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant); | ||
|
||
/// <summary> | ||
/// Given the specified code and cursor position, finds the location where replacement text will begin | ||
/// insertion, usually the location of the last dot ('.'). | ||
/// </summary> | ||
public static int ComputeReplacementStartPosition(string code, int cursorPosition) | ||
{ | ||
var pos = cursorPosition; | ||
|
||
if (pos > 0) | ||
{ | ||
var codeToCursor = code.Substring(0, pos); | ||
var match = _lastToken.Match(codeToCursor); | ||
if (match.Success) | ||
{ | ||
var token = match.Groups["lastToken"]; | ||
if (token.Success) | ||
{ | ||
var lastDotPosition = token.Value.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase); | ||
if (lastDotPosition >= 0) | ||
{ | ||
pos = token.Index + lastDotPosition + 1; | ||
} | ||
else | ||
{ | ||
pos = token.Index; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return pos; | ||
} | ||
} | ||
} |