我有一个 PHP 文件文件夹。这些文件的源代码被压缩成几行,没有换行符或其他传统格式,非常难以阅读。
我找到了几个在线工具来使源代码可读,但它们都是一一做到的:http : //thephppro.com/tools/beautify.php http://www.prettyprinter.de/
如何批量格式化源代码?有什么方法可以更有效地完成使 PHP 源代码的文件夹/子文件夹更具可读性的过程?是否有任何 IDE 可以以批处理模式格式化文件,或者我不知道的其他 php 脚本/cli 工具可以执行此操作?
如何使用 删除代码中的多余空格astyle?例如我想转换以下代码:
void foo ( int a , int c )
{
d = a+ c;
}
Run Code Online (Sandbox Code Playgroud)
对此:
void foo (int a, int c)
{
d = a + c;
}
Run Code Online (Sandbox Code Playgroud)
但是 astyle 目前正在将其转换为:
void foo (int a , int c)
{
d = a + c;
}
Run Code Online (Sandbox Code Playgroud) 在 IntelliJ IDEA 和基于它的 IDE PhpStorm 中,是否有键盘快捷键可以将水平列表转换为垂直列表,反之亦然?例如我有数组
$arr = [
'test',
'a' => 'b',
];
Run Code Online (Sandbox Code Playgroud)
我希望把它单行,我可以选择文本和使用Ctrl+ Shift+ J,我得到
$arr = ['test', 'a' => 'b', ];
Run Code Online (Sandbox Code Playgroud)
这几乎是好的,我可以,手动删除最后一个。但是如何做相反的事情:将水平列表转换为垂直列表?这不仅与数组有关,此问题也与函数签名有关,例如
public function test($arg1, $arg2, $arg3, $arg4)
Run Code Online (Sandbox Code Playgroud)
和函数调用
test($arg1, $arg2, $arg3, $arg4);
Run Code Online (Sandbox Code Playgroud)
有时字符串变得太长,需要将其拆分以提高可读性,如下所示:
test(
$arg1,
$arg2,
$arg3,
$arg4
);
Run Code Online (Sandbox Code Playgroud)
请注意,这个问题与代码折叠无关,我想真正更改格式,而不仅仅是为我显示隐藏。
如何强制 IntellJ 代码格式化程序在与包装的链式方法调用不同的级别上自动缩进包装的参数列表:
编辑:有关更好的问题描述,请参阅更新的示例。如果我将每个连续的方法调用包装到一个新行,默认格式化程序会按预期工作。只有当我想每行留下一个或多个点时才会出现问题:
包装这个:
new Something()
.chained("arg1", "arg2", "very long arg I want to see in new line")
.chained("arg1", "arg2", "very long arg I want to see in new line")
.extra().chained("arg1", "arg2", "very long arg I want to see in new line")
.extra().chained("arg1", "arg2", "very long arg I want to see in new line");
Run Code Online (Sandbox Code Playgroud)
我希望是这样的:
new Something()
.chained("arg1", "arg2",
"very long arg I want to see in new line")
.chained("arg1", "arg2",
"very long arg I want to see …Run Code Online (Sandbox Code Playgroud) 我正在用C编写一个程序,它应该根据符号常量的定义与否而采取不同的行动。举个简单的例子,我的第一个想法是这样写:
#include <stdio.h>
#define TEST
int main(void) {
int num;
#ifdef TEST
num=1;
printf("YES\n");
#else
num=0;
printf("NO\n");
#endif
printf("%d\n", num);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但是,在尝试自动缩进我的代码(特别是gg=G在 vim 中使用)时,我的缩进丢失了:
#include <stdio.h>
#define TEST
int main(void) {
int num;
#ifdef TEST
num=1;
printf("YES\n");
#else
num=0;
printf("NO\n");
#endif
printf("%d\n", num);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当然,如果我尝试自动缩进如下内容(中间有实际命令),就会出现混乱:
#ifdef TEST1
commands
#ifdef TEST2
commands
#else
#ifdef TEST3
commands
#endif
commands
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
那么,我可以做些什么来将上述缩进视为默认值?
当我在 NetBeans 中编写代码时,有时我想添加/删除 if 语句和循环。但是,每次我想添加/删除语句但保留内容时,我都必须修复包含代码的所有缩进。同样,每次我尝试将代码粘贴到语句中时,缩进都搞砸了,我必须修复它。
有没有办法突出显示代码并修复缩进?
我知道 Eclipse 可以选择在保存时修复所有格式。我不是在寻找在保存时改变格式的东西,而是在我想要修复格式时我可以点击的东西。我在下面举了一个例子来说明我在说什么。
if (condition == true){
//pasted code
//pasted code
//pasted code
}
Run Code Online (Sandbox Code Playgroud)
或者
//other code
//removed if statement
//original code that is spaced too far right
//original code that is spaced too far right
//removed }
//other code
Run Code Online (Sandbox Code Playgroud) 当我在 IntelliJ 中将参数拆分为多行时,它会将参数从上一行略微缩进。例如:
ClassName.staticMethod(argument1,
argument2,
argument3);
Run Code Online (Sandbox Code Playgroud)
快,上面的论据是什么?好的,这很容易,因为我选择了示例名称,但是下面呢?staticMethod下面将讨论哪些论点?
ClassName.staticMethod(this.instanceMethod(argument1,
argument2),
argument3);
Run Code Online (Sandbox Code Playgroud)
staticMethod正在获取对instanceMethodand的调用结果argment3,但使用 IntelliJ 的默认格式,一目了然,您将完全错过argument2作为instanceMethod, not的参数的事实staticMethod。
这是一个非常可怕的默认行为。我怎样才能让它正确地排列参数,如下面的修改示例所示?
ClassName.staticMethod(argument1,
argument2,
argument3);
ClassName.staticMethod(this.instanceMethod(argument1,
argument2),
argument3);
Run Code Online (Sandbox Code Playgroud) 这适用于 IntelliJ 2016.2。
我正在清理一些导致 CheckStyle 违规并停止我们的 CI 构建的代码,这似乎是由于 IntelliJ 的代码样式规则而发生的。我可以手动修复这些,但有两个问题:编辑器不加选择地应用这些规则,手动修复很多。如果有人再次运行代码格式化,它会将缩进设置回它想要的方式。
具体来说,多行方法签名的缩进如下所示:
我希望后续行中的参数在前一行开头的右侧缩进 4 个空格,而不是左括号。
有趣的是,按你输入的格式对我来说是正确的,但是当我使用代码格式化程序时,它会像上面那样重新格式化。
我可以通过在首选项中转到制表符和缩进并将“继续缩进”设置为 0 来获得一些方法:
但是这条规则被普遍应用,让其他东西看起来很糟糕,而且它不接受负值。
我已经转到首选项中的“Editor.Code Style.Java.Wrapping 和 Braces.Method 声明参数”,并尝试了那里的几乎所有内容。我有“Chop down if long”,但由于缩进规则,这使得行更长。我没有看到一种方法来指定它应该从哪里开始缩进或它应该缩进多少行。
这一点,再加上观察到方法调用的格式化表现出预期的行为(但更糟糕的是,不是构造函数调用!),让我认为这是一个错误。IntelliJ 的格式化程序似乎是硬编码的,以使用左括号中的继续缩进,而不是方法声明本身。
有什么我错过的或一些可行的解决方法吗?
我有数百个包含混合制表符和空格的可怕缩进PHP文件(我想还包括混合的行尾),我想用php-cs-fixer v2 + 修复它们。
我已经根据需要配置了php-cs-fixer,并且相应地清理了代码-除了缩进。我尝试了一种最小化的配置,如下面所示,以解决问题。但是我无法直接使用缩进修复程序:
return PhpCsFixer\Config::create()
->setRules([
'@PSR2' => true,
'indentation_type' => true,
'braces' => ['position_after_functions_and_oop_constructs' => 'same'],
])
->setIndent("\t")
->setLineEnding("\r\n")
Run Code Online (Sandbox Code Playgroud)
当前,我使用以下命令在Windows框上运行此命令(此处为单个文件):
php-cs-fixer.bat fix new_user.php --config /full/windowspath/to/php_cs.dist
Run Code Online (Sandbox Code Playgroud)
以防万一,生成的php_cs.cache(包含JSON中实际应用的规则)文件如下所示:
{
"php": "5.6.31",
"version": "2.6.0:v2.6.0#5642a36a60c11cdd01488d192541a89bb44a4abf",
"rules": {
"blank_line_after_namespace": true,
"braces": {
"position_after_functions_and_oop_constructs": "same"
},
"class_definition": true,
"elseif": true,
"function_declaration": true,
"indentation_type": true,
"line_ending": true,
"lowercase_constants": true,
"lowercase_keywords": true,
"method_argument_space": {
"ensure_fully_multiline": true
},
"no_break_comment": true,
"no_closing_tag": true,
"no_spaces_after_function_name": true,
"no_spaces_inside_parenthesis": true,
"no_trailing_whitespace": true,
"no_trailing_whitespace_in_comment": true,
"single_blank_line_at_eof": true,
"single_class_element_per_statement": {
"elements": …Run Code Online (Sandbox Code Playgroud) 前言:这个问题不是这个问题的重复:
这个问题也不是基于意见的。我不是在寻求“最佳”风格。我不是在问什么是“正确”的事情。
我要问的是不同的编码风格如何缩进 switch 语句、它们的 case 标签和实际语句。
我对 switch 语句的缩进方式特别感兴趣
- K&R 风格
- Linux 内核风格
- GNU 风格
- Java 风格
我的想法是能够在我使用的任何代码中保持一致,但大多数缩进样式示例没有切换案例。我喜欢一致性,并且认为我正在写的内容实际上与我正在写的内容不符的想法是可以容忍的,但很不好吃。
code-formatting ×10
java ×3
c ×2
indentation ×2
php ×2
arguments ×1
astyle ×1
auto-indent ×1
batch-file ×1
c++ ×1
coding-style ×1
formatting ×1
ide ×1
netbeans ×1
php-cs-fixer ×1
phpstorm ×1
vim ×1