Skip to content

Commit

Permalink
Moved matching logic into Parameter, SourceItem
Browse files Browse the repository at this point in the history
Improved Ast parsing

See aslo: #29
  • Loading branch information
AnderssonPeter committed Jan 27, 2022
1 parent 626de5e commit 178c099
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 26 deletions.
4 changes: 2 additions & 2 deletions PowerType.Tests/PowerType.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.3.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.13.0" />
<PackageReference Include="FluentAssertions" Version="6.4.0" />
<PackageReference Include="FluentAssertions.Analyzers" Version="0.15.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
Expand Down
12 changes: 6 additions & 6 deletions PowerType.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
},
"FluentAssertions": {
"type": "Direct",
"requested": "[6.3.0, )",
"resolved": "6.3.0",
"contentHash": "f+ywjTSNhc1HDtzCqtYHB6/Xuf69pznR8eUPJs1CoaFq00sCIg6KjGcXHwdWslnmy4x5TAq3kRw1728CSaQ/zw==",
"requested": "[6.4.0, )",
"resolved": "6.4.0",
"contentHash": "KOJE1UD7ndxoZAdmOraiUFLdcdGTArGQW8WYN+vwrC3mdt2G/B7gmsMOozgXKiNSh3fpD/Xp/0mhonRh8vrZKw==",
"dependencies": {
"System.Configuration.ConfigurationManager": "4.4.0"
}
},
"FluentAssertions.Analyzers": {
"type": "Direct",
"requested": "[0.13.0, )",
"resolved": "0.13.0",
"contentHash": "KXUw4o+1I/pH9FS3olKgg6cvYX+wuOBupG/SH9dSAK4H693W9fKLeEJBXqDkSyqag88+FkrqeQfNy4m3F57AQw=="
"requested": "[0.15.0, )",
"resolved": "0.15.0",
"contentHash": "MYRXsvx5nWXg1Ja7CJeueHW0VSMxz0cXy16LxT9N7DVX5thcchoyi8vDYfErdYE/J6cORmhBCZAf1j7JUxacKw=="
},
"Microsoft.NET.Test.Sdk": {
"type": "Direct",
Expand Down
17 changes: 5 additions & 12 deletions PowerType/DictionarySuggestor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private static IEnumerable<PredictiveSuggestion> GetAllSourceMatches(DictionaryP

private static IEnumerable<PredictiveSuggestion> GetPartialSourceMatches(DictionaryParsingContext context, Source source, PowerShellString value) => source
.GetItems()
.Where(x => x.Name.Contains(value.RawValue, StringComparison.OrdinalIgnoreCase))
.Where(x => x.IsPartialMatch(value))
.Select(x => new PredictiveSuggestion(context.Reconstruct(GetFromRawWithPreferredType(value.Type, x.Name)), x.Description));
private static bool IsValueDone(bool isLast, PowerShellString value) =>
!isLast || (value.Type != StringConstantType.BareWord && value.IsEscapedOpened() && value.IsEscapedClosed());
Expand All @@ -171,20 +171,13 @@ private static IEnumerable<PredictiveSuggestion> GetPartialMatches(DictionaryPar
{
if (parameter.IsAvailable(context))
{
if (parameter.HasKeys)
if (parameter.TryGetPartialKeyMatch(currentArgument, out var key))
{
foreach (var key in parameter.Keys)
{
if (key.Contains(currentArgument.RawValue, StringComparison.OrdinalIgnoreCase))
{
yield return new PredictiveSuggestion(context.Reconstruct(key), parameter.Description);
break;
}
}
yield return new PredictiveSuggestion(context.Reconstruct(key), parameter.Description);
}
else if (parameter is ValueParameter valueParameter && valueParameter.Source != null)
else if (parameter is ValueParameter valueParameter && !valueParameter.HasKeys && valueParameter.Source != null)
{
foreach (var sourceItem in valueParameter.Source.GetItems().Where(item => item.Name.Contains(currentArgument.RawValue, StringComparison.OrdinalIgnoreCase)))
foreach (var sourceItem in valueParameter.Source.GetItems().Where(item => item.IsPartialMatch(currentArgument)))
{
yield return new PredictiveSuggestion(context.Reconstruct(GetFromRawWithPreferredType(currentArgument.Type, sourceItem.Name)), sourceItem.Description);
}
Expand Down
16 changes: 15 additions & 1 deletion PowerType/Model/Parameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,19 @@ internal bool IsAvailable(DictionaryParsingContext dictionaryParsingContext) =>

internal virtual bool IsPerfectKeyMatch(PowerShellString value) => HasKeys && Keys.Exists(key => key.Equals(value.RawValue, StringComparison.OrdinalIgnoreCase));

internal bool IsPartialKeyMatch(PowerShellString value) => HasKeys && Keys.Exists(key => key.Contains(value.RawValue, StringComparison.OrdinalIgnoreCase));
internal bool TryGetPartialKeyMatch(PowerShellString value, out string matchingKey) {
if (HasKeys)
{
foreach (var key in Keys)
{
if (key.Contains(value.RawValue, StringComparison.OrdinalIgnoreCase))
{
matchingKey = key;
return true;
}
}
}
matchingKey = null!;
return false;
}
}
7 changes: 6 additions & 1 deletion PowerType/Model/SourceItem.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
namespace PowerType.Model;
using PowerType.Parsing;

namespace PowerType.Model;

public class SourceItem
{
public string Name { get; set; } = null!;
public string? Description { get; set; }

public static SourceItem FromName(string name) => new SourceItem { Name = name };

public bool IsPartialMatch(PowerShellString value) => Name.Contains(value.RawValue, StringComparison.OrdinalIgnoreCase);
public bool IsPerfectMatch(PowerShellString value) => Name.Equals(value.RawValue, StringComparison.OrdinalIgnoreCase);
}
6 changes: 6 additions & 0 deletions PowerType/Parsing/PowerShellString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public PowerShellString(CommandElementAst commandElementAst)
EscapedValue = stringConstant.ToString();
Type = stringConstant.StringConstantType;
}
else if (commandElementAst is CommandParameterAst commandParameter)
{
RawValue = commandParameter.ToString();
EscapedValue = commandParameter.ToString();
Type = StringConstantType.BareWord;
}
else
{
throw new InvalidOperationException("We have no idea how to handle this type: " + commandElementAst.GetType().ToString());
Expand Down
6 changes: 3 additions & 3 deletions PowerType/PowerTypePredictionSummaryCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

namespace PowerType;

public record struct PowerTypePredictionSummary(DateTime When, string Input, string? TokenAtCursor, Exception? Exception, string[] Suggestions, TimeSpan Duration);
public record struct PowerTypePredictionSummary(DateTime When, string Input, string? TokenAtCursor, Exception? Exception, string[]? Suggestions, TimeSpan Duration);

public class PowerTypePredictionSummaryCollector
{
private readonly object locker = new ();
private readonly PowerTypePredictionSummary[] items;
private int offset = 0;

public PowerTypePredictionSummaryCollector(int size = 20)
public PowerTypePredictionSummaryCollector(int size = 40)
{
items = new PowerTypePredictionSummary[size];
}
Expand All @@ -21,7 +21,7 @@ public void Add(DateTime when, PredictionContext predictionContext, Exception? e
lock (locker)
{
var index = offset++ % items.Length;
items[index] = new PowerTypePredictionSummary(when, predictionContext.InputAst.ToString(), predictionContext.TokenAtCursor?.ToString(), exception, suggestionPackage.SuggestionEntries.Select(x => x.SuggestionText).ToArray(), duration);
items[index] = new PowerTypePredictionSummary(when, predictionContext.InputAst.ToString(), predictionContext.TokenAtCursor?.ToString(), exception, suggestionPackage.SuggestionEntries?.Select(x => x.SuggestionText)?.ToArray(), duration);
}
}

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ It's not feature complete yet but if you want to try it out run the following:
Install-Module PowerType -AllowPrerelease
Enable-PowerType
```
Ensure that you are running `PowerShell 7.2` and `PSReadLine 2.2.0-beta4` or newer!

If you now type `git` you should get some basic autocompletion, the [dictionary for git](PowerType/Dictionaries/git.ps1) is far from complete and dosen't know about all commands and parameters yet.
If you now type `git` you should get some basic autocompletion, the [dictionary for git](PowerType/Dictionaries/git.ps1) is far from complete and doesn't know about all commands and parameters yet.

To uninstall run
```Powershell
Expand Down

0 comments on commit 178c099

Please sign in to comment.