Skip to content

Commit 34ccff0

Browse files
committed
Autocomplete improvements
1 parent 902cb61 commit 34ccff0

File tree

7 files changed

+98
-41
lines changed

7 files changed

+98
-41
lines changed

Cognifide.PowerShell/Commandlets/BaseCommand.cs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,8 @@
1919

2020
namespace Cognifide.PowerShell.Commandlets
2121
{
22-
public class BaseCommand : PSCmdlet, IDynamicParameters
22+
public class BaseCommand : PSCmdlet
2323
{
24-
private readonly RuntimeDefinedParameterDictionary parameters;
25-
26-
public BaseCommand()
27-
{
28-
parameters = new RuntimeDefinedParameterDictionary();
29-
}
30-
3124
protected bool IsCurrentDriveSitecore
3225
{
3326
get
@@ -222,14 +215,15 @@ protected void WriteItem(Item item)
222215
}
223216
}
224217

225-
public virtual object GetDynamicParameters()
218+
protected bool IsParameterSpecified(string name)
226219
{
227-
return parameters;
220+
return MyInvocation.BoundParameters.ContainsKey(name);
228221
}
229222

230-
protected bool IsParameterSpecified(string name)
223+
protected static string WrapNameWithSpacesInQuotes(string name)
231224
{
232-
return MyInvocation.BoundParameters.ContainsKey(name);
225+
return name.Contains(" ") ? "\"" + name + "\"" : name;
233226
}
227+
234228
}
235229
}

Cognifide.PowerShell/Commandlets/BaseItemCommand.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
using System.Collections.Generic;
22
using System.Data;
3+
using System.Globalization;
34
using System.Linq;
45
using System.Management.Automation;
6+
using Cognifide.PowerShell.Core.Validation;
57
using Sitecore.Data.Items;
68

79
namespace Cognifide.PowerShell.Commandlets
810
{
911
public abstract class BaseItemCommand : BaseLanguageAgnosticItemCommand
1012
{
13+
public static string[] Cultures =
14+
CultureInfo.GetCultures(CultureTypes.AllCultures).Where(culture => !string.IsNullOrEmpty(culture.Name)).Select(culture => culture.Name).ToArray();
15+
16+
[AutocompleteSet("Cultures")]
1117
[Alias("Languages")]
1218
[Parameter(ParameterSetName = "Item from Path")]
1319
[Parameter(ParameterSetName = "Item from ID")]

Cognifide.PowerShell/Commandlets/BaseLanguageAgnosticItemCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Cognifide.PowerShell.Commandlets
99
{
1010
public abstract class BaseLanguageAgnosticItemCommand : BaseShellCommand
1111
{
12-
private static readonly string[] Databases = Factory.GetDatabaseNames();
12+
public static readonly string[] Databases = Factory.GetDatabaseNames();
1313

1414
[Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true,
1515
ParameterSetName = "Item from Pipeline", Mandatory = true, Position = 0)]

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ namespace Cognifide.PowerShell.Commandlets.Data.Search
1818
[OutputType(typeof(Item))]
1919
public class FindItemCommand : BaseCommand
2020
{
21-
public static readonly string[] Indexes = ContentSearchManager.Indexes.Select(i => i.Name).ToArray();
21+
public static string[] Indexes
22+
{
23+
get { return ContentSearchManager.Indexes.Select(i => i.Name).ToArray(); }
24+
}
2225

2326
[AutocompleteSet("Indexes")]
2427
[Parameter(Mandatory = true, Position = 0)]

Cognifide.PowerShell/Commandlets/Data/TestRuleCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace Cognifide.PowerShell.Commandlets.Data
1414
[OutputType(typeof (bool))]
1515
public class TestRuleCommand : BaseCommand
1616
{
17-
private static readonly string[] Databases = Factory.GetDatabaseNames();
17+
public static readonly string[] Databases = Factory.GetDatabaseNames();
18+
1819
private RuleList<RuleContext> rules;
1920

2021
[Parameter]

Cognifide.PowerShell/Commandlets/Session/ImportFunctionCommand.cs

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Cognifide.PowerShell.Core.Settings;
1010
using Cognifide.PowerShell.Core.Utility;
1111
using Cognifide.PowerShell.Core.Validation;
12+
using Sitecore.ContentSearch.Utilities;
1213
using Sitecore.Data.Items;
1314
using Sitecore.Diagnostics;
1415

@@ -18,9 +19,10 @@ namespace Cognifide.PowerShell.Commandlets.Session
1819
[OutputType(typeof (object))]
1920
public class ImportFunctionCommand : BaseShellCommand
2021
{
21-
private static string[] Functions;
22-
private static string[] Libraries;
23-
private static string[] Modules;
22+
private static string[] functions;
23+
private static string[] libraries;
24+
private static string[] modules;
25+
2426

2527
[Parameter(Mandatory = true, Position = 0)]
2628
[AutocompleteSet("Functions")]
@@ -34,21 +36,47 @@ public class ImportFunctionCommand : BaseShellCommand
3436
[AutocompleteSet("Modules")]
3537
public string Module { get; set; }
3638

39+
public static string[] Functions
40+
{
41+
get
42+
{
43+
if (functions == null)
44+
{
45+
UpdateCache();
46+
}
47+
return functions;
48+
}
49+
}
3750

38-
public override object GetDynamicParameters()
51+
public static string[] Libraries
3952
{
40-
if (Functions == null)
53+
get
4154
{
42-
UpdateCache();
55+
if (functions == null)
56+
{
57+
UpdateCache();
58+
}
59+
return libraries;
4360
}
61+
}
4462

45-
return base.GetDynamicParameters();
63+
public static string[] Modules
64+
{
65+
get
66+
{
67+
if (functions == null)
68+
{
69+
UpdateCache();
70+
}
71+
return modules;
72+
}
4673
}
4774

75+
4876
static ImportFunctionCommand()
4977
{
5078
ModuleManager.OnInvalidate += InvalidateCache;
51-
Functions = null;
79+
functions = null;
5280
}
5381

5482
// Methods
@@ -126,6 +154,16 @@ private static void UpdateCache()
126154
var localFunctions = new List<string>();
127155
var roots = ModuleManager.GetFeatureRoots(IntegrationPoints.FunctionsFeature);
128156

157+
modules =
158+
(from module in ModuleManager.Modules where module.Enabled select module.Name).ToList()
159+
.ConvertAll(WrapNameWithSpacesInQuotes)
160+
.ToArray();
161+
162+
libraries = (from root in roots
163+
from Item library in root.GetChildren()
164+
where library.TemplateName == TemplateNames.ScriptLibraryTemplateName
165+
select library.Name).ToList().ConvertAll(WrapNameWithSpacesInQuotes).ToArray();
166+
129167
foreach (var root in roots)
130168
{
131169
var path = PathUtilities.PreparePathForQuery(root.Paths.Path);
@@ -135,26 +173,20 @@ private static void UpdateCache()
135173
try
136174
{
137175
var results = root.Database.SelectItems(query);
138-
localFunctions.AddRange(results.ToList().ConvertAll(p => p.Name));
176+
localFunctions.AddRange(
177+
results.ToList().ConvertAll(p => WrapNameWithSpacesInQuotes(p.Name)));
139178
}
140179
catch (Exception ex)
141180
{
142181
Log.Error("Error while querying for items", ex);
143182
}
144183
}
145-
Functions = localFunctions.ToArray();
146-
147-
Libraries = (from root in roots
148-
from Item library in root.GetChildren()
149-
where library.TemplateName == TemplateNames.ScriptLibraryTemplateName
150-
select library.Name).ToArray();
151-
152-
Modules = (from module in ModuleManager.Modules where module.Enabled select module.Name).ToArray();
184+
functions = localFunctions.ToArray();
153185
}
154186

155187
public static void InvalidateCache(object sender, EventArgs e)
156188
{
157-
Functions = null;
189+
functions = null;
158190
}
159191
}
160192
}

Cognifide.PowerShell/Core/Host/CommandCompletion.cs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,24 +46,45 @@ public static IEnumerable<string> FindMatches3(ScriptSession session, string com
4646
{
4747
string lastToken;
4848
var truncatedCommand = TruncatedCommand3(session, command, out lastToken);
49-
var options = new Hashtable(1);
50-
var completers = new Hashtable(CognifideSitecorePowerShellSnapIn.Completers.Count);
51-
options.Add("CustomArgumentCompleters",completers);
49+
Hashtable options = null;//session.GetVariable("options") as Hashtable;
50+
Hashtable completers = null;
51+
if (options == null)
52+
{
53+
options = new Hashtable(1);
54+
}
55+
56+
if (options.ContainsKey("CustomArgumentCompleters"))
57+
{
58+
completers = options["CustomArgumentCompleters"] as Hashtable;
59+
}
60+
if (completers == null)
61+
{
62+
completers = new Hashtable(CognifideSitecorePowerShellSnapIn.Completers.Count);
63+
options.Add("CustomArgumentCompleters", completers);
64+
}
65+
5266
foreach (var completer in CognifideSitecorePowerShellSnapIn.Completers)
5367
{
54-
completers.Add(completer.Key,
55-
session.GetScriptBlock(
56-
"param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) \r\n " +
57-
completer.Value));
68+
if (!completers.ContainsKey(completer.Key))
69+
{
70+
completers.Add(completer.Key,
71+
session.GetScriptBlock(
72+
"param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter) \r\n " +
73+
completer.Value +
74+
" | ForEach-Object { New-Object System.Management.Automation.CompletionResult $_, $_, 'ParameterValue', $_ }"
75+
));
76+
}
5877
}
5978
session.SetVariable("options",options);
79+
6080
const string TabExpansionHelper =
61-
@"function ScPsTabExpansionHelper( [string] $inputScript, [int]$cursorColumn ){ TabExpansion2 $inputScript $cursorColumn |% { $_.CompletionMatches } |% { ""$($_.ResultType)|$($_.CompletionText)"" } }";
81+
@"function ScPsTabExpansionHelper( [string] $inputScript, [int]$cursorColumn ){ TabExpansion2 $inputScript $cursorColumn -Options $options |% { $_.CompletionMatches } |% { ""$($_.ResultType)|$($_.CompletionText)"" } }";
6282
session.ExecuteScriptPart(TabExpansionHelper);
6383

6484
var teCmd = new Command("ScPsTabExpansionHelper");
6585
teCmd.Parameters.Add("inputScript", command);
6686
teCmd.Parameters.Add("cursorColumn", command.Length);
87+
teCmd.Parameters.Add("options", options);
6788

6889
var teResult = session.ExecuteCommand(teCmd, false, true).Cast<string>().ToArray();
6990
var result = new List<string>();

0 commit comments

Comments
 (0)