Skip to content

Commit 8cfd4a7

Browse files
committed
Store watch expressions between FD sessions.
Introduced a WatchManager class so plugin developers can work with watched expressions in a better way. Surely to get some changes when the hxcpp work gets finished.
1 parent baf5729 commit 8cfd4a7

File tree

5 files changed

+260
-25
lines changed

5 files changed

+260
-25
lines changed

External/Plugins/FlashDebugger/Controls/WatchUI.cs

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Windows.Forms;
43
using FlashDebugger.Controls.DataTree;
4+
using FlashDebugger.Debugger;
55
using PluginCore;
66
using PluginCore.Localization;
77
using flash.tools.debugger;
@@ -12,20 +12,26 @@ namespace FlashDebugger.Controls
1212
public class WatchUI : DockPanelControl
1313
{
1414
private DataTreeControl treeControl;
15-
private List<string> watches;
15+
private WatchManager watchManager;
1616

17-
public WatchUI()
17+
public WatchUI(WatchManager watchManager)
1818
{
1919
this.AutoKeyHandling = true;
20-
watches = new List<string>();
21-
treeControl = new DataTreeControl(true);
20+
this.treeControl = new DataTreeControl(true);
2221
this.treeControl.Tree.BorderStyle = BorderStyle.None;
2322
this.treeControl.Resize += new EventHandler(this.TreeControlResize);
2423
this.treeControl.Tree.Font = PluginBase.Settings.DefaultFont;
2524
this.treeControl.Dock = DockStyle.Fill;
2625
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
2726
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
2827
this.Controls.Add(this.treeControl);
28+
29+
this.watchManager = watchManager;
30+
this.watchManager.ExpressionAdded += WatchManager_ExpressionAdded;
31+
this.watchManager.ExpressionRemoved += WatchManager_ExpressionRemoved;
32+
this.watchManager.ExpressionReplaced += WatchManager_ExpressionReplaced;
33+
this.watchManager.ExpressionsCleared += WatchManager_ExpressionsCleared;
34+
this.watchManager.ExpressionsLoaded += WatchManager_ExpressionsLoaded;
2935
}
3036

3137
private void TreeControlResize(Object sender, EventArgs e)
@@ -35,50 +41,63 @@ private void TreeControlResize(Object sender, EventArgs e)
3541
this.treeControl.Tree.Columns[1].Width = w - 8;
3642
}
3743

38-
public bool AddElement(string item)
44+
private void WatchManager_ExpressionAdded(Object sender, WatchExpressionArgs e)
45+
{
46+
treeControl.Nodes.Insert(e.Position, GetExpressionNode(e.Expression));
47+
UpdateElements();
48+
}
49+
50+
private void WatchManager_ExpressionRemoved(Object sender, WatchExpressionArgs e)
51+
{
52+
UpdateElements();
53+
}
54+
55+
private void WatchManager_ExpressionReplaced(Object sender, WatchExpressionReplaceArgs e)
56+
{
57+
treeControl.Nodes[e.Position] = GetExpressionNode(e.NewExpression);
58+
}
59+
60+
private void WatchManager_ExpressionsCleared(Object sender, EventArgs e)
61+
{
62+
treeControl.Nodes.Clear();
63+
}
64+
65+
private void WatchManager_ExpressionsLoaded(Object sender, EventArgs e)
3966
{
40-
if (watches.Contains(item)) return false;
41-
watches.Add(item);
42-
treeControl.Nodes.Insert(watches.Count - 1, GetExpressionNode(item));
4367
UpdateElements();
44-
return true;
68+
}
69+
70+
public bool AddElement(string item)
71+
{
72+
return watchManager.Add(item);
4573
}
4674

4775
public void RemoveElement(string item)
4876
{
49-
if (watches.Remove(item)) UpdateElements();
77+
watchManager.Remove(item);
5078
}
5179

5280
public void RemoveElement(int itemN)
5381
{
54-
if (itemN < watches.Count) watches.RemoveAt(itemN);
55-
treeControl.Nodes.RemoveAt(itemN);
82+
watchManager.RemoveAt(itemN);
5683
}
5784

5885
public bool ReplaceElement(string oldItem, string newItem)
5986
{
60-
if (watches.Contains(newItem)) return false;
61-
int itemN = watches.IndexOf(oldItem);
62-
if (itemN == -1) AddElement(newItem);
63-
else
64-
{
65-
watches[itemN] = newItem;
66-
treeControl.Nodes[itemN] = GetExpressionNode(newItem);
67-
}
68-
return true;
87+
return watchManager.Replace(oldItem, newItem);
6988
}
7089

7190
public void Clear()
7291
{
73-
watches.Clear();
74-
treeControl.Nodes.Clear();
92+
watchManager.ClearAll();
7593
}
7694

7795
public void UpdateElements()
7896
{
7997
treeControl.Tree.BeginUpdate();
8098
treeControl.SaveState();
8199
treeControl.Nodes.Clear();
100+
var watches = watchManager.Watches;
82101
foreach (string item in watches)
83102
{
84103
treeControl.AddNode(GetExpressionNode(item));
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using PluginCore;
5+
using PluginCore.Helpers;
6+
using PluginCore.Utilities;
7+
8+
namespace FlashDebugger.Debugger
9+
{
10+
public class WatchManager
11+
{
12+
private IProject m_Project;
13+
private string m_SaveFileFullPath;
14+
private List<string> m_WatchList = new List<string>();
15+
16+
public event EventHandler<WatchExpressionArgs> ExpressionAdded;
17+
public event EventHandler<WatchExpressionArgs> ExpressionRemoved;
18+
public event EventHandler<WatchExpressionReplaceArgs> ExpressionReplaced;
19+
public event EventHandler ExpressionsCleared;
20+
public event EventHandler ExpressionsLoaded;
21+
22+
public IList<string> Watches
23+
{
24+
get { return m_WatchList.AsReadOnly(); }
25+
}
26+
27+
public IProject Project
28+
{
29+
get { return m_Project; }
30+
set
31+
{
32+
if (value != null)
33+
{
34+
m_Project = value;
35+
this.ClearAll();
36+
m_SaveFileFullPath = GetWatchFile(m_Project.ProjectPath);
37+
}
38+
}
39+
}
40+
41+
private string GetWatchFile(string path)
42+
{
43+
String dataDir = Path.Combine(PathHelper.DataDir, "FlashDebugger");
44+
String cacheDir = Path.Combine(dataDir, "Watch");
45+
if (!Directory.Exists(cacheDir)) Directory.CreateDirectory(cacheDir);
46+
String hashFileName = HashCalculator.CalculateSHA1(path);
47+
return Path.Combine(cacheDir, hashFileName + ".xml");
48+
}
49+
50+
public void ClearAll()
51+
{
52+
m_WatchList.Clear();
53+
OnExpressionsCleared(EventArgs.Empty);
54+
}
55+
56+
public bool Add(string expr)
57+
{
58+
if (m_WatchList.Contains(expr)) return false;
59+
m_WatchList.Add(expr);
60+
OnExpressionAdded(expr, m_WatchList.Count - 1);
61+
return true;
62+
}
63+
64+
public void InsertAt(int index, string expr)
65+
{
66+
m_WatchList.Insert(index, expr);
67+
OnExpressionAdded(expr, index);
68+
}
69+
70+
public bool Remove(string expr)
71+
{
72+
int index = m_WatchList.IndexOf(expr);
73+
if (index > -1)
74+
{
75+
m_WatchList.RemoveAt(index);
76+
OnExpressionRemoved(expr, index);
77+
return true;
78+
}
79+
return false;
80+
}
81+
82+
public bool RemoveAt(int index)
83+
{
84+
if (index >= m_WatchList.Count || index < 0)
85+
return false;
86+
87+
string expr = m_WatchList[index];
88+
m_WatchList.RemoveAt(index);
89+
OnExpressionRemoved(expr, index);
90+
return true;
91+
}
92+
93+
public bool Replace(string oldItem, string newItem)
94+
{
95+
if (m_WatchList.Contains(newItem)) return false;
96+
int itemN = m_WatchList.IndexOf(oldItem);
97+
if (itemN == -1) Add(newItem);
98+
else
99+
{
100+
m_WatchList[itemN] = newItem;
101+
OnExpressionReplaced(oldItem, newItem, itemN);
102+
}
103+
return true;
104+
}
105+
106+
public void Save()
107+
{
108+
Save(m_SaveFileFullPath);
109+
}
110+
111+
public void Save(string filePath)
112+
{
113+
if (m_Project != null)
114+
{
115+
Util.SerializeXML<List<string>>.SaveFile(filePath, m_WatchList);
116+
}
117+
}
118+
119+
public void Load()
120+
{
121+
Load(m_SaveFileFullPath);
122+
}
123+
124+
public void Load(string filePath)
125+
{
126+
if (File.Exists(filePath))
127+
{
128+
m_WatchList = Util.SerializeXML<List<string>>.LoadFile(filePath);
129+
}
130+
else
131+
{
132+
m_WatchList = new List<string>();
133+
}
134+
OnExpressionsLoaded(EventArgs.Empty);
135+
}
136+
137+
protected void OnExpressionAdded(string expr, int index)
138+
{
139+
if (ExpressionAdded != null)
140+
{
141+
ExpressionAdded(this, new WatchExpressionArgs(expr, index));
142+
}
143+
}
144+
145+
protected void OnExpressionRemoved(string expr, int index)
146+
{
147+
if (ExpressionRemoved != null)
148+
{
149+
ExpressionRemoved(this, new WatchExpressionArgs(expr, index));
150+
}
151+
}
152+
153+
protected void OnExpressionReplaced(string oldExpr, string newExpr, int index)
154+
{
155+
if (ExpressionReplaced != null)
156+
{
157+
ExpressionReplaced(this, new WatchExpressionReplaceArgs(oldExpr, newExpr, index));
158+
}
159+
}
160+
161+
protected void OnExpressionsCleared(EventArgs e)
162+
{
163+
if (ExpressionsCleared != null)
164+
{
165+
ExpressionsCleared(this, e);
166+
}
167+
}
168+
169+
protected void OnExpressionsLoaded(EventArgs e)
170+
{
171+
if (ExpressionsLoaded != null)
172+
{
173+
ExpressionsLoaded(this, e);
174+
}
175+
}
176+
}
177+
178+
public class WatchExpressionArgs : EventArgs
179+
{
180+
public string Expression { get; internal set; }
181+
182+
public int Position { get; internal set; }
183+
184+
public WatchExpressionArgs(string expression, int position)
185+
{
186+
this.Expression = expression;
187+
this.Position = position;
188+
}
189+
}
190+
191+
public class WatchExpressionReplaceArgs : EventArgs
192+
{
193+
public string NewExpression { get; internal set; }
194+
195+
public string OldExpression { get; internal set; }
196+
197+
public int Position { get; internal set; }
198+
199+
public WatchExpressionReplaceArgs(string oldExpression, string newExpression, int position)
200+
{
201+
this.OldExpression = oldExpression;
202+
this.NewExpression = newExpression;
203+
this.Position = position;
204+
}
205+
}
206+
}

External/Plugins/FlashDebugger/FlashDebugger.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
<Compile Include="Debugger\ExpressionContext.cs" />
148148
<Compile Include="Debugger\FlashInterface.cs" />
149149
<Compile Include="Debugger\VariableFacade.cs" />
150+
<Compile Include="Debugger\WatchManager.cs" />
150151
<Compile Include="Helpers\DataTreeExporterFactory.cs" />
151152
<Compile Include="Helpers\DefaultDataTreeExporter.cs" />
152153
<Compile Include="Helpers\IDataTreeExporter.cs" />

External/Plugins/FlashDebugger/Helpers/PanelsHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public PanelsHelper(PluginMain pluginMain, Image pluginImage)
4545
stackframePanel = PluginBase.MainForm.CreateDockablePanel(stackframeUI, stackframeGuid, pluginImage, DockState.DockLeft);
4646
stackframePanel.Hide();
4747

48-
watchUI = new WatchUI();
48+
watchUI = new WatchUI(PluginMain.watchManager);
4949
watchUI.Text = TextHelper.GetString("Title.Watch");
5050
watchPanel = PluginBase.MainForm.CreateDockablePanel(watchUI, watchGuid, pluginImage, DockState.DockLeft);
5151
watchPanel.Hide();

0 commit comments

Comments
 (0)