@@ -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