Skip to content

Commit 248b58f

Browse files
authored
Make STTextView first responder when opening an empty file (CodeEdit CodeEditApp#696) (CodeEditApp#144)
# Description <!--- REQUIRED: Describe what changed in detail --> * In `STTextViewController.swift`, the `setCursorPosition` function was changed so that, if the currently open file is empty, the cursor is automatically set to the first position and active. This behavior is similar to other text editors e.g. VSCode. A related PR is opened in CodeEdit, which works in tandem with this one, so that newly-created files automatically open in a new editor tab. # Related Issue <!--- REQUIRED: Tag all related issues (e.g. * CodeEditApp#23) --> * [CodeEditApp#696](CodeEditApp#696) from CodeEdit. * PR [CodeEditApp#1057](CodeEditApp#1057) from CodeEdit. # Checklist <!--- Add things that are not yet implemented above --> - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] I documented my code - [x] Review requested # Screenshots
1 parent d83b449 commit 248b58f

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Sources/CodeEditTextView/STTextViewController.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
321321
var (line, column) = position
322322
let string = textView.string
323323
if line > 0 {
324+
if string.isEmpty {
325+
// If the file is blank, automatically place the cursor in the first index.
326+
let range = NSRange(string.startIndex..<string.endIndex, in: string)
327+
if let newRange = NSTextRange(range, provider: provider) {
328+
_ = self.textView.becomeFirstResponder()
329+
self.textView.setSelectedRange(newRange)
330+
return
331+
}
332+
}
333+
324334
string.enumerateSubstrings(in: string.startIndex..<string.endIndex) { _, lineRange, _, done in
325335
line -= 1
326336
if line < 1 {

0 commit comments

Comments
 (0)