public override void InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable <AstNode> nodes) { var part = parentType.Parts.FirstOrDefault(); if (part == null) { return; } var loadedDocument = Ide.IdeApp.Workbench.OpenDocument(part.Region.FileName); loadedDocument.RunWhenLoaded(delegate { var editor = loadedDocument.Editor; var loc = part.Region.Begin; var parsedDocument = loadedDocument.UpdateParseDocument(); var declaringType = parsedDocument.GetInnermostTypeDefinition(loc); var mode = new InsertionCursorEditMode( editor.Parent, CodeGenerationService.GetInsertionPoints(loadedDocument, declaringType)); if (mode.InsertionPoints.Count == 0) { MessageService.ShowError( GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name) ); return; } var helpWindow = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow(); helpWindow.TransientFor = MonoDevelop.Ide.IdeApp.Workbench.RootWindow; helpWindow.TitleText = operation; mode.HelpWindow = helpWindow; mode.CurIndex = 0; mode.StartMode(); mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) { if (iCArgs.Success) { foreach (var node in nodes) { var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node); output.RegisterTrackedSegments(this, loadedDocument.Editor.LocationToOffset(iCArgs.InsertionPoint.Location)); iCArgs.InsertionPoint.Insert(editor, output.Text); } } }; }); }
public override Task <Script> InsertWithCursor(string operation, ITypeDefinition parentType, Func <Script, RefactoringContext, IEnumerable <AstNode> > nodeCallback) { var tcs = new TaskCompletionSource <Script>(); if (parentType == null) { return(tcs.Task); } var part = parentType.Parts.FirstOrDefault(); if (part == null) { return(tcs.Task); } var loadedDocument = Ide.IdeApp.Workbench.OpenDocument(part.Region.FileName); loadedDocument.RunWhenLoaded(delegate { var editor = loadedDocument.Editor; var loc = part.Region.Begin; var parsedDocument = loadedDocument.UpdateParseDocument(); var declaringType = parsedDocument.GetInnermostTypeDefinition(loc); MDRefactoringScript script; if (loadedDocument.Editor != this.context.TextEditor) { script = new MDRefactoringScript(new MDRefactoringContext(loadedDocument, loc, context.CancellationToken), FormattingOptions); startedScripts.Add(script); } else { script = this; } var nodes = nodeCallback(script, script.context); var mode = new InsertionCursorEditMode( editor.Parent, CodeGenerationService.GetInsertionPoints(loadedDocument, declaringType)); if (mode.InsertionPoints.Count == 0) { MessageService.ShowError( GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name) ); return; } if (declaringType.Kind == TypeKind.Enum) { foreach (var node in nodes.Reverse()) { var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node); var point = mode.InsertionPoints.First(); var offset = loadedDocument.Editor.LocationToOffset(point.Location); var text = output.Text + ","; var delta = point.Insert(editor, text); output.RegisterTrackedSegments(script, delta + offset); } tcs.SetResult(script); return; } var helpWindow = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow(); helpWindow.TitleText = operation; mode.HelpWindow = helpWindow; mode.CurIndex = 0; operationsRunning++; mode.StartMode(); mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) { if (iCArgs.Success) { if (iCArgs.InsertionPoint.LineAfter == NewLineInsertion.None && iCArgs.InsertionPoint.LineBefore == NewLineInsertion.None && nodes.Count() > 1) { iCArgs.InsertionPoint.LineAfter = NewLineInsertion.BlankLine; } foreach (var node in nodes.Reverse()) { var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node); var offset = loadedDocument.Editor.LocationToOffset(iCArgs.InsertionPoint.Location); var text = output.Text; var delta = iCArgs.InsertionPoint.Insert(editor, text); output.RegisterTrackedSegments(script, delta + offset); } tcs.SetResult(script); } else { Rollback(); } DisposeOnClose(); }; }); return(tcs.Task); }
public override Task <Script> InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable <AstNode> nodes) { var tcs = new TaskCompletionSource <Script> (); var editor = context.TextEditor; DocumentLocation loc = context.TextEditor.Caret.Location; var declaringType = context.ParsedDocument.GetInnermostTypeDefinition(loc); var mode = new InsertionCursorEditMode( editor.Parent, CodeGenerationService.GetInsertionPoints(context.TextEditor, context.ParsedDocument, declaringType)); if (mode.InsertionPoints.Count == 0) { MessageService.ShowError( GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name) ); return(tcs.Task); } var helpWindow = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow(); helpWindow.TitleText = operation; mode.HelpWindow = helpWindow; switch (defaultPosition) { case InsertPosition.Start: mode.CurIndex = 0; break; case InsertPosition.End: mode.CurIndex = mode.InsertionPoints.Count - 1; break; case InsertPosition.Before: for (int i = 0; i < mode.InsertionPoints.Count; i++) { if (mode.InsertionPoints [i].Location < loc) { mode.CurIndex = i; } } break; case InsertPosition.After: for (int i = 0; i < mode.InsertionPoints.Count; i++) { if (mode.InsertionPoints [i].Location > loc) { mode.CurIndex = i; break; } } break; } operationsRunning++; mode.StartMode(); mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) { if (iCArgs.Success) { if (iCArgs.InsertionPoint.LineAfter == NewLineInsertion.None && iCArgs.InsertionPoint.LineBefore == NewLineInsertion.None && nodes.Count() > 1) { iCArgs.InsertionPoint.LineAfter = NewLineInsertion.BlankLine; } foreach (var node in nodes.Reverse()) { var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node); var offset = context.TextEditor.LocationToOffset(iCArgs.InsertionPoint.Location); var delta = iCArgs.InsertionPoint.Insert(editor, output.Text); output.RegisterTrackedSegments(this, delta + offset); } tcs.SetResult(this); } else { Rollback(); } DisposeOnClose(); }; return(tcs.Task); }
public override void InsertWithCursor(string operation, InsertPosition defaultPosition, IEnumerable <AstNode> nodes) { var editor = document.Editor; DocumentLocation loc = document.Editor.Caret.Location; var declaringType = document.ParsedDocument.GetInnermostTypeDefinition(loc); var mode = new InsertionCursorEditMode( editor.Parent, CodeGenerationService.GetInsertionPoints(document, declaringType)); if (mode.InsertionPoints.Count == 0) { MessageService.ShowError( GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name) ); return; } var helpWindow = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow(); helpWindow.TransientFor = MonoDevelop.Ide.IdeApp.Workbench.RootWindow; helpWindow.TitleText = operation; mode.HelpWindow = helpWindow; switch (defaultPosition) { case InsertPosition.Start: mode.CurIndex = 0; break; case InsertPosition.End: mode.CurIndex = mode.InsertionPoints.Count - 1; break; case InsertPosition.Before: for (int i = 0; i < mode.InsertionPoints.Count; i++) { if (mode.InsertionPoints [i].Location < loc) { mode.CurIndex = i; } } break; case InsertPosition.After: for (int i = 0; i < mode.InsertionPoints.Count; i++) { if (mode.InsertionPoints [i].Location > loc) { mode.CurIndex = i; break; } } break; } mode.StartMode(); mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) { if (iCArgs.Success) { foreach (var node in nodes) { var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node); output.RegisterTrackedSegments(this, document.Editor.LocationToOffset(iCArgs.InsertionPoint.Location)); iCArgs.InsertionPoint.Insert(editor, output.Text); } } }; }
public override void InsertWithCursor(string operation, ITypeDefinition parentType, IEnumerable <AstNode> nodes, Action continuation) { if (parentType == null) { return; } var part = parentType.Parts.FirstOrDefault(); if (part == null) { return; } var loadedDocument = Ide.IdeApp.Workbench.OpenDocument(part.Region.FileName); loadedDocument.RunWhenLoaded(delegate { var editor = loadedDocument.Editor; var loc = part.Region.Begin; var parsedDocument = loadedDocument.UpdateParseDocument(); var declaringType = parsedDocument.GetInnermostTypeDefinition(loc); var mode = new InsertionCursorEditMode( editor.Parent, CodeGenerationService.GetInsertionPoints(loadedDocument, declaringType)); if (mode.InsertionPoints.Count == 0) { MessageService.ShowError( GettextCatalog.GetString("No valid insertion point can be found in type '{0}'.", declaringType.Name) ); return; } if (declaringType.Kind == TypeKind.Enum) { foreach (var node in nodes.Reverse()) { var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node); var point = mode.InsertionPoints.First(); var offset = loadedDocument.Editor.LocationToOffset(point.Location); var text = output.Text + ","; var delta = point.Insert(editor, text); output.RegisterTrackedSegments(this, delta + offset); } if (continuation != null) { continuation(); } return; } var helpWindow = new Mono.TextEditor.PopupWindow.InsertionCursorLayoutModeHelpWindow(); helpWindow.TransientFor = MonoDevelop.Ide.IdeApp.Workbench.RootWindow; helpWindow.TitleText = operation; helpWindow.Shown += (s, a) => DesktopService.RemoveWindowShadow(helpWindow); mode.HelpWindow = helpWindow; mode.CurIndex = 0; operationsRunning++; mode.StartMode(); mode.Exited += delegate(object s, InsertionCursorEventArgs iCArgs) { if (iCArgs.Success) { if (iCArgs.InsertionPoint.LineAfter == NewLineInsertion.None && iCArgs.InsertionPoint.LineBefore == NewLineInsertion.None && nodes.Count() > 1) { iCArgs.InsertionPoint.LineAfter = NewLineInsertion.BlankLine; } foreach (var node in nodes.Reverse()) { var output = OutputNode(CodeGenerationService.CalculateBodyIndentLevel(declaringType), node); var offset = loadedDocument.Editor.LocationToOffset(iCArgs.InsertionPoint.Location); var text = output.Text; var delta = iCArgs.InsertionPoint.Insert(editor, text); output.RegisterTrackedSegments(this, delta + offset); } if (continuation != null) { continuation(); } } else { Rollback(); } DisposeOnClose(); }; }); }