From 248b58f80a635dcf7cb042bff15fd0c0605c6321 Mon Sep 17 00:00:00 2001 From: Renan Greca Date: Fri, 17 Feb 2023 21:17:44 +0100 Subject: [PATCH] Make STTextView first responder when opening an empty file (CodeEdit #696) (#144) # Description * 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 * [#696](https://github.com/CodeEditApp/CodeEdit/issues/696) from CodeEdit. * PR [#1057](https://github.com/CodeEditApp/CodeEdit/pull/1057) from CodeEdit. # Checklist - [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 --- Sources/CodeEditTextView/STTextViewController.swift | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 92a149952..a8a272824 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -321,6 +321,16 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt var (line, column) = position let string = textView.string if line > 0 { + if string.isEmpty { + // If the file is blank, automatically place the cursor in the first index. + let range = NSRange(string.startIndex..