Skip to content
25 changes: 20 additions & 5 deletions CreateGUIDVSPlugin/CopyGuidCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,28 @@ private void MenuItemCallback(object sender, EventArgs e)
var configuration = this.package.GetConfiguration();
var formatString = configuration.FormatString;

var copyString = Template.Process(formatString);
try
{
var copyString = Template.Process(formatString);
#if DEBUG
this.ClearOutout();
this.ActivateOutout();
this.OutputString(copyString);
this.ClearOutout();
this.ActivateOutout();
this.OutputString(copyString);
#endif
Clipboard.SetDataObject(copyString);
Clipboard.SetDataObject(copyString);
}
catch (ProcessTemplate.OrphanedLeftBraceException ex)
{
var message = "found orphaned '{' at " + ex.Index.ToString() + " in template";
var caption = "Error";
MessageBox.Show(message, caption, MessageBoxButtons.OK);
}
catch (ProcessTemplate.OrphanedRightBraceException ex)
{
var message = "found orphaned '}' at " + ex.Index.ToString() + " in template";
var caption = "Error";
MessageBox.Show(message, caption, MessageBoxButtons.OK);
}
}
}
}
Expand Down
27 changes: 21 additions & 6 deletions CreateGUIDVSPlugin/InsertGuidCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,31 @@ private void MenuItemCallback(object sender, EventArgs e)
var configuration = this.package.GetConfiguration();
var formatString = configuration.FormatString;

var copyString = Template.Process(formatString);
var textView = this.package.GetTextView();
if (textView != null)
try
{
if (textView.HasAggregateFocus)
var copyString = Template.Process(formatString);
var textView = this.package.GetTextView();
if (textView != null)
{
// Insert GUID
textView.TextBuffer.Insert(textView.Caret.Position.BufferPosition, copyString);
if (textView.HasAggregateFocus)
{
// Insert GUID
textView.TextBuffer.Insert(textView.Caret.Position.BufferPosition, copyString);
}
}
}
catch (ProcessTemplate.OrphanedLeftBraceException ex)
{
var message = "found orphaned '{' at " + ex.Index.ToString() + " in template";
var caption = "Error";
MessageBox.Show(message, caption, MessageBoxButtons.OK);
}
catch (ProcessTemplate.OrphanedRightBraceException ex)
{
var message = "found orphaned '}' at " + ex.Index.ToString() + " in template";
var caption = "Error";
MessageBox.Show(message, caption, MessageBoxButtons.OK);
}
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions CreateGUIDVSPlugin/OptionsControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

98 changes: 91 additions & 7 deletions CreateGUIDVSPlugin/OptionsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace CreateGUIDVSPlugin
public partial class OptionsControl : UserControl
{
private OptionsPageSetting settingOptionsPage;

private int insertPos;

public OptionsControl()
{
InitializeComponent();
Expand All @@ -24,6 +25,9 @@ public OptionsControl()
this.comboBoxVariable.Items.Add(variableName.Description);
}
this.comboBoxVariable.SelectedIndex = 0;

this.insertPos = 0;
this.textBoxTemplate.LostFocus += new EventHandler(textBoxTemplate_LostFocus);
}

/// <summary>
Expand Down Expand Up @@ -57,28 +61,108 @@ public void SaveSetting(Configuration configuration)
/// <param name="configuration"></param>
public void LoadSetting(Configuration configuration)
{
this.textBoxTemplate.Text = configuration.FormatString;
SetText(configuration.FormatString);
}

/// <summary>
/// handler clicking 'Insert' button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonInsert_Click(object sender, EventArgs e)
{
var guidIndex = decimal.ToInt32(this.numericUpDownGUID.Value);
var keyname = Template.Variables[this.comboBoxVariable.SelectedIndex];
var variable = (guidIndex > 0 )? keyname.GetVariable(guidIndex) : keyname.GetVariable();
var textBox = this.textBoxTemplate;
textBox.Text = textBox.Text.Insert(textBox.SelectionStart, variable);
InsertText(variable);
}

/// <summary>
/// handler clicking 'EOL' button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonInsertLineEnding_Click(object sender, EventArgs e)
{
var textBox = this.textBoxTemplate;
textBox.Text = textBox.Text.Insert(textBox.SelectionStart, Environment.NewLine);
InsertText(Environment.NewLine);
}

/// <summary>
/// handler clicking '{' button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonLeftBrace_Click(object sender, EventArgs e)
{
InsertText("{{");
}

/// <summary>
/// handler clicking '}' button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonRightBrace_Click(object sender, EventArgs e)
{
InsertText("}}");
}

/// <summary>
/// hander clicking 'Default' button
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void buttonSetDefault_Click(object sender, EventArgs e)
{
SetText(Template.DefaultFormatString);
}

/// <summary>
/// handler that the textbox loses focus
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBoxTemplate_LostFocus(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
this.insertPos = textBox.SelectionStart;
}

/// <summary>
/// insert a text to the textbox
/// </summary>
/// <param name="text"></param>
private void InsertText(string text)
{
var textBox = this.textBoxTemplate;
textBox.Text = textBox.Text.Insert(this.insertPos, text);
this.insertPos += text.Length;

ScrollToCarect();
}

/// <summary>
/// overwrite textbox content
/// </summary>
/// <param name="text"></param>
private void SetText(string text)
{
var textBox = this.textBoxTemplate;
textBox.Text = text;
this.insertPos = text.Length;

ScrollToCarect();
}

/// <summary>
/// function to scroll the textbox to the caret position
/// </summary>
private void ScrollToCarect()
{
var textBox = this.textBoxTemplate;
textBox.Text = Template.DefaultFormatString;
textBox.SelectionStart = this.insertPos;
textBox.Focus();
textBox.ScrollToCaret();
}
}
}
Loading