forked from PowerShell/PowerShellEditorServices
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSymbolsService.cs
231 lines (201 loc) · 8.7 KB
/
SymbolsService.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
//
// 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.Collections.Specialized;
using System.Linq;
using System.Runtime.InteropServices;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Symbols;
namespace Microsoft.PowerShell.EditorServices
{
/// <summary>
/// Provides a high-level service for performing code completion and
/// navigation operations on PowerShell scripts.
/// </summary>
public class SymbolsService
{
#region Private Fields
private readonly ILogger _logger;
private readonly IDocumentSymbolProvider[] _documentSymbolProviders;
#endregion
#region Constructors
/// <summary>
/// Constructs an instance of the SymbolsService class and uses
/// the given Runspace to execute language service operations.
/// </summary>
/// <param name="factory">An ILoggerFactory implementation used for writing log messages.</param>
public SymbolsService(
ILoggerFactory factory)
{
_logger = factory.CreateLogger<SymbolsService>();
_documentSymbolProviders = new IDocumentSymbolProvider[]
{
new ScriptDocumentSymbolProvider(VersionUtils.PSVersion),
new PsdDocumentSymbolProvider(),
new PesterDocumentSymbolProvider()
};
}
#endregion
/// <summary>
/// Finds all the symbols in a file.
/// </summary>
/// <param name="scriptFile">The ScriptFile in which the symbol can be located.</param>
/// <returns></returns>
public List<SymbolReference> FindSymbolsInFile(ScriptFile scriptFile)
{
Validate.IsNotNull(nameof(scriptFile), scriptFile);
var foundOccurrences = new List<SymbolReference>();
foreach (IDocumentSymbolProvider symbolProvider in _documentSymbolProviders)
{
foreach (SymbolReference reference in symbolProvider.ProvideDocumentSymbols(scriptFile))
{
reference.SourceLine = scriptFile.GetLine(reference.ScriptRegion.StartLineNumber);
reference.FilePath = scriptFile.FilePath;
foundOccurrences.Add(reference);
}
}
return foundOccurrences;
}
/// <summary>
/// Finds the symbol in the script given a file location
/// </summary>
/// <param name="scriptFile">The details and contents of a open script file</param>
/// <param name="lineNumber">The line number of the cursor for the given script</param>
/// <param name="columnNumber">The coulumn number of the cursor for the given script</param>
/// <returns>A SymbolReference of the symbol found at the given location
/// or null if there is no symbol at that location
/// </returns>
public SymbolReference FindSymbolAtLocation(
ScriptFile scriptFile,
int lineNumber,
int columnNumber)
{
SymbolReference symbolReference =
AstOperations.FindSymbolAtPosition(
scriptFile.ScriptAst,
lineNumber,
columnNumber);
if (symbolReference != null)
{
symbolReference.FilePath = scriptFile.FilePath;
}
return symbolReference;
}
/// <summary>
/// Finds all the references of a symbol
/// </summary>
/// <param name="foundSymbol">The symbol to find all references for</param>
/// <param name="referencedFiles">An array of scriptFiles too search for references in</param>
/// <param name="workspace">The workspace that will be searched for symbols</param>
/// <returns>FindReferencesResult</returns>
public List<SymbolReference> FindReferencesOfSymbol(
SymbolReference foundSymbol,
ScriptFile[] referencedFiles,
WorkspaceService workspace)
{
if (foundSymbol == null)
{
return null;
}
// NOTE: we use to make sure aliases were loaded but took it out because we needed the pipeline thread.
// We want to look for references first in referenced files, hence we use ordered dictionary
// TODO: File system case-sensitivity is based on filesystem not OS, but OS is a much cheaper heuristic
var fileMap = RuntimeInformation.IsOSPlatform(OSPlatform.Linux)
? new OrderedDictionary()
: new OrderedDictionary(StringComparer.OrdinalIgnoreCase);
foreach (ScriptFile scriptFile in referencedFiles)
{
fileMap[scriptFile.FilePath] = scriptFile;
}
foreach (string filePath in workspace.EnumeratePSFiles())
{
if (!fileMap.Contains(filePath))
{
if (!workspace.TryGetFile(filePath, out ScriptFile scriptFile))
{
// If we can't access the file for some reason, just ignore it
continue;
}
fileMap[filePath] = scriptFile;
}
}
var symbolReferences = new List<SymbolReference>();
foreach (object fileName in fileMap.Keys)
{
var file = (ScriptFile)fileMap[fileName];
IEnumerable<SymbolReference> references = AstOperations.FindReferencesOfSymbol(
file.ScriptAst,
foundSymbol,
needsAliases: false);
foreach (SymbolReference reference in references)
{
try
{
reference.SourceLine = file.GetLine(reference.ScriptRegion.StartLineNumber);
}
catch (ArgumentOutOfRangeException e)
{
reference.SourceLine = string.Empty;
_logger.LogException("Found reference is out of range in script file", e);
}
reference.FilePath = file.FilePath;
symbolReferences.Add(reference);
}
}
return symbolReferences;
}
/// <summary>
/// Finds all the occurences of a symbol in the script given a file location
/// </summary>
/// <param name="file">The details and contents of a open script file</param>
/// <param name="symbolLineNumber">The line number of the cursor for the given script</param>
/// <param name="symbolColumnNumber">The coulumn number of the cursor for the given script</param>
/// <returns>FindOccurrencesResult</returns>
public IReadOnlyList<SymbolReference> FindOccurrencesInFile(
ScriptFile file,
int symbolLineNumber,
int symbolColumnNumber)
{
SymbolReference foundSymbol = AstOperations.FindSymbolAtPosition(
file.ScriptAst,
symbolLineNumber,
symbolColumnNumber);
if (foundSymbol == null)
{
return null;
}
return AstOperations.FindReferencesOfSymbol(
file.ScriptAst,
foundSymbol,
needsAliases: false).ToArray();
}
/// Finds a function definition in the script given a file location
/// </summary>
/// <param name="scriptFile">The details and contents of a open script file</param>
/// <param name="lineNumber">The line number of the cursor for the given script</param>
/// <param name="columnNumber">The coulumn number of the cursor for the given script</param>
/// <returns>A SymbolReference of the symbol found at the given location
/// or null if there is no symbol at that location
/// </returns>
public SymbolReference FindFunctionDefinitionAtLocation(
ScriptFile scriptFile,
int lineNumber,
int columnNumber)
{
SymbolReference symbolReference =
AstOperations.FindSymbolAtPosition(
scriptFile.ScriptAst,
lineNumber,
columnNumber,
includeFunctionDefinitions: true);
if (symbolReference != null)
{
symbolReference.FilePath = scriptFile.FilePath;
}
return symbolReference;
}
}
}