简体   繁体   English

Eclipse JavaScript格式化程序疯狂(即:填充太多)

[英]Eclipse JavaScript formatter crazy (i.e: too much padding)

I'm having trouble to get eclipse format my JavaScript well. 我很难将eclipse格式化为我的JavaScript。 In this first example, it behaves as expected (not the length of the second url): 在第一个示例中,它的行为与预期的一样(不是第二个URL的长度):

表现良好

Whenever i have a longer url, eclipse goes crazy formatting my code and I get extra padding everywhere, like the example below: 每当我有一个更长的网址时,eclipse会疯狂地格式化我的代码,并且我到处都得到额外的填充,如下例所示:

表现不好

Why does the JavaScript formatter behaving like this, and how do I fix it? 为什么JavaScript格式化程序的行为如此,我该如何解决?

(Just in case: I have not installed any formatter plugin, I just use the default that comes with Eclipse Juno) (以防万一:我没有安装任何格式化程序插件,我只使用Eclipse Juno附带的默认设置)

The problem can be solved by accessing 这个问题可以通过访问来解决

Preferences > JavaScript > Code Style > Formatter

These are the steps: 这些是步骤:

  1. Create a new profile (since you cannot edit the builted-in one), if you haven't already, and click Edit... . 创建一个新的配置文件(因为您无法编辑已构建的配置文件),如果尚未Edit... ,请单击Edit...
  2. Open the Line Wrapping tab. 打开“ Line Wrapping选项卡。
  3. In the Maximum line width field, enter 9999 . 在“ Maximum line width字段中,输入9999
  4. Click Apply , and Ok . 单击Apply ,然后单击Ok

The problem shows up again for code lines that have more than 9999 characters, but I can live with it. 对于具有超过9999个字符的代码行,问题再次出现,但我可以忍受它。

The long url in the example affects "$.ajax(..." and "$(function()...". 示例中的长URL会影响“$ .ajax(...”和“$(function()...”)。

As for "$(function()...", the setting is in Preferences/JavaScript/Code Style/Formatter/Line Wrapping/Function Calls/Arguments. 'Do not wrap' is available. 至于“$(function()...”,设置在Preferences / JavaScript / Code Style / Formatter / Line Wrapping / Function Calls / Arguments中。'do wrap'可用。

As for "$.ajax(...", it is similar to 'Qualified invocation' in the Java formatter.(Preferences/Java/Code Style/Formatter/Line Wrapping/Function Calls) Unfortunately, JSDT has no UI for this.(Preferences/JavaScript/Code Style/Formatter/Line Wrapping/Function Calls) 至于“$ .ajax(...”,它类似于Java格式化程序中的'合格调用'。(首选项/ Java /代码样式/格式化程序/行包装/函数调用)不幸的是,JSDT没有这方面的UI。 (首选项/ JavaScript /代码样式/格式化程序/行包装/函数调用)

This is a trick for JSDT 1.4.1(WTP R3.4.1). 这是JSDT 1.4.1(WTP R3.4.1)的一个技巧。

  1. Start Eclipse 启动Eclipse
  2. Open Preferences/JavaScript/Code Style/Formatter 打开首选项/ JavaScript /代码样式/格式化程序
  3. Click 'New' 点击“新建”
  4. Input profile name as you like, select 'Eclipse [built-in]'(or JavaScript Conventions) in listbox, uncheck 'Open the edit dialog now'(no need to edit), and click 'OK' 根据需要输入配置文件名称,在列表框中选择“Eclipse [内置]”(或JavaScript约定),取消选中“立即打开编辑对话框”(无需编辑),然后单击“确定”
  5. Close preferences and exit Eclipse 关闭首选项并退出Eclipse
  6. Open following file with a text editor 使用文本编辑器打开以下文件
    workspace/.metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.wst.jsdt.core.prefs
  7. Find following line and replace 16 to 0 找到以下行并将16替换为0
    org.eclipse.wst.jsdt.core.formatter.alignment_for_selector_in_method_invocation=16
  8. Save it 保存
  9. Start Eclipse 启动Eclipse

(since you have your own profile, maybe 1-5 are unnecessary) (因为你有自己的个人资料,可能不需要1-5个)

This hack will be reverted when you edited formatter settings in preferences. 在首选项中编辑格式化程序设置时,将恢复此黑客攻击。 - (To prevent overwriting, all you need to do is export you formatter profile, edit the XML and change the 'org.eclipse.wst.jsdt.core.formatter.alignment_for_selector_in_method_invocation' to 0. Save and reimport again now. This change is permanent. - (为了防止覆盖,您需要做的就是导出格式化程序配置文件,编辑XML并将'org.eclipse.wst.jsdt.core.formatter.alignment_for_selector_in_method_invocation'更改为0.现在再次保存并重新导入。此更改是常驻。

I don't think this satisfies your needs, it's better to use some other formatter(3rd party plugin), I guess. 我认为这不能满足你的需求,最好使用其他格式化程序(第三方插件)。

I've found a temporary solution: 我找到了一个临时解决方案:

  1. Go to Window -> Preferences -> JavaScript -> Code Style -> Formatter 转到Window -> Preferences -> JavaScript -> Code Style -> Formatter

  2. Set the active profile to JavaScript conventions. 将活动配置文件设置为JavaScript约定。

  3. Click Edit 单击编辑

  4. In the Indentation tab, set Tab policy to Spaces only . 在“ Indentation选项卡中,将“ Tab policy设置为“ 仅限空格”

  5. Set both Indentation size and Tab size to 3 . Indentation sizeTab size都设置为3

  6. As you cannot override the default profiles, change the profile name and save. 由于无法覆盖默认配置文件,请更改配置文件名称并保存。

  7. Press Ctrl + Shift + F and enjoy. Ctrl + Shift + F并欣赏。

NOTE: I don't like having 3 spaces, I'd rather prefer my good old 4 spaces, but this is a temporary solution, until someone finds a better way to solve this. 注意:我不喜欢有3个空格,我宁愿选择我的旧4空格,但这是一个临时解决方案,直到有人找到更好的解决方法。 I just felt like sharing. 我只想分享。

Here's what I found: in "Line Wrapping > Function Calls" set "Indentation Policy" at the bottom to "Indent by one". 以下是我发现的内容:在“Line Wrapping> Function Calls”中,将“Indentation Policy”设置为“Indent by one”。 This creates indentation without the wildly excessive spaces (or tabs). 这会创建缩进,而不会出现过多的空间(或制表符)。 I haven't tried it with your exact code, but I've seen similar issues with mine. 我没有用你的确切代码尝试过,但我看到了类似的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM