如何告诉 Eclipse 不格式化代码文件的一部分(按 Strg + Shift + F)

2022-01-15 00:00:00 format eclipse

我真的很喜欢 autofromat 功能.我让你的代码更具可读性,如果 JavaScript 会告诉你什么时候出现语法错误(缺少括号等).

I really love the autofromat feature. I makes your code more readable and in case of JavaScript tells you, when there are synatcs errors (missing brackets etc.).

但有时格式会使代码更难阅读.例如当它将长数组初始化放入一行时.在那种情况下,我不希望他格式化它,而是让它提供多行.例如

However sometimes the formatting makes the code harder to read. e.g. when it puts a long array inizalisation into a single line. In that case I don't want him to format it, but rather leave it ofer multiple lines. E.g.

define([
    'jquery', 
    'aloha', 
    'aloha/plugin', 
    'ui/ui', 
    'ui/scopes', 
    'ui/button', 
    'ui/toggleButton', 
    'ui/port-helper-attribute-field', 
    'ui/text'
// 'css!youtube/css/youtube.css'
], 
    function(
        $, 
        Aloha, 
        Plugin, 
        Ui, 
        Scopes, 
        Button, 
        ToggleButton, 
        AttributeField) 
        {

这个数组应该保持这样,不要变成这样:

this array should stay like this and don't become this:

define(['jquery', 'aloha', 'aloha/plugin', 'ui/ui', 'ui/scopes', 'ui/button', 'ui/toggleButton', 'ui/port-helper-attribute-field', 'ui/text' ], function($, Aloha, Plugin, Ui, Scopes, Button, ToggleButton, AttributeField) {

有没有特殊的标签,告诉eclipse不要格式化代码?

Is there a special tag, to tell eclipse not to format the code?

推荐答案

好的,我花了一些时间才找到正确的设置,所以我将在这里发布一个教程.

OK, it took me some time to find the right setting so I will post a toturial here.

转到窗口首选项并搜索您正在使用的格式化程序.就我而言,它位于Aptana Studia"->格式化程序"下.(取决于您的包,这会有所不同,例如 Java 格式化程序位于Java"->代码样式"->格式化程序"下).

Go to Window Preferences and Search the Formatter you are using. In my case it was under 'Aptana Studia' -> 'Formatter'. (Depending on your Package this differs, e.g. the Java Formatter is under 'Java' -> 'Code Style' -> 'Formater').

现在创建一个新的构建配置文件,因为您无法覆盖旧配置文件.

Noww create a new Build profile since you can't override the old one.

现在启用格式化程序标签.

Now enable the Formatter tags.

现在你可以使用

 - @formatter:on
 - @formatter:off

用于禁用代码格式化的标签.

tags to disable code formatting.

示例:这段代码:

    function hello() {             return 'hello';
}

//@formatter:off
/*
   |      _,,,---,,_
   /,`.-'`'    -.  ;-;;,_
  |,4-  ) )-,_..; (  `'-'
 '---''(_/--'  `-'\_)  fL

 */
//@formatter:on

function 


world() {
    return 'world';
}

会被格式化成这样

function hello() {
    return 'hello';
}

//@formatter:off
/*
   |      _,,,---,,_
   /,`.-'`'    -.  ;-;;,_
  |,4-  ) )-,_..; (  `'-'
 '---''(_/--'  `-'\_)  fL

 */
//@formatter:on

function world() {
    return 'world';
}

注意函数定义的格式是正确的,而 ascii 艺术不是

Note how the function definition is formatted correct, while the ascii art isn't

学分:

  1. Katja Christiansen 发表评论
  2. https://stackoverflow.com/a/3353765/639035:类似的答案
  1. Katja Christiansen for his comment
  2. https://stackoverflow.com/a/3353765/639035 : for a similar answer

相关文章