Example #1
0
        public CaretPosition MoveTo(VirtualSnapshotPoint bufferPosition, PositionAffinity caretAffinity) {
            _position = bufferPosition.Position.Position;

            return new CaretPosition(bufferPosition,
                  new MappingPointMock(bufferPosition.Position.Snapshot.TextBuffer, bufferPosition.Position),
                  PositionAffinity.Successor);
        }
Example #2
0
		public TextCaret(IWpfTextView textView, IAdornmentLayer caretLayer, ISmartIndentationService smartIndentationService, IClassificationFormatMap classificationFormatMap) {
			if (textView == null)
				throw new ArgumentNullException(nameof(textView));
			if (caretLayer == null)
				throw new ArgumentNullException(nameof(caretLayer));
			if (smartIndentationService == null)
				throw new ArgumentNullException(nameof(smartIndentationService));
			if (classificationFormatMap == null)
				throw new ArgumentNullException(nameof(classificationFormatMap));
			this.textView = textView;
			imeState = new ImeState();
			this.smartIndentationService = smartIndentationService;
			preferredXCoordinate = 0;
			__preferredYCoordinate = 0;
			Affinity = PositionAffinity.Successor;
			currentPosition = new VirtualSnapshotPoint(textView.TextSnapshot, 0);
			textView.TextBuffer.ChangedHighPriority += TextBuffer_ChangedHighPriority;
			textView.TextBuffer.ContentTypeChanged += TextBuffer_ContentTypeChanged;
			textView.Options.OptionChanged += Options_OptionChanged;
			textView.VisualElement.AddHandler(UIElement.GotKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(VisualElement_GotKeyboardFocus), true);
			textView.VisualElement.AddHandler(UIElement.LostKeyboardFocusEvent, new KeyboardFocusChangedEventHandler(VisualElement_LostKeyboardFocus), true);
			textView.LayoutChanged += TextView_LayoutChanged;
			textCaretLayer = new TextCaretLayer(this, caretLayer, classificationFormatMap);
			InputMethod.SetIsInputMethodSuspended(textView.VisualElement, true);
		}
        protected override Action GetNewCaretPositionSetter()
        {
            var currentSnapshot = TextView.TextBuffer.CurrentSnapshot;

            var caretPos = TextView.Caret.Position.BufferPosition;
            var caretLine = currentSnapshot.GetLineFromPosition(caretPos.Position);
            int line = currentSnapshot.GetLineNumberFromPosition(caretPos);
            int column = caretPos - caretLine.Start;

            // Get start line of scroll bar
            var scrollBarLine = TextView.TextViewLines.FirstOrDefault(l => l.VisibilityState != VisibilityState.Hidden);
            int scrollBarPos = (scrollBarLine == null) ? 0 : currentSnapshot.GetLineNumberFromPosition(scrollBarLine.Start);
            int maxLine = currentSnapshot.LineCount;

            Action setNewCaretPosition = () =>
                {
                    var newCurrentSnapshot = TextView.TextBuffer.CurrentSnapshot;
                    int newMaxLine = newCurrentSnapshot.LineCount;

                    // Scale caret positions in a linear way
                    int newLine = (int) ((float)line * (float)newMaxLine / (float)maxLine);
                    var newCaretPos = newCurrentSnapshot.GetLineFromLineNumber(newLine).Start.Add(column);
                    var caretPoint = new VirtualSnapshotPoint(newCurrentSnapshot, newCaretPos);
                    TextView.Caret.MoveTo(caretPoint);

                    // Assume that the document scales in a linear way
                    int newScrollBarPos = (int) ((float)scrollBarPos * (float)newMaxLine / (float)maxLine);
                    TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, newScrollBarPos);
                };

            return setNewCaretPosition;
        }
			public MouseReferenceInfo(SpanData<ReferenceInfo>? spanData, SpanData<ReferenceInfo>? realSpanData, VirtualSnapshotPoint point) {
				SpanData = spanData;
				RealSpanData = realSpanData;
				virtualSpaces = point.VirtualSpaces;
				position = point.Position.Position;
				versionNumber = point.Position.Snapshot.Version.VersionNumber;
			}
        private void CreateVisuals(VirtualSnapshotPoint caretPosition)
        {
            if ( !settings.CurrentColumnHighlightEnabled ) {
            return; // not enabled
              }
              IWpfTextViewLineCollection textViewLines = view.TextViewLines;
              if ( textViewLines == null )
            return; // not ready yet.
              // make sure the caret position is on the right buffer snapshot
              if ( caretPosition.Position.Snapshot != this.view.TextBuffer.CurrentSnapshot )
            return;

              var line = this.view.GetTextViewLineContainingBufferPosition(
            caretPosition.Position
            );
              var charBounds = line.GetCharacterBounds(caretPosition);

              this.columnRect.Width = charBounds.Width;
              this.columnRect.Height = this.view.ViewportHeight;
              if ( this.columnRect.Height > 2 ) {
            this.columnRect.Height -= 2;
              }

              // Align the image with the top of the bounds of the text geometry
              Canvas.SetLeft(this.columnRect, charBounds.Left);
              Canvas.SetTop(this.columnRect, this.view.ViewportTop);

              layer.AddAdornment(
             AdornmentPositioningBehavior.OwnerControlled, null,
             CUR_COL_TAG, columnRect, null
              );
        }
 public void AddOneOnSameLine1()
 {
     Create("dog cat");
     var point = new VirtualSnapshotPoint(_buffer.GetPoint(0));
     point = VirtualSnapshotPointUtil.AddOneOnSameLine(point);
     Assert.AreEqual(_buffer.GetPoint(1), point.Position);
     Assert.IsFalse(point.IsInVirtualSpace);
 }
Example #7
0
 internal static Range.pos GetFSharpPos(VirtualSnapshotPoint point)
 {
     var containingLine = point.Position.GetContainingLine();
     // F# compiler line numbers start at 1
     int lineNumber = containingLine.LineNumber + 1;
     int charIndex = point.Position.Position - containingLine.Start.Position;
     return Range.mkPos(lineNumber, charIndex);
 }
		bool IsInSelection(VirtualSnapshotPoint point) {
			if (wpfTextView.Selection.IsEmpty)
				return false;
			foreach (var span in wpfTextView.Selection.VirtualSelectedSpans) {
				if (span.Contains(point))
					return true;
			}
			return false;
		}
 public void AddOneOnSameLine3()
 {
     Create("dog");
     var point = new VirtualSnapshotPoint(_buffer.GetLine(0).EndIncludingLineBreak, 1);
     point = VirtualSnapshotPointUtil.AddOneOnSameLine(point);
     Assert.AreEqual(_buffer.GetLine(0).EndIncludingLineBreak, point.Position);
     Assert.IsTrue(point.IsInVirtualSpace);
     Assert.AreEqual(2, point.VirtualSpaces);
 }
Example #10
0
		MouseLocation(ITextViewLine textViewLine, VirtualSnapshotPoint position, Point point) {
			if (textViewLine == null)
				throw new ArgumentNullException(nameof(textViewLine));
			TextViewLine = textViewLine;
			Position = position;
			Affinity = textViewLine.IsLastTextViewLineForSnapshotLine || position.Position != textViewLine.End ? PositionAffinity.Successor : PositionAffinity.Predecessor;
			Debug.Assert(position.VirtualSpaces == 0 || Affinity == PositionAffinity.Successor);
			Point = point;
		}
Example #11
0
        public void MoveCaretToVirtualPoint1()
        {
            var buffer = EditorUtil.CreateBuffer("foo","bar");
            var caret = MockObjectFactory.CreateCaret();
            var textView = MockObjectFactory.CreateTextView(buffer:buffer, caret:caret.Object);
            var point = new VirtualSnapshotPoint(buffer.GetLine(0), 2);

            caret.Setup(x => x.MoveTo(point)).Returns(new CaretPosition()).Verifiable();
            caret.Setup(x => x.EnsureVisible()).Verifiable();
            TextViewUtil.MoveCaretToVirtualPoint(textView.Object, point);
            caret.Verify();
        }
        protected override string GetFormatted(bool isSignatureFile, string source, Fantomas.FormatConfig.FormatConfig config)
        {
            if (isFormattingCursor)
            {
                var caretPos = new VirtualSnapshotPoint(TextView.TextBuffer.CurrentSnapshot, TextView.Caret.Position.BufferPosition);
                Range.pos pos = TextUtils.GetFSharpPos(caretPos);
                return Fantomas.CodeFormatter.formatAroundCursor(isSignatureFile, pos, source, config);
            }

            Range.pos startPos = TextUtils.GetFSharpPos(TextView.Selection.Start);
            Range.pos endPos = TextUtils.GetFSharpPos(TextView.Selection.End);
            Range.range range = Range.mkRange("fsfile", startPos, endPos);

            return Fantomas.CodeFormatter.formatSelectionFromString(isSignatureFile, range, source, config);
        }
Example #13
0
		public static Geometry CreateGeometry(IWpfTextView textView, VirtualSnapshotSpan span, bool isMultiLine, bool clipToViewport = false) {
			var padding = isMultiLine ? LineMarkerPadding : TextMarkerPadding;
			var pos = span.Start;
			PathGeometry geo = null;
			bool createOutlinedPath = false;

			while (pos <= span.End) {
				var line = textView.GetTextViewLineContainingBufferPosition(pos.Position);
				bool useVspaces = line.IsLastDocumentLine();
				var lineExtent = new VirtualSnapshotSpan(new VirtualSnapshotPoint(line.Start), new VirtualSnapshotPoint(line.EndIncludingLineBreak, useVspaces ? span.End.VirtualSpaces : 0));
				var extentTmp = lineExtent.Intersection(new VirtualSnapshotSpan(pos, span.End));
				Debug.Assert(extentTmp != null);
				if (line.VisibilityState != VisibilityState.Unattached && extentTmp != null && extentTmp.Value.Length != 0) {
					var extent = extentTmp.Value;
					Collection<TextBounds> textBounds;
					if (extent.Start.IsInVirtualSpace) {
						var leading = line.TextRight + extent.Start.VirtualSpaces * textView.FormattedLineSource.ColumnWidth;
						double width = line.EndOfLineWidth;
						int vspaces = span.End.VirtualSpaces - span.Start.VirtualSpaces;
						if (vspaces > 0)
							width = vspaces * textView.FormattedLineSource.ColumnWidth;
						textBounds = new Collection<TextBounds>();
						textBounds.Add(new TextBounds(leading, line.Top, width, line.Height, line.TextTop, line.TextHeight));
					}
					else if (extent.End.IsInVirtualSpace) {
						textBounds = line.GetNormalizedTextBounds(extent.SnapshotSpan);
						double width = extent.End.VirtualSpaces * textView.FormattedLineSource.ColumnWidth;
						textBounds.Add(new TextBounds(line.TextRight, line.Top, width, line.Height, line.TextTop, line.TextHeight));
					}
					else
						textBounds = line.GetNormalizedTextBounds(extent.SnapshotSpan);
					AddGeometries(textView, textBounds, isMultiLine, clipToViewport, padding, SystemParameters.CaretWidth, ref geo, ref createOutlinedPath);
				}

				if (line.IsLastDocumentLine())
					break;
				pos = new VirtualSnapshotPoint(line.GetPointAfterLineBreak());
			}
			if (createOutlinedPath)
				geo = geo.GetOutlinedPathGeometry();
			if (geo != null && geo.CanFreeze)
				geo.Freeze();
			return geo;
		}
        protected override Action GetNewCaretPositionSetter()
        {
            var caretPos = TextView.Caret.Position.BufferPosition;

            if (isFormattingCursor || caretPos == TextView.Selection.Start.Position)
            {
                int selStartPos = TextView.Selection.Start.Position.Position;

                // Get start line of scroll bar
                var scrollBarLine = TextView.TextViewLines.FirstOrDefault(l => l.VisibilityState != VisibilityState.Hidden);
                int scrollBarPos = (scrollBarLine == null) ? 0 : scrollBarLine.Snapshot.GetLineNumberFromPosition(scrollBarLine.Start);

                Action setNewCaretPosition = () =>
                {
                    // The caret is at the start of selection, its position is unchanged
                    int newSelStartPos = selStartPos;
                    var newActivePoint = new VirtualSnapshotPoint(TextView.TextBuffer.CurrentSnapshot, newSelStartPos);
                    TextView.Caret.MoveTo(newActivePoint);
                    TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, scrollBarPos);
                };

                return setNewCaretPosition;
            }
            else
            {
                int selOffsetFromEnd = TextView.TextBuffer.CurrentSnapshot.Length - TextView.Selection.End.Position.Position;

                // Get start line of scroll bar
                var scrollBarLine = TextView.TextViewLines.FirstOrDefault(l => l.VisibilityState != VisibilityState.Hidden);
                int scrollBarPos = (scrollBarLine == null) ? 0 : scrollBarLine.Snapshot.GetLineNumberFromPosition(scrollBarLine.Start);

                Action setNewCaretPosition = () =>
                    {
                        // The caret is at the end of selection, its offset from the end of text is unchanged
                        int newSelEndPos = TextView.TextBuffer.CurrentSnapshot.Length - selOffsetFromEnd;
                        var newAnchorPoint = new VirtualSnapshotPoint(TextView.TextBuffer.CurrentSnapshot, newSelEndPos);

                        TextView.Caret.MoveTo(newAnchorPoint);
                        TextView.ViewScroller.ScrollViewportVerticallyByLines(ScrollDirection.Down, scrollBarPos);
                    };

                return setNewCaretPosition;
            }
        }
Example #15
0
        public void MoveCaretToVirtualPoint()
        {
            var buffer = CreateTextBuffer("foo","bar");
            var factory = new MockRepository(MockBehavior.Strict);
            var caret = MockObjectFactory.CreateCaret(factory: factory);
            caret.Setup(x => x.EnsureVisible()).Verifiable();

            var selection = MockObjectFactory.CreateSelection(factory: factory);
            selection.Setup(x => x.Clear()).Verifiable();

            var textView = MockObjectFactory.CreateTextView(
                textBuffer: buffer,
                selection: selection.Object,
                caret: caret.Object,
                factory: factory);
            var point = new VirtualSnapshotPoint(buffer.GetLine(0), 2);
            caret.Setup(x => x.MoveTo(point)).Returns(new CaretPosition()).Verifiable();

            TextViewUtil.MoveCaretToVirtualPoint(textView.Object, point);
            factory.Verify();
        }
        public IExecuteResult Execute(IEnumerable<Node> enodes, IWpfTextView textView, DragDropInfo dragDropInfo)
        {
            var dropLocation = GetDropLocation();
            var nodes = new List<Node>(enodes);
            normaliseNamespaces(dropLocation, nodes);

            var dropAction = getDropAction(nodes, dragDropInfo, dropLocation);

            if (dropAction != null)
            {
                // store the start buffer position
                var droppedPosition = dragDropInfo.VirtualBufferPosition.Position.Position;

                var result =  dropAction.Execute(nodes, textView, dragDropInfo);

                if (result.SelectAfterDrop)
                {

                    var startLine = textView.TextSnapshot.GetLineFromPosition(droppedPosition);
                    if (result.SelectionStartLine > 0)
                    {
                        startLine = textView.TextSnapshot.GetLineFromLineNumber(result.SelectionStartLine);
                    }
                    var start = new VirtualSnapshotPoint(startLine,Math.Max(0,  result.SelectionStartInChars));

                    var endLine = startLine;
                    if (result.SelectionHeightInLines > 1)
                       endLine= textView.TextSnapshot.GetLineFromLineNumber(startLine.LineNumber +
                                                                    result.SelectionHeightInLines - 1);

                    var end = new VirtualSnapshotPoint(endLine, result.SelectionWidthInChars + result.SelectionStartInChars );
                    textView.Selection.Mode = TextSelectionMode.Box;

                    textView.Selection.Select(start, end);
                }
                textView.Caret.MoveTo(textView.Selection.End);

            }
            return ExecuteResult.None ;
        }
        protected CaretHandlerBase(VirtualSnapshotPoint location, int tabSize)
        {
            if (location.Position.Snapshot != null) {
                var line = location.Position.GetContainingLine();
                LineNumber = line.LineNumber;
                Position = location.Position.Position - line.Start.Position + location.VirtualSpaces;

                int bufferIndent = 0;
                int visualIndent = 0;
                foreach (var c in line.GetText().Take(Position)) {
                    if (c == '\t')
                        bufferIndent += tabSize - (bufferIndent % tabSize);
                    else if (c == ' ')
                        bufferIndent += 1;
                    else
                        break;
                    visualIndent += 1;
                }
                Position += bufferIndent - visualIndent;
                Modified = new List<LineSpan>();
            }
        }
Example #18
0
		List<VirtualSnapshotSpan> GetSelectedSpans() {
			var list = new List<VirtualSnapshotSpan>();

			if (Mode == TextSelectionMode.Stream)
				list.Add(StreamSelectionSpan);
			else {
				var helper = new BoxSelectionHelper(this);
				var start = Start;
				while (start <= End) {
					var line = TextView.GetTextViewLineContainingBufferPosition(start.Position);
					list.Add(helper.GetSpan(line));
					if (line.IsLastDocumentLine())
						break;
					start = new VirtualSnapshotPoint(line.GetPointAfterLineBreak());
				}
			}

			// At least one span must be included, even if the span's empty
			if (list.Count == 0)
				list.Add(StreamSelectionSpan);

			return list;
		}
Example #19
0
		string TryGetSearchStringAtPoint(VirtualSnapshotPoint point) {
			if (point.IsInVirtualSpace)
				return null;

			var extent = textStructureNavigator.GetExtentOfWord(point.Position);
			if (extent.IsSignificant)
				return extent.Span.GetText();

			var line = point.Position.GetContainingLine();
			if (line.Start == point.Position)
				return null;

			extent = textStructureNavigator.GetExtentOfWord(point.Position - 1);
			if (extent.IsSignificant)
				return extent.Span.GetText();

			return null;
		}
 public TextBounds GetExtendedCharacterBounds(VirtualSnapshotPoint bufferPosition) {
     throw new NotImplementedException();
 }
Example #21
0
 public void NavigateToPoint_InOtherBuffer()
 {
     Create("hello world");
     var textBuffer = CreateTextBuffer("cat");
     var point = new VirtualSnapshotPoint(textBuffer.GetPoint(1));
     _vimHost.Setup(x => x.NavigateTo(point)).Returns(true).Verifiable();
     _operations.NavigateToPoint(point);
     _vimHost.Verify();
 }
Example #22
0
 public abstract bool NavigateTo(VirtualSnapshotPoint point);
Example #23
0
 bool IVimHost.NavigateTo(VirtualSnapshotPoint point)
 {
     return NavigateTo(point);
 }
Example #24
0
		void InitializeState(EditorPositionState state) {
			var textView = documentViewerControl.TextView;

			if (IsValid(state)) {
				textView.ViewportLeft = state.ViewportLeft;
				textView.DisplayTextLineContainingBufferPosition(new SnapshotPoint(textView.TextSnapshot, state.TopLinePosition), state.TopLineVerticalDistance, ViewRelativePosition.Top);
				var newPos = new VirtualSnapshotPoint(new SnapshotPoint(textView.TextSnapshot, state.CaretPosition), state.CaretVirtualSpaces);
				textView.Caret.MoveTo(newPos, state.CaretAffinity, true);
			}
			else
				textView.Caret.MoveTo(new VirtualSnapshotPoint(textView.TextSnapshot, 0));
			textView.Selection.Clear();
		}
Example #25
0
 public void NavigateTo1()
 {
     Create();
     var buffer = CreateTextBuffer("foo", "bar");
     var point = new VirtualSnapshotPoint(buffer.CurrentSnapshot, 2);
     _textManager.Setup(x => x.NavigateTo(point)).Returns(true);
     _host.NavigateTo(new VirtualSnapshotPoint(buffer.CurrentSnapshot, 2));
     _textManager.Verify();
 }
Example #26
0
 bool ITextManager.NavigateTo(VirtualSnapshotPoint point)
 {
     return NavigateTo(point);
 }
Example #27
0
 internal bool NavigateTo(VirtualSnapshotPoint point)
 {
     var tuple = SnapshotPointUtil.GetLineColumn(point.Position);
     var line = tuple.Item1;
     var column = tuple.Item2;
     var vsBuffer = _vsAdapter.EditorAdapter.GetBufferAdapter(point.Position.Snapshot.TextBuffer);
     var viewGuid = VSConstants.LOGVIEWID_Code;
     var hr = _textManager.NavigateToLineAndColumn(
         vsBuffer,
         ref viewGuid,
         line,
         column,
         line,
         column);
     return ErrorHandler.Succeeded(hr);
 }
Example #28
0
 bool IVimHost.NavigateTo(VirtualSnapshotPoint point)
 {
     NavigateToData = point;
     return NavigateToReturn;
 }
Example #29
0
 public override bool NavigateTo(VirtualSnapshotPoint point)
 {
     return _textManager.NavigateTo(point);
 }
                public void Dispose()
                {
                    var textView = _openTextBufferManager.ActiveTextview;
                    if (textView == null)
                    {
                        return;
                    }

                    if (_anchor.HasValue || _active.HasValue)
                    {
                        var selection = textView.Selection;
                        var snapshot = _openTextBufferManager._subjectBuffer.CurrentSnapshot;

                        var anchorSpan = _anchorSpan;
                        var anchorPoint = new VirtualSnapshotPoint(textView.TextSnapshot,
                            _anchor.HasValue && _openTextBufferManager._referenceSpanToLinkedRenameSpanMap.Keys.Any(s => s.OverlapsWith(anchorSpan))
                            ? GetNewEndpoint(_anchorSpan) - _anchor.Value
                            : selection.AnchorPoint.Position);

                        var activeSpan = _activeSpan;
                        var activePoint = new VirtualSnapshotPoint(textView.TextSnapshot,
                            _active.HasValue && _openTextBufferManager._referenceSpanToLinkedRenameSpanMap.Keys.Any(s => s.OverlapsWith(activeSpan))
                            ? GetNewEndpoint(_activeSpan) - _active.Value
                            : selection.ActivePoint.Position);

                        textView.SetSelection(anchorPoint, activePoint);
                    }
                }