简体   繁体   English

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

[英]How to automatically indent the JavaScript code in Aptana Studio?

Previously I used Eclipse for PHP and Java development, and I liked the command Ctrl + i to automatically indent the code.之前我用 Eclipse 进行 PHP 和 Java 开发,我喜欢命令Ctrl + i自动缩进代码。

Now am I using Aptana Studio that is based on Eclipse, for JavaScript development.现在我正在使用基于 Eclipse 的 Aptana Studio 进行 JavaScript 开发。 I would like to automatically indent my JavaScript code, but Ctrl + i deletes all my code.我想自动缩进我的 JavaScript 代码,但是Ctrl + i删除了我的所有代码。 Is there any other command that automatically indent JavaScript code in Aptana Studio?在 Aptana Studio 中是否有任何其他命令可以自动缩进 JavaScript 代码?

The Studio does not have this feature yet. Studio 尚无此功能。 You can add a feature request here: https://aptana.lighthouseapp.com/projects/35272-studio/tickets/new您可以在此处添加功能请求: https://aptana.lighthouseapp.com/projects/35272-studio/tickets/new

Ctrl+Shift+F will format your entire file, as Tim suggested.正如 Tim 建议的那样,Ctrl+Shift+F 将格式化您的整个文件。 You can control some of the formatter settings through the preferences at 'Window -> Preferences -> Aptana Studio -> Formatter'您可以通过“Window -> Preferences -> Aptana Studio -> Formatter”中的首选项控制一些格式化程序设置

Maybe you mean format feature CTRL+SHIFT+F ?也许您的意思是格式功能CTRL+SHIFT+F

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.我刚刚更新到 Aptana Studio 3.0.9,它现在似乎能够在某些情况下格式化 JavaScript、HTML 和 CSS 的特定(选定)行。 The default key binding for me was something odd so I changed the key binding to CTRL+I in Preferences > General > Keys .我的默认键绑定有点奇怪,所以我在Preferences > General > Keys键绑定更改为CTRL+I

If no code is selected then Source > Format seems to always reliably format the entire file.如果未选择任何代码,则Source > Format似乎总是可靠地格式化整个文件。 The behaviour of Source > Format on selections differs depending on the type of code you're editing: Source > Format选择的行为因您正在编辑的代码类型而异:

JavaScript JavaScript

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

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 > Format will do the right thing.如果您 select 一大块 HTML,并且如果该选择的顶行已经正确缩进,并且如果选择不包含任何未关闭的元素,那么Source > Format将做正确的事情。
  • Otherwise Source > Format seems to act unpredictably with HTML.否则Source > Format似乎对 HTML 的行为不可预测。

CSS CSS

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

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.org to 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/2012 I've posted this bug on the JIRA: https://jira.appcelerator.org/browse/APSTUD-4516更新 19/3/2012我在 JIRA 上发布了这个错误: https://jira.appcelerator.org/browse/APSTUD-4516

I was having the same issue and stumbled across this site: jsbeautifier You might wanna check it out.我遇到了同样的问题,偶然发现了这个网站: jsbeautifier你可能想看看。 Worked fine for me;)对我来说工作得很好;)

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

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