How do I get eclipse to wrap this line?

Is there a way to get eclipse to wrap a 120-length b string per string? I have not been able to customize the code formatting for string wrapping. It really drives me crazy ...

public class Position {
    public static void i() {


    private static void error(String string) {
        // TODO Auto-generated method stub




source to share

4 answers

I tried out user714695's suggestion: by hitting enter in the middle of the line, the pluses, quotes and indentation are automatically placed correctly.

This post Eclipse Shortcut to Split Long Strings has some more discussion on this issue.

On the other hand, as far as I know, there is no built-in way to do this: you would like to highlight the line and automatically format it to accommodate the newline and + characters respectively.

I recently wanted to solve a similar problem in which the goal is to select a paragraph and wrap words as soon as the number of characters in the line is> = 78 characters (similar to the gq functionality in Vim). Since I couldn't find a way to do this online, I decided to see how easy it was to write the plugin. This turned out to be much easier than I thought, so I thought I'd post some basic instructions if you're interested.

  • Create a new plugin project
  • Select Hello World, Command to start with
  • Add the required eclipse libraries depending on the plug-in. Right click on the project, go to PDE Tools and "Open Manifest" is the dependencies tab. This is the project overview page (if not already open for you). I added org.eclipse.jface.text and org.eclipse.ui.workbench.texteditor.
  • Edit the file to process the selected text and replace it in the document.
  • If you click the play button, accessible from the project's browse button, a new instance of eclipse will open up for you to test and interact with.
  • Edit "plugins.xml" (also available on the project overview page).
  • Once you're happy with the plugin, follow the export instructions on the project overview page. If you choose the "Catalog" option, the bank will be placed there. Add this jar to your workspace directory / .metadata / .plugins / or whatever path Eclipse looks for plugins.

Below is a very simple example code that does word wrapping in Scala, the language I used to write SampleHandler. The meat is in the "execute" function:

def execute(event: ExecutionEvent ): Object = {
  val window = HandlerUtil.getActiveWorkbenchWindowChecked(event)
  val editorPart = window.getActivePage().getActiveEditor()
  var offset = 0
  var length = 0
  var selectedText = ""

  val iSelection = editorPart.getEditorSite().getSelectionProvider().getSelection()
  val selection = iSelection.asInstanceOf[ITextSelection]
  offset = selection.getOffset()

  if (!iSelection.isEmpty()) {
    selectedText = selection.getText()

  length = selection.getLength()

  val editor = editorPart.asInstanceOf[ITextEditor]
  val dp = editor.getDocumentProvider()
  val doc = dp.getDocument(editor.getEditorInput())
  val words = selectedText.split("""\s+""")
  var wrapped = ""
  var linesize = 0

  words.foreach{ w => 

    if(linesize+w.size >= 78) { 
      wrapped += "\n" 
      linesize = 0  

    wrapped += w + " "
    linesize += w.size + 1


  return null;


Hope it helps



No, eclipse will not split String. If you place the cursor at some position on the line and press Enter, you might want what you want.



It looks like formatting or clearing won't fix this, but there is a preference for "Wrap automatically" strings that can be used when inserting a String ... Just enter a keyword like "String" at the top of the preferences window.



You might be able to do what you want with Search / Replace ( Ctrl+ f) with regex. There must be some way to capture the first N characters of the line and the rest of the line in separate capture groups and then insert "+"

between those groups in the Replace box. I'm not a regex guru, but unfortunately I can't imagine a magic formula ...

Once you split the line, you can use code formatting ( Ctrl+ Shift+ f) to fix the line breaks.



All Articles