Skip to content

Commit bf99de9

Browse files
committed
Autocompletion fix.
1 parent bf8f6cb commit bf99de9

File tree

5 files changed

+54
-27
lines changed

5 files changed

+54
-27
lines changed

Cognifide.PowerShell/Cognifide.PowerShell.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@
366366
<Compile Include="Core\Utility\RulesUtils.cs" />
367367
<Compile Include="Core\Utility\SpeTimer.cs" />
368368
<Compile Include="Core\Utility\WriteLogCommand.cs" />
369+
<Compile Include="Core\Validation\AutocompleteSetAttribute.cs" />
369370
<Compile Include="Core\VersionDecoupling\Interfaces\IDateConverter.cs" />
370371
<Compile Include="Core\VersionDecoupling\TypeResolver.cs" />
371372
<Compile Include="Core\VersionDecoupling\VersionResolver.cs" />

Cognifide.PowerShell/Commandlets/Data/Search/FindItemCommand.cs

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Management.Automation;
55
using Cognifide.PowerShell.Core.Extensions;
6+
using Cognifide.PowerShell.Core.Validation;
67
using Cognifide.PowerShell.Core.VersionDecoupling;
78
using Sitecore.ContentSearch;
89
using Sitecore.ContentSearch.Linq;
@@ -17,9 +18,9 @@ namespace Cognifide.PowerShell.Commandlets.Data.Search
1718
[OutputType(typeof(Item))]
1819
public class FindItemCommand : BaseCommand
1920
{
20-
private static readonly string[] indexes = ContentSearchManager.Indexes.Select(i => i.Name).ToArray();
21+
public static readonly string[] Indexes = ContentSearchManager.Indexes.Select(i => i.Name).ToArray();
2122

22-
[ValidateSet("*")]
23+
[AutocompleteSet("Indexes")]
2324
[Parameter(Mandatory = true, Position = 0)]
2425
public string Index { get; set; }
2526

@@ -194,20 +195,6 @@ private List<SearchResultItem> FilterByPosition(IQueryable<SearchResultItem> que
194195
// Concat not support by Sitecore.
195196
return firstObjects.ToList().Concat(query.Skip(takenAndSkipped + skipBeforeEnd).Take(takeLast)).ToList();
196197
}
197-
198-
public override object GetDynamicParameters()
199-
{
200-
if (!_reentrancyLock.WaitOne(0))
201-
{
202-
_reentrancyLock.Set();
203-
204-
SetValidationSetValues("Index", indexes);
205-
206-
_reentrancyLock.Reset();
207-
}
208-
209-
return base.GetDynamicParameters();
210-
}
211198
}
212199

213200
public enum FilterType

Cognifide.PowerShell/Core/Host/CognifideSitecorePowerShellSnapIn.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
using System.Reflection;
1010
using System.Xml;
1111
using Cognifide.PowerShell.Core.Provider;
12+
using Cognifide.PowerShell.Core.Validation;
1213
using Sitecore.Configuration;
14+
using Sitecore.ContentSearch.Utilities;
1315
using Sitecore.Diagnostics;
1416
using Sitecore.Pipelines;
1517
using Pipeline = Sitecore.Pipelines.Pipeline;
@@ -20,14 +22,15 @@ namespace Cognifide.PowerShell.Core.Host
2022
public class CognifideSitecorePowerShellSnapIn : CustomPSSnapIn
2123
{
2224
private static readonly List<CmdletConfigurationEntry> commandlets = new List<CmdletConfigurationEntry>();
25+
private static readonly Dictionary<string, string> completers = new Dictionary<string, string>();
2326

2427
/// <summary>
2528
/// Specify the providers that belong to this custom PowerShell snap-in.
2629
/// </summary>
27-
private Collection<ProviderConfigurationEntry> _providers;
30+
private Collection<ProviderConfigurationEntry> providers;
2831

2932
private bool initialized;
30-
33+
3134
static CognifideSitecorePowerShellSnapIn()
3235
{
3336
var cmdltsToIncludes = Factory.GetConfigNodes("powershell/commandlets/add");
@@ -113,17 +116,17 @@ public override Collection<ProviderConfigurationEntry> Providers
113116
{
114117
Initialize();
115118
}
116-
if (_providers == null)
119+
if (providers == null)
117120
{
118-
_providers = new Collection<ProviderConfigurationEntry>
121+
providers = new Collection<ProviderConfigurationEntry>
119122
{
120123
new ProviderConfigurationEntry("Sitecore PowerShell Provider",
121124
typeof (PsSitecoreItemProvider),
122125
@"..\sitecore modules\PowerShell\Assets\Cognifide.PowerShell.dll-Help.maml")
123126
};
124127
}
125128

126-
return _providers;
129+
return providers;
127130
}
128131
}
129132

@@ -132,6 +135,11 @@ public static List<CmdletConfigurationEntry> Commandlets
132135
get { return commandlets; }
133136
}
134137

138+
public static Dictionary<string, string> Completers
139+
{
140+
get { return completers; }
141+
}
142+
135143
private static void GetCommandletsFromAssembly(Assembly assembly, WildcardPattern wildcard)
136144
{
137145
var helpPath = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.PrivateBinPath) +
@@ -144,6 +152,16 @@ private static void GetCommandletsFromAssembly(Assembly assembly, WildcardPatter
144152
var attribute = (CmdletAttribute) (type.GetCustomAttributes(typeof (CmdletAttribute), true)[0]);
145153
Commandlets.Add(new CmdletConfigurationEntry(attribute.VerbName + "-" + attribute.NounName, type,
146154
helpPath));
155+
foreach (var property in type.GetProperties())
156+
{
157+
var propAttribute = (AutocompleteSetAttribute)
158+
property.GetCustomAttributes(typeof (AutocompleteSetAttribute), true).FirstOrDefault();
159+
if (propAttribute != null)
160+
{
161+
Completers.Add(attribute.VerbName + "-" + attribute.NounName+":"+property.Name,
162+
"["+type.FullName+"]::"+propAttribute.Values);
163+
}
164+
}
147165
}
148166
}
149167
}

Cognifide.PowerShell/Core/Host/CommandCompletion.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System.Collections;
2+
using System.Collections.Generic;
23
using System.Collections.ObjectModel;
34
using System.Linq;
45
using System.Management.Automation;
@@ -45,7 +46,17 @@ public static IEnumerable<string> FindMatches3(ScriptSession session, string com
4546
{
4647
string lastToken;
4748
var truncatedCommand = TruncatedCommand3(session, command, out lastToken);
48-
49+
var options = new Hashtable(1);
50+
var completers = new Hashtable(CognifideSitecorePowerShellSnapIn.Completers.Count);
51+
options.Add("CustomArgumentCompleters",completers);
52+
foreach (var completer in CognifideSitecorePowerShellSnapIn.Completers)
53+
{
54+
completers.Add(completer.Key,
55+
session.GetScriptBlock(
56+
"param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) \r\n " +
57+
completer.Value));
58+
}
59+
session.SetVariable("options",options);
4960
const string TabExpansionHelper =
5061
@"function ScPsTabExpansionHelper( [string] $inputScript, [int]$cursorColumn ){ TabExpansion2 $inputScript $cursorColumn |% { $_.CompletionMatches } |% { ""$($_.ResultType)|$($_.CompletionText)"" } }";
5162
session.ExecuteScriptPart(TabExpansionHelper);
@@ -71,12 +82,9 @@ public static IEnumerable<string> FindMatches2(ScriptSession session, string com
7182
teCmd.Parameters.Add("lastWord", lastToken);
7283
var teResult = session.ExecuteCommand(teCmd, false, true).Cast<string>();
7384

74-
//IEnumerable<string> teResult = session.ExecuteScriptPart(string.Format("TabExpansion \"{0}\" \"{1}\"", command, lastToken),false,true).Cast<string>();
7585
var result = new List<string>();
7686
WrapResults(truncatedCommand, teResult, result, aceResponse);
7787

78-
//int prefixFileNameLength = Path.GetFileName(lastToken).Length;
79-
//string pathPrefix = lastToken.Substring(lastToken.Length - prefixFileNameLength);
8088
var splitPathResult =
8189
(session.ExecuteScriptPart(string.Format("Split-Path \"{0}*\" -IsAbsolute", lastToken), false, true)
8290
.FirstOrDefault());
@@ -87,7 +95,6 @@ public static IEnumerable<string> FindMatches2(ScriptSession session, string com
8795
var commandLine = string.Format("Resolve-Path \"{0}*\"", lastToken);
8896
var psResults = session.ExecuteScriptPart(commandLine, false, true);
8997
var rpResult = psResults.Cast<PathInfo>().Select(p => p.Path);
90-
//result.AddRange(rpResult.Select(l => truncatedCommand + l.Path));
9198
WrapResults(truncatedCommand, rpResult, result, aceResponse);
9299
}
93100
else
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
3+
namespace Cognifide.PowerShell.Core.Validation
4+
{
5+
public class AutocompleteSetAttribute : Attribute
6+
{
7+
public string Values { get; private set; }
8+
9+
public AutocompleteSetAttribute(string values)
10+
{
11+
Values = values;
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)