public OverrideEqualsGetHashCodeMethodsDialog(InsertionContext context, ITextEditor editor, ITextAnchor endAnchor,
		                                              ITextAnchor insertionPosition, ITypeDefinition selectedClass, IMethod selectedMethod, AstNode baseCallNode)
			: base(context, editor, insertionPosition)
		{
			if (selectedClass == null)
				throw new ArgumentNullException("selectedClass");
			
			InitializeComponent();
			
			this.selectedClass = selectedClass;
			this.insertionEndAnchor = endAnchor;
			this.selectedMethod = selectedMethod;
			this.baseCallNode = baseCallNode;
			
			addIEquatable.Content = string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.OverrideEqualsGetHashCodeMethods.AddInterface}"),
			                                      "IEquatable<" + selectedClass.Name + ">");
			
			string otherMethod = selectedMethod.Name == "Equals" ? "GetHashCode" : "Equals";
			
			addOtherMethod.Content = StringParser.Parse("${res:AddIns.SharpRefactoring.OverrideEqualsGetHashCodeMethods.AddOtherMethod}", new StringTagPair("otherMethod", otherMethod));
			
			addIEquatable.IsEnabled = !selectedClass.GetAllBaseTypes().Any(
				type => {
					if (!type.IsParameterized || (type.TypeParameterCount != 1))
						return false;
					if (type.FullName != "System.IEquatable")
						return false;
					return type.TypeArguments.First().FullName == selectedClass.FullName;
				}
			);
		}
		/// <summary>
		/// Creates a new AnchorElement.
		/// </summary>
		public AnchorElement(AnchorSegment segment, string text, string name, InsertionContext context)
		{
			this.segment = segment;
			this.context = context;
			this.Text = text;
			this.Name = name;
		}
示例#3
0
		internal static CreatePropertiesDialog CreateDialog(InsertionContext context)
		{
			ITextEditor textEditor = context.TextArea.GetService(typeof(ITextEditor)) as ITextEditor;
			
			if (textEditor == null)
				return null;
			
			using (textEditor.Document.OpenUndoGroup()) {
				IEditorUIService uiService = textEditor.GetService(typeof(IEditorUIService)) as IEditorUIService;
				
				if (uiService == null)
					return null;
				
				ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition);
				anchor.MovementType = AnchorMovementType.BeforeInsertion;
				
				CreatePropertiesDialog dialog = new CreatePropertiesDialog(context, textEditor, anchor);
				
				dialog.Element = uiService.CreateInlineUIElement(anchor, dialog);
				
				// Add creation of this inline dialog as undoable operation
				TextDocument document = textEditor.Document as TextDocument;
				if (document != null) {
					document.UndoStack.Push(dialog.UndoableCreationOperation);
				}
				
				return dialog;
				
			}
		}
		public OverrideEqualsGetHashCodeMethodsDialog(InsertionContext context, ITextEditor editor, ITextAnchor startAnchor, ITextAnchor endAnchor,
		                                              ITextAnchor insertionPosition, IClass selectedClass, IMethod selectedMethod, string baseCall)
			: base(context, editor, insertionPosition)
		{
			if (selectedClass == null)
				throw new ArgumentNullException("selectedClass");
			
			InitializeComponent();
			
			this.selectedClass = selectedClass;
			this.startAnchor = startAnchor;
			this.insertionEndAnchor = endAnchor;
			this.selectedMethod = selectedMethod;
			this.baseCall = baseCall;
			
			addIEquatable.Content = string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.OverrideEqualsGetHashCodeMethods.AddInterface}"),
			                                      "IEquatable<" + selectedClass.Name + ">");
			
			string otherMethod = selectedMethod.Name == "Equals" ? "GetHashCode" : "Equals";
			
			addOtherMethod.Content = StringParser.Parse("${res:AddIns.SharpRefactoring.OverrideEqualsGetHashCodeMethods.AddOtherMethod}", new StringTagPair("otherMethod", otherMethod));
			
			addIEquatable.IsEnabled = !selectedClass.BaseTypes.Any(
				type => {
					if (!type.IsGenericReturnType)
						return false;
					var genericType = type.CastToGenericReturnType();
					var boundTo = genericType.TypeParameter.BoundTo;
					if (boundTo == null)
						return false;
					return boundTo.Name == selectedClass.Name;
				}
			);
		}
示例#5
0
 public BoundActiveElement(InsertionContext context, SnippetReplaceableTextElement targetSnippetElement, SnippetBoundElement boundElement, AnchorSegment segment)
 {
     this.context = context;
     this.targetSnippetElement = targetSnippetElement;
     this.boundElement         = boundElement;
     this.segment = segment;
 }
		public InsertCtorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor)
			: base(context, editor, anchor)
		{
			InitializeComponent();
			
			Visibility = System.Windows.Visibility.Collapsed;
		}
		/// <inheritdoc/>
		public override void Insert(InsertionContext context)
		{
			int start = context.InsertionPosition;
			base.Insert(context);
			int end = context.InsertionPosition;
			context.RegisterActiveElement(this, new ReplaceableActiveElement(context, start, end));
		}
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     if (!setCaretOnlyIfTextIsSelected || !string.IsNullOrEmpty(context.SelectedText))
     {
         SetCaret(context);
     }
 }
示例#9
0
        /// <summary>
        /// Inserts the snippet into the text area.
        /// </summary>
        public void Insert(TextArea textArea)
        {
            if (textArea == null)
                throw new ArgumentNullException("textArea");

            ISegment selection = textArea.Selection.SurroundingSegment;
            int insertionPosition = textArea.Caret.Offset;

            if (selection != null) // if something is selected
                // use selection start instead of caret position,
                // because caret could be at end of selection or anywhere inside.
                // Removal of the selected text causes the caret position to be invalid.
                insertionPosition = selection.Offset + TextUtilities.GetWhitespaceAfter(textArea.Document, selection.Offset).Length;

            InsertionContext context = new InsertionContext(textArea, insertionPosition);

            using (context.Document.RunUpdate()) {
                if (selection != null)
                    textArea.Document.Remove(insertionPosition, selection.EndOffset - insertionPosition);
                Insert(context);

                // [DIGITALRUNE] Format inserted lines.
                int beginLine = textArea.Document.GetLineByOffset(context.StartPosition).LineNumber;
                int endLine = textArea.Document.GetLineByOffset(context.InsertionPosition).LineNumber;
                textArea.IndentationStrategy?.IndentLines(textArea, beginLine, endLine);

                context.RaiseInsertionCompleted(EventArgs.Empty);
            }
        }
示例#10
0
        /// <summary>
        /// Inserts the snippet into the text area.
        /// </summary>
        public void Insert(TextArea textArea)
        {
            if (textArea == null)
            {
                throw new ArgumentNullException("textArea");
            }

            ISegment selection         = textArea.Selection.SurroundingSegment;
            int      insertionPosition = textArea.Caret.Offset;

            if (selection != null)             // if something is selected
            // use selection start instead of caret position,
            // because caret could be at end of selection or anywhere inside.
            // Removal of the selected text causes the caret position to be invalid.
            {
                insertionPosition = selection.Offset + TextUtilities.GetWhitespaceAfter(textArea.Document, selection.Offset).Length;
            }

            Context = new InsertionContext(textArea, insertionPosition);

            using (Context.Document.RunUpdate()) {
                if (selection != null)
                {
                    textArea.Document.Remove(insertionPosition, selection.EndOffset - insertionPosition);
                }
                Insert(Context);
                Context.RaiseInsertionCompleted(EventArgs.Empty);
            }
            IsSnippetActivated = true;
        }
示例#11
0
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     foreach (SnippetElement e in this.Elements)
     {
         e.Insert(context);
     }
 }
示例#12
0
        /// <summary>
        /// Inserts the snippet into the text area.
        /// </summary>
        public void Insert(TextArea textArea)
        {
            if (textArea == null)
            {
                throw new ArgumentNullException("textArea");
            }

            ISegment selection         = textArea.Selection.SurroundingSegment;
            int      insertionPosition = textArea.Caret.Offset;

            if (selection != null)             // if something is selected
            // use selection start instead of caret position,
            // because caret could be at end of selection or anywhere inside.
            // Removal of the selected text causes the caret position to be invalid.
            {
                insertionPosition = selection.Offset + TextUtilities.GetWhitespaceAfter(textArea.Document, selection.Offset).Length;
            }

            InsertionContext context = new InsertionContext(textArea, insertionPosition);

            using (context.Document.RunUpdate()) {
                if (selection != null)
                {
                    textArea.Document.Remove(insertionPosition, selection.EndOffset - insertionPosition);
                }
                Insert(context);

                // [DIGITALRUNE] Format inserted lines.
                int beginLine = textArea.Document.GetLineByOffset(context.StartPosition).LineNumber;
                int endLine   = textArea.Document.GetLineByOffset(context.InsertionPosition).LineNumber;
                textArea.IndentationStrategy?.IndentLines(textArea, beginLine, endLine);

                context.RaiseInsertionCompleted(EventArgs.Empty);
            }
        }
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     if (text != null)
     {
         context.InsertText(text);
     }
 }
示例#14
0
		public override Task Link(params AstNode[] nodes)
		{
			var segs = nodes.Select(node => GetSegment(node)).ToArray();
			InsertionContext c = new InsertionContext(editor.GetRequiredService<TextArea>(), segs.Min(seg => seg.Offset));
			c.InsertionPosition = segs.Max(seg => seg.EndOffset);
			
			var tcs = new TaskCompletionSource<bool>();
			c.Deactivated += (sender, e) => tcs.SetResult(true);
			
			if (segs.Length > 0) {
				// try to use node in identifier context to avoid the code completion popup.
				var identifier = nodes.OfType<Identifier>().FirstOrDefault();
				ISegment first;
				if (identifier == null)
					first = segs[0];
				else
					first = GetSegment(identifier);
				c.Link(first, segs.Except(new[]{first}).ToArray());
				c.RaiseInsertionCompleted(EventArgs.Empty);
			} else {
				c.RaiseInsertionCompleted(EventArgs.Empty);
				c.Deactivate(new SnippetEventArgs(DeactivateReason.NoActiveElements));
			}
			
			return tcs.Task;
		}
		/// <summary>
		/// Called when this switch body element is inserted to the editor.
		/// </summary>
		public override void Insert(InsertionContext context)
		{
			this.context = context;
			this.context.Deactivated += new EventHandler<SnippetEventArgs>(InteractiveModeCompleted);
			this.anchor = SetUpAnchorAtInsertion(context);
			this.classFinderContext = new ClassFinder(ParserService.ParseCurrentViewContent(), Editor.Document.Text, Editor.Caret.Offset);
		}
示例#16
0
        /// <summary>
        /// Inserts the snippet into the text area.
        /// </summary>
        public void Insert(TextArea textArea)
        {
            if (textArea == null)
            {
                throw new ArgumentNullException("textArea");
            }

            ISegment selection         = textArea.Selection.SurroundingSegment;
            int      insertionPosition = textArea.Caret.Offset;

            if (selection != null)                    // if something is selected
            {
                insertionPosition = selection.Offset; // use selection start instead of caret position,
            }
            // because caret could be at end of selection or anywhere inside.
            // Removal of the selected text causes the caret position to be invalid.

            InsertionContext context = new InsertionContext(textArea, insertionPosition);

            if (selection != null)
            {
                textArea.Document.Remove(selection);
            }

            using (context.Document.RunUpdate()) {
                Insert(context);
                context.RaiseInsertionCompleted(EventArgs.Empty);
            }
        }
		public BoundActiveElement(InsertionContext context, SnippetReplaceableTextElement targetSnippetElement, SnippetBoundElement boundElement, AnchorSegment segment)
		{
			this.context = context;
			this.targetSnippetElement = targetSnippetElement;
			this.boundElement = boundElement;
			this.segment = segment;
		}
		public void Insert(CompletionContext context, ICompletionItem item)
		{
			if (item == null)
				throw new ArgumentNullException("item");
			
			if (!(item is OverrideCompletionItem))
				throw new ArgumentException("item is not an OverrideCompletionItem");
			
			OverrideCompletionItem completionItem = item as OverrideCompletionItem;
			
			ITextEditor textEditor = context.Editor;
			
			IEditorUIService uiService = textEditor.GetService(typeof(IEditorUIService)) as IEditorUIService;
			
			if (uiService == null)
				return;
			
			ParseInformation parseInfo = ParserService.GetParseInformation(textEditor.FileName);
			
			if (parseInfo == null)
				return;
			
			CodeGenerator generator = parseInfo.CompilationUnit.Language.CodeGenerator;
			IClass current = parseInfo.CompilationUnit.GetInnermostClass(textEditor.Caret.Line, textEditor.Caret.Column);
			ClassFinder finder = new ClassFinder(current, textEditor.Caret.Line, textEditor.Caret.Column);
			
			if (current == null)
				return;
			
			using (textEditor.Document.OpenUndoGroup()) {
				ITextAnchor startAnchor = textEditor.Document.CreateAnchor(textEditor.Caret.Offset);
				startAnchor.MovementType = AnchorMovementType.BeforeInsertion;
				
				ITextAnchor endAnchor = textEditor.Document.CreateAnchor(textEditor.Caret.Offset);
				endAnchor.MovementType = AnchorMovementType.AfterInsertion;
				
				MethodDeclaration member = (MethodDeclaration)generator.GetOverridingMethod(completionItem.Member, finder);
				
				string indent = DocumentUtilitites.GetWhitespaceBefore(textEditor.Document, textEditor.Caret.Offset);
				string codeForBaseCall = generator.GenerateCode(member.Body.Children.OfType<AbstractNode>().First(), "");
				string code = generator.GenerateCode(member, indent);
				int marker = code.IndexOf(codeForBaseCall);
				
				textEditor.Document.Insert(startAnchor.Offset, code.Substring(0, marker).TrimStart());
				
				ITextAnchor insertionPos = textEditor.Document.CreateAnchor(endAnchor.Offset);
				insertionPos.MovementType = AnchorMovementType.BeforeInsertion;
				
				InsertionContext insertionContext = new InsertionContext(textEditor.GetService(typeof(TextArea)) as TextArea, startAnchor.Offset);
				
				AbstractInlineRefactorDialog dialog = new OverrideEqualsGetHashCodeMethodsDialog(insertionContext, textEditor, startAnchor, endAnchor, insertionPos, current, completionItem.Member as IMethod, codeForBaseCall.Trim());
				dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog);
				
				textEditor.Document.InsertNormalized(endAnchor.Offset, Environment.NewLine + code.Substring(marker + codeForBaseCall.Length));
				
				insertionContext.RegisterActiveElement(new InlineRefactorSnippetElement(cxt => null, ""), dialog);
				insertionContext.RaiseInsertionCompleted(EventArgs.Empty);
			}
		}
示例#19
0
		/// <inheritdoc />
		public override void Insert(InsertionContext context)
		{
			TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);
			start.MovementType = AnchorMovementType.BeforeInsertion;
			start.SurviveDeletion = true;
			AnchorSegment segment = new AnchorSegment(start, start);
			context.RegisterActiveElement(this, new AnchorElement(segment, Name, context));
		}
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     context.InsertText(context.SelectedText);
     if (string.IsNullOrEmpty(context.SelectedText))
     {
         SnippetCaretElement.SetCaret(context);
     }
 }
示例#21
0
        /// <inheritdoc/>
        public override void Insert(InsertionContext context)
        {
            int start = context.InsertionPosition;

            base.Insert(context);
            int end = context.InsertionPosition;

            context.RegisterActiveElement(this, new ReplaceableActiveElement(context, start, end));
        }
		public OverrideToStringMethodDialog(InsertionContext context, ITextEditor editor, ITextAnchor startAnchor, ITextAnchor anchor, IList<IField> fields, string baseCall)
			: base(context, editor, anchor)
		{
			InitializeComponent();
			
			this.baseCall = baseCall;
			this.fields = fields.Select(f => new PropertyOrFieldWrapper(f) { IsSelected = true }).ToList();
			this.listBox.ItemsSource = this.fields;
		}
示例#23
0
		public InsertCtorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IClass current, IList<PropertyOrFieldWrapper> possibleParameters)
			: base(context, editor, anchor)
		{
			InitializeComponent();
			
			this.varList.ItemsSource = parameterList = possibleParameters;
			
			if (!parameterList.Any())
				Visibility = System.Windows.Visibility.Collapsed;
		}
示例#24
0
 internal static void SetCaret(InsertionContext context)
 {
     TextAnchor pos = context.Document.CreateAnchor(context.InsertionPosition);
     pos.SurviveDeletion = true;
     context.Deactivated += (sender, e) => {
         if (e.Reason == DeactivateReason.ReturnPressed || e.Reason == DeactivateReason.NoActiveElements) {
             context.TextArea.Caret.Offset = pos.Offset;
         }
     };
 }
		public OverrideToStringMethodDialog(InsertionContext context, ITextEditor editor, ITextAnchor startAnchor, ITextAnchor anchor, IList<PropertyOrFieldWrapper> fields, string baseCall)
			: base(context, editor, anchor)
		{
			InitializeComponent();
			
			this.baseCall = baseCall;
			this.listBox.ItemsSource = fields;
			
			listBox.SelectAll();
		}
		public OverrideToStringMethodDialog(InsertionContext context, ITextEditor editor, ITextAnchor startAnchor, ITextAnchor anchor, IList<IField> fields, string baseCall)
			: base(context, editor, anchor)
		{
			InitializeComponent();
			
			this.baseCall = baseCall;
			this.listBox.ItemsSource = fields.Where(f => f.ReturnType != null).Select(f => new PropertyOrFieldWrapper(f)).ToList();
			
			listBox.SelectAll();
		}
示例#27
0
        /// <inheritdoc />
        public override void Insert(InsertionContext context)
        {
            TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);

            start.MovementType    = AnchorMovementType.BeforeInsertion;
            start.SurviveDeletion = true;
            AnchorSegment segment = new AnchorSegment(start, start);

            context.RegisterActiveElement(this, new AnchorElement(segment, Name, context));
        }
		public OverrideToStringMethodDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IList<PropertyOrFieldWrapper> fields, AstNode baseCallNode)
			: base(context, editor, anchor)
		{
			InitializeComponent();
			
			this.baseCallNode = baseCallNode;
			parameterList = fields;
			this.listBox.ItemsSource = fields;
			
			SelectAllChecked();
		}
示例#29
0
        internal static void SetCaret(InsertionContext context)
        {
            TextAnchor pos = context.Document.CreateAnchor(context.InsertionPosition);

            pos.SurviveDeletion  = true;
            context.Deactivated += (sender, e) => {
                if (e.Reason == DeactivateReason.ReturnPressed || e.Reason == DeactivateReason.NoActiveElements)
                {
                    context.TextArea.Caret.Offset = pos.Offset;
                }
            };
        }
		protected AbstractInlineRefactorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor)
		{
			if (context == null)
				throw new ArgumentNullException("context");
			
			this.anchor = insertionEndAnchor = anchor;
			this.editor = editor;
			this.context = context;
			
			this.classFinderContext = new ClassFinder(ParserService.ParseCurrentViewContent(), editor.Document.Text, anchor.Offset);
			
			this.Background = SystemColors.ControlBrush;
		}
		protected AbstractInlineRefactorDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor)
		{
			if (context == null)
				throw new ArgumentNullException("context");
			
			this.anchor = insertionEndAnchor = anchor;
			this.editor = editor;
			this.insertionContext = context;
			
			this.Background = SystemColors.ControlBrush;
			
			undoableCreationOperation = new UndoableInlineDialogCreation(this);
		}
		/// <inheritdoc/>
		public override void Insert(InsertionContext context)
		{
			if (targetElement != null) {
				int start = context.InsertionPosition;
				string inputText = targetElement.Text;
				if (inputText != null) {
					context.InsertText(ConvertText(inputText));
				}
				int end = context.InsertionPosition;
				AnchorSegment segment = new AnchorSegment(context.Document, start, end - start);
				context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment));
			}
		}
示例#33
0
 /// <inheritdoc/>
 public override void Insert(InsertionContext context)
 {
     if (targetElement != null)
     {
         int    start     = context.InsertionPosition;
         string inputText = targetElement.Text;
         if (inputText != null)
         {
             context.InsertText(ConvertText(inputText));
         }
         int           end     = context.InsertionPosition;
         AnchorSegment segment = new AnchorSegment(context.Document, start, end - start);
         context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment));
     }
 }
		public CreatePropertiesDialog(InsertionContext context, ITextEditor editor, ITextAnchor anchor, IClass current, IList<FieldWrapper> availableFields)
			: base(context, editor, anchor)
		{
			InitializeComponent();
			
			this.listBox.ItemsSource = fields = availableFields;
			
			if (!fields.Any())
				Visibility = System.Windows.Visibility.Collapsed;
			
			implementInterface.IsChecked = !current.IsStatic && HasOnPropertyChanged(current);
			if (current.IsStatic)
				implementInterface.Visibility = System.Windows.Visibility.Collapsed;
			
			listBox.UnselectAll();
		}
示例#35
0
		/// <inheritdoc/>
		public override void Insert(InsertionContext context)
		{
			if (targetElement != null) {
				TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);
				start.MovementType = AnchorMovementType.BeforeInsertion;
				start.SurviveDeletion = true;
				string inputText = targetElement.Text;
				if (inputText != null) {
					context.InsertText(ConvertText(inputText));
				}
				TextAnchor end = context.Document.CreateAnchor(context.InsertionPosition);
				end.MovementType = AnchorMovementType.BeforeInsertion;
				end.SurviveDeletion = true;
				AnchorSegment segment = new AnchorSegment(start, end);
				context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment));
			}
		}
示例#36
0
		/// <inheritdoc/>
		public override void Insert(InsertionContext context)
		{
			if (targetElement != null) {
				TextAnchor start = context.Document.CreateAnchor(context.InsertionPosition);
				start.MovementType = AnchorMovementType.BeforeInsertion;
				start.SurviveDeletion = true;
				string inputText = targetElement.Text;
				if (inputText != null) {
					context.InsertText(ConvertText(inputText));
				}
				TextAnchor end = context.Document.CreateAnchor(context.InsertionPosition);
				end.MovementType = AnchorMovementType.BeforeInsertion;
				end.SurviveDeletion = true;
				AnchorSegment segment = new AnchorSegment(start, end);
				context.RegisterActiveElement(this, new BoundActiveElement(context, targetElement, this, segment));
			}
		}
		InsertCtorDialog CreateDialog(InsertionContext context)
		{
			ITextEditor textEditor = context.TextArea.GetService(typeof(ITextEditor)) as ITextEditor;
			
			if (textEditor == null)
				return null;
			
			IEditorUIService uiService = textEditor.GetService(typeof(IEditorUIService)) as IEditorUIService;
			
			if (uiService == null)
				return null;
			
			ParseInformation parseInfo = ParserService.GetParseInformation(textEditor.FileName);
			
			if (parseInfo == null)
				return null;
			
			CodeGenerator generator = parseInfo.CompilationUnit.Language.CodeGenerator;
			
			// cannot use insertion position at this point, because it might not be
			// valid, because we are still generating the elements.
			// DOM is not updated
			ICSharpCode.AvalonEdit.Document.TextLocation loc = context.Document.GetLocation(context.StartPosition);
			
			IClass current = parseInfo.CompilationUnit.GetInnermostClass(loc.Line, loc.Column);
			
			if (current == null)
				return null;
			
			List<PropertyOrFieldWrapper> parameters = CreateCtorParams(current).ToList();
			
			if (!parameters.Any())
				return null;
			
			ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition);
			anchor.MovementType = AnchorMovementType.BeforeInsertion;
			
			InsertCtorDialog dialog = new InsertCtorDialog(context, textEditor, anchor, current, parameters);
			
			dialog.Element = uiService.CreateInlineUIElement(anchor, dialog);
			
			return dialog;
		}
		/// <inheritdoc/>
		public override void Insert(InsertionContext context)
		{
			StringBuilder tabString = new StringBuilder();
			
			for (int i = 0; i < Indentation; i++) {
				tabString.Append(context.Tab);
			}
			
			string indent = tabString.ToString();
			
			string text = context.SelectedText.TrimStart(' ', '\t');
			
			text = text.Replace(context.LineTerminator,
			                             context.LineTerminator + indent);
			
			context.Document.Insert(context.InsertionPosition, text);
			context.InsertionPosition += text.Length;
			
			if (string.IsNullOrEmpty(context.SelectedText))
				SnippetCaretElement.SetCaret(context);
		}
		InsertCtorDialog CreateDialog(InsertionContext context)
		{
			ITextEditor textEditor = context.TextArea.GetService(typeof(ITextEditor)) as ITextEditor;
			
			if (textEditor == null)
				return null;
			
			IEditorUIService uiService = textEditor.GetService(typeof(IEditorUIService)) as IEditorUIService;
			
			if (uiService == null)
				return null;
			
			ITextAnchor anchor = textEditor.Document.CreateAnchor(context.InsertionPosition);
			anchor.MovementType = AnchorMovementType.BeforeInsertion;
			
			InsertCtorDialog dialog = new InsertCtorDialog(context, textEditor, anchor);
			
			dialog.Element = uiService.CreateInlineUIElement(anchor, dialog);
			
			return dialog;
		}
示例#40
0
		/// <summary>
		/// Inserts the snippet into the text area.
		/// </summary>
		public void Insert(TextArea textArea)
		{
			if (textArea == null)
				throw new ArgumentNullException("textArea");
			
			ISegment selection = textArea.Selection.SurroundingSegment;
			int insertionPosition = textArea.Caret.Offset;
			
			if (selection != null) // if something is selected
				insertionPosition = selection.Offset; // use selection start instead of caret position,
													     // because caret could be at end of selection or anywhere inside.
													     // Removal of the selected text causes the caret position to be invalid.
			
			InsertionContext context = new InsertionContext(textArea, insertionPosition);
			
			if (selection != null)
				textArea.Document.Remove(selection);
			
			using (context.Document.RunUpdate()) {
				Insert(context);
				context.RaiseInsertionCompleted(EventArgs.Empty);
			}
		}
        /// <inheritdoc/>
        public override void Insert(InsertionContext context)
        {
            StringBuilder tabString = new StringBuilder();

            for (int i = 0; i < Indentation; i++)
            {
                tabString.Append(context.Tab);
            }

            string indent = tabString.ToString();

            string text = context.SelectedText.TrimStart(' ', '\t');

            text = text.Replace(context.LineTerminator,
                                context.LineTerminator + indent);

            context.Document.Insert(context.InsertionPosition, text);
            context.InsertionPosition += text.Length;

            if (string.IsNullOrEmpty(context.SelectedText))
            {
                SnippetCaretElement.SetCaret(context);
            }
        }
		/// <inheritdoc/>
		public override void Insert(InsertionContext context)
		{
			foreach (SnippetElement e in this.Elements) {
				e.Insert(context);
			}
		}
		public override void Complete(CompletionContext context)
		{
			if (declarationBegin > context.StartOffset) {
				base.Complete(context);
				return;
			}
			
			TypeSystemAstBuilder b = new TypeSystemAstBuilder(contextAtCaret);
			b.ShowTypeParameterConstraints = false;
			b.GenerateBody = true;
			
			var entityDeclaration = b.ConvertEntity(this.Entity);
			entityDeclaration.Modifiers &= ~(Modifiers.Virtual | Modifiers.Abstract);
			entityDeclaration.Modifiers |= Modifiers.Override;
			
			var body = entityDeclaration.GetChildByRole(Roles.Body);
			Statement baseCallStatement = body.Children.OfType<Statement>().FirstOrDefault();
			
			if (!this.Entity.IsAbstract) {
				// modify body to call the base method
				if (this.Entity.SymbolKind == SymbolKind.Method) {
					var baseCall = new BaseReferenceExpression().Invoke(this.Entity.Name, new Expression[] { });
					if (((IMethod)this.Entity).ReturnType.IsKnownType(KnownTypeCode.Void))
						baseCallStatement = new ExpressionStatement(baseCall);
					else
						baseCallStatement = new ReturnStatement(baseCall);
					
					// Clear body of inserted method
					entityDeclaration.GetChildByRole(Roles.Body).Statements.Clear();
				}
			}
			
			var document = context.Editor.Document;
			StringWriter w = new StringWriter();
			var formattingOptions = FormattingOptionsFactory.CreateSharpDevelop();
			var segmentDict = SegmentTrackingOutputFormatter.WriteNode(w, entityDeclaration, formattingOptions, context.Editor.Options);
			
			using (document.OpenUndoGroup()) {
				InsertionContext insertionContext = new InsertionContext(context.Editor.GetService(typeof(TextArea)) as TextArea, declarationBegin);
				insertionContext.InsertionPosition = context.Editor.Caret.Offset;
				
				string newText = w.ToString().TrimEnd();
				document.Replace(declarationBegin, context.EndOffset - declarationBegin, newText);
				var throwStatement = entityDeclaration.Descendants.FirstOrDefault(n => n is ThrowStatement);
				if (throwStatement != null) {
					var segment = segmentDict[throwStatement];
					context.Editor.Select(declarationBegin + segment.Offset, segment.Length);
				}
				CSharpFormatterHelper.Format(context.Editor, declarationBegin, newText.Length, formattingOptions);
				
				var refactoringContext = SDRefactoringContext.Create(context.Editor, CancellationToken.None);
				var typeResolveContext = refactoringContext.GetTypeResolveContext();
				if (typeResolveContext == null) {
					return;
				}
				var resolvedCurrent = typeResolveContext.CurrentTypeDefinition;
				var entities = FindFieldsAndProperties(resolvedCurrent).ToList();
				if (entities.Any()) {
					IEditorUIService uiService = context.Editor.GetService(typeof(IEditorUIService)) as IEditorUIService;
					
					ITextAnchor endAnchor = context.Editor.Document.CreateAnchor(context.Editor.Caret.Offset);
					endAnchor.MovementType = AnchorMovementType.AfterInsertion;
					
					ITextAnchor startAnchor = context.Editor.Document.CreateAnchor(context.Editor.Caret.Offset);
					startAnchor.MovementType = AnchorMovementType.BeforeInsertion;
					
					ITextAnchor insertionPos = context.Editor.Document.CreateAnchor(endAnchor.Offset);
					insertionPos.MovementType = AnchorMovementType.BeforeInsertion;

					AbstractInlineRefactorDialog dialog = new OverrideToStringMethodDialog(insertionContext, context.Editor, insertionPos, entities, baseCallStatement);
					dialog.Element = uiService.CreateInlineUIElement(insertionPos, dialog);
					
					insertionContext.RegisterActiveElement(new InlineRefactorSnippetElement(cxt => null, ""), dialog);
				} else {
					if (baseCallStatement != null) {
						// Add default base call
						MethodDeclaration insertedOverrideMethod = refactoringContext.GetNode().PrevSibling as MethodDeclaration;
						if (insertedOverrideMethod == null)
						{
							// We are not inside of a method declaration
							return;
						}
						using (Script script = refactoringContext.StartScript()) {
							script.AddTo(insertedOverrideMethod.Body, baseCallStatement);
						}
					}
				}
				
				insertionContext.RaiseInsertionCompleted(EventArgs.Empty);
			}
		}
示例#44
0
 public SnippetInputHandler(InsertionContext context)
     : base(context.TextArea)
 {
     this.context = context;
 }
示例#45
0
 /// <summary>
 /// Creates a new AnchorElement.
 /// </summary>
 public AnchorElement(AnchorSegment segment, string name, InsertionContext context)
 {
     this.segment = segment;
     this.context = context;
     this.Name    = name;
 }
示例#46
0
 public SnippetInputHandler(InsertionContext context)
     : base(context.TextArea)
 {
     this.context = context;
 }
示例#47
0
 public ReplaceableActiveElement(InsertionContext context, int startOffset, int endOffset)
 {
     this.context     = context;
     this.startOffset = startOffset;
     this.endOffset   = endOffset;
 }
示例#48
0
 /// <summary>
 /// Performs insertion of the snippet.
 /// </summary>
 public abstract void Insert(InsertionContext context);