1 /* 2 * InputEditorUtil.java 3 * 4 * Copyright (C) 2021 by RStudio, PBC 5 * 6 * Unless you have received this program directly from RStudio pursuant 7 * to the terms of a commercial license agreement with RStudio, then 8 * this program is licensed to you under the terms of version 3 of the 9 * GNU Affero General Public License. This program is distributed WITHOUT 10 * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, 11 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the 12 * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. 13 * 14 */ 15 package org.rstudio.studio.client.workbench.views.console.shell.editor; 16 17 public class InputEditorUtil 18 { yankAfterCursor(final InputEditorDisplay editor, final boolean saveValue)19 public static void yankAfterCursor(final InputEditorDisplay editor, 20 final boolean saveValue) 21 { 22 InputEditorSelection selection = editor.getSelection(); 23 if (selection != null) 24 { 25 selection = selection.extendToLineEnd(); 26 27 editor.setSelection(selection); 28 29 editor.setFocus(true); 30 String yanked = editor.replaceSelection("", true); 31 if (saveValue) 32 lastYanked = yanked; 33 } 34 } 35 yankBeforeCursor(final InputEditorDisplay editor, final boolean saveValue)36 public static void yankBeforeCursor(final InputEditorDisplay editor, 37 final boolean saveValue) 38 { 39 InputEditorSelection selection = editor.getSelection(); 40 if (selection != null) 41 { 42 selection = selection.extendToLineStart(); 43 44 editor.setSelection(selection); 45 46 editor.setFocus(true); 47 String yanked = editor.replaceSelection("", true); 48 if (saveValue) 49 lastYanked = yanked; 50 } 51 } 52 pasteYanked(InputEditorDisplay editor)53 public static void pasteYanked(InputEditorDisplay editor) 54 { 55 if (lastYanked != null) 56 { 57 editor.replaceSelection(lastYanked, true); 58 } 59 } 60 getLineWithCursorPosition( InputEditorDisplay editor)61 public static InputEditorLineWithCursorPosition getLineWithCursorPosition( 62 InputEditorDisplay editor) 63 { 64 String line; 65 int pos; 66 if (editor.getSelection().isEmpty()) 67 { 68 line = editor.getText(); 69 pos = editor.getSelection().getStart().getPosition(); 70 // Move pos to the right until we get to a break 71 for (; pos < line.length() && isRIdentifierChar(line.charAt(pos)); pos++) 72 { 73 } 74 } 75 else 76 { 77 line = editor.getSelectionValue(); 78 pos = line.length(); 79 } 80 81 return new InputEditorLineWithCursorPosition(line, pos); 82 } 83 isRIdentifierChar(char ch)84 private static boolean isRIdentifierChar(char ch) 85 { 86 return Character.isLetterOrDigit(ch) || ch == '.' || ch == '_'; 87 } 88 89 private static String lastYanked; 90 } 91