-
Notifications
You must be signed in to change notification settings - Fork 289
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
[BUG] fix (tab) indentation within CodeMirror #4832
Conversation
- indent within blocks; according to previous and next lines.
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.
Hello Hasan, what a nice and clever solution! I have a couple of comments:
With respect to indent with tab, we decided that it was better to be as accessible as possible and not trapping users in the editor on the case that they don't use pointing devices.
With respect to the approach, this is clever! But I think we can do a bit better. We actually can automatically add indentation when the CodeMirror parser detects a certain type of command, i.e: if the user writes: repeat 3 times
and hits enter, the next line will be indented, so, no need to even indent manually!
This would be a little trickier to implement, since it involves messing with the grammars and the tokenizer, but here is a simple example of how this can be done: https://lezer.codemirror.net/examples/indent/ and you can try this by going here: https://codemirror.net/try/ and changing the code to this:
import {basicSetup, EditorView} from "codemirror"
import {python} from "@codemirror/lang-python"
new EditorView({
doc: "print('hello')\n",
extensions: [basicSetup, python()],
parent: document.body
})
Then you can mess around with the indentation.
Do think you could do that approach? If not, we can always ship your solution and make this improvement later on!!! This is already a lot better over what we have 😄
@@ -117,7 +121,17 @@ export class HedyCodeMirrorEditor implements HedyEditor { | |||
...defaultKeymap, | |||
...searchKeymap, // we need to replace this with our own search widget | |||
...historyKeymap, | |||
indentWithTab, |
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.
It was a explicit decision not to use this. Felienne and I discussed it when making the change, and we ultimetaly decided to leave the default behaviour due to accessibility concerns that the CodeMirror maintainer expresses here: https://codemirror.net/examples/tab/
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.
See this #4830 (comment)
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.
If we do this, then we need to mention somewhere that this escape hatch is possible! Perhaps on the tutorial?
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.
I think users will know that naturally, but I do agree, that would be the perfect spot to include it.
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.
I'd be inclined to agree if our users were computer literates, but since our age group is around 12, I'm not sure they'd know this. We can also add it in the Teacher Manual in a future PR and merge this, what do you think?
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.
I agree.
static/js/cm-editor.ts
Outdated
const previousLine = context.lineAt(pos, -1); | ||
const nextLine = context.lineAt(pos + 1, -1); | ||
const nextIndentationSize = nextLine.text.match(/^\s*/)![0].length; | ||
const prevIndentationSize = previousLine.text.match(/^\s*/)![0].length; | ||
const indentBy = Math.max(prevIndentationSize, nextIndentationSize); | ||
return indentBy | ||
}), |
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.
When there's no next line, this returns an error. Also, can you add this code to its own function?
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.
Good one, fixed!
I do believe this would be far better indeed and more accurate. The difference though is that Hedy isn't python, so we have to kinda translate all grammars we have and apply indentation accrodingly. Perhaps we ship this and discuss in our next meeting when/how we wanna do this? |
Thanks for the detailed discussion here and in the issue on accessibility! I am not sure, reading the code, if this PR already adds the "press escape to exit the editor" hatch? If not, I think I would like to add it here so we are compliant with the rule of: "if it requires more than unmodified arrow or tab keys or other standard exit methods" (assuming escape key is a standard exit methods, I could not find a definition in the document) Also, let's indeed add this to the manual (for teachers) and docs (for developers). Can you pick that up @hasan-sh? |
Well, you say correct, but you mean the indentation of the line before, I think. Let's discuss this in the next meeting, I think there might be some learning value in doing it explicitly (esp in earlier levels) but we can totally merge this as this is what we had in Ace too! |
Sure, will do in this PR.
Yeah the current line or the next line indeed. So, this is really not considering the commands, but only the indentation of the current and next line w.r.t. where the cursor is. |
Thanks! Ping me if that is ready so I can merge!
No problem, that is what we have now too. We can discuss in the meeting how/if we want to make it smarter |
Yes it does! Hasan added it by applying an extension from CodeMirror that adds this as well. |
In that case we can merge this right away! |
Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork). |
Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork). |
Thank you for contributing! Your pull request is now going on the merge train (choo choo! Do not click update from main anymore, and be sure to allow changes to be pushed to your fork). |
Fixes #4830
How to test?
Enter
within a block (i.e., if or for), the correct indentation will be chosen based on the previous and next lines.