【问题标题】:Idea Live Template to Log Method ArgsIdea Live 模板记录方法参数
【发布时间】:2014-11-18 11:22:43
【问题描述】:

我希望能够在 Jetbrain 的 Idea 中创建一个实时模板,该模板将记录方法的参数。我们称它为“大”。它会像这样工作:

public void get(String one, String two) {
    larg<tab>

创造

public void get(String one, String two) {
    log.info("get: one = " + one + " two = " + two);

我可以输入方法名称,但还没有弄清楚如何提取方法参数。有什么想法吗?

【问题讨论】:

    标签: java ide intellij-idea


    【解决方案1】:

    我迟到了 4 年,但预定义的模板 soutp 几乎使用 groovyscript 变量来完成此操作。

    这是你正在寻找的 groovy 脚本

    groovyScript("'\"' + _1.collect { it + ' = [\" + ' + it + ' + \"]'}.join(', ') + '\"'", methodParameters())
    

    【讨论】:

    • 我不清楚这在 java 中是否有效。如果你去 [Live Templates] -> [output] -> [soutp] 那么你会看到这个脚本在运行,它肯定可以工作。谢谢!
    • 这也适用于 javascript 吗?尝试了没有运气,也许我错过了什么
    • 简单但相关:仅访问_1的第一、第二、第三等参数,只需使用_1[0]_1[1]等即可。
    • 受这个答案的启发,我为参数化日志消息创建了以下实时模板,它产生 LOG.debug("get - one: {}, two: {}", one, two): LOG.debug ($味精$);为变量 MSG 使用以下脚本: groovyScript("'\"' + _1 + (!_2.isEmpty() ? ' - ' : '') + _2.collect { it + ': {}'}.join( ', ') + '\"' + (!_2.isEmpty() ? ', ' + _2.collect {it}.join(', ') : '')", methodName(), methodParameters())
    【解决方案2】:

    目前似乎无法使用实时模板。

    来自Jetbrain's forum

    There is no predefined live template function to do this for you automatically.
    You can write a plugin that would provide such a function.
    

    【讨论】:

      猜你喜欢
      • 2013-10-01
      • 1970-01-01
      • 2021-02-11
      • 2020-02-23
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 1970-01-01
      • 2018-10-03
      相关资源
      最近更新 更多