如何在 Aptana Studio 中自动缩进 JavaScript 代码?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6138115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:24:37  来源:igfitidea点击:

How to automatically indent the JavaScript code in Aptana Studio?

javascriptaptanaindentationauto-indent

提问by Jonas

Previously I used Eclipse for PHP and Java development, and I liked the command Ctrl + ito automatically indent the code.

以前我使用Eclipse 进行PHP 和Java 开发,我喜欢Ctrl + i自动缩进代码的命令。

Now am I using Aptana Studio that is based on Eclipse, for JavaScript development. I would like to automatically indent my JavaScript code, but Ctrl + ideletes all my code. Is there any other command that automatically indent JavaScript code in Aptana Studio?

现在我使用基于 Eclipse 的 Aptana Studio 进行 JavaScript 开发。我想自动缩进我的 JavaScript 代码,但Ctrl + i删除了我的所有代码。在 Aptana Studio 中是否还有其他命令可以自动缩进 JavaScript 代码?

回答by sgibly

The Studio does not have this feature yet. You can add a feature request here: https://aptana.lighthouseapp.com/projects/35272-studio/tickets/new

Studio 尚无此功能。您可以在此处添加功能请求:https: //aptana.lighthouseapp.com/projects/35272-studio/tickets/new

Ctrl+Shift+F will format your entire file, as Tim suggested. You can control some of the formatter settings through the preferences at 'Window -> Preferences -> Aptana Studio -> Formatter'

正如蒂姆建议的那样,Ctrl+Shift+F 将格式化整个文件。您可以通过“Window -> Preferences -> Aptana Studio -> Formatter”中的首选项来控制一些格式化程序设置

回答by wassertim

Maybe you mean format feature CTRL+SHIFT+F?

也许你的意思是格式功能CTRL+SHIFT+F

回答by Hyman Kelly

I've just updated to Aptana Studio 3.0.9 and it now seems to be able to format specific (selected) lines of JavaScript, HTML and CSS, in some circumstances. The default key binding for me was something odd so I changed the key binding to CTRL+Iin Preferences > General > Keys.

我刚刚更新到 Aptana Studio 3.0.9,现在它似乎能够在某些情况下格式化 JavaScript、HTML 和 CSS 的特定(选定)行。我的默认键绑定很奇怪,所以我将键绑定更改为CTRL+Iin Preferences > General > Keys

If no code is selected then Source > Formatseems to always reliably format the entire file. The behaviour of Source > Formaton selectionsdiffers depending on the type of code you're editing:

如果没有选择任何代码,那么Source > Format似乎总是可靠地格式化整个文件。Source > Formaton selections的行为因您正在编辑的代码类型而异:

JavaScript

JavaScript

  • Editing a .js file: Source > Formatappears to work as you'd expect: if you select a few lines of code then Source > Formatwill correctly indent just the selected lines of code.
  • Editing JavaScript within HTML: Formatting JavaScript within an HTML file appears to work most of the time but sometimes fails; I have not been able to discover the rules.
  • 编辑 .js 文件Source > Format看起来像您期望的那样工作:如果您选择几行代码,那么Source > Format将正确缩进选定的代码行。
  • 在 HTML 中编辑 JavaScript:在 HTML 文件中格式化 JavaScript 似乎在大多数情况下都有效,但有时会失败;我一直无法发现规则。

HTML

HTML

  • If you select a chunk of HTML, AND IF the top line of that selection is correctly indented ALREADY, AND IF the selection does not include any unclosed elements then Source > Formatwill do the right thing.
  • Otherwise Source > Formatseems to act unpredictably with HTML.
  • 如果您选择了一段 HTML,并且如果该选择的第一行已经正确缩进,并且如果该选择不包含任何未闭合的元素,那么Source > Format将做正确的事情。
  • 否则Source > Format似乎对 HTML 的行为不可预测。

CSS

CSS

  • Editing a .css file:
    • works fine if I select an entire rule (i.e. including all of selector { declaration block; }) (in which case Aptana formats just that rule).
    • But if my selection does not include the entire rule then I get an error.
  • Editing css within HTML:
    • If partof the CSS is selected within a <style>block then source > formatwill format allthe css within that <style>block. There seems to be no way to format just a few lines of css within html.
  • 编辑 .css 文件
    • 如果我选择整个规则(即包括所有selector { declaration block; })(在这种情况下 Aptana 仅格式化该规则),则工作正常。
    • 但是,如果我的选择不包括整个规则,那么我会收到错误消息。
  • 在 HTML 中编辑 css
    • 如果在块中选择了部分CSS,则将格式化该块中的所有css 。似乎没有办法在 html 中格式化几行 css。<style>source > format<style>

Unless anyone points out that this post contains fatal flaws within the next few days, I'll post a feature request at https://jira.appcelerator.orgto ask for consistency in formatting behaviour across code types (a quick look at the existing feature and bug requests suggests that code formatting has received a considerable amount of attention recently).

除非有人在接下来的几天内指出这篇文章包含致命的缺陷,否则我会在https://jira.appcelerator.org上发布一个功能请求,要求跨代码类型的格式化行为保持一致(快速浏览现有的功能和错误请求表明代码格式最近受到了相当多的关注)。

update 19/3/2012I've posted this bug on the JIRA: https://jira.appcelerator.org/browse/APSTUD-4516

2012 年 3 月 19 日更新我在 JIRA 上发布了这个错误:https: //jira.appcelerator.org/browse/APSTUD-4516

回答by kanpeki

I was having the same issue and stumbled across this site: jsbeautifierYou might wanna check it out. Worked fine for me ;)

我遇到了同样的问题,偶然发现了这个网站: jsbeautifier你可能想看看。对我来说效果很好;)