void DisplayTooltip(MouseEventArgs e) { int line = GetLineFromMousePosition(e); if (line == 0) { return; } int startLine; bool added; string oldText = changeWatcher.GetOldVersionFromLine(line, out startLine, out added); TextEditor editor = this.TextView.GetService <TextEditor>(); markerService = this.TextView.GetService <ITextMarkerService>(); LineChangeInfo zeroLineInfo = changeWatcher.GetChange(0); int offset, length; bool hasNewVersion = changeWatcher.GetNewVersionFromLine(line, out offset, out length); if (line == 1 && zeroLineInfo.Change == ChangeType.Deleted) { int zeroStartLine; bool zeroAdded; startLine = 1; string deletedText = changeWatcher.GetOldVersionFromLine(0, out zeroStartLine, out zeroAdded); var docLine = editor.Document.GetLineByNumber(line); string newLine = DocumentUtilities.GetLineTerminator(changeWatcher.CurrentDocument, 1); deletedText += newLine; deletedText += editor.Document.GetText(docLine.Offset, docLine.Length); if (oldText != null) { oldText = deletedText + newLine + oldText; } else { oldText = deletedText; } if (!hasNewVersion) { offset = 0; length = docLine.Length; hasNewVersion = true; } } if (hasNewVersion) { if (marker != null) { markerService.Remove(marker); } if (length <= 0) { marker = null; length = 0; } else { marker = markerService.Create(offset, length); marker.BackgroundColor = Colors.LightGreen; } } if (oldText != null) { LineChangeInfo currLineInfo = changeWatcher.GetChange(startLine); if (currLineInfo.Change == ChangeType.Deleted && !(line == 1 && zeroLineInfo.Change == ChangeType.Deleted)) { var docLine = editor.Document.GetLineByNumber(startLine); if (docLine.DelimiterLength == 0) { oldText = DocumentUtilities.GetLineTerminator(changeWatcher.CurrentDocument, startLine) + oldText; } oldText = editor.Document.GetText(docLine.Offset, docLine.TotalLength) + oldText; } DiffControl differ = new DiffControl(); differ.CopyEditorSettingsAndHighlighting(editor); differ.editor.Document.Text = oldText; if (oldText == string.Empty) { differ.editor.Visibility = Visibility.Collapsed; differ.copyButton.Visibility = Visibility.Collapsed; } else { if (differ.editor.SyntaxHighlighting != null) { var baseDocument = new ReadOnlyDocument(changeWatcher.BaseDocument, TextView.Document.FileName); var mainHighlighter = new DocumentHighlighter(baseDocument, differ.editor.SyntaxHighlighting); var popupHighlighter = differ.editor.TextArea.GetService(typeof(IHighlighter)) as DocumentHighlighter; popupHighlighter.InitialSpanStack = mainHighlighter.GetSpanStack(currLineInfo.OldStartLineNumber); } } differ.revertButton.Click += delegate { if (hasNewVersion) { Document.Replace(offset, length, oldText); tooltip.IsOpen = false; } }; const double borderThickness = 1; tooltip.Child = new Border { Child = differ, BorderBrush = editor.TextArea.Foreground, BorderThickness = new Thickness(borderThickness) }; if (tooltip.IsOpen) { tooltip.IsOpen = false; } tooltip.Closed += delegate { if (marker != null) { markerService.Remove(marker); } }; tooltip.HorizontalOffset = -borderThickness - TextView.ScrollOffset.X; tooltip.VerticalOffset = TextView.GetVisualTopByDocumentLine(startLine) - TextView.ScrollOffset.Y; tooltip.Placement = PlacementMode.Top; tooltip.PlacementTarget = this.TextView; tooltip.IsOpen = true; } }
void DisplayTooltip(MouseEventArgs e) { int line = GetLineFromMousePosition(e); if (line == 0) { return; } int startLine; bool added; string oldText = changeWatcher.GetOldVersionFromLine(line, out startLine, out added); TextEditor editor = this.TextView.Services.GetService(typeof(TextEditor)) as TextEditor; markerService = this.TextView.Services.GetService(typeof(ITextMarkerService)) as ITextMarkerService; int offset, length; bool hasNewVersion = changeWatcher.GetNewVersionFromLine(line, out offset, out length); if (hasNewVersion) { if (marker != null) { markerService.Remove(marker); } if (length <= 0) { marker = null; length = 0; } else { marker = markerService.Create(offset, length); marker.BackgroundColor = Colors.LightGreen; } } if (oldText != null) { DiffControl differ = new DiffControl(); differ.editor.SyntaxHighlighting = editor.SyntaxHighlighting; differ.editor.HorizontalScrollBarVisibility = ScrollBarVisibility.Hidden; differ.editor.VerticalScrollBarVisibility = ScrollBarVisibility.Hidden; differ.editor.Document.Text = oldText; differ.Background = Brushes.White; // TODO : deletions on line 0 cannot be displayed. LineChangeInfo prevLineInfo = changeWatcher.GetChange(startLine - 1); LineChangeInfo lineInfo = changeWatcher.GetChange(startLine); if (prevLineInfo.Change == ChangeType.Deleted) { var docLine = editor.Document.GetLineByNumber(startLine - 1); differ.editor.Document.Insert(0, editor.Document.GetText(docLine.Offset, docLine.TotalLength)); } if (oldText == string.Empty) { differ.editor.Visibility = Visibility.Collapsed; differ.copyButton.Visibility = Visibility.Collapsed; } else { var baseDocument = new TextDocument(changeWatcher.BaseDocument.Text); if (differ.editor.SyntaxHighlighting != null) { var mainHighlighter = new DocumentHighlighter(baseDocument, differ.editor.SyntaxHighlighting.MainRuleSet); var popupHighlighter = differ.editor.TextArea.GetService(typeof(IHighlighter)) as DocumentHighlighter; if (prevLineInfo.Change == ChangeType.Deleted) { popupHighlighter.InitialSpanStack = mainHighlighter.GetSpanStack(prevLineInfo.OldStartLineNumber); } else { popupHighlighter.InitialSpanStack = mainHighlighter.GetSpanStack(lineInfo.OldStartLineNumber); } } } differ.revertButton.Click += delegate { if (hasNewVersion) { int delimiter = 0; DocumentLine l = Document.GetLineByOffset(offset + length); if (added) { delimiter = l.DelimiterLength; } if (length == 0) { oldText += DocumentUtilitites.GetLineTerminator(new AvalonEditDocumentAdapter(Document, null), l.LineNumber); } Document.Replace(offset, length + delimiter, oldText); tooltip.IsOpen = false; } }; tooltip.Child = new Border() { Child = differ, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) }; if (tooltip.IsOpen) { tooltip.IsOpen = false; } tooltip.IsOpen = true; tooltip.Closed += delegate { if (marker != null) { markerService.Remove(marker); } }; tooltip.HorizontalOffset = -10; tooltip.VerticalOffset = TextView.GetVisualTopByDocumentLine(startLine) - TextView.ScrollOffset.Y; tooltip.Placement = PlacementMode.Top; tooltip.PlacementTarget = this.TextView; } }