Skip to content

Commit 8f3855c

Browse files
author
Kapil Borle
committed
Update methods to check for psd1 file
1 parent d08212a commit 8f3855c

File tree

1 file changed

+24
-48
lines changed

1 file changed

+24
-48
lines changed

src/PowerShellEditorServices/Language/AstOperations.cs

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -209,12 +209,7 @@ static public IEnumerable<SymbolReference> FindSymbolsInDocument(Ast scriptAst,
209209
// }
210210
// else
211211
{
212-
// sometimes we don't have reliable access to the filename
213-
// so we employ heuristics to check if the contents are
214-
// part of a psd1 file.
215-
if ((scriptAst.Extent.File != null
216-
&& scriptAst.Extent.File.EndsWith(".psd1", StringComparison.OrdinalIgnoreCase))
217-
|| IsPowerShellDataFile(scriptAst))
212+
if (IsPowerShellDataFileAst(scriptAst))
218213
{
219214
var findHashtableSymbolsVisitor = new FindHashtabeSymbolsVisitor();
220215
scriptAst.Visit(findHashtableSymbolsVisitor);
@@ -231,26 +226,25 @@ static public IEnumerable<SymbolReference> FindSymbolsInDocument(Ast scriptAst,
231226
return symbolReferences;
232227
}
233228

234-
static private bool IsPowerShellDataFile(Ast ast)
229+
static private bool IsPowerShellDataFileAst(Ast ast)
235230
{
236-
//var node = new { Item = ast, Children = new List<dynamic>() };
237-
//GenerateTree(node);
238-
//return node.Children.Count == 1 && node.Children[0].Item is NamedBlockAst
239-
// && node.Children[0].Children.Count == 1 && node.Children[0].Children[0].Item is PipelineAst
240-
// && node.Children[0].Children[0].Children.Count == 1 && ;
241-
242-
return IsPowerShellDataFile(
243-
new { Item = ast, Children = new List<dynamic>() },
244-
new Type[] {
245-
typeof(ScriptBlockAst),
246-
typeof(NamedBlockAst),
247-
typeof(PipelineAst),
248-
typeof(CommandExpressionAst),
249-
typeof(HashtableAst) },
250-
0);
231+
// sometimes we don't have reliable access to the filename
232+
// so we employ heuristics to check if the contents are
233+
// part of a psd1 file.
234+
return (ast.Extent.File != null
235+
&& ast.Extent.File.EndsWith(".psd1", StringComparison.OrdinalIgnoreCase))
236+
|| IsPowerShellDataFileAstNode(
237+
new { Item = ast, Children = new List<dynamic>() },
238+
new Type[] {
239+
typeof(ScriptBlockAst),
240+
typeof(NamedBlockAst),
241+
typeof(PipelineAst),
242+
typeof(CommandExpressionAst),
243+
typeof(HashtableAst) },
244+
0);
251245
}
252246

253-
static private bool IsPowerShellDataFile(dynamic node, Type[] levelAstMap, int level)
247+
static private bool IsPowerShellDataFileAstNode(dynamic node, Type[] levelAstMap, int level)
254248
{
255249
var levelAstTypeMatch = node.Item.GetType().Equals(levelAstMap[level]);
256250
if (!levelAstTypeMatch)
@@ -268,39 +262,21 @@ static private bool IsPowerShellDataFile(dynamic node, Type[] levelAstMap, int l
268262
{
269263
foreach (var astFound in astsFound)
270264
{
271-
if (!astFound.Equals(node.Item) && node.Item.Equals(astFound.Parent))
265+
if (!astFound.Equals(node.Item)
266+
&& node.Item.Equals(astFound.Parent)
267+
&& IsPowerShellDataFileAstNode(
268+
new { Item = astFound, Children = new List<dynamic>() },
269+
levelAstMap,
270+
level + 1))
272271
{
273-
if (IsPowerShellDataFile(new { Item = astFound, Children = new List<dynamic>() }, levelAstMap, level + 1))
274-
{
275-
return true;
276-
}
272+
return true;
277273
}
278274
}
279275
}
280276

281277
return false;
282278
}
283279

284-
static private void GenerateTree(dynamic node)
285-
{
286-
var astsFound = (node.Item as Ast).FindAll(a => a is Ast, false);
287-
if (astsFound != null)
288-
{
289-
foreach (var astFound in astsFound)
290-
{
291-
if (!astFound.Equals(node.Item) && node.Item.Equals(astFound.Parent))
292-
{
293-
node.Children.Add(new { Item = astFound, Children = new List<dynamic>() });
294-
}
295-
}
296-
}
297-
298-
foreach (var child in node.Children)
299-
{
300-
GenerateTree(child);
301-
}
302-
}
303-
304280
/// <summary>
305281
/// Finds all files dot sourced in a script
306282
/// </summary>

0 commit comments

Comments
 (0)