eclipse 驼峰插件

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("(?

plugin.xml





   
      
      
      
      
   
   
      
      
   
   
      
      
   
   

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

eclipse 驼峰插件_第1张图片

com.dgqjava.shortcuts_1.0.0.201712271252.jar

选中eclipse编辑器中的一段内容
例如ab_cd_ef按快捷键ctrl+shift+z则会转化为abCdEf,
再按一下快捷键转化回ab_cd_ef, 默认驼峰转为下划线后为全小写,
因为另一个自带的转大写快捷键ctrl+shift+x就在该插件的快捷键旁边,
如果需要 转大写可以很方便用自带快捷键实现

你可能感兴趣的:(java)