├── icons └── sample.gif ├── linecopypaste.jar ├── .classpath ├── .settings └── org.eclipse.jdt.core.prefs ├── META-INF └── MANIFEST.MF ├── .project ├── src └── com │ └── larsch │ └── linecopypaste │ ├── handlers │ ├── CutHandler.java │ ├── CopyHandler.java │ └── PasteHandler.java │ ├── Activator.java │ └── Utility.java ├── linecopypaste.jardesc ├── plugin.xml └── README.markdown /icons/sample.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/larsch/eclipse-linecopypaste/master/icons/sample.gif -------------------------------------------------------------------------------- /linecopypaste.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/larsch/eclipse-linecopypaste/master/linecopypaste.jar -------------------------------------------------------------------------------- /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /.settings/org.eclipse.jdt.core.prefs: -------------------------------------------------------------------------------- 1 | #Sun Jan 30 12:53:23 CET 2011 2 | eclipse.preferences.version=1 3 | org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled 4 | org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 5 | org.eclipse.jdt.core.compiler.compliance=1.6 6 | org.eclipse.jdt.core.compiler.problem.assertIdentifier=error 7 | org.eclipse.jdt.core.compiler.problem.enumIdentifier=error 8 | org.eclipse.jdt.core.compiler.source=1.6 9 | -------------------------------------------------------------------------------- /META-INF/MANIFEST.MF: -------------------------------------------------------------------------------- 1 | Manifest-Version: 1.0 2 | Bundle-ManifestVersion: 2 3 | Bundle-Name: LineCopyPaste 4 | Bundle-SymbolicName: com.larsch.linecopypaste;singleton:=true 5 | Bundle-Version: 1.0.0.qualifier 6 | Bundle-Activator: com.larsch.linecopypaste.Activator 7 | Bundle-Vendor: larsch 8 | Require-Bundle: org.eclipse.ui, 9 | org.eclipse.core.runtime, 10 | org.eclipse.jface.text;bundle-version="3.6.1", 11 | org.eclipse.ui.workbench.texteditor;bundle-version="3.6.1" 12 | Bundle-RequiredExecutionEnvironment: JavaSE-1.6 13 | Bundle-ActivationPolicy: lazy 14 | -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | com.larsch.linecopypaste 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | org.eclipse.pde.ManifestBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.pde.SchemaBuilder 20 | 21 | 22 | 23 | 24 | 25 | org.eclipse.pde.PluginNature 26 | org.eclipse.jdt.core.javanature 27 | 28 | 29 | -------------------------------------------------------------------------------- /src/com/larsch/linecopypaste/handlers/CutHandler.java: -------------------------------------------------------------------------------- 1 | package com.larsch.linecopypaste.handlers; 2 | 3 | import org.eclipse.core.commands.AbstractHandler; 4 | import org.eclipse.core.commands.ExecutionEvent; 5 | import org.eclipse.core.commands.ExecutionException; 6 | import org.eclipse.jface.text.ITextSelection; 7 | import org.eclipse.ui.texteditor.ITextEditor; 8 | import org.eclipse.ui.texteditor.ITextEditorActionConstants; 9 | 10 | import com.larsch.linecopypaste.Utility; 11 | 12 | /** 13 | * The CutHandler wraps the default cut actions CUT and CUT_LINE. If nothing is 14 | * currently selected, CUT_LINE is executed, otherwise the standard CUT. 15 | */ 16 | public class CutHandler extends AbstractHandler { 17 | public Object execute(ExecutionEvent event) throws ExecutionException { 18 | ITextSelection textSelection = Utility.getTextSelection(event); 19 | ITextEditor editor = Utility.getEditorForEvent(event); 20 | if (textSelection.getLength() == 0) { 21 | editor.getAction(ITextEditorActionConstants.CUT_LINE).run(); 22 | } else { 23 | editor.getAction(ITextEditorActionConstants.CUT).run(); 24 | } 25 | return null; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /linecopypaste.jardesc: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /plugin.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /src/com/larsch/linecopypaste/Activator.java: -------------------------------------------------------------------------------- 1 | package com.larsch.linecopypaste; 2 | 3 | import org.eclipse.jface.resource.ImageDescriptor; 4 | import org.eclipse.ui.plugin.AbstractUIPlugin; 5 | import org.osgi.framework.BundleContext; 6 | 7 | /** 8 | * The activator class controls the plug-in life cycle 9 | */ 10 | public class Activator extends AbstractUIPlugin { 11 | 12 | // The plug-in ID 13 | public static final String PLUGIN_ID = "com.larsch.linecopypaste"; //$NON-NLS-1$ 14 | 15 | // The shared instance 16 | private static Activator plugin; 17 | 18 | /** 19 | * The constructor 20 | */ 21 | public Activator() { 22 | } 23 | 24 | /* 25 | * (non-Javadoc) 26 | * 27 | * @see 28 | * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext 29 | * ) 30 | */ 31 | public void start(BundleContext context) throws Exception { 32 | super.start(context); 33 | plugin = this; 34 | } 35 | 36 | /* 37 | * (non-Javadoc) 38 | * 39 | * @see 40 | * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext 41 | * ) 42 | */ 43 | public void stop(BundleContext context) throws Exception { 44 | plugin = null; 45 | super.stop(context); 46 | } 47 | 48 | /** 49 | * Returns the shared instance 50 | * 51 | * @return the shared instance 52 | */ 53 | public static Activator getDefault() { 54 | return plugin; 55 | } 56 | 57 | /** 58 | * Returns an image descriptor for the image file at the given plug-in 59 | * relative path 60 | * 61 | * @param path 62 | * the path 63 | * @return the image descriptor 64 | */ 65 | public static ImageDescriptor getImageDescriptor(String path) { 66 | return imageDescriptorFromPlugin(PLUGIN_ID, path); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | # LineCopyPaste (Eclipse Plugin) 2 | 3 | This is an Eclipse plugin that provides "smarter versions" of Copy/Cut/Paste actions. 4 | When nothing is selected, the Copy and Cut actions will work on the current line. Paste 5 | will insert lines at the start of the current line rather than at the current cursor location. 6 | When anything is selected, the default Copy/Cut/Paste behaviour will function without any 7 | modifications. 8 | 9 | ## Installation 10 | 11 | Copy linecopypaste.jar to the plugins directory under the Eclipse installation and restart Eclipse. 12 | The LineCopyPaste plugin overrides Ctrl-X/C/V by default, but the bindings can be changed 13 | 14 | ## Copyright 15 | 16 | Copyright (c) 2011 Lars Christensen 17 | 18 | Permission is hereby granted, free of charge, to any person obtaining a copy 19 | of this software and associated documentation files (the "Software"), to deal 20 | in the Software without restriction, including without limitation the rights 21 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 | copies of the Software, and to permit persons to whom the Software is 23 | furnished to do so, subject to the following conditions: 24 | 25 | The above copyright notice and this permission notice shall be included in 26 | all copies or substantial portions of the Software. 27 | 28 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 29 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 30 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 31 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 32 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 33 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 34 | THE SOFTWARE. -------------------------------------------------------------------------------- /src/com/larsch/linecopypaste/handlers/CopyHandler.java: -------------------------------------------------------------------------------- 1 | package com.larsch.linecopypaste.handlers; 2 | 3 | import org.eclipse.core.commands.AbstractHandler; 4 | import org.eclipse.core.commands.ExecutionEvent; 5 | import org.eclipse.core.commands.ExecutionException; 6 | import org.eclipse.jface.text.BadLocationException; 7 | import org.eclipse.jface.text.IDocument; 8 | import org.eclipse.jface.text.IRegion; 9 | import org.eclipse.jface.text.ITextSelection; 10 | import org.eclipse.swt.dnd.TextTransfer; 11 | import org.eclipse.swt.dnd.Transfer; 12 | import org.eclipse.ui.PlatformUI; 13 | import org.eclipse.ui.texteditor.ITextEditor; 14 | import org.eclipse.ui.texteditor.ITextEditorActionConstants; 15 | 16 | import com.larsch.linecopypaste.Utility; 17 | 18 | /** 19 | * The CopyHandler replaces the default copy COPY action. If nothing is 20 | * currently selected, places the contents of the current line in the clipboard, 21 | * otherwise runs the standard COPY. 22 | */ 23 | public class CopyHandler extends AbstractHandler { 24 | public Object execute(ExecutionEvent event) throws ExecutionException { 25 | ITextSelection textSelection = Utility.getTextSelection(event); 26 | ITextEditor editor = Utility.getEditorForEvent(event); 27 | if (textSelection.getLength() == 0) { 28 | org.eclipse.swt.dnd.Clipboard cb = new org.eclipse.swt.dnd.Clipboard( 29 | PlatformUI.getWorkbench().getDisplay()); 30 | IDocument doc = Utility.getCurrentDocument(event); 31 | try { 32 | IRegion line = doc.getLineInformation(textSelection 33 | .getStartLine()); 34 | String text = doc.get(line.getOffset(), line.getLength()); 35 | Object[] data = { text + "\n" }; 36 | Transfer[] transfers = { TextTransfer.getInstance() }; 37 | cb.setContents(data, transfers); 38 | } catch (BadLocationException e) { 39 | } 40 | } else { 41 | editor.getAction(ITextEditorActionConstants.COPY).run(); 42 | } 43 | return null; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/com/larsch/linecopypaste/Utility.java: -------------------------------------------------------------------------------- 1 | package com.larsch.linecopypaste; 2 | 3 | import org.eclipse.core.commands.ExecutionEvent; 4 | import org.eclipse.core.commands.ExecutionException; 5 | import org.eclipse.jface.text.IDocument; 6 | import org.eclipse.jface.text.ITextSelection; 7 | import org.eclipse.ui.IEditorPart; 8 | import org.eclipse.ui.handlers.HandlerUtil; 9 | import org.eclipse.ui.part.MultiPageEditorPart; 10 | import org.eclipse.ui.texteditor.IDocumentProvider; 11 | import org.eclipse.ui.texteditor.ITextEditor; 12 | 13 | /** 14 | * Utility methods for the LineClipboard plugin (stolen from nflath's plugin at 15 | * https://github.com/nflath/eclipse-hungry-delete/) 16 | */ 17 | public class Utility { 18 | 19 | public static ITextEditor getEditorForEvent(ExecutionEvent event) 20 | throws ExecutionException { 21 | IEditorPart part = HandlerUtil.getActiveEditorChecked(event); 22 | if (part instanceof ITextEditor) 23 | { 24 | return (ITextEditor) part; 25 | } 26 | else if (part instanceof MultiPageEditorPart) 27 | { 28 | MultiPageEditorPart multiPart = (MultiPageEditorPart)part; 29 | Object page = multiPart.getSelectedPage(); 30 | if (page instanceof ITextEditor) 31 | { 32 | return (ITextEditor)page; 33 | } 34 | } 35 | return null; 36 | } 37 | 38 | public static IDocument getCurrentDocument(ExecutionEvent event) 39 | throws ExecutionException { 40 | IDocumentProvider dp = getEditorForEvent(event).getDocumentProvider(); 41 | return dp.getDocument(getEditorForEvent(event).getEditorInput()); 42 | } 43 | 44 | public static ITextSelection getTextSelection(ExecutionEvent event) 45 | throws ExecutionException { 46 | return (ITextSelection) getEditorForEvent(event).getSelectionProvider() 47 | .getSelection(); 48 | } 49 | 50 | public static int getCurrentOffset(ExecutionEvent event) 51 | throws ExecutionException { 52 | return getTextSelection(event).getOffset(); 53 | } 54 | 55 | public static boolean selectionDefined(ExecutionEvent event) 56 | throws ExecutionException { 57 | return getTextSelection(event).getLength() != 0; 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/com/larsch/linecopypaste/handlers/PasteHandler.java: -------------------------------------------------------------------------------- 1 | package com.larsch.linecopypaste.handlers; 2 | 3 | import org.eclipse.core.commands.AbstractHandler; 4 | import org.eclipse.core.commands.ExecutionEvent; 5 | import org.eclipse.core.commands.ExecutionException; 6 | import org.eclipse.jface.text.BadLocationException; 7 | import org.eclipse.jface.text.IDocument; 8 | import org.eclipse.jface.text.ITextSelection; 9 | import org.eclipse.jface.text.TextSelection; 10 | import org.eclipse.swt.dnd.TextTransfer; 11 | import org.eclipse.ui.PlatformUI; 12 | import org.eclipse.ui.texteditor.ITextEditor; 13 | import org.eclipse.ui.texteditor.ITextEditorActionConstants; 14 | 15 | import com.larsch.linecopypaste.Utility; 16 | 17 | /** 18 | * The paste handler wraps the default paste action. If the contents of the 19 | * clipboard contains a newline at the end, and nothing is currently selected, 20 | * the cursor is moved to the start of the line to past the lines as is, rather 21 | * than in the middle of the text. 22 | */ 23 | public class PasteHandler extends AbstractHandler { 24 | public Object execute(ExecutionEvent event) throws ExecutionException { 25 | ITextEditor editor = Utility.getEditorForEvent(event); 26 | org.eclipse.swt.dnd.Clipboard cb = new org.eclipse.swt.dnd.Clipboard( 27 | PlatformUI.getWorkbench().getDisplay()); 28 | TextTransfer tt = TextTransfer.getInstance(); 29 | cb.getContents(tt); 30 | String textData = (String) cb.getContents(tt); 31 | 32 | ITextSelection textSelection = Utility.getTextSelection(event); 33 | 34 | int adjustment = 0; 35 | 36 | boolean lastCharIsNewLine = textData.charAt(textData.length() - 1) == 10; 37 | boolean selectionIsEmpty = textSelection.getLength() == 0; 38 | 39 | if (lastCharIsNewLine && selectionIsEmpty) { 40 | IDocument doc = Utility.getCurrentDocument(event); 41 | try { 42 | // Move cursor to start of line 43 | int lineOffset = doc 44 | .getLineOffset(textSelection.getStartLine()); 45 | TextSelection ts = new TextSelection(lineOffset, 0); 46 | editor.getSelectionProvider().setSelection(ts); 47 | adjustment = lineOffset - textSelection.getOffset(); 48 | } catch (BadLocationException e) { 49 | } 50 | } 51 | 52 | // Paste! 53 | editor.getAction(ITextEditorActionConstants.PASTE).run(); 54 | 55 | // Move the cursor back where it was 56 | if (adjustment != 0) { 57 | ITextSelection newSelection = Utility.getTextSelection(event); 58 | TextSelection ts = new TextSelection(newSelection.getOffset() 59 | - adjustment, 0); 60 | editor.getSelectionProvider().setSelection(ts); 61 | } 62 | return null; 63 | } 64 | } 65 | --------------------------------------------------------------------------------