在IntelliJ IDEA中将JS行注释转换为间隔块注释

时间:2018-02-24 23:11:13

标签: javascript intellij-idea coding-style comments webstorm

我正在使用IntelliJ IDEA清理一些Javascript代码以匹配a code style standard

我的目标是改变这一点:

// A comment
// About some things
// That spans multiple lines

进入这个:

 /**
  * A comment
  * About some things
  * That spans multiple lines
  */

...无需手动编辑每个评论栏。

我知道IntelliJ有Comment with Block Comment功能,但是当我选择一个块并使用它时,我只能得到它来生成它:

/*A comment
About some things
That spans multiple lines*/

IntelliJ是否支持JSDoc评论风格?

2 个答案:

答案 0 :(得分:2)

IntelliJ IDEA中没有可以自动将现有的行尾注释序列转换为JSDoc注释的功能。要自动执行这样的转换,您可以编写一个简单的插件,或者只编写一个在从命令行调用时执行转换的脚本。

答案 1 :(得分:1)

您可以使用正则表达式和编程来执行此操作。这是我的Julia函数,可完成多行注释和单行注释的技巧:

function comment2jsdoc(text)

  # match multi-line comment
  firstline_m = match( r"(\/\/.*\n)(?:\/\/.*\n)+", text)
  if firstline_m !== nothing

    # add /** and */
    text = replace(text, r"(\/\/.*\n)+\/\/.*\n" => s"/** \n \0 */ \n")

    # replace // with *
    while match( r"((?:\/\/.*\n)+)\/\/(.*\n)", text) !== nothing
      text = replace(text, r"((?:\/\/.*\n)+)\/\/(.*\n)" => s"\1 * \2")
    end

    # replace first line
    firstline_cap = firstline_m.captures[1]
    text = replace(text,  firstline_cap => "* $(firstline_cap[3:end])")
  end

  # match single-line comment
  while match(r"\/\/(.*)\n(?!\/\/.*\n)", text) !== nothing
    text = replace(text, r"\/\/(.*)\n(?!\/\/.*\n)" => s"/** \1 */")
  end

  return text
end

例如:

text = """
// A comment
// About some things
// That spans multiple lines

// single line
"""

text = comment2jsdoc(text)
println(text)

将导致:

/**
 *  A comment
 *  About some things
 *  That spans multiple lines
 */

/**  single line */

您还可以从文件读取文本/向文件写入文本:

# read file:
text = Base.read("path/to/file", String)

text = comment2jsdoc(text)

# write file
Base.write("path/to/file", text)