How to tell eclipse to not format parts of a codefile (pressing Strg + Shift + F)

蓝咒 提交于 2019-12-20 06:25:53

问题


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) {

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


回答1:


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

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.

Now you can use the

 - @formatter:on
 - @formatter:off

tags to disable code formatting.

Example: this code:

    function hello() {             return 'hello';
}

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

 */
//@formatter:on

function 


world() {
    return 'world';
}

Will get formatted to like this

function hello() {
    return 'hello';
}

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

 */
//@formatter:on

function world() {
    return 'world';
}

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

Credits:

  1. Katja Christiansen for his comment
  2. https://stackoverflow.com/a/3353765/639035 : for a similar answer



回答2:


Try to make an empty comment after each line:

define([ //
    'jquery', //
    'aloha', //
    'aloha/plugin', //
    'ui/ui', //
    'ui/scopes', //
    'ui/button', //
    'ui/toggleButton', //
...

Not nice, but I think it will work.



来源:https://stackoverflow.com/questions/12388185/how-to-tell-eclipse-to-not-format-parts-of-a-codefile-pressing-strg-shift-f

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!