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

[BUG] fix (tab) indentation within CodeMirror #4832

Merged
merged 7 commits into from
Dec 13, 2023
Merged

[BUG] fix (tab) indentation within CodeMirror #4832

merged 7 commits into from
Dec 13, 2023

Conversation

hasan-sh
Copy link
Collaborator

Fixes #4830

How to test?

  • go to code editor
  • try indenting with tab (tip: shift-tab removes indentation as well!)
  • and within blocks; e.g., now when you click Enter within a block (i.e., if or for), the correct indentation will be chosen based on the previous and next lines.
for i in range 1 to 5 # (CLICK ENTER)
    # CURSOR_STARTSHERE
    print i 

or 

for i in range 1 to 5
    print i #  (CLICK ENTER)
    # CURSOR_STARTS_HERE

- indent within blocks; according to previous and next lines.
@ghost
Copy link

ghost commented Nov 30, 2023

👇 Click on the image for a new way to code review

Review these changes using an interactive CodeSee Map

Legend

CodeSee Map legend

@hasan-sh hasan-sh requested a review from jpelay November 30, 2023 14:50
jpelay
jpelay previously requested changes Nov 30, 2023
Copy link
Member

@jpelay jpelay left a 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,
Copy link
Member

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/

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

See this #4830 (comment)

Copy link
Member

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?

Copy link
Collaborator Author

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.

Copy link
Member

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I agree.

Comment on lines 128 to 134
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
}),
Copy link
Member

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?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good one, fixed!

@hasan-sh
Copy link
Collaborator Author

hasan-sh commented Dec 4, 2023

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 😄

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?

@Felienne
Copy link
Member

Felienne commented Dec 7, 2023

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?

@Felienne
Copy link
Member

Felienne commented Dec 7, 2023

  • and within blocks; e.g., now when you click Enter within a block (i.e., if or for), the correct indentation will be chosen based on the previous and next lines.

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!

@hasan-sh
Copy link
Collaborator Author

hasan-sh commented Dec 8, 2023

Also, let's indeed add this to the manual (for teachers) and docs (for developers). Can you pick that up @hasan-sh?

Sure, will do in this PR.

  • and within blocks; e.g., now when you click Enter within a block (i.e., if or for), the correct indentation will be chosen based on the previous and next lines.

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!

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.

@Felienne
Copy link
Member

Felienne commented Dec 8, 2023

Sure, will do in this PR.

Thanks! Ping me if that is ready so I can merge!

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.

No problem, that is what we have now too. We can discuss in the meeting how/if we want to make it smarter

@jpelay
Copy link
Member

jpelay commented Dec 12, 2023

I am not sure, reading the code, if this PR already adds the "press escape to exit the editor" hatch?

Yes it does! Hasan added it by applying an extension from CodeMirror that adds this as well.

@Felienne
Copy link
Member

I am not sure, reading the code, if this PR already adds the "press escape to exit the editor" hatch?

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!

Copy link
Contributor

mergify bot commented Dec 12, 2023

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

Copy link
Contributor

mergify bot commented Dec 12, 2023

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

Copy link
Contributor

mergify bot commented Dec 13, 2023

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

@mergify mergify bot merged commit 4e01118 into main Dec 13, 2023
11 checks passed
@mergify mergify bot deleted the fix-indentation branch December 13, 2023 11:51
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.

[BUG] Tab key doesn't seem to work in Hedy anymore?
3 participants