PHP #region 用于代码折叠?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5615447/
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
PHP #region for code folding?
提问by mattdwen
Is there an equivilent of c#'s #region in PHP?
PHP 中是否有 C# 的 #region 等效项?
采纳答案by Lightness Races in Orbit
No.
不。
The thing is, C# is sort of designed to be written by only one IDE, because Microsoft need you to always use their tools. So, built into the language (sort of) are things that affect the IDE.
问题是,C# 设计为只能由一个 IDE 编写,因为 Microsoft 需要您始终使用他们的工具。因此,内置于语言(某种程度)中的是影响 IDE 的东西。
PHP, on the other hand, is just a language. It's not supposed to have a symbiotic relationship with some specific editor, so it doesn't. So, no, it has nothing to control your editor.
另一方面,PHP只是一种语言。它不应该与某个特定的编辑器有共生关系,所以它没有。所以,不,它无法控制您的编辑器。
Still, all proper programming text editors support folding class definitions, function definitions, and most scope blocks. Some editors may allow certain extensions to go beyond this.
尽管如此,所有适当的编程文本编辑器都支持折叠类定义、函数定义和大多数范围块。一些编辑器可能允许某些扩展超出此范围。
回答by krtek
No, there's nothing directly in the language.
不,语言中没有直接内容。
But every decent editors allow some kind of markup to allow this.
但是每个体面的编辑器都允许某种标记来实现这一点。
For example in Netbeans :
例如在 Netbeans 中:
// <editor-fold defaultstate="collapsed" desc="user-description">
...any code...
// </editor-fold>
This syntax also works in all the Intellij IDEA editor family, see http://blog.jetbrains.com/webide/2012/03/new-in-4-0-custom-code-folding-regions/
此语法也适用于所有 Intellij IDEA 编辑器系列,请参阅http://blog.jetbrains.com/webide/2012/03/new-in-4-0-custom-code-folding-regions/
You can also emulate the feature in Eclipse via a plugin : Code folding plugin for Eclipse?
您还可以通过插件模拟 Eclipse 中的功能:Eclipse 的代码折叠插件?
Visual Studio Code includes it since version 1.19:
Visual Studio Code 从1.19版开始包含它:
#region user description
...any code...
#endregion
回答by slashCoder
As everybody has said before, the #region in .Net is actually a Visual Studio feature, not a C# one, but the fact that the C# grammar supports the syntax, allows its usage by any IDE that wants to implement it. Since in PHP, the '#' character is also used for comments, the same can be done by the IDEs. In fact, JetBrains' PhpStorm does it: https://blog.jetbrains.com/phpstorm/2012/03/new-in-4-0-custom-code-folding-regions/
正如大家之前所说,.Net 中的#region 实际上是 Visual Studio 的一项功能,而不是 C# 功能,但事实上 C# 语法支持该语法,允许任何想要实现它的 IDE 使用它。因为在 PHP 中,'#' 字符也用于注释,IDE 也可以这样做。事实上,JetBrains 的 PhpStorm 做到了:https://blog.jetbrains.com/phpstorm/2012/03/new-in-4-0-custom-code-folding-regions/
回答by col000r
There is no equivalent (the other answers explain why), but let me show you how I do it:
没有等价物(其他答案解释了原因),但让我向您展示我是如何做到的:
//////////////////////////
/* REGION A */ {
function SomeFunction() {
return true;
}
function AnotherFunction() {
return false;
}
}
//////////////////////////
/* REGION B */ {
function ThirdFunction() {
return true;
}
function FourthFunction() {
return false;
}
}
The curly braces allow me to fold the block of code (your editor will need to support cold folding, but almost all of them do), while I still see the region name and have a simple visual divider.
花括号允许我折叠代码块(你的编辑器需要支持冷折叠,但几乎所有的都支持),而我仍然可以看到区域名称并有一个简单的视觉分隔符。
Folded Result:
折叠结果:
//////////////////////////
/* REGION A */ {
//////////////////////////
/* REGION B */ {
回答by Darin Peterson
PhpStorm supports code folding similar to C#:
PhpStorm 支持类似于 C# 的代码折叠:
#region [comment about what the code does]
code....
#endregion
回答by AgentMrBig
If you are using notepad++ you can define that sort of thing with the ide in user defined language dialog, and since good old region starts with the pound sign, it works great because the pound "#" is actually one way to mark in php a comment. But, I tried it in version 6.6.6 of notepad++ and it already has #region #endregion functionality already no need to do anything.
如果您使用的是记事本++,您可以在用户定义的语言对话框中使用 ide 定义此类内容,并且由于良好的旧区域以井号开头,因此效果很好,因为井号“#”实际上是在 php 中标记的一种方式评论。但是,我在 notepad++ 的 6.6.6 版本中尝试过它,它已经具有 #region #endregion 功能,无需执行任何操作。
回答by sammarks
You mightbe able to just use curly brackets {}, but it really depends on your editor.
您也许可以只使用大括号 {},但这实际上取决于您的编辑器。
I know some editors might pick that up as a code block, allowing you to collapse it, but it probably won't allow you to name the section that you're collapsing purely because that function isn't native to the language.
我知道有些编辑器可能会将其作为代码块选择,允许您折叠它,但它可能不允许您命名您正在折叠的部分,因为该功能不是该语言的本机功能。
回答by Daniel Ribeiro
I'm using Geany in Linux to do my PHP Programming, and it DO support the #region and #endregion to mark regions of code, and it works just like the MS Visual Studio to in C# language.
我在 Linux 中使用 Geany 进行 PHP 编程,它确实支持 #region 和 #endregion 来标记代码区域,它的工作方式就像 MS Visual Studio 在 C# 语言中一样。
PHP Comments can be done using //, /* and */, and # too, so the PHP will just bypass these lines started with # and it's up to your text editor/IDE to make use of this... And Geany does!
PHP 注释也可以使用 //、/* 和 */ 以及 # 来完成,因此 PHP 将绕过这些以 # 开头的行,这取决于您的文本编辑器/IDE 来使用它...而 Geany 会这样做!
回答by Andre Rodier
I know it's an old thread, but in case you are interested.
我知道这是一个旧线程,但如果您有兴趣。
I have successfully achieved #region / #endregion code folding in emacs, by adapting this code sample to php mode: http://blogs.msdn.com/b/dotnetinterop/archive/2008/04/14/making-hideshow-el-work-with-csharp-mode-el-and-region-endregion.aspx
通过将此代码示例调整为 php 模式,我已经成功地在 emacs 中实现了 #region / #endregion 代码折叠:http: //blogs.msdn.com/b/dotnetinterop/archive/2008/04/14/making-hideshow-el -work-with-csharp-mode-el-and-region-endregion.aspx
The result is here:
结果在这里:
; ===== PHP style region folding
(defun php-hs-forward-sexp (&optional arg)
"I set hs-forward-sexp-func to this function.
I found this customization necessary to do the hide/show magic in PHP
code, when dealing with region/endregion. This routine
goes forward one s-expression, whether it is defined by curly braces
or region/endregion. It handles nesting, too.
The forward-sexp method takes an arg which can be negative, which
indicates the move should be backward. Therefore, to be fully
correct this function should also handle a negative arg. However,
the hideshow.el package never uses negative args to its
hs-forward-sexp-func, so it doesn't matter that this function does not
do negative numbers.
The arg can also be greater than 1, which means go forward
multiple times. This function doesn't handle that EITHER. But
again, I haven't see that as a problem."
(message "php-hs-forward-sexp, (arg %d) (point %d)..."
(if (numberp arg) arg -1)
(point)
)
(let ((nestlevel 0)
(mark1 (point))
(done nil)
)
(if (and arg (< arg 0))
(message "negative arg (%d) is not supported..." arg)
;; else, we have a positive argument, hence move forward.
;; simple case is just move forward one brace
(if (looking-at "{")
(forward-sexp arg)
; The more complex case is dealing with a "region/endregion" block.
; We have to deal with nested regions!
(and
(while (not done)
(re-search-forward "^[ \t]*#[ \t]*\(region\|endregion\)\b"
(point-max) 'move)
(cond
((eobp)) ; do nothing if at end of buffer
((and
(match-beginning 1)
;; if the match is longer than 6 chars, we know it is "endregion"
(if (> (- (match-end 1) (match-beginning 1)) 6)
(setq nestlevel (1- nestlevel))
(setq nestlevel (1+ nestlevel))
)
)))
(setq done (not (and (> nestlevel 0) (not (eobp)))))
); while
(if (= nest 0)
(goto-char (match-end 2))
)
))
)
)
)
(unless (assoc 'php-mode hs-special-modes-alist)
(push '(php-mode
; "\(^\s*#\s*region\b\)\|{" ; regexp for start block DID NOT WORK
"\(^[ \t]*#[ \t]*region\b\)\|{" ; regexp for start block
; "\(^\s*#\s*endregion\b\)\|}" ; regexp for end block NO WORKY!
"\(^[ \t]*#[ \t]*endregion\b\)\|}" ; regexp for end block
"/[*/]" ; regexp for comment start
php-hs-forward-sexp ; hs-forward-sexp-func
hs-c-like-adjust-block-beginning ; c-like adjust (1 char)
;php-hs-adjust-block-beginning ; php adjust ?
)
hs-special-modes-alist)
)
;;
;; To use this, put this into your php-mode-hook:
;;
; for hide/show support
(add-hook 'php-mode-hook 'php-region-mode-stuff)
(defun php-region-mode-stuff ()
(hs-minor-mode 1)
(setq hs-isearch-open t)
; with point inside the block, use these keys to hide/show
(local-set-key "\C-c>" 'hs-hide-block)
(local-set-key "\C-c<" 'hs-show-block)
)
回答by Armfoot
In Sublime Text there's a package called SyntaxFold.
在 Sublime Text 中有一个名为SyntaxFold的包。
Installation
安装
- Ctrl+Shift+Ptype installand select Package Control: Install Package;
- In the new menu type SyntaxFold, begin the download and installation by selecting it;
- Check on the status bar below if it was installed correctly.
- Ctrl+ Shift+P键入install并选择Package Control: Install Package;
- 在新的菜单类型SyntaxFold 中,选择它开始下载和安装;
- 检查下面的状态栏是否安装正确。
Configuration
配置
- Press Shift+F5to pop-up the fold panel and select Add Anotherto accesss the config file;
Ensure that it has a defaultwith the strings you want (for some odd reason mine had a quote before the hash:
'#Start
). I ended up changing it to:{ //you can change in here other config options "default": { "endMarker": "#EOB", "name": "Default", "startMarker": "#BOB" } }
You can also alter the key bindings (the ones below in the Usagesection) on Windows by changing them in the file:
C:\Users\YOUR_USER\AppData\Roaming\Sublime Text 3\Packages\SyntaxFold\Default (Windows).sublime-keymap
- 按Shift+F5弹出折叠面板,选择Add Another访问配置文件;
确保它具有您想要的字符串的默认值(出于某种奇怪的原因,我的哈希值前有一个引号:)
'#Start
。我最终将其更改为:{ //you can change in here other config options "default": { "endMarker": "#EOB", "name": "Default", "startMarker": "#BOB" } }
您还可以通过在文件中更改它们来更改 Windows 上的键绑定(下面使用部分中的那些):
C:\Users\YOUR_USER\AppData\Roaming\Sublime Text 3\Packages\SyntaxFold\Default (Windows).sublime-keymap
Usage
用法
Add the enclosing tags you defined in the config file (you can add a description after each tag), in my case:
#BOB pandora box public function alpha() { # code... } public function omega() { # code... } #EOB pandora box
- To hide the code between the tags, place the cursor between them and press Alt+1twice.
- To show it again, place the cursor on the first tagand press Alt+Shift+1twice.
添加您在配置文件中定义的封闭标签(您可以在每个标签后添加描述),在我的情况下:
#BOB pandora box public function alpha() { # code... } public function omega() { # code... } #EOB pandora box
- 要隐藏标签之间的代码,请将光标放在它们之间并按Alt+1两次。
- 要再次显示,将光标放在第一个标签,然后按Alt+ Shift+1两次。
Alternative for Comment blocks
注释块的替代方案
If you are just interested in folding comment blocks then with Fold Commentspackage you can press Ctrl+Shift+Cto hide and show all comments (it may hide tags of SyntaxFold though).
如果您只对折叠注释块感兴趣,那么使用折叠注释包,您可以按Ctrl+ Shift+C隐藏和显示所有注释(尽管它可能隐藏 SyntaxFold 的标签)。