-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathReferencesCodeLensProvider.cs
177 lines (159 loc) · 6.97 KB
/
ReferencesCodeLensProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.PowerShell.EditorServices.Commands;
using Microsoft.PowerShell.EditorServices.Protocol.LanguageServer;
using Microsoft.PowerShell.EditorServices.Symbols;
namespace Microsoft.PowerShell.EditorServices.CodeLenses
{
/// <summary>
/// Provides the "reference" code lens by extracting document symbols.
/// </summary>
internal class ReferencesCodeLensProvider : FeatureProviderBase, ICodeLensProvider
{
private static readonly Location[] s_emptyLocationArray = new Location[0];
/// <summary>
/// The editor session code lenses are being provided from.
/// </summary>
private EditorSession _editorSession;
/// <summary>
/// The document symbol provider to supply symbols to generate the code lenses.
/// </summary>
private IDocumentSymbolProvider _symbolProvider;
/// <summary>
/// Construct a new ReferencesCodeLensProvider for a given EditorSession.
/// </summary>
/// <param name="editorSession"></param>
public ReferencesCodeLensProvider(EditorSession editorSession)
{
_editorSession = editorSession;
// TODO: Pull this from components
_symbolProvider = new ScriptDocumentSymbolProvider(
editorSession.PowerShellContext.LocalPowerShellVersion.Version);
}
/// <summary>
/// Get all reference code lenses for a given script file.
/// </summary>
/// <param name="scriptFile">The PowerShell script file to get code lenses for.</param>
/// <returns>An array of CodeLenses describing all functions in the given script file.</returns>
public CodeLens[] ProvideCodeLenses(ScriptFile scriptFile)
{
var acc = new List<CodeLens>();
foreach (SymbolReference sym in _symbolProvider.ProvideDocumentSymbols(scriptFile))
{
if (sym.SymbolType == SymbolType.Function)
{
acc.Add(new CodeLens(this, scriptFile, sym.ScriptRegion));
}
}
return acc.ToArray();
}
/// <summary>
/// Take a codelens and create a new codelens object with updated references.
/// </summary>
/// <param name="codeLens">The old code lens to get updated references for.</param>
/// <param name="cancellationToken">The cancellation token for this request.</param>
/// <returns>A new code lens object describing the same data as the old one but with updated references.</returns>
public async Task<CodeLens> ResolveCodeLensAsync(
CodeLens codeLens,
CancellationToken cancellationToken)
{
ScriptFile[] references = _editorSession.Workspace.ExpandScriptReferences(
codeLens.File);
SymbolReference foundSymbol = _editorSession.LanguageService.FindFunctionDefinitionAtLocation(
codeLens.File,
codeLens.ScriptExtent.StartLineNumber,
codeLens.ScriptExtent.StartColumnNumber);
FindReferencesResult referencesResult = await _editorSession.LanguageService.FindReferencesOfSymbolAsync(
foundSymbol,
references,
_editorSession.Workspace);
Location[] referenceLocations;
if (referencesResult == null)
{
referenceLocations = s_emptyLocationArray;
}
else
{
var acc = new List<Location>();
foreach (SymbolReference foundReference in referencesResult.FoundReferences)
{
if (!NotReferenceDefinition(foundSymbol, foundReference))
{
continue;
}
acc.Add(new Location
{
Uri = GetFileUri(foundReference.FilePath),
Range = foundReference.ScriptRegion.ToRange()
});
}
referenceLocations = acc.ToArray();
}
return new CodeLens(
codeLens,
new ClientCommand(
"editor.action.showReferences",
GetReferenceCountHeader(referenceLocations.Length),
new object[]
{
codeLens.File.ClientFilePath,
codeLens.ScriptExtent.ToRange().Start,
referenceLocations,
}
));
}
/// <summary>
/// Check whether a SymbolReference is not a reference to another defined symbol.
/// </summary>
/// <param name="definition">The symbol definition that may be referenced.</param>
/// <param name="reference">The reference symbol to check.</param>
/// <returns>True if the reference is not a reference to the definition, false otherwise.</returns>
private static bool NotReferenceDefinition(
SymbolReference definition,
SymbolReference reference)
{
return
definition.ScriptRegion.StartLineNumber != reference.ScriptRegion.StartLineNumber
|| definition.SymbolType != reference.SymbolType
|| !string.Equals(definition.SymbolName, reference.SymbolName, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Get a URI for a given file path.
/// </summary>
/// <param name="filePath">A file path that may be prefixed with URI scheme already.</param>
/// <returns>A URI to the file.</returns>
private static string GetFileUri(string filePath)
{
// If the file isn't untitled, return a URI-style path
return
!filePath.StartsWith("untitled") && !filePath.StartsWith("inmemory")
? new Uri("file://" + filePath).AbsoluteUri
: filePath;
}
/// <summary>
/// Get the code lens header for the number of references on a definition,
/// given the number of references.
/// </summary>
/// <param name="referenceCount">The number of references found for a given definition.</param>
/// <returns>The header string for the reference code lens.</returns>
private static string GetReferenceCountHeader(int referenceCount)
{
if (referenceCount == 1)
{
return "1 reference";
}
var sb = new StringBuilder(14); // "100 references".Length = 14
sb.Append(referenceCount);
sb.Append(" references");
return sb.ToString();
}
}
}