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

Create file if it doesn't exist on save #45

Merged
merged 3 commits into from
Jun 6, 2021

Conversation

vv9k
Copy link
Contributor

@vv9k vv9k commented Jun 2, 2021

This PR fixes a crash on startup when the document that should be opened doesn't exist. For now the file will be created at launch, and will exist even if the user doesn't save it.

Closes: #25

@sellerie98
Copy link

My personal opinion would be that it's better to create it as soon as the user saves the file instead.

@vv9k
Copy link
Contributor Author

vv9k commented Jun 2, 2021

I do agree, perhaps I submitted it too fast. I'll try to come up with a solution that will first create a buffer and later, if the user wishes to do so, it will save the file (just like any other sane editor behaves).

@vv9k vv9k force-pushed the create-file branch 2 times, most recently from 61b6549 to 513eb58 Compare June 2, 2021 15:39
@vv9k
Copy link
Contributor Author

vv9k commented Jun 2, 2021

I updated the code, now it will first create an empty buffer and if the user writes to the file it will create it.

@pickfire
Copy link
Contributor

pickfire commented Jun 2, 2021

Title should be updated to create file if it does not exists on save, another behavior I would like is to create the parent directories if they don't exists. I wish neovim have this, or maybe if that's too intrusive, maybe we can prompt.

@vv9k vv9k changed the title Create file if it doesn't exist Create file if it doesn't exist on save Jun 2, 2021
@vv9k
Copy link
Contributor Author

vv9k commented Jun 2, 2021

Do agree on the title, but I think creating parent directories seems a bit too intrusive. Perhaps in the future there could some sort of a prompt asking user if the parent directories should be created with y/n choice.

helix-view/src/document.rs Outdated Show resolved Hide resolved
helix-view/src/document.rs Outdated Show resolved Hide resolved
helix-view/src/editor.rs Outdated Show resolved Hide resolved
@vv9k
Copy link
Contributor Author

vv9k commented Jun 3, 2021

All done, tested and it works as expected on absolute, and not absolute paths.

@archseer
Copy link
Member

archseer commented Jun 3, 2021

What happens if the intermediate path doesn't exist? I'm not sure if :write shows a warning for that.

i.e.

hx does/not/exist/file.txt

@vv9k
Copy link
Contributor Author

vv9k commented Jun 3, 2021

It returns an error, but at the moment write errors seem to be silently ignored.


// TODO: create if not found
let doc = if !path.exists() {
Ok(Rope::from("\n"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why \n but not new rope?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The document sort of expects at least one trailing new line or things break -- that's why pressing d in a new buffer makes the line number vanish.

helix-view/src/document.rs Outdated Show resolved Hide resolved
@archseer
Copy link
Member

archseer commented Jun 3, 2021

Hmm, we probably want to address that: You should check if path.parent() exists and return early here with the error.

let path = self.path.clone().expect("Can't save with no path set!"); // TODO: handle no path

Places that use save can then use set_error: editor.set_error(format!("invalid filepath: {}", err));

@vv9k vv9k force-pushed the create-file branch 3 times, most recently from 1487bec to 641689e Compare June 6, 2021 04:43
@vv9k
Copy link
Contributor Author

vv9k commented Jun 6, 2021

Sorry for the delay, I've been on vacation for a bit.

Hmm, we probably want to address that: You should check if path.parent() exists and return early here with the error.

That is correct, done. I also don't think we should error out in Document::set_path when the parent directory doesn't exist. This way someone can still open the buffer and potentially see the error only on write.

Places that use save can then use set_error: editor.set_error(format!("invalid filepath: {}", err));

Currently Document::save is spawned as a tokio task and I couldn't easily figure out how to later set the error after the tasks finishes.

@pickfire
Copy link
Contributor

pickfire commented Jun 6, 2021

Currently Document::save is spawned as a tokio task and I couldn't easily figure out how to later set the error after the tasks finishes.

Yeah, there are no error on this when it failed to save, like if there are a directory. I don't think we can pass editor in to save. We could probably solve this in another pull request. We could do like a channel to show status and have a thread or task to manage that.

"can't save file, parent directory does not exist",
));
}
}
let mut file = File::create(path).await?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, doesn't this already create the file? Currently it already does that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does create the file, that’s correct

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not sure what you’re getting at, could you explain the question further?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m also thinking there should probably be an explicit error type so it can later be handled for example to show the prompt asking the user if the directories should be created.

@pickfire
Copy link
Contributor

pickfire commented Jun 6, 2021

Okay, I guess I will merge this since current it fails when the file doesn't exists.

@pickfire pickfire merged commit bcb1afe into helix-editor:master Jun 6, 2021
@archseer
Copy link
Member

archseer commented Jun 6, 2021

Thanks! 🎉

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.

Panic when trying to run heilx on non-existent file
4 participants