protected int CreateEventHandler(string document, string codeBehind, string codeBehindFile, string className, string objectTypeName, string eventName, string eventHandlerName) { var binder = GetBinder(document, codeBehind, codeBehindFile); if (binder == null) { return(NativeMethods.E_FAIL); } CodeFunction codeFunction = null; UndoContext undoContext = binder.FileCodeModel.DTE.UndoContext; if (undoContext != null) { if (!undoContext.IsOpen) { undoContext.Open(eventHandlerName, false); } else { undoContext = null; } } try { codeFunction = binder.CreateEventHandler(codeBehindFile, className, objectTypeName, eventName, eventHandlerName); EditPoint point = codeFunction.StartPoint.CreateEditPoint(); if (point != null) { point.SmartFormat(codeFunction.EndPoint); } return(NativeMethods.S_OK); } finally { if (undoContext != null) { if (codeFunction != null) { undoContext.Close(); var nemerleFileCodeModel = binder.FileCodeModel as Nemerle.VisualStudio.FileCodeModel.NemerleFileCodeModel; if (nemerleFileCodeModel != null) { nemerleFileCodeModel.FlushChanges(); } } else { undoContext.SetAborted(); } } } }
public void Execute(string commandName, Action commandAction) { ThreadHelper.ThrowIfNotOnUIThread(); // Put all of the changes in a single Undo context. UndoContext undo = this.dte.UndoContext; bool undoAlreadyOpen = undo.IsOpen; if (!undoAlreadyOpen) { if (string.IsNullOrEmpty(commandName)) { commandName = "Set Selected Text"; } undo.Open(commandName + UndoContextSuffix, false); } try { commandAction(); } catch (Exception ex) { // Most commands won't change anything until they try to push in the final SelectedText value. // But some commands (e.g., Sort Members) need to update things in intermediate steps (e.g., // removing members from the editor's code model). If any exception occurs, we need to abort // everything that was done in the current context to avoid data loss (e.g., losing the members // that were temporarily removed from the editor's code model). undo.SetAborted(); // If the file is read-only (e.g., in source control) and the user cancels the edit, // then a COMException is thrown with HRESULT 0x80041005 (WBEM_E_TYPE_MISMATCH). // We'll ignore that exception, but we'll re-throw anything else. const uint WBEM_E_TYPE_MISMATCH = 0x80041005; if (!(ex is COMException) || ex.HResult != unchecked ((int)WBEM_E_TYPE_MISMATCH)) { throw; } } finally { // Make sure we close the UndoContext. if (!undoAlreadyOpen && !undo.IsAborted) { undo.Close(); } } }
/// <summary>Extracts the string to a resource file and replaces it with a reference to the new entry</summary> /// <param name="file">A resource file to extract string to</param> /// <param name="resourceName">Name ot be given to the resource</param> public void ExtractStringToResource(ResourceFile file, string resourceName) { UndoContext undo = StringToExtract.Parent.Document.DTE.UndoContext; undo.Open(Strings.ExtractResourceUndoContextName); try { Project project = null; try { project = this.StringToExtract.Parent.Document.ProjectItem.ContainingProject; } catch { } string reference = this.actionObject.GetResourceReference(file, resourceName, project, stringInstance); reference = this.StringToExtract.GetShortestReference(reference, this.StringToExtract.GetImportedNamespaces()); this.StringToExtract.Replace(reference); undo.Close(); } catch { undo.SetAborted(); throw; } }
/// <summary> /// Implements the Exec method of the IDTCommandTarget interface. /// This is called when the command is invoked. /// </summary> /// <param term='commandName'> /// The name of the command to execute. /// </param> /// <param term='executeOption'> /// Describes how the command should be run. /// </param> /// <param term='varIn'> /// Parameters passed from the caller to the command handler. /// </param> /// <param term='varOut'> /// Parameters passed from the command handler to the caller. /// </param> /// <param term='handled'> /// Informs the caller if the command was handled or not. /// </param> /// <seealso class='Exec' /> public void Exec(string commandName, EnvDTE.vsCommandExecOption executeOption, ref object varIn, ref object varOut, ref bool handled) { handled = false; if (executeOption == EnvDTE.vsCommandExecOption.vsCommandExecOptionDoDefault) { if (commandName == "SynchAdd.Connect.SynchAdd") { handled = true; if (((Project)((System.Array)applicationObject.ActiveSolutionProjects).GetValue(0)).Kind != VSLangProj.PrjKind.prjKindCSharpProject) { System.Windows.Forms.MessageBox.Show("Sorry - C# only."); return; } //get target class and modification requirements UndoContext undoCtxt = null; try{ TagetClassSelect tcs = new TagetClassSelect(applicationObject); tcs.ShowDialog(); CodeClass cc = tcs.TargetClass; if (cc == null) { return; } undoCtxt = applicationObject.UndoContext; undoCtxt.Open("Base class synch. modifications", false); CodeProcessor processor = new CodeProcessor(cc); if (!processor.MakeBaseClassCompatiableWithSyncPattern()) { if (undoCtxt.IsOpen) { undoCtxt.SetAborted(); if (undoCtxt.IsOpen) { undoCtxt.Close(); } return; } } undoCtxt.Close(); undoCtxt.Open("Synch. class creation", false); if (processor.CreateSynchClass()) { System.Windows.Forms.MessageBox.Show("Synch. type created."); } else { System.Windows.Forms.MessageBox.Show("Error during synch. type creation."); undoCtxt.SetAborted(); cc.ProjectItem.Document.Undo(); } if (undoCtxt.IsOpen) { undoCtxt.Close(); } } catch (Exception e) { System.Windows.Forms.MessageBox.Show("The following exception occured - (" + e.Message + ").\nThe synchronized wrapped cannot be added at this time."); if (undoCtxt != null && undoCtxt.IsOpen) { undoCtxt.SetAborted(); undoCtxt.Close(); } } return; } } }