如何强制eclipse换行? [英] How to force eclipse wrap that line?

查看:358
本文介绍了如何强制eclipse换行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使eclipse包装线的b的到每行120的长度?
我无法配置代码格式化程序换行。这真的让我疯了...

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

    }
}


推荐答案

我测试了user714695的建议:

I tested user714695's suggestion: by pressing enter in the middle of a string, the pluses, quotes, and indentation are automatically placed correctly.

此帖子分割长字符串的Eclipse快捷方式有关此问题的更多讨论。

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.

我最近想要解决一个类似的问题,其目的是突出一个段落,并在行中的字符数> 78个字符(类似于'gq'在Vim中的功能)包装单词。由于我不能立即找到一个在线的方法,我决定看看写一个插件是多么容易。原来是比我想象的很容易,所以我想我会发布一些基本的说明,如果这感兴趣你。

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.


  1. 创建一个新的插件项目

  2. 选择Hello World,命令一开始

  3. 向插件依赖项添加必要的eclipse库。右键单击项目,转到PDE工具,并且打开清单有一个依赖项选项卡。这是项目概述页面(如果它尚未打开)。我添加了org.eclipse.jface.text和org.eclipse.ui.workbench.texteditor。

  4. 编辑SampleHandler.java文件以处理突出显示的文本并在文档中替换。 / li>
  5. 如果单击从项目概述按钮访问的播放按钮,eclipse的新实例将启动,以便您可以测试和与之交互。

  6. 编辑plugins.xml(也可从项目概述页面访问)

  7. 对插件满意后,请按照项目概述页面中的导出说明进行操作。如果你选择'目录'选项,一个jar将放在那里。将此jar添加到您的工作区/ .metadata / .plugins /目录或Eclipse查找插件的任何其他路径。

  1. Create a new plugin project
  2. Choose the Hello World, Command one to start with
  3. 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.
  4. Edit the SampleHandler.java file to process the highlighted text and replace it in the document.
  5. 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.
  6. Edit the 'plugins.xml' (also accessible from the project overview page)
  7. 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.

在Scala中执行词包装的非常基本的示例代码,我用来编写SampleHandler的语言。肉在执行函数中:

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;
}

希望这有助于

这篇关于如何强制eclipse换行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆