Exemple #1
0
		/// <summary>
		/// Don't use this unless you're implementing ICodeTemplateWidget. Use Insert instead.
		/// </summary>
		public TemplateResult InsertTemplateContents (TextEditor editor, DocumentContext context)
		{
			var data = editor;
			
			int offset = data.CaretOffset;
//			string leadingWhiteSpace = GetLeadingWhiteSpace (editor, editor.CursorLine);
			
			var templateCtx = new TemplateContext {
				Template = this,
				DocumentContext = context,
				Editor = editor,
				//ParsedDocument = context.ParsedDocument != null ? context.ParsedDocument.ParsedFile : null,
				InsertPosition = data.CaretLocation,
				LineIndent = data.GetLineIndent (data.CaretLocation.Line),
				TemplateCode = Code
			};

			if (data.IsSomethingSelected) {
				int start = data.SelectionRange.Offset;
				while (Char.IsWhiteSpace (data.GetCharAt (start))) {
					start++;
				}
				int end = data.SelectionRange.EndOffset;
				while (Char.IsWhiteSpace (data.GetCharAt (end - 1))) {
					end--;
				}
				templateCtx.LineIndent = data.GetLineIndent (data.OffsetToLineNumber (start));
				templateCtx.SelectedText = RemoveIndent (data.GetTextBetween (start, end), templateCtx.LineIndent);
				data.RemoveText (start, end - start);
				offset = start;
			} else {
				string word = GetTemplateShortcutBeforeCaret (data).Trim ();
				if (word.Length > 0)
					offset = DeleteTemplateShortcutBeforeCaret (data);
			}
			
			TemplateResult template = FillVariables (templateCtx);
			template.InsertPosition = offset;
			editor.InsertText (offset, template.Code);
			
			int newoffset;
			if (template.CaretEndOffset >= 0) {
				newoffset = offset + template.CaretEndOffset; 
			} else {
				newoffset = offset + template.Code.Length; 
			}

			editor.CaretLocation = editor.OffsetToLocation (newoffset) ;

			var prettyPrinter = CodeFormatterService.GetFormatter (data.MimeType);
			if (prettyPrinter != null && prettyPrinter.SupportsOnTheFlyFormatting) {
				int endOffset = template.InsertPosition + template.Code.Length;
				var oldVersion = data.Version;
				prettyPrinter.OnTheFlyFormat (editor, context, TextSegment.FromBounds (template.InsertPosition, endOffset));
				foreach (var textLink in template.TextLinks) {
					for (int i = 0; i < textLink.Links.Count; i++) {
						var segment = textLink.Links [i];
						var translatedOffset = oldVersion.MoveOffsetTo (data.Version, template.InsertPosition + segment.Offset) - template.InsertPosition;
						textLink.Links [i] = new TextSegment (translatedOffset, segment.Length);
					}
				}
			}
			return template;
		}
 public int InsertText(int position, string text)
 {
     data.InsertText(position, text);
     return(text.Length);
 }
		protected virtual ICompletionDataList ClosingTagCompletion (TextEditor buf, DocumentLocation currentLocation)

		{

			//get name of current node in document that's being ended

			var el = tracker.Engine.Nodes.Peek () as XElement;

			if (el != null && el.Region.End >= currentLocation && !el.IsClosed && el.IsNamed) {

				string tag = String.Concat ("</", el.Name.FullName, ">");

				if (XmlEditorOptions.AutoCompleteElements) {



					//						//make sure we have a clean atomic undo so the user can undo the tag insertion

					//						//independently of the >

					//						bool wasInAtomicUndo = this.Editor.Document.IsInAtomicUndo;

					//						if (wasInAtomicUndo)

					//							this.Editor.Document.EndAtomicUndo ();



					using (var undo = buf.OpenUndoGroup ()) {

						buf.InsertText (buf.CaretOffset, tag);

						buf.CaretOffset -= tag.Length;

					}



					//						if (wasInAtomicUndo)

					//							this.Editor.Document.BeginAtomicUndo ();



					return null;

				} else {

					var cp = new CompletionDataList ();

					cp.Add (new XmlTagCompletionData (tag, 0, true));

					return cp;

				}

			}

			return null;
		}
		public void AddRegisterDirective (RegisterDirective directive, TextEditor editor, bool preserveCaretPosition)
		{
			var node = GetRegisterInsertionPointNode ();
			if (node == null)
				return;
			
			Doc.Info.RegisteredTags.Add (directive);
			
			var line = Math.Max (node.Location.EndLine, node.Location.BeginLine);
			var pos = editor.GetPositionFromLineColumn (line, editor.GetLineLength (line) + 1);
			if (pos < 0)
				return;
			
			editor.BeginAtomicUndo ();
			var oldCaret = editor.CursorPosition;
			
			var inserted = editor.InsertText (pos, editor.NewLine + directive.ToString ());
			if (preserveCaretPosition) {
				editor.CursorPosition = (pos < oldCaret)? oldCaret + inserted : oldCaret;
			}
			editor.EndAtomicUndo ();
		}