EditorShortcutsBaseHandler
package com.dgqjava.shortcuts.handlers;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.MultiPageEditorPart;
import org.eclipse.ui.texteditor.IDocumentProvider;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.wst.sse.ui.StructuredTextEditor;
public class EditorShortcutsBaseHandler extends AbstractHandler
{
public Object execute(ExecutionEvent event)
throws ExecutionException
{
IEditorPart iep = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
ITextEditor ite = null;
TextSelection selection = null;
IDocument document = null;
ISelectionProvider selectionProvider = null;
if ((iep instanceof ITextEditor)) {
ite = (ITextEditor)iep;
selectionProvider = ite.getSelectionProvider();
selection = (TextSelection)selectionProvider.getSelection();
document = ite.getDocumentProvider().getDocument(ite.getEditorInput());
} else if ((iep instanceof MultiPageEditorPart)) {
MultiPageEditorPart part = (MultiPageEditorPart)iep;
Object sp = part.getSelectedPage();
if (!(sp instanceof StructuredTextEditor)) {
return null;
}
ite = (StructuredTextEditor)sp;
selectionProvider = ite.getSelectionProvider();
selection = (TextSelection)selectionProvider.getSelection();
document = ite.getDocumentProvider().getDocument(ite.getEditorInput());
} else {
return null;
}
if ((selection == null) || (document == null) || (selectionProvider == null)) {
return null;
}
String title = iep.getTitle();
TextProcessResult result = process(document.get(), selection.getOffset(), selection.getLength(), (title != null) && (title.endsWith(".java")));
if (result == null) {
return null;
}
try
{
document.replace(result.replaceOffset, result.replaceLength, result.replaceText);
} catch (BadLocationException e) {
return null;
}
ite.selectAndReveal(result.newSelectionOffset, result.newSelectionLength);
return null;
}
protected TextProcessResult process(String text, int selectionOffset, int selectionLength, boolean isJavaFile) {
if ((text == null) || (selectionOffset < 0) || (selectionLength <= 2)) {
return null;
}
String selected = text.substring(selectionOffset, selectionOffset + selectionLength);
if (selected.indexOf('_') != -1) {
selected = selected.replaceAll("_([A-Z])", "$1");
int i = -32;
for (char c = 'a'; c <= 'z'; c = (char)(c + '\001'))
selected = selected.replaceAll("_" + c, String.valueOf((char)(c + i)));
}
else {
selected = selected.replaceAll("(?<![A-Z])([A-Z])", "_$1");
selected = selected.toLowerCase();
if (selected.charAt(0) == '_') {
selected = selected.substring(1);
}
}
TextProcessResult result = new TextProcessResult();
result.replaceText = selected;
result.newSelectionOffset = selectionOffset;
result.newSelectionLength = selected.length();
result.replaceOffset = selectionOffset;
result.replaceLength = selectionLength;
return result;
}
protected static final class TextProcessResult
{
int newSelectionOffset;
int newSelectionLength;
int replaceOffset;
int replaceLength;
String replaceText;
}
}
plugin.xml
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.0"?>
<plugin>
<extension
point="org.eclipse.ui.commands">
<category
name="Category1"
id="com.dgqjava.shortcuts.commands.category1">
</category>
<command
name="Command1"
categoryId="com.dgqjava.shortcuts.commands.category1"
id="com.dgqjava.shortcuts.commands.command1">
</command>
</extension>
<extension
point="org.eclipse.ui.handlers">
<handler
commandId="com.dgqjava.shortcuts.commands.command1"
class="com.dgqjava.shortcuts.handlers.EditorShortcutsBaseHandler">
</handler>
</extension>
<extension
point="org.eclipse.ui.bindings">
<key
commandId="com.dgqjava.shortcuts.commands.command1"
contextId="org.eclipse.ui.contexts.window"
sequence="M1+M2+Z"
schemeId="org.eclipse.ui.defaultAcceleratorConfiguration">
</key>
</extension>
</plugin>
MANIFEST.MF
Manifest-Version: 1.0
Bundle-SymbolicName: com.dgqjava.shortcuts;singleton:=true
Bundle-Name: Shortcuts
Bundle-Version: 1.0.0.201712271252
Require-Bundle: org.eclipse.core.commands,org.eclipse.ui.workbench.tex
teditor,org.eclipse.text,org.eclipse.jface.text,org.eclipse.jface,org
.eclipse.wst.sse.ui,org.eclipse.ui.workbench
Bundle-ManifestVersion: 2
Bundle-RequiredExecutionEnvironment: J2SE-1.5
Bundle-Vendor: dgqjava
com.dgqjava.shortcuts_1.0.0.201712271252.jar
选中eclipse编辑器中的一段内容
例如ab_cd_ef按快捷键ctrl+shift+z则会转化为abCdEf,
再按一下快捷键转化回ab_cd_ef, 默认驼峰转为下划线后为全小写,
因为另一个自带的转大写快捷键ctrl+shift+x就在该插件的快捷键旁边,
如果需要 转大写可以很方便用自带快捷键实现