Skip to content

Commit 5976ec4

Browse files
committed
Use CompletionHandlerBase ABC instead of interfaces
1 parent 5098f0d commit 5976ec4

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

src/PowerShellEditorServices/Services/TextDocument/Handlers/CompletionHandler.cs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,12 @@
2222

2323
namespace Microsoft.PowerShell.EditorServices.Handlers
2424
{
25-
// TODO: Use ABCs.
26-
internal class PsesCompletionHandler : ICompletionHandler, ICompletionResolveHandler
25+
internal class PsesCompletionHandler : CompletionHandlerBase
2726
{
2827
private readonly ILogger _logger;
2928
private readonly IRunspaceContext _runspaceContext;
3029
private readonly IInternalPowerShellExecutionService _executionService;
3130
private readonly WorkspaceService _workspaceService;
32-
private CompletionCapability _capability;
33-
private readonly Guid _id = Guid.NewGuid();
34-
35-
Guid ICanBeIdentifiedHandler.Id => _id;
3631

3732
public PsesCompletionHandler(
3833
ILoggerFactory factory,
@@ -46,14 +41,15 @@ public PsesCompletionHandler(
4641
_workspaceService = workspaceService;
4742
}
4843

49-
public CompletionRegistrationOptions GetRegistrationOptions(CompletionCapability capability, ClientCapabilities clientCapabilities) => new()
44+
protected override CompletionRegistrationOptions CreateRegistrationOptions(CompletionCapability capability, ClientCapabilities clientCapabilities) => new()
5045
{
46+
// TODO: What do we do with the arguments?
5147
DocumentSelector = LspUtils.PowerShellDocumentSelector,
5248
ResolveProvider = true,
5349
TriggerCharacters = new[] { ".", "-", ":", "\\", "$" }
5450
};
5551

56-
public async Task<CompletionList> Handle(CompletionParams request, CancellationToken cancellationToken)
52+
public override async Task<CompletionList> Handle(CompletionParams request, CancellationToken cancellationToken)
5753
{
5854
int cursorLine = request.Position.Line + 1;
5955
int cursorColumn = request.Position.Character + 1;
@@ -68,13 +64,8 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT
6864
return new CompletionList(completionResults);
6965
}
7066

71-
public static bool CanResolve(CompletionItem value)
72-
{
73-
return value.Kind == CompletionItemKind.Function;
74-
}
75-
7667
// Handler for "completionItem/resolve". In VSCode this is fired when a completion item is highlighted in the completion list.
77-
public async Task<CompletionItem> Handle(CompletionItem request, CancellationToken cancellationToken)
68+
public override async Task<CompletionItem> Handle(CompletionItem request, CancellationToken cancellationToken)
7869
{
7970
// We currently only support this request for anything that returns a CommandInfo:
8071
// functions, cmdlets, aliases. No detail means the module hasn't been imported yet and
@@ -105,11 +96,6 @@ public async Task<CompletionItem> Handle(CompletionItem request, CancellationTok
10596
return request;
10697
}
10798

108-
public void SetCapability(CompletionCapability capability, ClientCapabilities clientCapabilities)
109-
{
110-
_capability = capability;
111-
}
112-
11399
/// <summary>
114100
/// Gets completions for a statement contained in the given
115101
/// script file at the specified line and column position.
@@ -126,7 +112,7 @@ public void SetCapability(CompletionCapability capability, ClientCapabilities cl
126112
/// <returns>
127113
/// A CommandCompletion instance completions for the identified statement.
128114
/// </returns>
129-
public async Task<IEnumerable<CompletionItem>> GetCompletionsInFileAsync(
115+
internal async Task<IEnumerable<CompletionItem>> GetCompletionsInFileAsync(
130116
ScriptFile scriptFile,
131117
int lineNumber,
132118
int columnNumber,
@@ -142,15 +128,15 @@ public async Task<IEnumerable<CompletionItem>> GetCompletionsInFileAsync(
142128
_logger,
143129
cancellationToken).ConfigureAwait(false);
144130

145-
// Only calculate the replacement range if there are completions.
146-
BufferRange replacedRange = new(0, 0, 0, 0);
147-
if (result.CompletionMatches.Count > 0)
131+
if (result.CompletionMatches.Count == 0)
148132
{
149-
replacedRange = scriptFile.GetRangeBetweenOffsets(
150-
result.ReplacementIndex,
151-
result.ReplacementIndex + result.ReplacementLength);
133+
return Array.Empty<CompletionItem>();
152134
}
153135

136+
BufferRange replacedRange = scriptFile.GetRangeBetweenOffsets(
137+
result.ReplacementIndex,
138+
result.ReplacementIndex + result.ReplacementLength);
139+
154140
// Create OmniSharp CompletionItems from PowerShell CompletionResults. We use a for loop
155141
// because the index is used for sorting.
156142
CompletionItem[] completionItems = new CompletionItem[result.CompletionMatches.Count];
@@ -282,8 +268,8 @@ private static bool TryBuildSnippet(string completionText, out string snippet)
282268
// Since we want to use a "tab stop" we need to escape a few things.
283269
StringBuilder sb = new StringBuilder(completionText)
284270
.Replace(@"\", @"\\")
285-
.Replace(@"}", @"\}")
286-
.Replace(@"$", @"\$");
271+
.Replace("}", @"\}")
272+
.Replace("$", @"\$");
287273
snippet = sb.Insert(sb.Length - 1, "$0").ToString();
288274
return true;
289275
}

0 commit comments

Comments
 (0)