Skip to content

Commit

Permalink
Reading control from MsApp (#508)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrew Petrochuk (from Dev Box) <[email protected]>
  • Loading branch information
petrochuk and anpetroc authored Jan 21, 2024
1 parent 3ff0572 commit c397208
Show file tree
Hide file tree
Showing 20 changed files with 275 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.PowerPlatform.Formulas.Tools.EditorState;
/// Per control, this is the studio state content that doesn't wind up in the IR
/// Similar to <seealso cref="ControlInfoJson.Item"/> without the info encoded by .pa
/// </summary>
public class ControlEditorState
public class ControlState
{
public string Name { get; set; }

Expand Down Expand Up @@ -54,7 +54,7 @@ public class ControlEditorState
// Used in GroupControlTransform.cs, and not written to .editorstate.json
internal List<string> GroupedControlsKey;

public ControlEditorState Clone()
public ControlState Clone()
{
var newState = Utilities.JsonClone(this);
newState.TopParentName = TopParentName;
Expand Down
4 changes: 2 additions & 2 deletions src/PAModel/EditorState/ControlTreeState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal class ControlTreeState
public string TopParentName { get; set; }

/// <summary>
/// Collection of <seealso cref="ControlEditorState"/> objects for
/// Collection of <seealso cref="ControlState"/> objects for
/// this editor state.
/// </summary>
public Dictionary<string, ControlEditorState> ControlStates { get; set; }
public Dictionary<string, ControlState> ControlStates { get; set; }
}
12 changes: 6 additions & 6 deletions src/PAModel/EditorState/EditorStateStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ namespace Microsoft.PowerPlatform.Formulas.Tools.EditorState;
internal class EditorStateStore
{
// Key is control name, case-sensitive
private readonly Dictionary<string, ControlEditorState> _controls;
private readonly Dictionary<string, ControlState> _controls;

public EditorStateStore()
{
_controls = new Dictionary<string, ControlEditorState>(StringComparer.Ordinal);
_controls = new Dictionary<string, ControlState>(StringComparer.Ordinal);
}

public EditorStateStore(EditorStateStore other)
Expand All @@ -25,7 +25,7 @@ public bool ContainsControl(string name)
return _controls.ContainsKey(name);
}

public bool TryAddControl(ControlEditorState control)
public bool TryAddControl(ControlState control)
{
if (_controls.ContainsKey(control.Name))
return false;
Expand All @@ -34,7 +34,7 @@ public bool TryAddControl(ControlEditorState control)
return true;
}

public bool TryGetControlState(string controlName, out ControlEditorState state)
public bool TryGetControlState(string controlName, out ControlState state)
{
return _controls.TryGetValue(controlName, out state);
}
Expand All @@ -44,10 +44,10 @@ public void Remove(string controlName)
_controls.Remove(controlName);
}

public IEnumerable<ControlEditorState> GetControlsWithTopParent(string topParent)
public IEnumerable<ControlState> GetControlsWithTopParent(string topParent)
{
return _controls.Values.Where(ctrl => ctrl.TopParentName == topParent);
}

public IEnumerable<ControlEditorState> Contents => _controls.Values;
public IEnumerable<ControlState> Contents => _controls.Values;
}
2 changes: 1 addition & 1 deletion src/PAModel/Entropy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public void GetProperties(DocumentPropertiesJson documentProperties)
}
}

public void AddGroupControl(ControlEditorState groupControl)
public void AddGroupControl(ControlState groupControl)
{
var name = groupControl.Name;
var groupOrder = new Dictionary<string, int>(StringComparer.Ordinal);
Expand Down
24 changes: 24 additions & 0 deletions src/PAModel/Exceptions/SerializationException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.PowerPlatform.Formulas.Tools;

internal class SerializationException : Exception
{
public string FileName { get; init; }

public SerializationException(string message)
: base(message)
{
}

public SerializationException(string message, Exception innerException)
: base(message, innerException)
{
}

public SerializationException(string message, string fileName, Exception innerException)
{
FileName = fileName;
}
}
10 changes: 5 additions & 5 deletions src/PAModel/IR/IRStateHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private static void SplitIRAndState(Item control, string topParentName, int inde
{
entropy.ControlUniqueGuids.Add(control.Name, Guid.Parse(control.ControlUniqueId));
}
var controlState = new ControlEditorState()
var controlState = new ControlState()
{
Name = control.Name,
TopParentName = topParentName,
Expand Down Expand Up @@ -545,7 +545,7 @@ private static void RepopulateTemplateCustomProperties(FunctionNode func, Combin
}
}

private static RuleEntry CombinePropertyIRAndState(PropertyNode node, ErrorContainer errors, ControlEditorState state = null)
private static RuleEntry CombinePropertyIRAndState(PropertyNode node, ErrorContainer errors, ControlState state = null)
{
var propName = node.Identifier;
var expression = node.Expression.Expression;
Expand All @@ -567,7 +567,7 @@ private static RuleEntry CombinePropertyIRAndState(PropertyNode node, ErrorConta
}
}

private static DynamicPropertyJson CombineDynamicPropertyIRAndState(PropertyNode node, ControlEditorState state = null)
private static DynamicPropertyJson CombineDynamicPropertyIRAndState(PropertyNode node, ControlState state = null)
{
var propName = node.Identifier;
var expression = node.Expression.Expression;
Expand All @@ -589,7 +589,7 @@ private static DynamicPropertyJson CombineDynamicPropertyIRAndState(PropertyNode
}
}

private static RuleEntry GetPropertyEntry(ControlEditorState state, ErrorContainer errors, string propName, string expression)
private static RuleEntry GetPropertyEntry(ControlState state, ErrorContainer errors, string propName, string expression)
{
var property = new RuleEntry
{
Expand Down Expand Up @@ -617,7 +617,7 @@ private static RuleEntry GetPropertyEntry(ControlEditorState state, ErrorContain
return property;
}

private static DynamicPropertyJson GetDynamicPropertyEntry(ControlEditorState state, string propName, string expression)
private static DynamicPropertyJson GetDynamicPropertyEntry(ControlState state, string propName, string expression)
{
var property = new DynamicPropertyJson
{
Expand Down
8 changes: 4 additions & 4 deletions src/PAModel/MergeTool/ControlDiffVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ private ControlDiffVisitor(EditorStateStore childStateStore, TemplateStore paren
_childTemplateStore = childTemplateStore;
}

private Dictionary<string, ControlEditorState> GetSubtreeStates(BlockNode node)
private Dictionary<string, ControlState> GetSubtreeStates(BlockNode node)
{
return GetSubtreeStatesImpl(node).ToDictionary(state => state.Name);
}

private IEnumerable<ControlEditorState> GetSubtreeStatesImpl(BlockNode node)
private IEnumerable<ControlState> GetSubtreeStatesImpl(BlockNode node)
{
var childstates = node.Children?.SelectMany(GetSubtreeStatesImpl) ?? Enumerable.Empty<ControlEditorState>();
var childstates = node.Children?.SelectMany(GetSubtreeStatesImpl) ?? Enumerable.Empty<ControlState>();

if (!_childStateStore.TryGetControlState(node.Name.Identifier, out var state))
return childstates;

return childstates.Concat(new List<ControlEditorState>() { state });
return childstates.Concat(new List<ControlState>() { state });
}

public override void Visit(BlockNode node, ControlDiffContext context)
Expand Down
8 changes: 4 additions & 4 deletions src/PAModel/MergeTool/Deltas/AddControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ internal class AddControl : IDelta
private readonly bool _isInComponent;
private readonly ControlPath _parentControlPath;
private readonly BlockNode _control;
private readonly Dictionary<string, ControlEditorState> _controlStates;
private readonly Dictionary<string, ControlState> _controlStates;

public string ControlName => _control.Name.Identifier;

public AddControl(ControlPath parentControlPath, BlockNode control, Dictionary<string, ControlEditorState> controlStates, bool isInComponent)
public AddControl(ControlPath parentControlPath, BlockNode control, Dictionary<string, ControlState> controlStates, bool isInComponent)
{
_isInComponent = isInComponent;
_parentControlPath = parentControlPath;
Expand Down Expand Up @@ -79,7 +79,7 @@ public void Apply(CanvasDocument document)
control.Children.Add(repairedControl);
}

private static BlockNode MakeControlTreeCollisionFree(BlockNode root, Dictionary<string, ControlEditorState> states, EditorStateStore stateStore)
private static BlockNode MakeControlTreeCollisionFree(BlockNode root, Dictionary<string, ControlState> states, EditorStateStore stateStore)
{
var name = root.Name.Identifier;
if (stateStore.ContainsControl(name))
Expand All @@ -100,7 +100,7 @@ private static BlockNode MakeControlTreeCollisionFree(BlockNode root, Dictionary
return root;
}

private static void RemoveStates(BlockNode root, Dictionary<string, ControlEditorState> states)
private static void RemoveStates(BlockNode root, Dictionary<string, ControlState> states)
{
var name = root.Name.Identifier;
states.Remove(name);
Expand Down
42 changes: 42 additions & 0 deletions src/PAModel/Model/Control.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics;
using YamlDotNet.Serialization;

namespace Microsoft.PowerPlatform.Formulas.Tools.Model;

[DebuggerDisplay("{Name}")]
public record Control
{
public Control()
{

}

public Control(ControlEditorState editorState)
{
EditorState = editorState;
Name = editorState.Name;

if (editorState.Children != null)
{
var childControls = new List<Control>();
foreach (var child in editorState.Children)
{
childControls.Add(new Control(child));
}
Controls = childControls;
editorState.Children = null;
}
}

[YamlIgnore]
public ControlEditorState EditorState { get; set; }

public string Name { get; init; }

public IList<Control> Controls { get; init; }

public IDictionary<string, object> Properties { get; init; }
}
13 changes: 13 additions & 0 deletions src/PAModel/Model/ControlEditorState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.PowerPlatform.Formulas.Tools.Model;

public record ControlEditorState
{
public string Name { get; init; }

public ControlEditorState[] Children { get; set; }

public IList<RuleEditorState> Rules { get; init; }
}
17 changes: 17 additions & 0 deletions src/PAModel/Model/RuleEditorState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System.Diagnostics;

namespace Microsoft.PowerPlatform.Formulas.Tools.Model;

[DebuggerDisplay("{Category} / {Property} / {InvariantScript}")]
public record RuleEditorState
{
public string Category { get; init; }
public string Property { get; init; }
public string NameMap { get; init; }
public string InvariantScript { get; init; }
public string RuleProviderType { get; init; }
public IList<TaggedRuleEditorState> TaggedRuleArray { get; init; }
}
9 changes: 9 additions & 0 deletions src/PAModel/Model/TaggedRuleEditorState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

namespace Microsoft.PowerPlatform.Formulas.Tools.Model;

public record TaggedRuleEditorState : RuleEditorState
{
public string Tag { get; init; }
}
Loading

0 comments on commit c397208

Please sign in to comment.