-
-
Notifications
You must be signed in to change notification settings - Fork 971
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
Allow text files to be edited even when empty #1724
base: main
Are you sure you want to change the base?
Allow text files to be edited even when empty #1724
Conversation
@@ -26,7 +26,7 @@ struct EditorAreaFileView: View { | |||
@ViewBuilder var editorAreaFileView: some View { | |||
if let document = file.fileDocument { | |||
|
|||
if let utType = document.utType, utType.conforms(to: .text) { | |||
if let fileURL = document.fileURL, (try? String(contentsOf: fileURL)) != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of reading the entire file into memory like this, would it work the same way to just check the document's content
property for isEmpty
? That would be a much more efficient operation, as this would potentially read a large file into memory quite often.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an !isEmpty
check in the utType
computed property.
I attempted different ways to partially read from the file document and determine if it is a text file but I could not get it to work properly.
I'll close #1723 in favor of working on it here, it clearly doesn't solve the entire problem. |
The previous commit works as expected. I made the document's There is a helper |
@@ -51,7 +51,7 @@ final class CodeFileDocument: NSDocument, ObservableObject { | |||
/// - Note: The UTType doesn't necessarily mean the file extension, it can be the MIME | |||
/// type or any other form of data representation. | |||
var utType: UTType? { | |||
if !self.content.isEmpty { | |||
if self.content != nil { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can omit the self
here 🙏
@@ -109,7 +115,7 @@ struct CodeFileView: View { | |||
|
|||
var body: some View { | |||
CodeEditSourceEditor( | |||
$codeFile.content, | |||
sourceEditorText, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think about this approach? Then we will don't need this sourceEditorText
anymore
CodeEditSourceEditor(
Binding(
get: { codeFile.content ?? "" },
set: { codeFile.content = $0 }
)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That will work. But using the sourceEditorText variable can make the code more readable.
I’m not advanced but I think creating the binding once in the initializer, versus everytime in a computed property might be a better way of doing things.
Description
This is done by making sure the opened file can be encoded as text.
Has been tested against a plethora of file types.
Related Issues
Checklist
Screenshots
allow-text-files-to-be-edited-even-when-empty.mov