Java 相当于 C# 中的 #region

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2344524/
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-08-07 01:39:07  来源:igfitidea点击:

Java equivalent to #region in C#

javac#eclipsefolding

提问by Pentium10

I want to use regions for code folding in Eclipse; how can that be done in Java?

我想在Eclipse 中使用区域进行代码折叠;在 Java 中如何做到这一点?

An example usage in C#:

C# 中的示例用法:

#region name
//code
#endregion

采纳答案by Brian Agnew

There's no such standardequivalent. Some IDEs - Intellij, for instance, or Eclipse - can fold depending on the code types involved (constructors, imports etc.), but there's nothing quite like #region.

没有这样的标准等价物。某些 IDE(例如 Intellij 或 Eclipse)可以根据所涉及的代码类型(构造函数、导入等)折叠,但没有什么比#region.

回答by Justin Niessner

#region

// code

#endregion

Really only gets you any benefit in the IDE. With Java, there's no set standard in IDE, so there's really no standardparallel to #region.

真的只会让您在 IDE 中受益。使用Java,有一个在IDE中没有固定的标准,所以真的没有很标准的平行#region

回答by heavyd

This is more of an IDE feature than a language feature. Netbeans allows you to define your own folding definitionsusing the following definition:

这与其说是一种语言功能,不如说是一种 IDE 功能。Netbeans 允许您使用以下定义定义自己的折叠定义:

// <editor-fold defaultstate="collapsed" desc="user-description">
  ...any code...
// </editor-fold>

As noted in the article, this may be supported by other editors too, but there are no guarantees.

正如文章中所指出的,其他编辑器也可能支持这一点,但不能保证。

回答by Kevin LaBranche

No equivalent in the language... Based on IDEs...

语言中没有对等物......基于IDE......

For example in netbeans:

例如在 netbeans 中:

NetBeans/Creator supports this syntax:

NetBeans/Creator 支持以下语法:

// <editor-fold defaultstate="collapsed" desc="Your Fold Comment">
...
// </editor-fold>

http://forums.java.net/jive/thread.jspa?threadID=1311

http://forums.java.net/jive/thread.jspa?threadID=1311

回答by Ricky

If anyone is interested, in Eclipse you can collapse all your methods etc in one go, just right click when you'd normally insert a break point, click 'Folding' > 'Collapse all'. It know it's not an answer to the question, but just providing an alternative to quick code folding.

如果有人感兴趣,在 Eclipse 中,您可以一次性折叠所有方法等,只需在通常插入断点时右键单击,单击“折叠”>“全部折叠”。它知道这不是问题的答案,而只是提供快速代码折叠的替代方案。

回答by Johann

Contrary to what most are posting, this is NOT an IDE thing. It is a language thing. The #region is a C# statement.

与大多数发布的内容相反,这不是 IDE 的事情。这是语言的事情。#region 是一个 C# 语句。

回答by siriusblack

Actually johann, the #indicates that it's a preprocessor directive, which basically means it tells the IDE what to do.

实际上 johann,这#表明它是一个预处理器指令,这基本上意味着它告诉 IDE 要做什么。

In the case of using #regionand #endregionin your code, it makes NO difference in the final code whether it's there or not. Can you really call it a language element if using it changes nothing?

在您的代码中使用#region和的情况下,#endregion无论是否存在,最终代码都没有区别。如果使用它没有任何改变,你真的可以称它为语言元素吗?

Apart from that, java doesn't have preprocessor directives, which means the option of code folding is defined on a per-ide basis, in netbeans for example with a //< code-fold> statement

除此之外,java 没有预处理器指令,这意味着代码折叠选项是基于每个 ide 定义的,例如在 netbeans 中使用 //<code-fold> 语句

回答by Dennis

In Eclipse you can collapse the brackets wrapping variable region block. The closest is to do something like this:

在 Eclipse 中,您可以折叠包装可变区域块的括号。最接近的是做这样的事情:

public class counter_class 
{ 

    { // Region

        int variable = 0;

    }
}

回答by Alexander Bezrodniy

Jet Brains IDEA has this feature. You can use hotkey surround with for that (ctrl + alt + T). It's just IDEA feature.

Jet Brains IDEA 有这个功能。您可以为此使用热键环绕(ctrl + alt + T)。这只是 IDEA 功能。

Regions there look like this:

那里的地区如下所示:

//region Description

Some code

//endregion