Skip to content

Commit e12019f

Browse files
daviwilKayla Davis
authored andcommitted
Add Workspace class to manage open session files
This change introduces the Workspace class which is responsible for managing the set of open files in a session. This class is also responsible for resolving a script's references to other scripts and handling the necessary file operations so that those references scripts can be used for service operations.
1 parent 8cd2c2f commit e12019f

File tree

8 files changed

+209
-126
lines changed

8 files changed

+209
-126
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void ProcessMessage(
2727
GetDefinitionResult definition =
2828
editorSession.LanguageService.GetDefinitionOfSymbol(
2929
foundSymbol,
30-
editorSession.ExpandScriptReferences(scriptFile));
30+
editorSession.Workspace.ExpandScriptReferences(scriptFile));
3131

3232
if (definition != null)
3333
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public override void ProcessMessage(
3535
{
3636
ScriptFile scriptFile = null;
3737

38-
if (!editorSession.TryGetFile(filePath, out scriptFile))
38+
if (!editorSession.Workspace.TryGetFile(filePath, out scriptFile))
3939
{
4040
// Skip this file and log the file load error
4141
// TODO: Trace out the error message

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected ScriptFile GetScriptFile(EditorSession editorSession)
1515
{
1616
ScriptFile scriptFile = null;
1717

18-
if(!editorSession.TryGetFile(
18+
if(!editorSession.Workspace.TryGetFile(
1919
this.Arguments.File,
2020
out scriptFile))
2121
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void ProcessMessage(
2727
MessageWriter messageWriter)
2828
{
2929
// Open the file in the current session
30-
editorSession.OpenFile(this.Arguments.File);
30+
editorSession.Workspace.OpenFile(this.Arguments.File);
3131
}
3232
}
3333
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override void ProcessMessage(
2727
FindReferencesResult referencesResult =
2828
editorSession.LanguageService.FindReferencesOfSymbol(
2929
foundSymbol,
30-
editorSession.ExpandScriptReferences(scriptFile));
30+
editorSession.Workspace.ExpandScriptReferences(scriptFile));
3131

3232
ReferencesResponse referencesResponse =
3333
ReferencesResponse.Create(referencesResult, this.Arguments.File);

src/PowerShellEditorServices/PowerShellEditorServices.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
<Compile Include="Session\FileChange.cs" />
9090
<Compile Include="Session\ScriptFileMarker.cs" />
9191
<Compile Include="Session\ScriptRegion.cs" />
92+
<Compile Include="Session\Workspace.cs" />
9293
<Compile Include="Utility\Validate.cs" />
9394
</ItemGroup>
9495
<ItemGroup>

src/PowerShellEditorServices/Session/EditorSession.cs

Lines changed: 9 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ public class EditorSession
2626
#region Private Fields
2727

2828
private Runspace languageRunspace;
29-
private Dictionary<string, ScriptFile> workspaceFiles = new Dictionary<string, ScriptFile>();
3029

3130
#endregion
3231

3332
#region Properties
3433

34+
/// <summary>
35+
/// Gets the Workspace instance for this session.
36+
/// </summary>
37+
public Workspace Workspace { get; private set; }
38+
3539
/// <summary>
3640
/// Gets the LanguageService instance for this session.
3741
/// </summary>
@@ -46,7 +50,7 @@ public class EditorSession
4650
/// Gets the ConsoleService instance for this session.
4751
/// </summary>
4852
public ConsoleService ConsoleService { get; private set; }
49-
53+
5054
#endregion
5155

5256
#region Public Methods
@@ -63,6 +67,9 @@ public void StartSession(IConsoleHost consoleHost)
6367
{
6468
InitialSessionState initialSessionState = InitialSessionState.CreateDefault2();
6569

70+
// Create a workspace to contain open files
71+
this.Workspace = new Workspace();
72+
6673
// Create a runspace to share between the language and analysis services
6774
this.languageRunspace = RunspaceFactory.CreateRunspace(initialSessionState);
6875
this.languageRunspace.ApartmentState = ApartmentState.STA;
@@ -76,125 +83,6 @@ public void StartSession(IConsoleHost consoleHost)
7683
this.ConsoleService = new ConsoleService(consoleHost, initialSessionState);
7784
}
7885

79-
/// <summary>
80-
/// Opens a script file with the given file path.
81-
/// </summary>
82-
/// <param name="filePath">The file path at which the script resides.</param>
83-
/// <exception cref="FileNotFoundException">
84-
/// <paramref name="filePath"/> is not found.
85-
/// </exception>
86-
/// <exception cref="ArgumentException">
87-
/// <paramref name="filePath"/> has already been loaded in the session.
88-
/// </exception>
89-
/// <exception cref="ArgumentException">
90-
/// <paramref name="filePath"/> contains a null or empty string.
91-
/// </exception>
92-
public ScriptFile OpenFile(string filePath)
93-
{
94-
Validate.IsNotNullOrEmptyString("filePath", filePath);
95-
96-
// Make sure the file isn't already loaded into the session
97-
if (!this.workspaceFiles.ContainsKey(filePath))
98-
{
99-
// This method allows FileNotFoundException to bubble up
100-
// if the file isn't found.
101-
102-
using (StreamReader streamReader = new StreamReader(filePath, Encoding.UTF8))
103-
{
104-
ScriptFile newFile = new ScriptFile(filePath, streamReader);
105-
this.workspaceFiles.Add(filePath, newFile);
106-
return newFile;
107-
}
108-
}
109-
else
110-
{
111-
throw new ArgumentException(
112-
"The specified file has already been loaded: " + filePath,
113-
"filePath");
114-
}
115-
}
116-
117-
/// <summary>
118-
/// Closes a currently open script file with the given file path.
119-
/// </summary>
120-
/// <param name="scriptFile">The file path at which the script resides.</param>
121-
public void CloseFile(ScriptFile scriptFile)
122-
{
123-
Validate.IsNotNull("scriptFile", scriptFile);
124-
125-
this.workspaceFiles.Remove(scriptFile.FilePath);
126-
}
127-
128-
/// <summary>
129-
/// Attempts to get a currently open script file with the given file path.
130-
/// </summary>
131-
/// <param name="filePath">The file path at which the script resides.</param>
132-
/// <param name="scriptFile">The output variable in which the ScriptFile will be stored.</param>
133-
/// <returns>A ScriptFile instance</returns>
134-
public bool TryGetFile(string filePath, out ScriptFile scriptFile)
135-
{
136-
scriptFile = null;
137-
return this.workspaceFiles.TryGetValue(filePath, out scriptFile);
138-
}
139-
140-
/// <summary>
141-
/// Gets all open files in the session.
142-
/// </summary>
143-
/// <returns>A collection of all open ScriptFiles in the session.</returns>
144-
public IEnumerable<ScriptFile> GetOpenFiles()
145-
{
146-
return this.workspaceFiles.Values;
147-
}
148-
149-
/// <summary>
150-
/// Gets all file references by recursively searching
151-
/// through referenced files in a scriptfile
152-
/// </summary>
153-
/// <param name="scriptFile">Contains the details and contents of an open script file</param>
154-
/// <returns>A scriptfile array where the first file
155-
/// in the array is the "root file" of the search</returns>
156-
public ScriptFile[] ExpandScriptReferences(ScriptFile scriptFile)
157-
{
158-
Dictionary<string, ScriptFile> referencedScriptFiles = new Dictionary<string, ScriptFile>();
159-
List<ScriptFile> expandedReferences = new List<ScriptFile>();
160-
161-
RecursivelyFindReferences(scriptFile, referencedScriptFiles);
162-
expandedReferences.Add(scriptFile); // add original file first
163-
if (referencedScriptFiles.Count != 0)
164-
{
165-
expandedReferences.AddRange(referencedScriptFiles.Values);
166-
}
167-
return expandedReferences.ToArray();
168-
}
169-
170-
#endregion
171-
172-
#region Private Methods
173-
/// <summary>
174-
/// Recusrively searches through referencedFiles in scriptFiles
175-
/// and builds a Dictonary of the file references
176-
/// </summary>
177-
/// <param name="scriptFile">Details an contents of "root" script file</param>
178-
/// <param name="referencedScriptFiles">A Dictionary of referenced script files</param>
179-
private void RecursivelyFindReferences(
180-
ScriptFile scriptFile,
181-
Dictionary<string, ScriptFile> referencedScriptFiles)
182-
{
183-
ScriptFile newFile;
184-
foreach (string filename in scriptFile.ReferencedFiles)
185-
{
186-
string filePath = Path.GetFullPath(filename);
187-
if (referencedScriptFiles.ContainsKey(filePath))
188-
{
189-
if (TryGetFile(filePath, out newFile))
190-
{
191-
newFile = OpenFile(filePath);
192-
referencedScriptFiles.Add(filePath, newFile);
193-
}
194-
RecursivelyFindReferences(newFile, referencedScriptFiles);
195-
}
196-
}
197-
}
19886
#endregion
19987

20088
#region IDisposable Implementation

0 commit comments

Comments
 (0)