Is there a way to make eclipse wrap the line with the b's to a length of 120 per line?
I wasn't able to configure the code formatter to wrap the line. This really drives me crazy...
public class Position {
public static void i() {
error("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb");
}
private static void error(String string) {
// TODO Auto-generated method stub
}
}
解决方案
I tested user714695's suggestion: by pressing enter in the middle of a string, the pluses, quotes, and indentation are automatically placed correctly.
This post Eclipse Shortcut to Split Long Strings has some more discussion on the issue.
On the other hand, to my knowledge, there's no built-in way to do this: you would like to highlight a string and auto-format it to place newlines and +'s appropriately.
I recently wanted to solve a similar problem where the goal is to highlight a paragraph and wrap the words once the number of characters in the line is >= 78 characters (similar to the 'gq' functionality in Vim). Since I couldn't find immediately a way to do this online, I decided to see how easy it was to write a plugin. It turned out to be a lot easier than I thought, so I thought I'd post some basic instructions if this interests you.
Create a new plugin project
Choose the Hello World, Command one to start with
Add the necessary eclipse libraries to the plugin dependencies. Right click on the project, go to PDE Tools, and 'Open Manifest' there is a dependencies tab. This is the project overview page (if it's not already open for you) . I added org.eclipse.jface.text and org.eclipse.ui.workbench.texteditor.
Edit the SampleHandler.java file to process the highlighted text and replace it in the document.
If you click the 'play' button accessible from the project overview button, a new instance of eclipse will launch so you can test and interact with it.
Edit the 'plugins.xml' (also accessible from the project overview page)
Once you are happy with the plugin, follow the instructions for exporting in the project overview page. If you choose the 'Directory' option, a jar will be placed in there. Add this jar to your workspace/.metadata/.plugins/ directory or any other path that Eclipse looks for plugins.
Below is some very basic sample code that does the word wrapping in Scala, the 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
}
doc.replace(offset,length,wrapped)
return null;
}
Hope this helps