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

feat: Auto indent when Enter is pressed in ScriptPanel #250

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Cuperino
Copy link
Member

This change is meant to boost productivity when prototyping a Knut script from the ScriptPanel.

Behavior aims to replicate what one finds in Qt Creator and standard editors. Pressing Enter|Return results in a line that has been properly indented relative to the current line. When a new block is about to begin, a new indentation is added. Blocks are determined by checking for Javascript verbs that result in a block and by checking for an opening curly brace.

When an opening brace is typed, the closing brace that follows is inserted automatically. Typing a closing brace after a closing brace was automatically inserted results in the cursor being moved to the right of the automatically typed brace. Pressing Enter after a right brace was automatically inserted results in the closing brace being properly re-indented bellow the new line.

I've replaced use of if/else statements with a switch, so the compiler can do a better job at optimizing this section.

@Cuperino Cuperino added the ⬆️ feature New feature or request label Mar 20, 2025
@Cuperino Cuperino requested a review from narnaud March 20, 2025 22:36
@Cuperino Cuperino self-assigned this Mar 20, 2025
@Cuperino Cuperino force-pushed the scriptpanel/autoindent branch from 1870144 to 3d66a20 Compare March 20, 2025 22:37
QTextCursor cursor = textCursor();
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
if (cursor.selectedText() == "}") {
cursor.setPosition(cursor.anchor());
Copy link
Member

Choose a reason for hiding this comment

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

I think you should move it after the }, not before.
It's very strange that I type a letter and it does nothing.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed. My mistake. I forgot to update the document's cursor and it wouldn't update as a result. Set position was also redundant there.

}
if (cursor.selectedText() == "}") {
cursor.clearSelection();
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor);
Copy link
Member

Choose a reason for hiding this comment

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

You need to break here too.
For example, if you type if () {} and enter, the line will have too much indentation.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch. This helped me notice other similar bugs. Fixed it in a different way because of that.

Core::indentTextInTextEdit(this, 1);
return;
} else if (event->key() == Qt::Key_F1) {
case Qt::Key_BraceLeft: {
Copy link
Member

Choose a reason for hiding this comment

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

This is nice, could you do that also for [] too?
Using the event, it should be possible to do that easily without any code duplication.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch! Done.

This change is meant to boost productivity when prototyping a Knut
script on the ScriptPanel.
@Cuperino Cuperino force-pushed the scriptpanel/autoindent branch from 3d66a20 to 63adf7a Compare March 22, 2025 04:06
@Cuperino Cuperino requested a review from narnaud March 22, 2025 04:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⬆️ feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants