IntelliJ getter/setter 格式(单行与多行)

发布于 2024-08-12 08:15:02 字数 283 浏览 4 评论 0原文

如何让 IntelliJ 在一行上生成 getter/setter 访问器方法,如下所示:

public String getAbc() { return abc; }

...而不是像这样的多行:

public String getAbc() {
   return abc;
}

How can you get IntelliJ to generate getter/setters accessor methods on one line like this:

public String getAbc() { return abc; }

… instead of multiple lines like this:

public String getAbc() {
   return abc;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

长梦不多时 2024-08-19 08:15:02

我使用的是 IntelliJ IDEA 14.1.0,您可以自定义此行为。

只需使用“生成...”选项,或使用 Alt+Insert 快捷键,然后选择“Getter 和 Setter”。

在打开的“选择字段”窗口中,顶部有“Getter 模板”选项。使用下拉列表旁边的“...”按钮来编辑模板。

选择“IntelliJ Default”并单击“复制”按钮创建一个名为“AlwayStartWithGet”的新文件,您可以对其进行编辑。

只需删除以下部分:

#if ($field.boolean)
  #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
    #set($name = $StringUtil.decapitalize($name))
  #else
    is##
#end
#else
  get##
#end

并将其替换为简单的

get##

You should be left with:

public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}() {
  return $field.name;
}

现在,您可以在生成代码时使用自定义模板,方法是在 getter 模板下拉列表中选择它。

I'm using IntelliJ IDEA 14.1.0 and you can customise this behaviour.

Just use the "Generate..." option, or use Alt+Insert shortcut, and select "Getter and Setter".

In the "Select Fields" window that gets opened, you have the "Getter Template" option at the top. Use the "..." button next to the dropdown, to edit the template.

Select "IntelliJ Default" and click the "Copy" button to create a new one named "AlwayStartWithGet", which you can edit.

Just remove the following section:

#if ($field.boolean)
  #if ($StringUtil.startsWithIgnoreCase($name, 'is'))
    #set($name = $StringUtil.decapitalize($name))
  #else
    is##
#end
#else
  get##
#end

And replace it with a simple

get##

You should be left with:

public ##
#if($field.modifierStatic)
  static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}() {
  return $field.name;
}

Now you can use the custom template when generating code, by selecting it in the getter template dropdown.

潇烟暮雨 2024-08-19 08:15:02

对于 Idea 2016。

Getter 模板

将最后 3 行合并为一行:

${name}() { return $field.name; }

Setter 模板

在最长行的末尾添加双散列(不带空格):

[...] ($field.type, $paramName) {##

合并最后两行合并为一行:

$field.name = $paramName; }

注意:正如 @mindas 所评论的,您可能需要不进行版本控制的视觉自动折叠。

For Idea 2016.

Getter template

Merge the last 3 lines into a single line:

${name}() { return $field.name; }

Setter template

Add double hash (without a space) at the end of the longest line:

[...] ($field.type, $paramName) {##

Merge the last 2 lines into a single line:

$field.name = $paramName; }

Note: as commented by @mindas, you will probably want instead the visual auto folding that doesn't get versioned.

演出会有结束 2024-08-19 08:15:02

无论是 getter 还是 equals/hashcode 都没有模板。这些在 IDEA 中是硬编码的。

来源

你可以在这个IntelliJ 愿望清单

There are no templates neither for getters nor for equals/hashcode. These are hardcoded in IDEA.

Source

You can see that in this IntelliJ Wishlist

夜灵血窟げ 2024-08-19 08:15:02

你没有提到你正在使用的 IDEA 版本,所以我假设最近的 8 或 9。

检查“对齐和大括号”下的代码样式设置。您应该在那里找到“一行简单方法”选项。

You didn't mention what version of IDEA you are using, so I assume the recent 8 or 9.

Check your Code Style settings, under "Alignment and Braces". You should find a "Simple methods in one line" option there.

氛圍 2024-08-19 08:15:02

我不知道你为什么要这样做,大概是为了节省视觉空间。如果是这样,只需使用 IntelliJ 的功能来折叠琐碎的 getter/setter 并忘记它需要多少行。
折叠功能可以在

“设置”->“设置”中找到。 IDE设置->编辑->代码折叠->显示代码折叠轮廓->简单的属性访问器

I don't know why you want to do this, presumably for saving visual space. If so, just use IntelliJ's feature to fold trivial getters/setters and forget about how lines does it take.
Folding feature can be found in

Settings -> IDE Settings -> Editor -> Code Folding -> Show code folding outline -> Simple property accessors

于我来说 2024-08-19 08:15:02

Alex G 和 lafuste 提供的答案对我有帮助,但即使您按照这些说明进行操作,IntelliJ 仍可能会自动格式化您的代码。要阻止这种情况发生,请转到“设置”->“编辑->代码风格-> Java(或其他语言)。单击标题为“包裹和大括号”的选项卡。在“重新格式化时保留”标题下,单击“一行中的简单方法”旁边的复选框。

为了进一步简洁,您可以消除 getter 和 setter 方法之间的空行。为此,请单击标题为“空白行”的选项卡。在标题为“最少空白行”的部分中,找到“周围方法”旁边的文本框。在文本框中输入 0。

准备

public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}(){ return $field.name; }

#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) { ##
#if ($field.name == $paramName)
    #if (!$field.modifierStatic)
    this.##
    #else
        $classname.##
    #end
#end
$field.name = $paramName; }

Alex G and laffuste provided answers that helped me, but even after you follow these instructions, IntelliJ may still automatically format your code. To stop this from happening, go to Settings -> Editor -> Code Style -> Java (or other language). Click on the tab titled "Wrapping and Braces". Under the title "Keep when reformatting" click the checkbox next to "Simple methods in one line".

For further brevity, you can eliminate the blank lines between the getter and setter methods. To do this, click on the tab titled "Blank Lines". In the section titled "Minimum Blank Lines", find the text box next to "Around method". Enter 0 in the text box.

Get

public ##
#if($field.modifierStatic)
static ##
#end
$field.type ##
#set($name = $StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project))))
get##
${name}(){ return $field.name; }

Set

#set($paramName = $helper.getParamName($field, $project))
#if($field.modifierStatic)
static ##
#end
void set$StringUtil.capitalizeWithJavaBeanConvention($StringUtil.sanitizeJavaIdentifier($helper.getPropertyName($field, $project)))($field.type $paramName) { ##
#if ($field.name == $paramName)
    #if (!$field.modifierStatic)
    this.##
    #else
        $classname.##
    #end
#end
$field.name = $paramName; }
看透却不说透 2024-08-19 08:15:02

至少更新至 2023.1.2 的新版本:

您还需要确保以下设置也已打开:

  • 首选项 >编辑>代码风格>爪哇>空格>之内
    • 代码大括号

否则,大括号之间的空格将被格式化。

Update for at least versions as new as 2023.1.2:

You'll need to also make sure that the following setting is also turned on:

  • Preferences > Editor > Code Style > Java > Spaces > Within
    • Code Braces

Otherwise the spaces between your curly braces will be formatted away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文