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

c#中的一个例子:

#region name
//code
#endregion

当前回答

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

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

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

其他回答

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

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

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

与大多数人所发布的相反,这不是一个IDE的事情。这是语言问题。区域是一个c#语句。

Android Studio(或IntelliJ IDEA)中最快的方法

突出显示要围绕它的代码 按CTRL + Alt + t 按c ==>输入描述 享受