diff --git a/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/AbstractDesignerGenerator.cs b/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/AbstractDesignerGenerator.cs index 0a5ac915de..bdd6f4f792 100644 --- a/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/AbstractDesignerGenerator.cs +++ b/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/AbstractDesignerGenerator.cs @@ -101,9 +101,7 @@ namespace ICSharpCode.FormsDesigner Reparse(); IField field = GetField(formClass, fieldName); if (field != null) { - int startOffset = this.ViewContent.DesignerCodeFileDocument.PositionToOffset(new TextLocation(0, field.Region.BeginLine - 1)); - int endOffset = this.ViewContent.DesignerCodeFileDocument.PositionToOffset(new TextLocation(0, field.Region.EndLine)); - this.ViewContent.DesignerCodeFileDocument.Remove(startOffset, endOffset - startOffset); + this.RemoveFieldDeclaration(this.ViewContent.DesignerCodeFileDocument, field); } else if ((field = GetField(completeClass, fieldName)) != null) { // TODO: Remove the field in the part where it is declared LoggingService.Warn("Removing field declaration in non-designer part currently not supported"); @@ -138,9 +136,7 @@ namespace ICSharpCode.FormsDesigner Reparse(); IField oldField = GetField(formClass, newField.Name); if (oldField != null) { - int startOffset = this.ViewContent.DesignerCodeFileDocument.PositionToOffset(new TextLocation(0, oldField.Region.BeginLine - 1)); - int endOffset = this.ViewContent.DesignerCodeFileDocument.PositionToOffset(new TextLocation(0, oldField.Region.EndLine)); - this.ViewContent.DesignerCodeFileDocument.Replace(startOffset, endOffset - startOffset, tabs + GenerateFieldDeclaration(domGenerator, newField) + Environment.NewLine); + this.ReplaceFieldDeclaration(this.ViewContent.DesignerCodeFileDocument, oldField, GenerateFieldDeclaration(domGenerator, newField)); } else { if ((oldField = GetField(completeClass, newField.Name)) != null) { // TODO: Replace the field in the part where it is declared @@ -159,6 +155,36 @@ namespace ICSharpCode.FormsDesigner protected abstract DomRegion GetReplaceRegion(ICSharpCode.TextEditor.Document.IDocument document, IMethod method); + /// <summary> + /// Removes a field declaration from the source code document. + /// </summary> + /// <remarks> + /// The default implementation assumes that the field region starts at the very beginning + /// of the line of the field declaration and ends at the end of that line. + /// Override this method if that is not the case in a specific language. + /// </remarks> + protected virtual void RemoveFieldDeclaration(IDocument document, IField field) + { + int startOffset = document.PositionToOffset(new TextLocation(0, field.Region.BeginLine - 1)); + int endOffset = document.PositionToOffset(new TextLocation(0, field.Region.EndLine)); + document.Remove(startOffset, endOffset - startOffset); + } + + /// <summary> + /// Replaces a field declaration in the source code document. + /// </summary> + /// <remarks> + /// The default implementation assumes that the field region starts at the very beginning + /// of the line of the field declaration and ends at the end of that line. + /// Override this method if that is not the case in a specific language. + /// </remarks> + protected virtual void ReplaceFieldDeclaration(IDocument document, IField oldField, string newFieldDeclaration) + { + int startOffset = document.PositionToOffset(new TextLocation(0, oldField.Region.BeginLine - 1)); + int endOffset = document.PositionToOffset(new TextLocation(0, oldField.Region.EndLine)); + document.Replace(startOffset, endOffset - startOffset, tabs + newFieldDeclaration + Environment.NewLine); + } + protected virtual void FixGeneratedCode(IClass formClass, CodeMemberMethod code) { } diff --git a/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/VBNetDesignerGenerator.cs b/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/VBNetDesignerGenerator.cs index 94b7add6ee..ae3c50b162 100644 --- a/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/VBNetDesignerGenerator.cs +++ b/src/AddIns/DisplayBindings/FormsDesigner/Project/Src/DesignerGenerator/VBNetDesignerGenerator.cs @@ -12,6 +12,7 @@ using System.Text; using ICSharpCode.Core; using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.TextEditor; using ICSharpCode.NRefactory.Ast; using ICSharpCode.NRefactory.PrettyPrinter; @@ -30,6 +31,24 @@ namespace ICSharpCode.FormsDesigner return new DomRegion(r.BeginLine + 1, 1, r.EndLine, 1); } + protected override void RemoveFieldDeclaration(ICSharpCode.TextEditor.Document.IDocument document, IField field) + { + // In VB, the field region begins at the start of the declaration + // and ends on the first column of the line following the declaration. + int startOffset = document.PositionToOffset(new TextLocation(0, field.Region.BeginLine - 1)); + int endOffset = document.PositionToOffset(new TextLocation(0, field.Region.EndLine - 1)); + document.Remove(startOffset, endOffset - startOffset); + } + + protected override void ReplaceFieldDeclaration(ICSharpCode.TextEditor.Document.IDocument document, IField oldField, string newFieldDeclaration) + { + // In VB, the field region begins at the start of the declaration + // and ends on the first column of the line following the declaration. + int startOffset = document.PositionToOffset(new TextLocation(0, oldField.Region.BeginLine - 1)); + int endOffset = document.PositionToOffset(new TextLocation(0, oldField.Region.EndLine - 1)); + document.Replace(startOffset, endOffset - startOffset, tabs + newFieldDeclaration + Environment.NewLine); + } + protected override string CreateEventHandler(Type eventType, string eventMethodName, string body, string indentation) { string param = GenerateParams(eventType);