public void GetLineColumnFromPosition(int position, out int line, out int column)
        {
            var loc = data.OffsetToLocation(position);

            line   = loc.Line;
            column = loc.Column;
        }
Exemple #2
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 override void ShowTooltipWindow (TextEditor editor, Control tipWindow, TooltipItem item, Gdk.ModifierType modifierState, int mouseX, int mouseY)
		{
			var location = editor.OffsetToLocation (item.Offset);
			var point = editor.LocationToPoint (location);
			int lineHeight = (int) editor.LineHeight;
			int y = (int)point.Y;

			// find the top of the line that the mouse is hovering over
			while (y + lineHeight < mouseY)
				y += lineHeight;

			var caret = new Gdk.Rectangle (mouseX, y, 1, lineHeight);
			tooltip = (DebugValueWindow)tipWindow;
			tooltip.ShowPopup (editor, caret, PopupPosition.TopLeft);
		}
		protected void JumpToCurrentLocation (TextEditor editor)
		{
			if (version.BelongsToSameDocumentAs (editor.Version)) {
				var currentOffset = version.MoveOffsetTo (editor.Version, offset);
				var loc = editor.OffsetToLocation (currentOffset);
				editor.SetCaretLocation (loc);
			} else {
				editor.SetCaretLocation (Math.Max (line, 1), Math.Max (column, 1));
			}
		}