22
22
23
23
namespace Microsoft . PowerShell . EditorServices . Handlers
24
24
{
25
- // TODO: Use ABCs.
26
- internal class PsesCompletionHandler : ICompletionHandler , ICompletionResolveHandler
25
+ internal class PsesCompletionHandler : CompletionHandlerBase
27
26
{
28
27
private readonly ILogger _logger ;
29
28
private readonly IRunspaceContext _runspaceContext ;
30
29
private readonly IInternalPowerShellExecutionService _executionService ;
31
30
private readonly WorkspaceService _workspaceService ;
32
- private CompletionCapability _capability ;
33
- private readonly Guid _id = Guid . NewGuid ( ) ;
34
-
35
- Guid ICanBeIdentifiedHandler . Id => _id ;
36
31
37
32
public PsesCompletionHandler (
38
33
ILoggerFactory factory ,
@@ -46,14 +41,15 @@ public PsesCompletionHandler(
46
41
_workspaceService = workspaceService ;
47
42
}
48
43
49
- public CompletionRegistrationOptions GetRegistrationOptions ( CompletionCapability capability , ClientCapabilities clientCapabilities ) => new ( )
44
+ protected override CompletionRegistrationOptions CreateRegistrationOptions ( CompletionCapability capability , ClientCapabilities clientCapabilities ) => new ( )
50
45
{
46
+ // TODO: What do we do with the arguments?
51
47
DocumentSelector = LspUtils . PowerShellDocumentSelector ,
52
48
ResolveProvider = true ,
53
49
TriggerCharacters = new [ ] { "." , "-" , ":" , "\\ " , "$" }
54
50
} ;
55
51
56
- public async Task < CompletionList > Handle ( CompletionParams request , CancellationToken cancellationToken )
52
+ public override async Task < CompletionList > Handle ( CompletionParams request , CancellationToken cancellationToken )
57
53
{
58
54
int cursorLine = request . Position . Line + 1 ;
59
55
int cursorColumn = request . Position . Character + 1 ;
@@ -68,13 +64,8 @@ public async Task<CompletionList> Handle(CompletionParams request, CancellationT
68
64
return new CompletionList ( completionResults ) ;
69
65
}
70
66
71
- public static bool CanResolve ( CompletionItem value )
72
- {
73
- return value . Kind == CompletionItemKind . Function ;
74
- }
75
-
76
67
// 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 )
78
69
{
79
70
// We currently only support this request for anything that returns a CommandInfo:
80
71
// 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
105
96
return request ;
106
97
}
107
98
108
- public void SetCapability ( CompletionCapability capability , ClientCapabilities clientCapabilities )
109
- {
110
- _capability = capability ;
111
- }
112
-
113
99
/// <summary>
114
100
/// Gets completions for a statement contained in the given
115
101
/// script file at the specified line and column position.
@@ -126,7 +112,7 @@ public void SetCapability(CompletionCapability capability, ClientCapabilities cl
126
112
/// <returns>
127
113
/// A CommandCompletion instance completions for the identified statement.
128
114
/// </returns>
129
- public async Task < IEnumerable < CompletionItem > > GetCompletionsInFileAsync (
115
+ internal async Task < IEnumerable < CompletionItem > > GetCompletionsInFileAsync (
130
116
ScriptFile scriptFile ,
131
117
int lineNumber ,
132
118
int columnNumber ,
@@ -142,15 +128,15 @@ public async Task<IEnumerable<CompletionItem>> GetCompletionsInFileAsync(
142
128
_logger ,
143
129
cancellationToken ) . ConfigureAwait ( false ) ;
144
130
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 )
148
132
{
149
- replacedRange = scriptFile . GetRangeBetweenOffsets (
150
- result . ReplacementIndex ,
151
- result . ReplacementIndex + result . ReplacementLength ) ;
133
+ return Array . Empty < CompletionItem > ( ) ;
152
134
}
153
135
136
+ BufferRange replacedRange = scriptFile . GetRangeBetweenOffsets (
137
+ result . ReplacementIndex ,
138
+ result . ReplacementIndex + result . ReplacementLength ) ;
139
+
154
140
// Create OmniSharp CompletionItems from PowerShell CompletionResults. We use a for loop
155
141
// because the index is used for sorting.
156
142
CompletionItem [ ] completionItems = new CompletionItem [ result . CompletionMatches . Count ] ;
@@ -282,8 +268,8 @@ private static bool TryBuildSnippet(string completionText, out string snippet)
282
268
// Since we want to use a "tab stop" we need to escape a few things.
283
269
StringBuilder sb = new StringBuilder ( completionText )
284
270
. Replace ( @"\" , @"\\" )
285
- . Replace ( @ "}", @"\}" )
286
- . Replace ( @ "$", @"\$" ) ;
271
+ . Replace ( "}" , @"\}" )
272
+ . Replace ( "$" , @"\$" ) ;
287
273
snippet = sb . Insert ( sb . Length - 1 , "$0" ) . ToString ( ) ;
288
274
return true ;
289
275
}
0 commit comments