有什么方法可以在 Java/Eclipse 中对方法进行分组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3589065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:03:17  来源:igfitidea点击:

Any way to group methods in Java/Eclipse?

javaeclipsepragma

提问by Sheehan Alam

I would like to be able to group similar methods and have them appear in my Outline view in Eclipse. This makes navigating large swaths of code a little easier on the eye, and easier to find methods you need. In Objective-C there was a pragma mark command that you could set.

我希望能够对类似的方法进行分组,并让它们出现在 Eclipse 的大纲视图中。这使得在眼睛上浏览大量代码更容易一些,并且更容易找到您需要的方法。在Objective-C 中有一个pragma mark 命令可以设置。

Anything like that for java/eclipse?

java/eclipse 有类似的东西吗?

采纳答案by Vineet Reynolds

I use the Coffee Bytes pluginfor code folding, specifically configuring it for folding code that has start and end tags.

我使用Coffee Bytes 插件进行代码折叠,专门针对具有开始和结束标记的折叠代码进行配置。

Although the plugin is not downloadable off the page listed on the Google Codepage, it has been recompiled against Eclipse 3.5 and made available elsewhere; the version appears to work against Eclipse 3.6 and 3.7 as well. It is also available in the Yoxos marketplace.

尽管该插件不能从Google 代码上列出页面下载,但它已针对 Eclipse 3.5 重新编译并在其他地方提供;该版本似乎也适用于 Eclipse 3.6 和 3.7。它也可以在Yoxos 市场上买到

I use the following notation to group getters and setters of properties along with declaration of the property, although the same notation could be extended for your use.

我使用以下表示法将属性的 getter 和 setter 与属性声明一起分组,尽管可以扩展相同的表示法供您使用。

// {{ Id
private String id;

public String getId() {
    return id;
}

public void setId(final String id) {
    this.id = id;
}
// }}

Configuration of the same needs to be done by setting appropriate preference in the code folding section available via Windows > Preferences > Java > Editor > Folding. Remember to choose Coffee Bytes Java Folding, and enable support for User Defined Regions.

需要通过在Windows > Preferences > Java > Editor > Folding可用的代码折叠部分设置适当的首选项来完成相同的配置。请记住选择 Coffee Bytes Java Folding,并启用对用户定义区域的支持。

Although the support for grouping/folding is restricted to the editor, the natural order of the methods within the fold can be retain in the outline view. I'm afraid that I'm unaware of any grouping capabilities beyond this plug-in.

尽管对分组/折叠的支持仅限于编辑器,但折叠中方法的自然顺序可以保留在大纲视图中。恐怕我不知道该插件之外的任何分组功能。

回答by zvikico

I really miss it from the days of Smalltalk.

我真的很怀念 Smalltalk 的日子。

The best way to denote these categories would be adding annotations to the code itself. You would then need to create a specialized outline view which uses these annotations. Sounds like a good Google Summer of Code project.

表示这些类别的最佳方法是向代码本身添加注释。然后,您需要创建一个使用这些注释的专用大纲视图。听起来像是一个不错的 Google Summer of Code 项目。

回答by Nissi

To help with Vineet Reynolds answer, I would also like to offer this:

为了帮助 Vineet Reynolds 回答,我还想提供以下信息:

USAGE:

in User Defined Regions tab use e.g.:

Start identifier: region    
End identifier: endregion

In code:

//region SomeName
your code
//endregion SomeName

Installation instructions:

  1. Install plugin
  2. Unpack the downloaded file eclipse-folding-plugin.tar.gz
  3. Copy the contents of the:
  4. features folder => eclipse features folder
  5. plugins folder => eclipse plugins folder
  6. Configure plugin in Eclipse:
  7. Select "Windows->Preferences"
  8. Select "Java->Editor->Folding"
  9. Check the "Enable folding" option
  10. Select "Coffee Bytes Java Folding" in the "Select folding to use:" option
  11. Check "User Defined Regions" in the "General Fold Setting:" option

用法:

in User Defined Regions tab use e.g.:

Start identifier: region    
End identifier: endregion

在代码中:

//region SomeName
your code
//endregion SomeName

安装说明:

  1. 安装插件
  2. 解压下载的文件eclipse-folding-plugin.tar.gz
  3. 复制以下内容:
  4. 功能文件夹 => eclipse 功能文件夹
  5. 插件文件夹 => eclipse 插件文件夹
  6. 在 Eclipse 中配置插件:
  7. 选择“Windows->首选项”
  8. 选择“Java->编辑器->折叠”
  9. 选中“启用折叠”选项
  10. 在“选择要使用的折叠:”选项中选择“Coffee Bytes Java Folding”
  11. 在“常规折叠设置:”选项中选中“用户定义区域”

This is from: http://kosiara87.blogspot.com/2011/12/how-to-install-coffee-bytes-plugin-in.html

这是来自:http: //kosiara87.blogspot.com/2011/12/how-to-install-coffee-bytes-plugin-in.html

回答by Jo?o Silva

As to your original question, I believe that is not possible with Java/Eclipse.

至于你最初的问题,我相信 Java/Eclipse 是不可能的。

However, if you have a group of similar methods that you need to distinguish from another group of methods within the same class, why not create a new classwith the group of similar methods?

但是,如果您有一组相似的方法需要与同一类中的另一组方法区分开来,为什么不创建一个具有相似方法组的新类呢?

回答by u290629

If you mean group by name, click the button 'Sort' on 'Outline view'.

如果您的意思是按名称分组,请单击“大纲视图”上的“排序”按钮。

Moreover, there are some filtersin the view to 'hide static', 'hide non-public'...

此外,视图中有一些过滤器可以“隐藏静态”、“隐藏非公开”......

回答by melston

I just ran across this and thought I should point out that while this doesn't exactly meet your original statement you can add a @categorydirective in the documentation block of each method then filter for just the method categories you would like to see. I have used this to only look at those groups of methods I am interested in at any one time.

我刚刚遇到了这个问题,并认为我应该指出,虽然这并不完全符合您的原始声明,但您可以@category在每个方法的文档块中添加一个指令,然后仅过滤您想要查看的方法类别。我曾经用它来只查看我在任何时候感兴趣的那些方法组。