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

Output breaks when backslashes and quotation marks are contained in the result #121

Closed
control13 opened this issue Dec 10, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@control13
Copy link

Describe the bug
When the result of the execution of a line or a code block contains a bad constellation of \ and ", the there is no output und an error in the sniprun.log appears.
I found the example print("\\\"") in python 3.9. The expected output would be \". In the python terminal it works. Same for R (e.g. cat("\\\"")).
Sniprun don't print anything (I tested classic, virtualtext and terminal). The logoutput for the example shows this:

[00:00:00.156] (7f8e703b3640) INFO message '\"' cleaned out
[00:00:00.157] (7f8e703b3640) INFO done displaying virtual text, Err(NeovimError(0, "Vim(lua):E5107: Error loading lua [string ":lua"]:1: ')' expected near '","'"))

What do the bug concerns? Sniprun itself, a particular interpreter?
I tracked the problem down to the cleanup_and_escape function in display.rs

sniprun/src/display.rs

Lines 330 to 333 in 545aa90

fn cleanup_and_escape(message: &str) -> String {
let answer_str = message.replace("\\", "\\\\");
let answer_str = answer_str.replace("\\\"", "\"");
let answer_str = answer_str.replace("\"", "\\\"");

The first replace statement adds a second backslash ("\\""), the second removes it ("\"") and the last one escapes all quotes (\"\\"\"). This is than pasted to the lua command for printing, but the resulting command is not sane.

To Reproduce
Steps to reproduce the behavior:

  1. Open an file in a language with sniprun support
  2. add a line with a print statement, that contains "\\\"" for example in python print("\\\"")
  3. run SnipRun
  4. See error at the end of the log (~/.cache/sniprun/sniprun.log)

Expected behavior
Output of \".

Environment:

  • NVIM v0.6.0
  • OS: Linux thinkpad 5.15.6-arch2-1
  • rustc 1.57.0
  • LuaJIT 2.0.5
  • ldd (GNU libc) 2.33
  • Sniprun 1.0.5, (also tested the current master)

Additional context
My first intuition was to switch the first and the second replace statement. Sniprun is then able to print something, but the result is wrong. For the example print("\\\"") results in ".

@control13 control13 added the bug Something isn't working label Dec 10, 2021
@michaelb
Copy link
Owner

Thanks for reporting.

It's true I didn't think through this, i'll try to make a proper fix soon

@michaelb
Copy link
Owner

#122 fixes this

@control13
Copy link
Author

I tested it and yes it works. Thank you very much.

But (sorry) code block or prints with \n are not working in the terminal disply any more.
I wrote a terminal variant in this PR #123 and I also put a small fix to this in it. (Just a replace("\n", "\\n") locally in the display_terminal function.) Hope this is ok?

@michaelb
Copy link
Owner

michaelb commented Dec 12, 2021

Nice catch also, I've missed that one! (since I my test config is also my work config, I don't always test all the features to check everything)

I should have no reason to refuse #123 , and while it's mixing up a bit the issues/fixes, I think it's alright as-is. For clarity purpose, the \n in out for terminal display problem now has its own issue #124

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants