-
Notifications
You must be signed in to change notification settings - Fork 112
Add the ability to move selected lines up and down #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
thecoolwinter
merged 2 commits into
CodeEditApp:main
from
Bogdan-Belogurov:feat/lineUpAndDown
Jun 2, 2025
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
Sources/CodeEditSourceEditor/Controller/TextViewController+MoveLines.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // | ||
| // TextViewController+MoveLines.swift | ||
| // CodeEditSourceEditor | ||
| // | ||
| // Created by Bogdan Belogurov on 01/06/2025. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| extension TextViewController { | ||
| /// Moves the selected lines up by one line. | ||
| public func moveLinesUp() { | ||
| guard !cursorPositions.isEmpty else { return } | ||
|
|
||
| textView.undoManager?.beginUndoGrouping() | ||
|
|
||
| textView.editSelections { textView, selection in | ||
| guard let lineIndexes = getOverlappingLines(for: selection.range) else { return } | ||
| let lowerBound = lineIndexes.lowerBound | ||
| guard lowerBound > .zero, | ||
| let prevLineInfo = textView.layoutManager.textLineForIndex(lowerBound - 1), | ||
| let prevString = textView.textStorage.substring(from: prevLineInfo.range), | ||
| let lastSelectedString = textView.layoutManager.textLineForIndex(lineIndexes.upperBound) else { | ||
| return | ||
| } | ||
|
|
||
| textView.insertString(prevString, at: lastSelectedString.range.upperBound) | ||
| textView.replaceCharacters(in: [prevLineInfo.range], with: String()) | ||
|
|
||
| let rangeToSelect = NSRange( | ||
| start: prevLineInfo.range.lowerBound, | ||
| end: lastSelectedString.range.location - prevLineInfo.range.length + lastSelectedString.range.length | ||
| ) | ||
|
|
||
| setCursorPositions([CursorPosition(range: rangeToSelect)], scrollToVisible: true) | ||
| } | ||
|
|
||
| textView.undoManager?.endUndoGrouping() | ||
| } | ||
|
|
||
| /// Moves the selected lines down by one line. | ||
| public func moveLinesDown() { | ||
thecoolwinter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| guard !cursorPositions.isEmpty else { return } | ||
|
|
||
| textView.undoManager?.beginUndoGrouping() | ||
|
|
||
| textView.editSelections { textView, selection in | ||
| guard let lineIndexes = getOverlappingLines(for: selection.range) else { return } | ||
| let totalLines = textView.layoutManager.lineCount | ||
| let upperBound = lineIndexes.upperBound | ||
| guard upperBound + 1 < totalLines, | ||
| let nextLineInfo = textView.layoutManager.textLineForIndex(upperBound + 1), | ||
| let nextString = textView.textStorage.substring(from: nextLineInfo.range), | ||
| let firstSelectedString = textView.layoutManager.textLineForIndex(lineIndexes.lowerBound) else { | ||
| return | ||
| } | ||
|
|
||
| textView.replaceCharacters(in: [nextLineInfo.range], with: String()) | ||
| textView.insertString(nextString, at: firstSelectedString.range.lowerBound) | ||
|
|
||
| let rangeToSelect = NSRange( | ||
| start: firstSelectedString.range.location + nextLineInfo.range.length, | ||
| end: nextLineInfo.range.upperBound | ||
| ) | ||
|
|
||
| setCursorPositions([CursorPosition(range: rangeToSelect)], scrollToVisible: true) | ||
| } | ||
|
|
||
| textView.undoManager?.endUndoGrouping() | ||
| } | ||
| } | ||
107 changes: 107 additions & 0 deletions
107
Tests/CodeEditSourceEditorTests/Controller/TextViewController+MoveLinesTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // | ||
| // TextViewController+MoveLinesTests.swift | ||
| // CodeEditSourceEditor | ||
| // | ||
| // Created by Bogdan Belogurov on 01/06/2025. | ||
| // | ||
|
|
||
| import XCTest | ||
| @testable import CodeEditSourceEditor | ||
| @testable import CodeEditTextView | ||
| import CustomDump | ||
|
|
||
| final class TextViewControllerMoveLinesTests: XCTestCase { | ||
| var controller: TextViewController! | ||
|
|
||
| override func setUpWithError() throws { | ||
| controller = Mock.textViewController(theme: Mock.theme()) | ||
|
|
||
| controller.loadView() | ||
| } | ||
|
|
||
| func testHandleMoveLinesUpForSingleLine() { | ||
| let strings: [(NSString, Int)] = [ | ||
| ("This is a test string\n", 0), | ||
| ("With multiple lines\n", 22) | ||
| ] | ||
| for (insertedString, location) in strings { | ||
| controller.textView.replaceCharacters( | ||
| in: [NSRange(location: location, length: 0)], | ||
| with: insertedString as String | ||
| ) | ||
| } | ||
|
|
||
| let cursorRange = NSRange(location: 40, length: 0) | ||
| controller.textView.selectionManager.textSelections = [.init(range: cursorRange)] | ||
| controller.cursorPositions = [CursorPosition(range: cursorRange)] | ||
|
|
||
| controller.moveLinesUp() | ||
| let expectedString = "With multiple lines\nThis is a test string\n" | ||
| expectNoDifference(controller.string, expectedString) | ||
| } | ||
|
|
||
| func testHandleMoveLinesDownForSingleLine() { | ||
| let strings: [(NSString, Int)] = [ | ||
| ("This is a test string\n", 0), | ||
| ("With multiple lines\n", 22) | ||
| ] | ||
| for (insertedString, location) in strings { | ||
| controller.textView.replaceCharacters( | ||
| in: [NSRange(location: location, length: 0)], | ||
| with: insertedString as String | ||
| ) | ||
| } | ||
|
|
||
| let cursorRange = NSRange(location: 0, length: 0) | ||
| controller.textView.selectionManager.textSelections = [.init(range: cursorRange)] | ||
| controller.cursorPositions = [CursorPosition(range: cursorRange)] | ||
|
|
||
| controller.moveLinesDown() | ||
| let expectedString = "With multiple lines\nThis is a test string\n" | ||
| expectNoDifference(controller.string, expectedString) | ||
| } | ||
|
|
||
| func testHandleMoveLinesUpForMultiLine() { | ||
| let strings: [(NSString, Int)] = [ | ||
| ("This is a test string\n", 0), | ||
| ("With multiple lines\n", 22), | ||
| ("And additional info\n", 42) | ||
| ] | ||
| for (insertedString, location) in strings { | ||
| controller.textView.replaceCharacters( | ||
| in: [NSRange(location: location, length: 0)], | ||
| with: insertedString as String | ||
| ) | ||
| } | ||
|
|
||
| let cursorRange = NSRange(location: 40, length: 15) | ||
| controller.textView.selectionManager.textSelections = [.init(range: cursorRange)] | ||
| controller.cursorPositions = [CursorPosition(range: cursorRange)] | ||
|
|
||
| controller.moveLinesUp() | ||
| let expectedString = "With multiple lines\nAnd additional info\nThis is a test string\n" | ||
| expectNoDifference(controller.string, expectedString) | ||
| } | ||
|
|
||
| func testHandleMoveLinesDownForMultiLine() { | ||
| let strings: [(NSString, Int)] = [ | ||
| ("This is a test string\n", 0), | ||
| ("With multiple lines\n", 22), | ||
| ("And additional info\n", 42) | ||
| ] | ||
| for (insertedString, location) in strings { | ||
| controller.textView.replaceCharacters( | ||
| in: [NSRange(location: location, length: 0)], | ||
| with: insertedString as String | ||
| ) | ||
| } | ||
|
|
||
| let cursorRange = NSRange(location: 0, length: 30) | ||
| controller.textView.selectionManager.textSelections = [.init(range: cursorRange)] | ||
| controller.cursorPositions = [CursorPosition(range: cursorRange)] | ||
|
|
||
| controller.moveLinesDown() | ||
| let expectedString = "And additional info\nThis is a test string\nWith multiple lines\n" | ||
| expectNoDifference(controller.string, expectedString) | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.