在IntelliJ IDEA中将JS单行注释转换为带空格的块注释

3

我是一名IntelliJ IDEA工程师,需要清理一些Javascript代码以符合代码风格标准

我的目标是将下面的代码更改为:

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

将此转换为:

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

...无需手动编辑每个注释块。

我知道IntelliJ有一个“用块注释进行注释”的功能,但当我选择一个块并使用它时,我只能得到以下生成结果:

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

IntelliJ是否支持JSDoc风格的注释?


我不知道有关于“块注释中的注释”这样的配置。也许您可以使用正则表达式来添加*到这些注释中。 - acdcjunior
我不确定是否存在,但有时候当我需要生成重复的内容时,我喜欢打开浏览器控制台并使用函数让它为我生成一些字符串。 - cyn
我希望知道为什么有人对这个问题进行了负评。如果我知道有人认为它质量低下的原因,我很乐意改进它。 - bryan kennedy
2个回答

2

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


谢谢回复。有没有任何函数可以将常规文本转换为JSDoc注释? - bryan kennedy
1
没有,也没有一个。 - yole

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接