Skip to content

Commit

Permalink
GP-4605 - Allow Ctrl-a to select all in the Python window
Browse files Browse the repository at this point in the history
  • Loading branch information
dragonmacher committed May 20, 2024
1 parent 75a86e5 commit 9ff21db
Showing 1 changed file with 106 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,101 +195,9 @@ public void remove(FilterBypass fb, int offset, int length)
}
});

outputTextPane.addKeyListener(new KeyListener() {
private void handleEvent(KeyEvent e) {

// Ignore the copy event, as the output text pane knows how to copy its text
KeyStroke copyKeyStroke =
KeyStroke.getKeyStroke(KeyEvent.VK_C, DockingUtils.CONTROL_KEY_MODIFIER_MASK);
if (copyKeyStroke.equals(KeyStroke.getKeyStrokeForEvent(e))) {
return;
}

// Send everything else down to the inputTextPane.
KeyBindingUtils.retargetEvent(inputTextPane, e);
}

@Override
public void keyTyped(KeyEvent e) {
handleEvent(e);
}

@Override
public void keyReleased(KeyEvent e) {
handleEvent(e);
}

@Override
public void keyPressed(KeyEvent e) {
handleEvent(e);
}
});

inputTextPane.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
CodeCompletionWindow completionWindow = getCodeCompletionWindow();

switch (e.getKeyCode()) {
case KeyEvent.VK_ENTER:
if (completionWindow.isVisible()) {
/* As opposed to TAB, ENTER inserts the selected
* completion (if there is one selected) then
* *closes* the completionWindow
*/
insertCompletion(completionWindow.getCompletion());
completionWindow.setVisible(false);
e.consume();
}
else {
inputTextPane.setCaretPosition(inputTextPane.getDocument().getLength());
}
break;
case KeyEvent.VK_UP:
if (completionWindow.isVisible()) {
/* scroll up in the completion window */
completionWindow.selectPrevious();
}
else {
String historyUp = history.getHistoryUp();
if (historyUp != null) {
setInputTextPaneText(historyUp);
}
}
e.consume();
break;
case KeyEvent.VK_DOWN:
if (completionWindow.isVisible()) {
/* scroll down in the completion window */
completionWindow.selectNext();
}
else {
String historyDown = history.getHistoryDown();
if (historyDown != null) {
setInputTextPaneText(historyDown);
}
}
e.consume();
break;
case KeyEvent.VK_ESCAPE:
completionWindow.setVisible(false);
e.consume();
break;
default:

// Check for the completion window trigger on input that contains text
if (completionWindowTrigger.isTrigger(e) &&
!inputTextPane.getText().trim().isEmpty()) {
completionWindowTriggered(completionWindow);
e.consume();
break;
}
outputTextPane.addKeyListener(new OutputTextPaneKeyListener());

updateCompletionList();
// and let the key go through to the text input field
}
}
});
inputTextPane.addKeyListener(new InputTextPaneKeyListener());

outputTextPane.addCaretListener(e -> {
Caret caret = inputTextPane.getCaret();
Expand Down Expand Up @@ -772,4 +680,108 @@ void resetStream() {
queuedBytes.offer(EMPTY_BYTES);
}
}

private class OutputTextPaneKeyListener implements KeyListener {

private final KeyStroke COPY_KEY_STROKE =
KeyStroke.getKeyStroke(KeyEvent.VK_C, DockingUtils.CONTROL_KEY_MODIFIER_MASK);
KeyStroke SELECT_ALL_KEY_STROKE =
KeyStroke.getKeyStroke(KeyEvent.VK_A, DockingUtils.CONTROL_KEY_MODIFIER_MASK);

private void handleEvent(KeyEvent e) {

// Ignore the events we wish for the output text pane to process
if (COPY_KEY_STROKE.equals(KeyStroke.getKeyStrokeForEvent(e))) {
return;
}

if (SELECT_ALL_KEY_STROKE.equals(KeyStroke.getKeyStrokeForEvent(e))) {
return;
}

// Send everything else down to the inputTextPane.
KeyBindingUtils.retargetEvent(inputTextPane, e);
}

@Override
public void keyTyped(KeyEvent e) {
handleEvent(e);
}

@Override
public void keyReleased(KeyEvent e) {
handleEvent(e);
}

@Override
public void keyPressed(KeyEvent e) {
handleEvent(e);
}
}

private class InputTextPaneKeyListener extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
CodeCompletionWindow completionWindow = getCodeCompletionWindow();

switch (e.getKeyCode()) {
case KeyEvent.VK_ENTER:
if (completionWindow.isVisible()) {
/* As opposed to TAB, ENTER inserts the selected
* completion (if there is one selected) then
* *closes* the completionWindow
*/
insertCompletion(completionWindow.getCompletion());
completionWindow.setVisible(false);
e.consume();
}
else {
inputTextPane.setCaretPosition(inputTextPane.getDocument().getLength());
}
break;
case KeyEvent.VK_UP:
if (completionWindow.isVisible()) {
/* scroll up in the completion window */
completionWindow.selectPrevious();
}
else {
String historyUp = history.getHistoryUp();
if (historyUp != null) {
setInputTextPaneText(historyUp);
}
}
e.consume();
break;
case KeyEvent.VK_DOWN:
if (completionWindow.isVisible()) {
/* scroll down in the completion window */
completionWindow.selectNext();
}
else {
String historyDown = history.getHistoryDown();
if (historyDown != null) {
setInputTextPaneText(historyDown);
}
}
e.consume();
break;
case KeyEvent.VK_ESCAPE:
completionWindow.setVisible(false);
e.consume();
break;
default:

// Check for the completion window trigger on input that contains text
if (completionWindowTrigger.isTrigger(e) &&
!inputTextPane.getText().trim().isEmpty()) {
completionWindowTriggered(completionWindow);
e.consume();
break;
}

updateCompletionList();
// and let the key go through to the text input field
}
}
}
}

0 comments on commit 9ff21db

Please sign in to comment.