2009-08-05 15 views
3

Eclipseエディタで選択したコードセクション(マウスの選択で選択)を置き換え、プラグインを使用して/* selected text */の同じコードに置き換えるにはどうすればよいですか? 私はすでにツールバーにボタンを作成するためのプラグインを設計しました。クリックすると、選択したテキストを変更して/* */に挿入する必要があります。プラグインコマンドを使用してEclipseエディタから選択したコードを置き換えます

答えて

7

あなたの仕事を行うのに十分なヒントを与えるべきである、このスニペットを試してみてください。

try {    
    IEditorPart part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); 
    if (part instanceof ITextEditor) { 
     final ITextEditor editor = (ITextEditor)part; 
     IDocumentProvider prov = editor.getDocumentProvider(); 
     IDocument doc = prov.getDocument(editor.getEditorInput()); 
     ISelection sel = editor.getSelectionProvider().getSelection(); 
     if (sel instanceof TextSelection) { 
      final TextSelection textSel = (TextSelection)sel; 
      String newText = "/*" + textSel.getText() + "*/"; 
      doc.replace(textSel.getOffset(), textSel.getLength(), newText); 
     } 
    } 
} catch (Exception ex) { 
    ex.printStackTrace(); 
}  
関連する問題