@@ -26,12 +26,16 @@ public class EditorSession
26
26
#region Private Fields
27
27
28
28
private Runspace languageRunspace ;
29
- private Dictionary < string , ScriptFile > workspaceFiles = new Dictionary < string , ScriptFile > ( ) ;
30
29
31
30
#endregion
32
31
33
32
#region Properties
34
33
34
+ /// <summary>
35
+ /// Gets the Workspace instance for this session.
36
+ /// </summary>
37
+ public Workspace Workspace { get ; private set ; }
38
+
35
39
/// <summary>
36
40
/// Gets the LanguageService instance for this session.
37
41
/// </summary>
@@ -46,7 +50,7 @@ public class EditorSession
46
50
/// Gets the ConsoleService instance for this session.
47
51
/// </summary>
48
52
public ConsoleService ConsoleService { get ; private set ; }
49
-
53
+
50
54
#endregion
51
55
52
56
#region Public Methods
@@ -63,6 +67,9 @@ public void StartSession(IConsoleHost consoleHost)
63
67
{
64
68
InitialSessionState initialSessionState = InitialSessionState . CreateDefault2 ( ) ;
65
69
70
+ // Create a workspace to contain open files
71
+ this . Workspace = new Workspace ( ) ;
72
+
66
73
// Create a runspace to share between the language and analysis services
67
74
this . languageRunspace = RunspaceFactory . CreateRunspace ( initialSessionState ) ;
68
75
this . languageRunspace . ApartmentState = ApartmentState . STA ;
@@ -76,125 +83,6 @@ public void StartSession(IConsoleHost consoleHost)
76
83
this . ConsoleService = new ConsoleService ( consoleHost , initialSessionState ) ;
77
84
}
78
85
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
- }
198
86
#endregion
199
87
200
88
#region IDisposable Implementation
0 commit comments