eclipse java换行,如何强制eclipse换行?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值