-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Conversation
1870144
to
3d66a20
Compare
src/gui/scriptpanel.cpp
Outdated
QTextCursor cursor = textCursor(); | ||
cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor); | ||
if (cursor.selectedText() == "}") { | ||
cursor.setPosition(cursor.anchor()); |
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 you should move it after the }
, not before.
It's very strange that I type a letter and it does nothing.
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.
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.
src/gui/scriptpanel.cpp
Outdated
} | ||
if (cursor.selectedText() == "}") { | ||
cursor.clearSelection(); | ||
cursor.movePosition(QTextCursor::PreviousCharacter, QTextCursor::KeepAnchor); |
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.
You need to break here too.
For example, if you type if () {}
and enter, the line will have too much indentation.
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.
Nice catch. This helped me notice other similar bugs. Fixed it in a different way because of that.
src/gui/scriptpanel.cpp
Outdated
Core::indentTextInTextEdit(this, 1); | ||
return; | ||
} else if (event->key() == Qt::Key_F1) { | ||
case Qt::Key_BraceLeft: { |
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.
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.
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.
Nice catch! Done.
This change is meant to boost productivity when prototyping a Knut script on the ScriptPanel.
3d66a20
to
63adf7a
Compare
for more information, see https://pre-commit.ci
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.