Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

plbstl
Copy link
Contributor

@plbstl plbstl commented May 19, 2024

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

  • I read and understood the contributing guide as well as the code of conduct
  • The issues this PR addresses are related to each other
  • My changes generate no new warnings
  • My code builds and runs on my machine
  • My changes are all related to the related issue above
  • I documented my code

Screenshots

allow-text-files-to-be-edited-even-when-empty.mov

@@ -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 {
Copy link
Collaborator

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.

Copy link
Contributor Author

@plbstl plbstl May 19, 2024

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.

https://github.com/plbstl/CodeEdit/blob/83980b99dec26de57085e7d84901127ea4d1ce78/CodeEdit/Features/Documents/CodeFileDocument.swift#L53-L70

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.

@thecoolwinter
Copy link
Collaborator

I'll close #1723 in favor of working on it here, it clearly doesn't solve the entire problem.

@plbstl
Copy link
Contributor Author

plbstl commented May 19, 2024

The previous commit works as expected. I made the document's content optional, we can then check to see if the content is not nil.

There is a helper sourceEditorText binding in CodeEdit/Features/Editor/Views/CodeFileView.swift for loading the text content into the CodeEditSourceEditor.

@plbstl plbstl requested a review from thecoolwinter May 19, 2024 16:44
@@ -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 {

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,

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 }
        )
}

Copy link
Contributor Author

@plbstl plbstl May 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐞 Source editor disappears when deleting all text
3 participants