Skip to content

Commit de5a30d

Browse files
authored
Merge pull request #46 from m-tmatma/feature/issue44-escape-brace
escape brace
2 parents 5430758 + 870bbb1 commit de5a30d

File tree

11 files changed

+676
-104
lines changed

11 files changed

+676
-104
lines changed

CreateGUIDVSPlugin/CopyGuidCommand.cs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,28 @@ private void MenuItemCallback(object sender, EventArgs e)
168168
var configuration = this.package.GetConfiguration();
169169
var formatString = configuration.FormatString;
170170

171-
var copyString = Template.Process(formatString);
171+
try
172+
{
173+
var copyString = Template.Process(formatString);
172174
#if DEBUG
173-
this.ClearOutout();
174-
this.ActivateOutout();
175-
this.OutputString(copyString);
175+
this.ClearOutout();
176+
this.ActivateOutout();
177+
this.OutputString(copyString);
176178
#endif
177-
Clipboard.SetDataObject(copyString);
179+
Clipboard.SetDataObject(copyString);
180+
}
181+
catch (ProcessTemplate.OrphanedLeftBraceException ex)
182+
{
183+
var message = "found orphaned '{' at " + ex.Index.ToString() + " in template";
184+
var caption = "Error";
185+
MessageBox.Show(message, caption, MessageBoxButtons.OK);
186+
}
187+
catch (ProcessTemplate.OrphanedRightBraceException ex)
188+
{
189+
var message = "found orphaned '}' at " + ex.Index.ToString() + " in template";
190+
var caption = "Error";
191+
MessageBox.Show(message, caption, MessageBoxButtons.OK);
192+
}
178193
}
179194
}
180195
}

CreateGUIDVSPlugin/InsertGuidCommand.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,31 @@ private void MenuItemCallback(object sender, EventArgs e)
168168
var configuration = this.package.GetConfiguration();
169169
var formatString = configuration.FormatString;
170170

171-
var copyString = Template.Process(formatString);
172-
var textView = this.package.GetTextView();
173-
if (textView != null)
171+
try
174172
{
175-
if (textView.HasAggregateFocus)
173+
var copyString = Template.Process(formatString);
174+
var textView = this.package.GetTextView();
175+
if (textView != null)
176176
{
177-
// Insert GUID
178-
textView.TextBuffer.Insert(textView.Caret.Position.BufferPosition, copyString);
177+
if (textView.HasAggregateFocus)
178+
{
179+
// Insert GUID
180+
textView.TextBuffer.Insert(textView.Caret.Position.BufferPosition, copyString);
181+
}
179182
}
180183
}
184+
catch (ProcessTemplate.OrphanedLeftBraceException ex)
185+
{
186+
var message = "found orphaned '{' at " + ex.Index.ToString() + " in template";
187+
var caption = "Error";
188+
MessageBox.Show(message, caption, MessageBoxButtons.OK);
189+
}
190+
catch (ProcessTemplate.OrphanedRightBraceException ex)
191+
{
192+
var message = "found orphaned '}' at " + ex.Index.ToString() + " in template";
193+
var caption = "Error";
194+
MessageBox.Show(message, caption, MessageBoxButtons.OK);
195+
}
181196
}
182197
}
183198
}

CreateGUIDVSPlugin/OptionsControl.Designer.cs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CreateGUIDVSPlugin/OptionsControl.cs

Lines changed: 91 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ namespace CreateGUIDVSPlugin
1414
public partial class OptionsControl : UserControl
1515
{
1616
private OptionsPageSetting settingOptionsPage;
17-
17+
private int insertPos;
18+
1819
public OptionsControl()
1920
{
2021
InitializeComponent();
@@ -24,6 +25,9 @@ public OptionsControl()
2425
this.comboBoxVariable.Items.Add(variableName.Description);
2526
}
2627
this.comboBoxVariable.SelectedIndex = 0;
28+
29+
this.insertPos = 0;
30+
this.textBoxTemplate.LostFocus += new EventHandler(textBoxTemplate_LostFocus);
2731
}
2832

2933
/// <summary>
@@ -57,28 +61,108 @@ public void SaveSetting(Configuration configuration)
5761
/// <param name="configuration"></param>
5862
public void LoadSetting(Configuration configuration)
5963
{
60-
this.textBoxTemplate.Text = configuration.FormatString;
64+
SetText(configuration.FormatString);
6165
}
6266

67+
/// <summary>
68+
/// handler clicking 'Insert' button
69+
/// </summary>
70+
/// <param name="sender"></param>
71+
/// <param name="e"></param>
6372
private void buttonInsert_Click(object sender, EventArgs e)
6473
{
6574
var guidIndex = decimal.ToInt32(this.numericUpDownGUID.Value);
6675
var keyname = Template.Variables[this.comboBoxVariable.SelectedIndex];
6776
var variable = (guidIndex > 0 )? keyname.GetVariable(guidIndex) : keyname.GetVariable();
68-
var textBox = this.textBoxTemplate;
69-
textBox.Text = textBox.Text.Insert(textBox.SelectionStart, variable);
77+
InsertText(variable);
7078
}
7179

80+
/// <summary>
81+
/// handler clicking 'EOL' button
82+
/// </summary>
83+
/// <param name="sender"></param>
84+
/// <param name="e"></param>
7285
private void buttonInsertLineEnding_Click(object sender, EventArgs e)
7386
{
74-
var textBox = this.textBoxTemplate;
75-
textBox.Text = textBox.Text.Insert(textBox.SelectionStart, Environment.NewLine);
87+
InsertText(Environment.NewLine);
88+
}
89+
90+
/// <summary>
91+
/// handler clicking '{' button
92+
/// </summary>
93+
/// <param name="sender"></param>
94+
/// <param name="e"></param>
95+
private void buttonLeftBrace_Click(object sender, EventArgs e)
96+
{
97+
InsertText("{{");
7698
}
7799

100+
/// <summary>
101+
/// handler clicking '}' button
102+
/// </summary>
103+
/// <param name="sender"></param>
104+
/// <param name="e"></param>
105+
private void buttonRightBrace_Click(object sender, EventArgs e)
106+
{
107+
InsertText("}}");
108+
}
109+
110+
/// <summary>
111+
/// hander clicking 'Default' button
112+
/// </summary>
113+
/// <param name="sender"></param>
114+
/// <param name="e"></param>
78115
private void buttonSetDefault_Click(object sender, EventArgs e)
116+
{
117+
SetText(Template.DefaultFormatString);
118+
}
119+
120+
/// <summary>
121+
/// handler that the textbox loses focus
122+
/// </summary>
123+
/// <param name="sender"></param>
124+
/// <param name="e"></param>
125+
private void textBoxTemplate_LostFocus(object sender, EventArgs e)
126+
{
127+
TextBox textBox = (TextBox)sender;
128+
this.insertPos = textBox.SelectionStart;
129+
}
130+
131+
/// <summary>
132+
/// insert a text to the textbox
133+
/// </summary>
134+
/// <param name="text"></param>
135+
private void InsertText(string text)
136+
{
137+
var textBox = this.textBoxTemplate;
138+
textBox.Text = textBox.Text.Insert(this.insertPos, text);
139+
this.insertPos += text.Length;
140+
141+
ScrollToCarect();
142+
}
143+
144+
/// <summary>
145+
/// overwrite textbox content
146+
/// </summary>
147+
/// <param name="text"></param>
148+
private void SetText(string text)
149+
{
150+
var textBox = this.textBoxTemplate;
151+
textBox.Text = text;
152+
this.insertPos = text.Length;
153+
154+
ScrollToCarect();
155+
}
156+
157+
/// <summary>
158+
/// function to scroll the textbox to the caret position
159+
/// </summary>
160+
private void ScrollToCarect()
79161
{
80162
var textBox = this.textBoxTemplate;
81-
textBox.Text = Template.DefaultFormatString;
163+
textBox.SelectionStart = this.insertPos;
164+
textBox.Focus();
165+
textBox.ScrollToCaret();
82166
}
83167
}
84168
}

0 commit comments

Comments
 (0)