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

c#中的一个例子:

#region name
//code
#endregion

当前回答

Jet Brains IDEA拥有这一功能。你可以使用热键环绕(ctrl + alt + T),这只是IDEA功能。

区域是这样的:

//region Description

Some code

//endregion

其他回答

Jet Brains IDEA拥有这一功能。你可以使用热键环绕(ctrl + alt + T),这只是IDEA功能。

区域是这样的:

//region Description

Some code

//endregion

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

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

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)按已保存的快捷键

结果如下:

我通常需要注释代码,所以我在开始和结束时使用花括号。

{
// Code
// Code
// Code
// Code
}

它可以用于代码片段,但在某些代码中会产生问题,因为它改变了变量的作用域。

自定义代码折叠功能可以使用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

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