Skip to content

Commit 57392ba

Browse files
daviwilKayla Davis
authored andcommitted
Simplify Workspace file operations and fix bugs
1 parent 7755795 commit 57392ba

File tree

10 files changed

+108
-134
lines changed

10 files changed

+108
-134
lines changed

src/PowerShellEditorServices.Transport.Stdio/Request/DeclarationRequest.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ public override void ProcessMessage(
2626

2727
GetDefinitionResult definition =
2828
editorSession.LanguageService.GetDefinitionOfSymbol(
29+
scriptFile,
2930
foundSymbol,
30-
editorSession.Workspace.ExpandScriptReferences(scriptFile));
31+
editorSession.Workspace);
3132

3233
if (definition != null)
3334
{

src/PowerShellEditorServices.Transport.Stdio/Request/ErrorRequest.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,9 @@ public override void ProcessMessage(
3333
// Get the requested files
3434
foreach (string filePath in this.Arguments.Files)
3535
{
36-
ScriptFile scriptFile = null;
37-
38-
if (!editorSession.Workspace.TryGetFile(filePath, out scriptFile))
39-
{
40-
// Skip this file and log the file load error
41-
// TODO: Trace out the error message
42-
continue;
43-
}
36+
ScriptFile scriptFile =
37+
editorSession.Workspace.GetFile(
38+
filePath);
4439

4540
var semanticMarkers =
4641
editorSession.AnalysisService.GetSemanticMarkers(

src/PowerShellEditorServices.Transport.Stdio/Request/FileRequest.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,9 @@ public abstract class FileRequest<TArguments> : RequestBase<TArguments>
1313
{
1414
protected ScriptFile GetScriptFile(EditorSession editorSession)
1515
{
16-
ScriptFile scriptFile = null;
17-
18-
if(!editorSession.Workspace.TryGetFile(
19-
this.Arguments.File,
20-
out scriptFile))
21-
{
22-
// TODO: Throw an exception that the message loop can create a response out of
23-
24-
throw new FileNotFoundException(
25-
"A ScriptFile with the following path was not found in the EditorSession: {0}",
16+
return
17+
editorSession.Workspace.GetFile(
2618
this.Arguments.File);
27-
}
28-
29-
return scriptFile;
3019
}
3120
}
3221

src/PowerShellEditorServices.Transport.Stdio/Request/OpenFileRequest.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,8 @@ public override void ProcessMessage(
2626
EditorSession editorSession,
2727
MessageWriter messageWriter)
2828
{
29-
ScriptFile scriptFile = null;
30-
31-
// Only load the file if it isn't loaded already
32-
if (!editorSession.Workspace.TryGetFile(this.Arguments.File, out scriptFile))
33-
{
34-
// Open the file in the current session
35-
editorSession.Workspace.OpenFile(this.Arguments.File);
36-
}
29+
// Open the file in the current session
30+
editorSession.Workspace.GetFile(this.Arguments.File);
3731
}
3832
}
3933
}

src/PowerShellEditorServices/Analysis/AnalysisService.cs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,26 +58,34 @@ public AnalysisService(Runspace analysisRunspace)
5858
/// <returns>An array of ScriptFileMarkers containing semantic analysis results.</returns>
5959
public ScriptFileMarker[] GetSemanticMarkers(ScriptFile file)
6060
{
61-
// TODO: This is a temporary fix until we can change how
62-
// ScriptAnalyzer invokes their async tasks.
63-
Task<ScriptFileMarker[]> analysisTask =
64-
Task.Factory.StartNew<ScriptFileMarker[]>(
65-
() =>
66-
{
67-
return
68-
this.scriptAnalyzer
69-
.AnalyzeSyntaxTree(
70-
file.ScriptAst,
71-
file.ScriptTokens,
72-
file.FilePath)
73-
.Select(ScriptFileMarker.FromDiagnosticRecord)
74-
.ToArray();
75-
},
76-
CancellationToken.None,
77-
TaskCreationOptions.None,
78-
TaskScheduler.Default);
61+
if (file.IsAnalysisEnabled)
62+
{
63+
// TODO: This is a temporary fix until we can change how
64+
// ScriptAnalyzer invokes their async tasks.
65+
Task<ScriptFileMarker[]> analysisTask =
66+
Task.Factory.StartNew<ScriptFileMarker[]>(
67+
() =>
68+
{
69+
return
70+
this.scriptAnalyzer
71+
.AnalyzeSyntaxTree(
72+
file.ScriptAst,
73+
file.ScriptTokens,
74+
file.FilePath)
75+
.Select(ScriptFileMarker.FromDiagnosticRecord)
76+
.ToArray();
77+
},
78+
CancellationToken.None,
79+
TaskCreationOptions.None,
80+
TaskScheduler.Default);
7981

80-
return analysisTask.Result;
82+
return analysisTask.Result;
83+
}
84+
else
85+
{
86+
// Return an empty marker list
87+
return new ScriptFileMarker[0];
88+
}
8189
}
8290

8391
#endregion

src/PowerShellEditorServices/Language/LanguageService.cs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
namespace Microsoft.PowerShell.EditorServices.Language
1111
{
1212
using Microsoft.PowerShell.EditorServices.Utility;
13-
using System.IO;
1413
using System.Management.Automation;
1514
using System.Management.Automation.Runspaces;
16-
using System.Text;
1715

1816
/// <summary>
1917
/// Provides a high-level service for performing code completion and
@@ -185,6 +183,7 @@ public FindReferencesResult FindReferencesOfSymbol(
185183
reference.FilePath = file.FilePath;
186184
return reference;
187185
});
186+
188187
symbolReferences.AddRange(symbolReferencesinFile);
189188
}
190189

@@ -206,10 +205,17 @@ public FindReferencesResult FindReferencesOfSymbol(
206205
/// <param name="referencedFiles">An array of scriptFiles too search for the definition in</param>
207206
/// <returns>GetDefinitionResult</returns>
208207
public GetDefinitionResult GetDefinitionOfSymbol(
208+
ScriptFile sourceFile,
209209
SymbolReference foundSymbol,
210-
ScriptFile[] referencedFiles)
210+
Workspace workspace)
211211
{
212+
Validate.IsNotNull("sourceFile", sourceFile);
212213
Validate.IsNotNull("foundSymbol", foundSymbol);
214+
Validate.IsNotNull("workspace", workspace);
215+
216+
ScriptFile[] referencedFiles =
217+
workspace.ExpandScriptReferences(
218+
sourceFile);
213219

214220
// look through the referenced files until definition is found
215221
// or there are no more file to look through
@@ -233,7 +239,11 @@ public GetDefinitionResult GetDefinitionOfSymbol(
233239
if (foundDefinition == null)
234240
{
235241
CommandInfo cmdInfo = GetCommandInfo(foundSymbol.SymbolName);
236-
foundDefinition = FindDeclarationForBuiltinCommand(cmdInfo, foundSymbol);
242+
foundDefinition =
243+
FindDeclarationForBuiltinCommand(
244+
cmdInfo,
245+
foundSymbol,
246+
workspace);
237247
}
238248

239249
return new GetDefinitionResult(foundDefinition);
@@ -334,7 +344,9 @@ private CommandInfo GetCommandInfo(string commandName)
334344
return commandInfo;
335345
}
336346

337-
private ScriptFile[] GetBuiltinCommandScriptFiles(PSModuleInfo moduleInfo)
347+
private ScriptFile[] GetBuiltinCommandScriptFiles(
348+
PSModuleInfo moduleInfo,
349+
Workspace workspace)
338350
{
339351
if (moduleInfo != null)
340352
{
@@ -344,25 +356,20 @@ private ScriptFile[] GetBuiltinCommandScriptFiles(PSModuleInfo moduleInfo)
344356

345357
if (modPath.EndsWith(@".ps1") || modPath.EndsWith(@".psm1"))
346358
{
347-
using (StreamReader streamReader = new StreamReader(modPath, Encoding.UTF8))
348-
{
349-
newFile = new ScriptFile(modPath, streamReader);
350-
scriptFiles.Add(newFile);
351-
}
359+
newFile = workspace.GetFile(modPath);
360+
newFile.IsAnalysisEnabled = false;
361+
scriptFiles.Add(newFile);
352362
}
353363
if (moduleInfo.NestedModules.Count > 0)
354364
{
355-
string nestedModPath;
356365
foreach (PSModuleInfo nestedInfo in moduleInfo.NestedModules)
357366
{
358-
nestedModPath = nestedInfo.Path;
367+
string nestedModPath = nestedInfo.Path;
359368
if (nestedModPath.EndsWith(@".ps1") || nestedModPath.EndsWith(@".psm1"))
360369
{
361-
using (StreamReader streamReader = new StreamReader(nestedModPath, Encoding.UTF8))
362-
{
363-
newFile = new ScriptFile(nestedModPath, streamReader);
364-
scriptFiles.Add(newFile);
365-
}
370+
newFile = workspace.GetFile(nestedModPath);
371+
newFile.IsAnalysisEnabled = false;
372+
scriptFiles.Add(newFile);
366373
}
367374
}
368375
}
@@ -373,7 +380,10 @@ private ScriptFile[] GetBuiltinCommandScriptFiles(PSModuleInfo moduleInfo)
373380
return new List<ScriptFile>().ToArray();
374381
}
375382

376-
private SymbolReference FindDeclarationForBuiltinCommand(CommandInfo cmdInfo, SymbolReference foundSymbol)
383+
private SymbolReference FindDeclarationForBuiltinCommand(
384+
CommandInfo cmdInfo,
385+
SymbolReference foundSymbol,
386+
Workspace workspace)
377387
{
378388
SymbolReference foundDefinition = null;
379389
if (cmdInfo != null)
@@ -382,7 +392,9 @@ private SymbolReference FindDeclarationForBuiltinCommand(CommandInfo cmdInfo, Sy
382392
ScriptFile[] nestedModuleFiles;
383393

384394
nestedModuleFiles =
385-
GetBuiltinCommandScriptFiles(GetCommandInfo(foundSymbol.SymbolName).Module);
395+
GetBuiltinCommandScriptFiles(
396+
GetCommandInfo(foundSymbol.SymbolName).Module,
397+
workspace);
386398

387399
while (foundDefinition == null && index < nestedModuleFiles.Length)
388400
{

src/PowerShellEditorServices/Language/SymbolReference.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ public class SymbolReference
5959
/// Gets the contents of the line the given symbol is on
6060
/// </summary>
6161
public string SourceLine { get; internal set; }
62-
#endregion
6362

6463
/// <summary>
65-
/// Gets the Filepath of the symbol
64+
/// Gets the path of the file in which the symbol was found.
6665
/// </summary>
6766
public string FilePath { get; internal set; }
6867

68+
#endregion
69+
6970
/// <summary>
7071
/// Constructs and instance of a SymbolReference
7172
/// </summary>

src/PowerShellEditorServices/Session/ScriptFile.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ public class ScriptFile
3232
/// </summary>
3333
public string FilePath { get; private set; }
3434

35+
/// <summary>
36+
/// Gets or sets a boolean that determines whether
37+
/// semantic analysis should be enabled for this file.
38+
/// For internal use only.
39+
/// </summary>
40+
internal bool IsAnalysisEnabled { get; set; }
41+
3542
/// <summary>
3643
/// Gets a string containing the full contents of the file.
3744
/// </summary>
@@ -101,6 +108,8 @@ public string[] ReferencedFiles
101108
public ScriptFile(string filePath, TextReader textReader)
102109
{
103110
this.FilePath = filePath;
111+
this.IsAnalysisEnabled = true;
112+
104113
this.ReadFile(textReader);
105114
}
106115

0 commit comments

Comments
 (0)