我知道一个人可以通过将代码括起来来防止eclipse中的代码格式化

// @formatter:off
// my awesome formatted code...
public Set<Person> transformCustomersToPersons(List<Customer> customers){

    Set<Person> persons = new HashSet<Person>();
    for (Customer customer : customers) {
        persons.add(new Person(
                customer.getFirstname(),
                customer.getLastname(),
                customer.getTitle(),
                customer.getBirthday(),
                customer.getAddress(0).getCity(),
                customer.getAddress(0).getStreet(),
                customer.getAddress(0).getHousenumber(),
                customer.isMan() ? "m" : "w",
                customer.getCardNumber())
        );
    }
    return persons;
}
// @formatter:on

但是我不想一直手动编写它。只需标记代码,按热键,代码就会被格式化,这将是完美的。

有人知道吗?谢谢!

最佳答案

是的,这是可能的:

创建新模板:Windows->Preferences->Java->Editor->Templates->New(屏幕截图)

给模板起一个类似non-formater的名称,然后放置Pattern:

   // @formatter:off
   ${line_selection}${cursor}
   // @formatter:on

现在,您可以选择文本并输入Alt + Shift + Z和一些模板-这里是4

10-04 11:58
查看更多