Skip to content

Commit

Permalink
Merge pull request #277 from AvaloniaUI/fix-276
Browse files Browse the repository at this point in the history
Fix triple click causes NRE
  • Loading branch information
danipen authored Oct 25, 2022
2 parents cf278a3 + f939891 commit 8719db6
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/AvaloniaEdit/Editing/SelectionMouseHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

using Avalonia;
using Avalonia.Input;

using AvaloniaEdit.Document;
using AvaloniaEdit.Utils;

using System;
using System.ComponentModel;
using System.Linq;
Expand Down Expand Up @@ -463,10 +465,6 @@ private void TextArea_MouseLeftButtonDown(object sender, PointerPressedEventArgs
else
{
SimpleSegment startWord;

_mode = SelectionMode.WholeWord;
startWord = GetWordAtMousePosition(e);

if (e.ClickCount == 3)
{
_mode = SelectionMode.WholeLine;
Expand Down Expand Up @@ -531,7 +529,7 @@ private SimpleSegment GetWordAtMousePosition(PointerEventArgs e)
pos = pos.WithY(textView.Bounds.Height);
pos += textView.ScrollOffset;
var line = textView.GetVisualLineFromVisualTop(pos.Y);
if (line != null)
if (line != null && line.TextLines != null)
{
var visualColumn = line.GetVisualColumn(pos, TextArea.Selection.EnableVirtualSpace);
var wordStartVc = line.GetNextCaretPosition(visualColumn + 1, LogicalDirection.Backward, CaretPositioningMode.WordStartOrSymbol, TextArea.Selection.EnableVirtualSpace);
Expand Down Expand Up @@ -562,7 +560,7 @@ private SimpleSegment GetLineAtMousePosition(PointerEventArgs e)
pos = pos.WithY(textView.Bounds.Height);
pos += textView.ScrollOffset;
var line = textView.GetVisualLineFromVisualTop(pos.Y);
return line != null
return line != null && line.TextLines != null
? new SimpleSegment(line.StartOffset, line.LastDocumentLine.EndOffset - line.StartOffset)
: SimpleSegment.Invalid;
}
Expand All @@ -585,7 +583,7 @@ private int GetOffsetFromMousePosition(Point positionRelativeToTextView, out int
if (pos.Y >= textView.DocumentHeight)
pos = pos.WithY(textView.DocumentHeight - ExtensionMethods.Epsilon);
var line = textView.GetVisualLineFromVisualTop(pos.Y);
if (line != null)
if (line != null && line.TextLines != null)
{
visualColumn = line.GetVisualColumn(pos, TextArea.Selection.EnableVirtualSpace, out isAtEndOfLine);
return line.GetRelativeOffset(visualColumn) + line.FirstDocumentLine.Offset;
Expand All @@ -607,7 +605,7 @@ private int GetOffsetFromMousePositionFirstTextLineOnly(Point positionRelativeTo
if (pos.Y >= textView.DocumentHeight)
pos = pos.WithY(textView.DocumentHeight - ExtensionMethods.Epsilon);
var line = textView.GetVisualLineFromVisualTop(pos.Y);
if (line != null)
if (line != null && line.TextLines != null)
{
visualColumn = line.GetVisualColumn(line.TextLines.First(), pos.X, TextArea.Selection.EnableVirtualSpace);
return line.GetRelativeOffset(visualColumn) + line.FirstDocumentLine.Offset;
Expand Down

0 comments on commit 8719db6

Please sign in to comment.