Support for getting a bound scope at a specific character position

This commit is contained in:
2018-11-27 19:04:58 +01:00
parent 8ece53db5b
commit 14e30d0855
9 changed files with 51 additions and 18 deletions

View File

@@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Linq;
using Upsilon.Binder;
@@ -7,17 +8,35 @@ namespace Upsilon.Utilities
{
public static BoundScope GetScopeAt(this BoundScript script, int characterPosition)
{
var scope = script.Scope;
foreach (var node in script.GetNodeAtPosition(characterPosition))
{
if (node is IBoundNodeScopeOwner scopeOwner && scopeOwner.Scope != null)
{
return scopeOwner.Scope;
}
}
return scope;
return script.Scope;
}
public static BoundNode GetBottomNodeAtPosition(this BoundNode node, int characterPosition)
{
return node.GetNodeAtPosition(characterPosition).First();
}
public static Dictionary<string, VariableSymbol> GetBoundScopeVisibleVariables(this BoundScope scope)
{
IEnumerable<KeyValuePair<string, VariableSymbol>> dictionary = scope.Variables;
if (scope.ParentScope != null)
{
dictionary= dictionary.Union(scope.ParentScope.GetBoundScopeVisibleVariables());
}
if (scope.ReadOnlyScope != null)
{
dictionary = dictionary.Union(scope.ReadOnlyScope.GetBoundScopeVisibleVariables());
}
return dictionary.ToDictionary(k => k.Key, v => v.Value);
}
}
}