|
| 1 | +// |
| 2 | +// TextViewController+MoveLines.swift |
| 3 | +// CodeEditSourceEditor |
| 4 | +// |
| 5 | +// Created by Bogdan Belogurov on 01/06/2025. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | + |
| 10 | +extension TextViewController { |
| 11 | + /// Moves the selected lines up by one line. |
| 12 | + public func moveLinesUp() { |
| 13 | + guard !cursorPositions.isEmpty else { return } |
| 14 | + |
| 15 | + textView.undoManager?.beginUndoGrouping() |
| 16 | + |
| 17 | + textView.editSelections { textView, selection in |
| 18 | + guard let lineIndexes = getOverlappingLines(for: selection.range) else { return } |
| 19 | + let lowerBound = lineIndexes.lowerBound |
| 20 | + guard lowerBound > .zero, |
| 21 | + let prevLineInfo = textView.layoutManager.textLineForIndex(lowerBound - 1), |
| 22 | + let prevString = textView.textStorage.substring(from: prevLineInfo.range), |
| 23 | + let lastSelectedString = textView.layoutManager.textLineForIndex(lineIndexes.upperBound) else { |
| 24 | + return |
| 25 | + } |
| 26 | + |
| 27 | + textView.insertString(prevString, at: lastSelectedString.range.upperBound) |
| 28 | + textView.replaceCharacters(in: [prevLineInfo.range], with: String()) |
| 29 | + |
| 30 | + let rangeToSelect = NSRange( |
| 31 | + start: prevLineInfo.range.lowerBound, |
| 32 | + end: lastSelectedString.range.location - prevLineInfo.range.length + lastSelectedString.range.length |
| 33 | + ) |
| 34 | + |
| 35 | + setCursorPositions([CursorPosition(range: rangeToSelect)], scrollToVisible: true) |
| 36 | + } |
| 37 | + |
| 38 | + textView.undoManager?.endUndoGrouping() |
| 39 | + } |
| 40 | + |
| 41 | + /// Moves the selected lines down by one line. |
| 42 | + public func moveLinesDown() { |
| 43 | + guard !cursorPositions.isEmpty else { return } |
| 44 | + |
| 45 | + textView.undoManager?.beginUndoGrouping() |
| 46 | + |
| 47 | + textView.editSelections { textView, selection in |
| 48 | + guard let lineIndexes = getOverlappingLines(for: selection.range) else { return } |
| 49 | + let totalLines = textView.layoutManager.lineCount |
| 50 | + let upperBound = lineIndexes.upperBound |
| 51 | + guard upperBound + 1 < totalLines, |
| 52 | + let nextLineInfo = textView.layoutManager.textLineForIndex(upperBound + 1), |
| 53 | + let nextString = textView.textStorage.substring(from: nextLineInfo.range), |
| 54 | + let firstSelectedString = textView.layoutManager.textLineForIndex(lineIndexes.lowerBound) else { |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + textView.replaceCharacters(in: [nextLineInfo.range], with: String()) |
| 59 | + textView.insertString(nextString, at: firstSelectedString.range.lowerBound) |
| 60 | + |
| 61 | + let rangeToSelect = NSRange( |
| 62 | + start: firstSelectedString.range.location + nextLineInfo.range.length, |
| 63 | + end: nextLineInfo.range.upperBound |
| 64 | + ) |
| 65 | + |
| 66 | + setCursorPositions([CursorPosition(range: rangeToSelect)], scrollToVisible: true) |
| 67 | + } |
| 68 | + |
| 69 | + textView.undoManager?.endUndoGrouping() |
| 70 | + } |
| 71 | +} |
0 commit comments