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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With