Skip to content

Commit

Permalink
Restore autoCloseBracket settings and factorize autoInsertImport
Browse files Browse the repository at this point in the history
  • Loading branch information
YoannQDQ authored and nyalldawson committed Jan 10, 2023
1 parent 5b87323 commit e992797
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 95 deletions.
14 changes: 0 additions & 14 deletions python/console/console_editor.py
Expand Up @@ -469,20 +469,6 @@ def syntaxCheck(self):

return True

def keyPressEvent(self, e):
t = e.text()
line, pos = self.getCursorPosition()
self.autoImport = self.settings.value("pythonConsole/autoInsertionImport", True, type=bool)
txt = self.text(line)[:pos]

# Automatically insert "import" after "from xxx "
if t == ' ' and self.autoImport:
ptrn = r'^[ \t]*from [\w.]+$'
if re.match(ptrn, txt):
self.insert(' import')
self.setCursorPosition(line, pos + 7)
QgsCodeEditorPython.keyPressEvent(self, e)

def focusInEvent(self, e):
pathfile = self.parent.path
if pathfile:
Expand Down
10 changes: 0 additions & 10 deletions python/console/console_sci.py
Expand Up @@ -205,16 +205,6 @@ def keyPressEvent(self, e):
sys.stdout.fire_keyboard_interrupt = True
return

autoImport = self.settings.value("pythonConsole/autoInsertionImport", True, type=bool)
# Automatically insert "import" after "from xxx "
if e.text() == ' ' and autoImport:
line, index = self.getCursorPosition()
cmd = self.text(line)
ptrn = r'^[ \t]*from [\w.]+$'
if re.match(ptrn, cmd):
self.insert(' import')
self.setCursorPosition(line, index + 7)

QgsCodeEditorPython.keyPressEvent(self, e)
self.updatePrompt()

Expand Down
6 changes: 4 additions & 2 deletions python/console/console_settings.py
Expand Up @@ -202,7 +202,8 @@ def saveSettings(self):
settings.setValue("pythonConsole/autoCompleteSource", 'fromDocAPI')

settings.setValue("pythonConsole/enableObjectInsp", self.enableObjectInspector.isChecked())
settings.setValue("pythonConsole/autoInsertionImport", self.autoInsertionImport.isChecked())
settings.setValue("pythonConsole/autoCloseBracket", self.autoCloseBracket.isChecked())
settings.setValue("pythonConsole/autoInsertImport", self.autoInsertImport.isChecked())

def restoreSettings(self):
settings = QgsSettings()
Expand All @@ -225,7 +226,8 @@ def restoreSettings(self):
self.groupBoxAutoCompletion.setChecked(settings.value("pythonConsole/autoCompleteEnabled", True, type=bool))

self.enableObjectInspector.setChecked(settings.value("pythonConsole/enableObjectInsp", False, type=bool))
self.autoInsertionImport.setChecked(settings.value("pythonConsole/autoInsertionImport", True, type=bool))
self.autoCloseBracket.setChecked(settings.value("pythonConsole/autoCloseBracket", False, type=bool))
self.autoInsertImport.setChecked(settings.value("pythonConsole/autoInsertImport", False, type=bool))

if settings.value("pythonConsole/autoCompleteSource") == 'fromDoc':
self.autoCompFromDoc.setChecked(True)
Expand Down
14 changes: 11 additions & 3 deletions python/console/console_settings.ui
Expand Up @@ -83,8 +83,15 @@
<bool>true</bool>
</property>
<layout class="QGridLayout" name="gridLayout_11">
<item row="0" column="0">
<widget class="QCheckBox" name="autoInsertionImport">
<item row="0" column="0">
<widget class="QCheckBox" name="autoCloseBracket">
<property name="text">
<string>Automatic parentheses insertion</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="autoInsertImport">
<property name="text">
<string>Automatic insertion of the 'import' string on 'from xxx'</string>
</property>
Expand Down Expand Up @@ -456,7 +463,8 @@
<tabstop>autoCompFromDoc</tabstop>
<tabstop>autoCompFromAPI</tabstop>
<tabstop>autoCompFromDocAPI</tabstop>
<tabstop>autoInsertionImport</tabstop>
<tabstop>autoCloseBracket</tabstop>
<tabstop>autoInsertImport</tabstop>
<tabstop>enableObjectInspector</tabstop>
<tabstop>autoSaveScript</tabstop>
<tabstop>preloadAPI</tabstop>
Expand Down
158 changes: 92 additions & 66 deletions src/gui/codeeditors/qgscodeeditorpython.cpp
Expand Up @@ -218,6 +218,11 @@ void QgsCodeEditorPython::keyPressEvent( QKeyEvent *event )
return;
}

const QgsSettings settings;

bool autoCloseBracket = settings.value( QStringLiteral( "/pythonConsole/autoCloseBracket" ), false ).toBool();
bool autoInsertImport = settings.value( QStringLiteral( "/pythonConsole/autoInsertImport" ), false ).toBool();

// Update calltips when cursor position changes with left and right keys
if ( event->key() == Qt::Key_Left ||
event->key() == Qt::Key_Right ||
Expand All @@ -229,100 +234,121 @@ void QgsCodeEditorPython::keyPressEvent( QKeyEvent *event )
return;
}


// Handle closing and opening
const QString prevChar = characterBeforeCursor();
const QString nextChar = characterAfterCursor();
// Get entered text and cursor position
const QString eText = event->text();

int line, column;
getCursorPosition( &line, &column );

if ( !hasSelectedText() )
// If some text is selected and user presses an opening character
// surround the selection with the opening-closing pair
if ( hasSelectedText() )
{
// When backspace is pressed inside an opening/closing pair, remove both characters
if ( event->key() == Qt::Key_Backspace )
if ( PAIRS.contains( eText ) )
{
if ( PAIRS.contains( prevChar ) && PAIRS[prevChar] == nextChar )
int startLine, startPos, endLine, endPos;
getSelection( &startLine, &startPos, &endLine, &endPos );

// Special case for Multi line quotes (insert triple quotes)
if ( startLine != endLine && ( eText == "\"" || eText == "'" ) )
{
setSelection( line, column - 1, line, column + 1 );
removeSelectedText();
event->accept();
replaceSelectedText(
QString( "%1%1%1%2%3%3%3" ).arg( eText ).arg( selectedText() ).arg( PAIRS[eText] )
);
setSelection( startLine, startPos + 3, endLine, endPos + 3 );
}
else
{
QgsCodeEditor::keyPressEvent( event );
replaceSelectedText(
QString( "%1%2%3" ).arg( eText ).arg( selectedText() ).arg( PAIRS[eText] )
);
setSelection( startLine, startPos + 1, endLine, endPos + 1 );
}

// Update calltips (cursor position has changed)
callTip();
event->accept();
return;
}

// When closing character is entered inside an opening/closing pair, shift the cursor
else if ( PAIRS.key( eText ) != "" && nextChar == eText )
else if ( SINGLE_CHARS.contains( eText ) )
{
setCursorPosition( line, column + 1 );
int startLine, startPos, endLine, endPos;
getSelection( &startLine, &startPos, &endLine, &endPos );
replaceSelectedText(
QString( "%1%2%1" ).arg( eText ).arg( selectedText() )
);
setSelection( startLine, startPos + 1, endLine, endPos + 1 );
event->accept();

// Will hide calltips when a closing parenthesis is entered
callTip();
return;
}
}

// Else, if not inside a string or comment and an opening character
// is entered, also insert the closing character
else if ( !isCursorInsideString() && PAIRS.contains( eText ) )
// No selected text
else
{
// Automatically insert "import" after "from xxx " if option is enabled
if ( autoInsertImport && eText == " " )
{
// Check if user is not entering triple quotes
if ( !( ( eText == "\"" || eText == "'" ) && prevChar == eText ) )
const QString txt = text( line );
const QRegularExpression re( QStringLiteral( "^from [\\w.]+$" ) );
if ( re.match( txt.trimmed() ).hasMatch() )
{
QgsCodeEditor::keyPressEvent( event );
insert( PAIRS[eText] );
event->accept();
return;
insert( QStringLiteral( " import" ) );
setCursorPosition( line, column + 7 );
return QgsCodeEditor::keyPressEvent( event );
}
}
}

// Handle automatic bracket insertion/deletion if option is enabled
else if ( autoCloseBracket )
{
const QString prevChar = characterBeforeCursor();
const QString nextChar = characterAfterCursor();

// If some text is selected and user presses an opening character
// surround the selection with the opening-closing pair
else if ( PAIRS.contains( eText ) )
{
int startLine, startPos, endLine, endPos;
getSelection( &startLine, &startPos, &endLine, &endPos );
// When backspace is pressed inside an opening/closing pair, remove both characters
if ( event->key() == Qt::Key_Backspace )
{
if ( PAIRS.contains( prevChar ) && PAIRS[prevChar] == nextChar )
{
setSelection( line, column - 1, line, column + 1 );
removeSelectedText();
event->accept();
}
else
{
QgsCodeEditor::keyPressEvent( event );
}

// Special case for Multi line quotes (insert triple quotes)
if ( startLine != endLine && ( eText == "\"" || eText == "'" ) )
{
replaceSelectedText(
QString( "%1%1%1%2%3%3%3" ).arg( eText ).arg( selectedText() ).arg( PAIRS[eText] )
);
setSelection( startLine, startPos + 3, endLine, endPos + 3 );
}
else
{
replaceSelectedText(
QString( "%1%2%3" ).arg( eText ).arg( selectedText() ).arg( PAIRS[eText] )
);
setSelection( startLine, startPos + 1, endLine, endPos + 1 );
// Update calltips (cursor position has changed)
callTip();
return;
}

// When closing character is entered inside an opening/closing pair, shift the cursor
else if ( PAIRS.key( eText ) != "" && nextChar == eText )
{
setCursorPosition( line, column + 1 );
event->accept();

// Will hide calltips when a closing parenthesis is entered
callTip();
return;
}

// Else, if not inside a string or comment and an opening character
// is entered, also insert the closing character
else if ( !isCursorInsideString() && PAIRS.contains( eText ) )
{
// Check if user is not entering triple quotes
if ( !( ( eText == "\"" || eText == "'" ) && prevChar == eText ) )
{
QgsCodeEditor::keyPressEvent( event );
insert( PAIRS[eText] );
event->accept();
return;
}
}
}
event->accept();
return;
}
else if ( SINGLE_CHARS.contains( eText ) )
{
int startLine, startPos, endLine, endPos;
getSelection( &startLine, &startPos, &endLine, &endPos );
replaceSelectedText(
QString( "%1%2%1" ).arg( eText ).arg( selectedText() )
);
setSelection( startLine, startPos + 1, endLine, endPos + 1 );
event->accept();
return;
}



// Let QgsCodeEditor handle the keyboard event
return QgsCodeEditor::keyPressEvent( event );
}
Expand Down

0 comments on commit e992797

Please sign in to comment.