【问题标题】:Open curly braces should be opened in the same line of if statement, for loops, methods, etc打开的花括号应该在 if 语句、for 循环、方法等的同一行中打开
【发布时间】:2018-07-26 02:33:42
【问题描述】:

假设我有类似 sn-p 的代码:

if(some condition)
{
some code here;
}
else if (some other condition)
{
some more code;
}
public static some_method(something passed here maybe) 
{
some other code;
}

然后它应该被格式化为:

if(some condition) {
some code here;
} else if (some other condition) {
some more code;
}
public static some_method(something passed here maybe) {
some other code;
}

这只是示例代码。我想为包含“if 语句、for 循环、方法等”的整个文件运行 sed 脚本。可能相似或格式不同。因此,脚本的主要目的应该是将这些打开的花括号移动一行。提前谢谢..

【问题讨论】:

  • 我的建议是不要自己实现这个,而是使用现有的美化工具来编写你正在编写的语言。
  • @TomFenech - 我尊重你的建议,但那些美化/美化工具仅适用于 2-3 个文件。如果您有成百上千的文件,您不能继续浪费一整天的时间来逐个浏览文件,那就是如果我们有一些脚本,那么我们可以循环遍历该脚本并一次尝试处理所有文件。
  • 如果您下载的任何工具不支持每次调用美化多个文件,我会感到非常惊讶,如果支持,那么您始终可以编写一个简单的 for 循环或使用 @987654324 @ 在 shell 中。

标签: regex shell awk sed powershell-2.0


【解决方案1】:

只是重复您从 Tom Fenech 那里得到的评论,尤其是在要求变得更加复杂的情况下:

我的建议是不要自己实现,而是使用 您正在编写的语言的现有美化工具

但是,这里有一个可能的解决方案,但我不知道它在现实世界中的效果如何。 RS="\n[[:space:]]*{" 在大括号前的换行符后跟空格、制表符等处拆分输入,然后将其替换为 ' {'。保存一行并稍后打印可避免在输出末尾添加最终的{

awk '
    BEGIN { RS="\n[[:space:]]*{"
    NR == 1 {
        line=$0; 
        next 
    } 
    { 
        printf "%s", line" {"; 
        line=$0 
    } 
    END {
        print line
}' _file_with_code_

或者,这可以写成如下,使用更多awk 功能。这确实会导致文件末尾出现尾随 {,但添加的 sed 程序会将其删除

awk '
    BEGIN { 
        # break record on `\n{`
        RS="\n[[:space:]]*{";
        # replace with `{`
        ORS=" {" 
    } 
    # print for each line
    { print }
' input_file | 
sed '
    # for last line ($), `substitute` trailing `{` with ``
    $ s/{$//
'

【讨论】:

  • 您好,感谢您的帮助。但是我无法运行它,请您帮忙,语法错误即将到来。我尝试通过将此代码保存在 .awk 文件中来运行它,然后我运行命令:awk -f myFile.awk 我需要保存整个代码还是缺少某些东西,我不确定是't Begin 不会在第一个代码中的任何地方关闭,但又不确定。
【解决方案2】:
#!/bin/bash

# get input filename ...

filename="${1}"

# Do basic checks (etc) ...
if [[ -z "${filename}" ]]
then
    echo "[ERR] No file name supplied."
    echo "Usage: `basename ${0}` \"the file name\""

    exit 1
fi

if [[ ! -f "${filename}" ]]
then
    echo "[ERR] No file named \"${filename}\" exists anywhere."

    exit 1
fi

# Create  "SED crunch script" ...
sedscript="/tmp/sedscript.sed"

cat << EOF > "${sedscript}"

    # if "function syntax found, then read in next line
    # and print, else just print current line ...

    /^.*([^)]*)[ |  ]*$/{

    N

        /^.*([^)]*)[ |  ]*\n[ | ]*{.*$/{

            # Remove newline from first and appended line ...
            s/\n//g

            # ... and print what we have to STDOUT ...
            p

            b skip_default_print
        }

        # Next line did not start with open curly brace,
        # so just print what we currently have and
        # skip default print ..

        p

        b skip_default_print
    }

    p

    :skip_default_print

EOF

# Execute crunch script against code ...
sed -n -f "${sedscript}" "${filename}"

将上述脚本保存到 bash 脚本中。然后像下面这样执行它: (以下示例假设脚本保存为 crunch.sh)

./crunch.sh "code.java"

... 其中“code.java”是您希望“处理”的代码。结果发送到 STDOUT。

这是我使用的输入:

if(some condition)
{
some code here;
}
else if (some other condition)
{
some more code;
}
public static some_method(something passed here maybe) 
{
some other code;
}

然后输出:

if(some condition){
some code here;
}
else if (some other condition){
some more code;
}
public static some_method(something passed here maybe) {
some other code;
}

【讨论】:

  • 您好,感谢您的帮助。这一切正常但部分工作,它仅适用于控制语句而不适用于方法。你能检查一下还是我错过了什么。
  • 它非常适合我(“...他说看起来很困惑”)。这是我在接下来的几个 cmets 中的输入和输出(注意方法本身也在输出中进行了处理)...
  • if(some condition) { some code here; } else if (一些其他条件) { 更多代码; } public static some_method(可能在这里传递的东西){ 一些其他代码; }
  • if(some condition){ some code here; } else if (一些其他条件){ 更多代码; } public static some_method(可能在这里传递的东西){ 一些其他代码; }
  • ...实际上您无法通过 cmets 分辨,所以我将其添加到最初发布的原始解决方案/消息中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-07-31
  • 1970-01-01
  • 1970-01-01
  • 2021-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-03-25
相关资源
最近更新 更多