@@ -209,14 +209,96 @@ static public IEnumerable<SymbolReference> FindSymbolsInDocument(Ast scriptAst,
209209// }
210210// else
211211 {
212- FindSymbolsVisitor findSymbolsVisitor = new FindSymbolsVisitor ( ) ;
213- scriptAst . Visit ( findSymbolsVisitor ) ;
214- symbolReferences = findSymbolsVisitor . SymbolReferences ;
212+ // we don't have reliable access to the filename here
213+ // so we employ the following heuristic to check if the
214+ // contents are part of a psd1 file.
215+ if ( IsPowerShellDataFile ( scriptAst ) )
216+ {
217+ var findHashtableSymbolsVisitor = new FindHashtabeSymbolsVisitor ( ) ;
218+ scriptAst . Visit ( findHashtableSymbolsVisitor ) ;
219+ symbolReferences = findHashtableSymbolsVisitor . SymbolReferences ;
220+ }
221+ else
222+ {
223+ FindSymbolsVisitor findSymbolsVisitor = new FindSymbolsVisitor ( ) ;
224+ scriptAst . Visit ( findSymbolsVisitor ) ;
225+ symbolReferences = findSymbolsVisitor . SymbolReferences ;
226+ }
215227 }
216228
217229 return symbolReferences ;
218230 }
219231
232+ static private bool IsPowerShellDataFile ( Ast ast )
233+ {
234+ //var node = new { Item = ast, Children = new List<dynamic>() };
235+ //GenerateTree(node);
236+ //return node.Children.Count == 1 && node.Children[0].Item is NamedBlockAst
237+ // && node.Children[0].Children.Count == 1 && node.Children[0].Children[0].Item is PipelineAst
238+ // && node.Children[0].Children[0].Children.Count == 1 && ;
239+
240+ return IsPowerShellDataFile (
241+ new { Item = ast , Children = new List < dynamic > ( ) } ,
242+ new Type [ ] {
243+ typeof ( ScriptBlockAst ) ,
244+ typeof ( NamedBlockAst ) ,
245+ typeof ( PipelineAst ) ,
246+ typeof ( CommandExpressionAst ) ,
247+ typeof ( HashtableAst ) } ,
248+ 0 ) ;
249+ }
250+
251+ static private bool IsPowerShellDataFile ( dynamic node , Type [ ] levelAstMap , int level )
252+ {
253+ var levelAstTypeMatch = node . Item . GetType ( ) . Equals ( levelAstMap [ level ] ) ;
254+ if ( ! levelAstTypeMatch )
255+ {
256+ return false ;
257+ }
258+
259+ if ( level == levelAstMap . Length - 1 )
260+ {
261+ return levelAstTypeMatch ;
262+ }
263+
264+ var astsFound = ( node . Item as Ast ) . FindAll ( a => a is Ast , false ) ;
265+ if ( astsFound != null )
266+ {
267+ foreach ( var astFound in astsFound )
268+ {
269+ if ( ! astFound . Equals ( node . Item ) && node . Item . Equals ( astFound . Parent ) )
270+ {
271+ if ( IsPowerShellDataFile ( new { Item = astFound , Children = new List < dynamic > ( ) } , levelAstMap , level + 1 ) )
272+ {
273+ return true ;
274+ }
275+ }
276+ }
277+ }
278+
279+ return false ;
280+ }
281+
282+ static private void GenerateTree ( dynamic node )
283+ {
284+ var astsFound = ( node . Item as Ast ) . FindAll ( a => a is Ast , false ) ;
285+ if ( astsFound != null )
286+ {
287+ foreach ( var astFound in astsFound )
288+ {
289+ if ( ! astFound . Equals ( node . Item ) && node . Item . Equals ( astFound . Parent ) )
290+ {
291+ node . Children . Add ( new { Item = astFound , Children = new List < dynamic > ( ) } ) ;
292+ }
293+ }
294+ }
295+
296+ foreach ( var child in node . Children )
297+ {
298+ GenerateTree ( child ) ;
299+ }
300+ }
301+
220302 /// <summary>
221303 /// Finds all files dot sourced in a script
222304 /// </summary>
0 commit comments