webdevcody / code-racer

https://code-racer-eight.vercel.app
MIT License
694 stars 500 forks source link

Tabs not recognized in practice tests. #548

Open RafaelZasas opened 1 year ago

RafaelZasas commented 1 year ago

Description

Added a snippet of Go code which had a tab before the "fmt" import. When doing a practice run, I was not able to trigger the tab with a tab or spaces.

(optional) What browsers are you seeing the problem on?

Chrome

Code of Conduct

RafaelZasas commented 1 year ago

It is also not possible to use tabs when adding code snippets by hand.

sudo-adduser-jordan commented 1 year ago

I will look into this.

sudo-adduser-jordan commented 1 year ago

Can you please include the snippet? A video would be extra helpful. Thank you

writeonlycode commented 1 year ago

I notice the same thing, and I have a fix for it.

It's true that you cannot to use tabs when adding code snippets by hand. But if you copy and paste the snippet form somewhere else, the tab will be inserted. That's how I replicated the bug.

Can I take care of this issue or there's someone else already working at it?

writeonlycode commented 1 year ago

Here's a step-by-step on how to reproduce the issue:

  1. Run a local instance of the App.
  2. Click "Add Snippet"
  3. Copy the code below & paste in the snippet to be added, & add the snippet:
#include <iostream>

int main() {
    std::cout << "Hello World!";
    return 0;
}

The code above has tab characters in it, so the snippet will be added with tab characters to the database.

  1. Click "Race", select "C++" and run. You should see the new code snippet. If not, refresh until you got the correct snippet.
  2. When you reach the tab character, pressing tab does nothing and you are stuck.

That's it. I'll create a PR with a fix later today.

sudo-adduser-jordan commented 1 year ago

Thanks for the snippet. The /t needs to be stripped and replaced with whitespace before insert into the database. I would prefer to avoid editing the race logic.

writeonlycode commented 1 year ago

Yep. I was thinking about these alternatives. The way I found to deal with the issue, is to tweak a little the race logic in packages/app/src/app/race/_components/race/race-practice.tsx. But it turns out, the tweaks are really minimal: it's just about replicating some of the logic to deal with whitespaces in the beginning of a line, so it can also deal with \t at the beginning of a line.

I suspect it's worth having the race practice to be able to deal with tabs, in case they end up in the database somehow. The idea of stripping the tabs before inserting into the database is good too. This would also give more consistency across different snippets, to avoid some coming with tabs and others with whitespaces.

It might be worth implementing both. I'll create a PR with the fix I did, so you can take a look. Let me know if this looks good!

sudo-adduser-jordan commented 1 year ago

@writeonlycode

This will work. However, the race-multiplayer.tsx file will also have to be changed.

The snippets inside the database will not be uniform. This can make debugging accuracy more difficult. A .replace("/t", " ") inside add-snippet-form.tsx will format the strings to be uniform upon insert into the database. This way the race logic does not need to be edited or perform an extra step on every enter(), only once on insert.

Tab will still not work inside the /add-snippet-form input.

This issue should be reopened or a new issue should be created.

writeonlycode commented 1 year ago

Good points. I'll explore further this issue, and I'll create another PRs once I implement these other tweaks.

vignesh-gupta commented 1 year ago

Hey @writeonlycode could you please share updates, if any?