Skip to content

Commit

Permalink
Sync selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Gillibald committed Jul 6, 2023
1 parent 2c1e2aa commit 9fce6f2
Showing 1 changed file with 23 additions and 29 deletions.
52 changes: 23 additions & 29 deletions src/AvaloniaEdit/Editing/TextArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static TextArea()
if (!ta.IsReadOnly)
{
e.Client = ta._imClient;
}
}
});
}

Expand Down Expand Up @@ -627,7 +627,7 @@ public IDisposable AllowCaretOutsideSelection()
/// If the textview can be scrolled.
/// </summary>
/// <param name="line">The line to scroll to.</param>
public void ScrollToLine (int line)
public void ScrollToLine(int line)
{
var viewPortLines = (int)(this as IScrollable).Viewport.Height;

Expand Down Expand Up @@ -1146,8 +1146,8 @@ public TextAreaTextInputMethodClient()
public override Rect CursorRectangle
{
get
{
if(_textArea == null)
{
if (_textArea == null)
{
return default;
}
Expand Down Expand Up @@ -1175,7 +1175,7 @@ public override string SurroundingText
{
get
{
if(_textArea == null)
if (_textArea == null)
{
return default;
}
Expand All @@ -1190,56 +1190,50 @@ public override string SurroundingText
}
}

public override TextSelection Selection { get; set; }
public override TextSelection Selection
{
get => new TextSelection(_textArea.Caret.Position.Column, _textArea.Caret.Position.Column + _textArea.Selection.Length);
set
{
var selection = _textArea.Selection;

_textArea.Selection = selection.StartSelectionOrSetEndpoint(
new TextViewPosition(selection.StartPosition.Line, value.Start),
new TextViewPosition(selection.StartPosition.Line, value.End));
}
}

public void SetTextArea(TextArea textArea)
{
if(_textArea != null)
if (_textArea != null)
{
_textArea.Caret.PositionChanged -= Caret_PositionChanged;
_textArea.SelectionChanged -= TextArea_SelectionChanged;
}

_textArea = textArea;

if(_textArea != null)
if (_textArea != null)
{
_textArea.Caret.PositionChanged += Caret_PositionChanged;
_textArea.SelectionChanged += TextArea_SelectionChanged;
}

RaiseTextViewVisualChanged();

RaiseCursorRectangleChanged();

RaiseSurroundingTextChanged();
}

private void Caret_PositionChanged(object sender, EventArgs e)
{
RaiseCursorRectangleChanged();
}

private void TextArea_SelectionChanged(object sender, EventArgs e)
{
RaiseSurroundingTextChanged();
}

public void SelectInSurroundingText(int start, int end)
{
if(_textArea == null)
{
return;
}

var selection = _textArea.Selection;

_textArea.Selection = _textArea.Selection.StartSelectionOrSetEndpoint(
new TextViewPosition(selection.StartPosition.Line, start),
new TextViewPosition(selection.StartPosition.Line, end));
RaiseSelectionChanged();
}

public override void SetPreeditText(string text)
{

}
}
}
Expand Down

0 comments on commit 9fce6f2

Please sign in to comment.