Changeset 11743


Ignore:
Timestamp:
01/09/15 16:42:32 (7 years ago)
Author:
jkarder
Message:

#2077: improved line moving

Location:
branches/CodeEditor
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/CodeEditor/HeuristicLab.CodeEditor/3.4/CodeEditor.cs

    r11741 r11743  
    2424using System.Collections.Generic;
    2525using System.ComponentModel;
    26 using System.Linq;
    2726using System.Reflection;
    2827using System.Threading.Tasks;
     
    5453    private const double DefaultTextEditorFontSize = 13.0;
    5554    private const bool DefaultTextEditorShowLineNumbers = true;
    56     private const bool DefaultTextEditorShowEndOfLine = true;
    5755    private const bool DefaultTextEditorShowSpaces = true;
    5856    private const bool DefaultTextEditorShowTabs = true;
     
    134132    }
    135133
    136     [DefaultValue(DefaultTextEditorShowEndOfLine)]
    137     public bool TextEditorShowEndOfLine {
    138       get { return TextEditor.Options.ShowEndOfLine; }
    139       set { TextEditor.Options.ShowEndOfLine = value; }
    140     }
    141 
    142134    [DefaultValue(DefaultTextEditorShowSpaces)]
    143135    public bool TextEditorShowSpaces {
     
    230222      TextEditorSyntaxHighlighting = DefaultTextEditorSyntaxHighlighting;
    231223      TextEditorShowLineNumbers = DefaultTextEditorShowLineNumbers;
    232       TextEditorShowEndOfLine = DefaultTextEditorShowEndOfLine;
    233224      TextEditorShowSpaces = DefaultTextEditorShowSpaces;
    234225      TextEditorShowTabs = DefaultTextEditorShowTabs;
     
    241232      TextEditor.FontFamily = new Media.FontFamily(DefaultTextEditorFontFamily);
    242233      TextEditor.FontSize = DefaultTextEditorFontSize;
     234      TextEditor.Options.EnableVirtualSpace = true;
    243235
    244236      TextEditor.TextChanged += (sender, args) => {
     
    281273      var textArea = TextEditor.TextArea;
    282274      var selection = textArea.Selection;
    283 
    284       int selectionStart, selectionEnd;
    285       if (!(selection is RectangleSelection) && selection.IsEmpty) {
    286         selectionStart = selectionEnd = TextEditor.SelectionStart;
     275      var caret = textArea.Caret;
     276
     277      var selectionStartPosition = selection.StartPosition;
     278      var selectionEndPosition = selection.EndPosition;
     279      var caretPosition = caret.Position;
     280      int caretOffset = caret.Offset;
     281
     282      bool advancedPositionCalcualtion = selection is RectangleSelection || !selection.IsEmpty;
     283
     284      int selectionStartOffset, selectionEndOffset;
     285      if (advancedPositionCalcualtion) {
     286        if (selectionStartPosition.CompareTo(selectionEndPosition) > 0) {
     287          var temp = selectionStartPosition;
     288          selectionStartPosition = selectionEndPosition;
     289          selectionEndPosition = temp;
     290        }
     291        selectionStartOffset = Doc.GetOffset(selectionStartPosition.Location);
     292        selectionEndOffset = Doc.GetOffset(selectionEndPosition.Location);
    287293      } else {
    288         selectionStart = Doc.GetOffset(selection.StartPosition.Location);
    289         selectionEnd = Doc.GetOffset(selection.EndPosition.Location);
    290         if (selectionStart > selectionEnd) {
    291           int temp = selectionStart;
    292           selectionStart = selectionEnd;
    293           selectionEnd = temp;
    294         }
     294        selectionStartOffset = selectionEndOffset = TextEditor.SelectionStart;
    295295      }
    296296
    297297      int selectionLength = selection.Length;
    298       var selectionSegments = selection.Segments;
    299       int caretOffset = TextEditor.CaretOffset;
    300 
    301       var startLine = Doc.GetLineByOffset(selectionStart);
    302       var endLine = Doc.GetLineByOffset(selectionEnd);
     298
     299      var startLine = Doc.GetLineByOffset(selectionStartOffset);
     300      var endLine = Doc.GetLineByOffset(selectionEndOffset);
     301
     302      if (selection.IsMultiline && selectionEndOffset == endLine.Offset) {
     303        if (movementDirection == MovementDirection.Down && endLine.TotalLength == 0) return;
     304        endLine = endLine.PreviousLine;
     305      }
    303306
    304307      if (movementDirection == MovementDirection.Up && startLine.LineNumber == 1) return;
     
    312315      string secondaryText = Doc.GetText(secondaryLine.Offset, secondaryLine.Length);
    313316      string secondaryDelimiter = Doc.GetText(secondaryLine.EndOffset, secondaryLine.DelimiterLength);
     317
     318      if (string.IsNullOrEmpty(primaryText + primaryDelimiter) || string.IsNullOrEmpty(secondaryText + secondaryDelimiter)) return;
    314319
    315320      if (movementDirection == MovementDirection.Up) {
     
    317322        Doc.Replace(secondaryLine.Offset, replacementText.Length, replacementText);
    318323        int correctionLength = secondaryText.Length + secondaryDelimiter.Length;
    319         selectionStart -= correctionLength;
     324        selectionStartOffset -= correctionLength;
    320325        caretOffset -= correctionLength;
    321326      } else {
     
    323328        Doc.Replace(startLine.Offset, replacementText.Length, replacementText);
    324329        int correctionLength = secondaryText.Length + primaryDelimiter.Length;
    325         selectionStart += correctionLength;
     330        selectionStartOffset += correctionLength;
    326331        caretOffset += correctionLength;
    327332      }
    328333
    329       if (textArea.Selection is RectangleSelection) {
    330         textArea.ClearSelection();
    331         int lowestOffset = selectionSegments.Min(x => x.StartOffset);
    332         int highestOffset = selectionSegments.Max(x => x.EndOffset);
    333         selectionLength = highestOffset - lowestOffset;
    334         var startPosition = new TextViewPosition(Doc.GetLocation(selectionStart));
    335         var endPosition = new TextViewPosition(Doc.GetLocation(selectionStart + selectionLength));
    336         textArea.Selection = new RectangleSelection(textArea, startPosition, endPosition);
     334      if (advancedPositionCalcualtion) {
     335        var newSelectionStartLocation = Doc.GetLocation(selectionStartOffset);
     336        int selectionLineOffset = newSelectionStartLocation.Line - Math.Min(selectionStartPosition.Line, selectionEndPosition.Line);
     337        selectionStartPosition.Line += selectionLineOffset;
     338        selectionEndPosition.Line += selectionLineOffset;
     339        if (selectionEndPosition.Line > Doc.LineCount) {
     340          var newLocation = Doc.GetLocation(Doc.TextLength);
     341          selectionEndPosition.Line = newLocation.Line;
     342          selectionEndPosition.Column = newLocation.Column;
     343          selectionEndPosition.VisualColumn = newLocation.Column - 1;
     344          selectionEndPosition.IsAtEndOfLine = selectionStartPosition.IsAtEndOfLine; // actual value does not matter; needed for comparison
     345        }
     346
     347        if (selectionStartPosition == selectionEndPosition)
     348          textArea.ClearSelection();
     349        else {
     350          if (selection is RectangleSelection)
     351            textArea.Selection = new RectangleSelection(textArea, selectionStartPosition, selectionEndPosition);
     352          else
     353            textArea.Selection = new SimpleSelection(textArea, selectionStartPosition, selectionEndPosition);
     354        }
    337355      } else {
    338         TextEditor.SelectionStart = selectionStart;
     356        TextEditor.SelectionStart = selectionStartOffset;
    339357        TextEditor.SelectionLength = selectionLength;
    340358      }
    341359
    342       TextEditor.CaretOffset = caretOffset;
     360      var newCaretLocation = Doc.GetLocation(Math.Min(caretOffset, Doc.TextLength));
     361      var newCaretPosition = new TextViewPosition(newCaretLocation);
     362      if (caretPosition.VisualColumn > caretPosition.Column) {
     363        newCaretPosition.VisualColumn = caretPosition.VisualColumn;
     364      }
     365      caret.Position = newCaretPosition;
    343366    }
    344367
  • branches/CodeEditor/HeuristicLab.ExtLibs/HeuristicLab.AvalonEdit/5.0.1/AvalonEdit-5.0.1/Editing/SimpleSelection.cs

    r11700 r11743  
    3030  /// A simple selection.
    3131  /// </summary>
    32   sealed class SimpleSelection : Selection
     32  public sealed class SimpleSelection : Selection
    3333  {
    3434    readonly TextViewPosition start, end;
     
    3838    /// Creates a new SimpleSelection instance.
    3939    /// </summary>
    40     internal SimpleSelection(TextArea textArea, TextViewPosition start, TextViewPosition end)
     40    public SimpleSelection(TextArea textArea, TextViewPosition start, TextViewPosition end)
    4141      : base(textArea)
    4242    {
Note: See TracChangeset for help on using the changeset viewer.