-
Notifications
You must be signed in to change notification settings - Fork 135
Indent/Unindent Selected Lines #266
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
Merged
Changes from 7 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9281dcf
feat: add indent lines
tom-ludwig 0277db4
Iterate over cursor positions for multiple cursors.
tom-ludwig c24c533
Clean up code
tom-ludwig 51b3353
Handle Tab and Shift+Tab
tom-ludwig f389bd4
Add documentation and fix spelling error
tom-ludwig 677574a
Ensure tab is handled as expected
tom-ludwig 63e644a
Add tests
tom-ludwig 57daf76
Add better countLeadingSpacesUpTo
tom-ludwig 57228f4
Apply suggestions from code review
tom-ludwig a1e048c
Use ClosedRange instead of Array<Int>
tom-ludwig 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
131 changes: 131 additions & 0 deletions
131
Sources/CodeEditSourceEditor/Controller/TextViewController+IndentLines.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,131 @@ | ||
| // | ||
| // TextViewController+IndentLines.swift | ||
| // | ||
| // | ||
| // Created by Ludwig, Tom on 11.09.24. | ||
| // | ||
|
|
||
| import CodeEditTextView | ||
| import AppKit | ||
|
|
||
| extension TextViewController { | ||
| /// Handels indentation and unindentation | ||
| /// | ||
| /// Handles the indentation of lines in the text view based on the current indentation option. | ||
| /// | ||
| /// This function assumes that the document is formatted according to the current selected indentation option. | ||
| /// It will not indent a tab character if spaces are selected, and vice versa. Ensure that the document is | ||
| /// properly formatted before invoking this function. | ||
| /// | ||
| /// - Parameter inwards: A Boolean flag indicating whether to outdent (default is `false`). | ||
| public func handleIndent(inwards: Bool = false) { | ||
| let indentationChars: String = indentOption.stringValue | ||
| guard !cursorPositions.isEmpty else { return } | ||
|
|
||
| textView.undoManager?.beginUndoGrouping() | ||
| for cursorPosition in self.cursorPositions { | ||
|
tom-ludwig marked this conversation as resolved.
Outdated
|
||
| // get lineindex, i.e line-numbers+1 | ||
| guard let lineIndexes = getHighlightedLines(for: cursorPosition.range) else { continue } | ||
|
|
||
| for lineIndex in lineIndexes { | ||
| adjustIndentation( | ||
| lineIndex: lineIndex, | ||
| indentationChars: indentationChars, | ||
| inwards: inwards | ||
| ) | ||
| } | ||
| } | ||
| textView.undoManager?.endUndoGrouping() | ||
| } | ||
|
|
||
| /// This method is used to handle tabs appropriately when multiple lines are selected, | ||
| /// allowing normal use of tabs. | ||
| /// | ||
| /// - Returns: A Boolean value indicating whether multiple lines are highlighted. | ||
| func multipleLinesHighlighted() -> Bool { | ||
| for cursorPosition in self.cursorPositions { | ||
| if let startLineInfo = textView.layoutManager.textLineForOffset(cursorPosition.range.lowerBound), | ||
| let endLineInfo = textView.layoutManager.textLineForOffset(cursorPosition.range.upperBound), | ||
| startLineInfo.index != endLineInfo.index { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| private func getHighlightedLines(for range: NSRange) -> [Int]? { | ||
| guard let startLineInfo = textView.layoutManager.textLineForOffset(range.lowerBound) else { | ||
| return nil | ||
| } | ||
| var lines: [Int] = [startLineInfo.index] | ||
|
|
||
| guard let endLineInfo = textView.layoutManager.textLineForOffset(range.upperBound), | ||
| endLineInfo.index != startLineInfo.index else { | ||
| return lines | ||
| } | ||
| if endLineInfo.index == startLineInfo.index + 1 { | ||
| lines.append(endLineInfo.index) | ||
| return lines | ||
| } | ||
|
|
||
| return Array(startLineInfo.index...endLineInfo.index) | ||
|
tom-ludwig marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| private func adjustIndentation(lineIndex: Int, indentationChars: String, inwards: Bool) { | ||
| guard let lineInfo = textView.layoutManager.textLineForIndex(lineIndex) else { return } | ||
|
|
||
| if inwards { | ||
| if indentOption != .tab { | ||
| removeLeadingSpaces(lineInfo: lineInfo, spaceCount: indentationChars.count) | ||
| } else { | ||
| removeLeadingTab(lineInfo: lineInfo) | ||
| } | ||
| } else { | ||
| addIndentation(lineInfo: lineInfo, indentationChars: indentationChars) | ||
| } | ||
| } | ||
|
|
||
| private func addIndentation( | ||
| lineInfo: TextLineStorage<TextLine>.TextLinePosition, | ||
| indentationChars: String | ||
| ) { | ||
| textView.replaceCharacters( | ||
| in: NSRange(location: lineInfo.range.lowerBound, length: 0), | ||
| with: indentationChars | ||
| ) | ||
| } | ||
|
|
||
| private func removeLeadingSpaces( | ||
| lineInfo: TextLineStorage<TextLine>.TextLinePosition, | ||
| spaceCount: Int | ||
| ) { | ||
| guard let lineContent = textView.textStorage.substring(from: lineInfo.range) else { return } | ||
|
|
||
| let removeSpacesCount = countLeadingSpacesUpTo(line: lineContent, maxCount: spaceCount) | ||
|
|
||
| guard removeSpacesCount > 0 else { return } | ||
|
|
||
| textView.replaceCharacters( | ||
| in: NSRange(location: lineInfo.range.lowerBound, length: removeSpacesCount), | ||
| with: "" | ||
| ) | ||
| } | ||
|
|
||
| private func removeLeadingTab(lineInfo: TextLineStorage<TextLine>.TextLinePosition) { | ||
| guard let lineContent = textView.textStorage.substring(from: lineInfo.range) else { | ||
| return | ||
| } | ||
|
|
||
| if lineContent.first == "\t" { | ||
| textView.replaceCharacters( | ||
| in: NSRange(location: lineInfo.range.lowerBound, length: 1), | ||
| with: "" | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| private func countLeadingSpacesUpTo(line: String, maxCount: Int) -> Int { | ||
| // Count leading spaces using prefix and `filter` | ||
| return line.prefix(maxCount).filter { $0 == " " }.count | ||
|
tom-ludwig marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
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
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
105 changes: 105 additions & 0 deletions
105
Tests/CodeEditSourceEditorTests/Controller/TextViewController+IndentTests.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,105 @@ | ||
| // | ||
| // TextViewController+IndentTests.swift | ||
| // CodeEditSourceEditor | ||
| // | ||
| // Created by Ludwig, Tom on 08.10.24. | ||
| // | ||
|
|
||
| import XCTest | ||
| @testable import CodeEditSourceEditor | ||
|
|
||
| final class TextViewControllerIndentTests: XCTestCase { | ||
| var controller: TextViewController! | ||
|
|
||
| override func setUpWithError() throws { | ||
| controller = Mock.textViewController(theme: Mock.theme()) | ||
|
|
||
| controller.loadView() | ||
| } | ||
|
|
||
| func testHandleIndentWithSpacesInwards() { | ||
| controller.setText(" This is a test string") | ||
| let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 0))] | ||
| controller.cursorPositions = cursorPositions | ||
| controller.handleIndent(inwards: true) | ||
|
|
||
| XCTAssertEqual(controller.string, "This is a test string") | ||
|
|
||
| // Normally, 4 spaces are used for indentation; however, now we only insert 2 leading spaces. | ||
| // The outcome should be the same, though. | ||
| controller.setText(" This is a test string") | ||
| controller.cursorPositions = cursorPositions | ||
| controller.handleIndent(inwards: true) | ||
|
|
||
| XCTAssertEqual(controller.string, "This is a test string") | ||
| } | ||
|
|
||
| func testHandleIndentWithSpacesOutwards() { | ||
| controller.setText("This is a test string") | ||
| let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 0))] | ||
| controller.cursorPositions = cursorPositions | ||
|
|
||
| controller.handleIndent(inwards: false) | ||
|
|
||
| XCTAssertEqual(controller.string, " This is a test string") | ||
| } | ||
|
|
||
| func testHandleIndentWithTabsInwards() { | ||
| controller.setText("\tThis is a test string") | ||
| controller.indentOption = .tab | ||
| let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 0))] | ||
| controller.cursorPositions = cursorPositions | ||
|
|
||
| controller.handleIndent(inwards: true) | ||
|
|
||
| XCTAssertEqual(controller.string, "This is a test string") | ||
| } | ||
|
|
||
| func testHandleIndentWithTabsOutwards() { | ||
| controller.setText("This is a test string") | ||
| controller.indentOption = .tab | ||
| let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 0))] | ||
| controller.cursorPositions = cursorPositions | ||
|
|
||
| controller.handleIndent() | ||
|
|
||
| // Normally, we expect nothing to happen because only one line is selected. | ||
| // However, this logic is not handled inside `handleIndent`. | ||
| XCTAssertEqual(controller.string, "\tThis is a test string") | ||
| } | ||
|
|
||
| func testHandleIndentMultiLine() { | ||
| controller.indentOption = .tab | ||
| controller.setText("This is a test string\nWith multiple lines\nAnd some indentation") | ||
| let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 5))] | ||
| controller.cursorPositions = cursorPositions | ||
|
|
||
| controller.handleIndent() | ||
| let expectedString = "\tThis is a test string\nWith multiple lines\nAnd some indentation" | ||
| XCTAssertEqual(controller.string, expectedString) | ||
| } | ||
|
|
||
| func testHandleInwardIndentMultiLine() { | ||
| controller.indentOption = .tab | ||
| controller.setText("\tThis is a test string\n\tWith multiple lines\n\tAnd some indentation") | ||
| let cursorPositions = [CursorPosition(range: NSRange(location: 0, length: controller.string.count))] | ||
| controller.cursorPositions = cursorPositions | ||
|
|
||
| controller.handleIndent(inwards: true) | ||
| let expectedString = "This is a test string\nWith multiple lines\nAnd some indentation" | ||
| XCTAssertEqual(controller.string, expectedString) | ||
| } | ||
|
|
||
| func testMultipleLinesHighlighted() { | ||
| controller.setText("\tThis is a test string\n\tWith multiple lines\n\tAnd some indentation") | ||
| var cursorPositions = [CursorPosition(range: NSRange(location: 0, length: controller.string.count))] | ||
| controller.cursorPositions = cursorPositions | ||
|
|
||
| XCTAssert(controller.multipleLinesHighlighted()) | ||
|
|
||
| cursorPositions = [CursorPosition(range: NSRange(location: 0, length: 5))] | ||
| controller.cursorPositions = cursorPositions | ||
|
|
||
| XCTAssertFalse(controller.multipleLinesHighlighted()) | ||
| } | ||
| } |
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.