我想在Eclipse中使用区域进行代码折叠;在Java中怎么做到呢?

c#中的一个例子:

#region name
//code
#endregion

当前回答

在Visual Studio Code中,尝试这样做:

//region Variables
// Code you need
//endregion

其他回答

没有这样的标准对等物。一些ide(例如Intellij或Eclipse)可以根据所涉及的代码类型(构造函数、导入等)进行折叠,但没有像#region这样的ide。

在语言中没有对应的…基于ide…

例如在netbeans中:

NetBeans/Creator支持以下语法:

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

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

自定义代码折叠功能可以使用CoffeeScript代码折叠插件添加到eclipse。

这已被测试用于月食Luna和Juno。以下是步骤

Download the plugin from here Extract the contents of archive Copy paste the contents of plugin and features folder to the same named folder inside eclipse installation directory Restart the eclipse Navigate Window >Preferences >Java >Editor >Folding >Select folding to use: Coffee Bytes Java >General tab >Tick checkboxes in front of User Defined Fold Create new region as shown: Restart the Eclipse. Try out if folding works with comments prefixed with specified starting and ending identifiers

你也可以在这个博客上下载存档和查找步骤。

我刚从c#转到java,遇到了同样的问题,最好的和准确的区域替代方案如下(在Android Studio工作,不知道intelliJ):

 //region [Description]
 int a;
 int b;
 int c;
//endregion

快捷方式如下:

1-选择代码

2-按下ctrl + alt + t

3-按c并写下你的描述

有一些选项可以达到同样的效果,遵循以下几点。

1)打开宏资源管理器:

2)创建新的宏:

3)命名为“OutlineRegions”(或者其他你想要的名字)

4)右键单击“OutlineRegions”(在宏资源管理器上显示)选择“编辑”选项,并将以下VB代码粘贴进去:

    Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Imports System.Collections

Public Module OutlineRegions

    Sub OutlineRegions()
        Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection

        Const REGION_START As String = "//#region"
        Const REGION_END As String = "//#endregion"

        selection.SelectAll()
        Dim text As String = selection.Text
        selection.StartOfDocument(True)

        Dim startIndex As Integer
        Dim endIndex As Integer
        Dim lastIndex As Integer = 0
        Dim startRegions As Stack = New Stack()

        Do
            startIndex = text.IndexOf(REGION_START, lastIndex)
            endIndex = text.IndexOf(REGION_END, lastIndex)

            If startIndex = -1 AndAlso endIndex = -1 Then
                Exit Do
            End If

            If startIndex <> -1 AndAlso startIndex < endIndex Then
                startRegions.Push(startIndex)
                lastIndex = startIndex + 1
            Else
                ' Outline region ...
                selection.MoveToLineAndOffset(CalcLineNumber(text, CInt(startRegions.Pop())), 1)
                selection.MoveToLineAndOffset(CalcLineNumber(text, endIndex) + 1, 1, True)
                selection.OutlineSection()

                lastIndex = endIndex + 1
            End If
        Loop

        selection.StartOfDocument()
    End Sub

    Private Function CalcLineNumber(ByVal text As String, ByVal index As Integer)
        Dim lineNumber As Integer = 1
        Dim i As Integer = 0

        While i < index
            If text.Chars(i) = vbCr Then
                lineNumber += 1
                i += 1
            End If

            i += 1
        End While

        Return lineNumber
    End Function
End Module

5)保存宏并关闭编辑器。

6)现在让我们为宏分配快捷方式。进入工具->选项->环境->键盘,在“显示包含的命令”文本框中搜索你的宏(在文本框中输入:宏,它会提示宏的名称,选择你的一个。)

7)现在在文本框下的“按快捷键”即可输入所需的快捷键。我用Ctrl+M+N。

使用:

return
{
//Properties
//#region
Name:null,
Address:null
//#endregion
}

8)按已保存的快捷键

结果如下: