netbeans 代码对齐

tech2023-12-01  36

netbeans 代码对齐

Writing an application in PHP or any other language is both a rewarding and sometimes challenging endeavor. It’s also a time consuming process depending on the complexity of the project you’re working on. Some IDEs have built in tools or plugins that aim to speed up the programming process. There are also other helpful pieces of software such as libraries, code snippets, and third party applications. In this article I’ll show you how I use a feature built into NetBeans along with two other applications to increase my productivity when programming on Windows. These tips and techniques offer a different way of thinking about things which should be helpful to any developer, regardless of his skill level.

用PHP或任何其他语言编写应用程序既是一项有意义的工作,又是一项充满挑战的工作。 根据您正在处理的项目的复杂性,这也是一个耗时的过程。 一些IDE内置了旨在加快编程过程的工具或插件。 还有其他有用的软件,例如库,代码段和第三方应用程序。 在本文中,我将向您展示如何在Windows上进行编程时如何使用NetBeans内置的功能以及其他两个应用程序来提高生产率。 这些技巧和技巧提供了一种不同的思考方式,无论开发人员的技术水平如何,这对任何开发人员都应有所帮助。

I’m working with the NetBeans IDE for the purposes of this article. If you don’t already have it installed, you’ll need to make sure the Java JDK is installed first since it is a requirement for NetBeans. You can download the latest JDK from Oracle. Once the JDK is installed, you can download NetBeans from target=”_blank”>netbeans.org/downloads. There are several versions of NetBeans available; I downloaded the All bundle because in addition to PHP I also do some C and Java development. If you only need PHP support, then feel free to install the PHP bundle instead.

出于本文的目的,我正在使用NetBeans IDE。 如果尚未安装Java JDK,则需要先安装,因为这是NetBeans的要求。 您可以从Oracle下载最新的JDK 。 安装JDK后,您可以从target =” _ blank”> netbeans.org/downloads下载NetBeans。 有几种可用的NetBeans版本。 我下载了All捆绑包,因为除了PHP之外,我还进行了一些C和Java开发。 如果只需要PHP支持,则可以随意安装PHP捆绑包。

NetBeans的代码模板 (NetBeans’ Code Templates)

Go ahead and start NetBeans and create a new project; a dummy project will be sufficient for our purposes right now. Select File > New Project from the main menu bar, and in the New Project dialog select PHP Application and click Next. Since you are only creating a dummy project, you can click Finish on the next screen. NetBeans creates the new project and opens the new index.php file for you to edit.

继续并启动NetBeans并创建一个新项目。 一个虚拟项目就足以满足我们目前的目的。 从主菜单栏中选择“ File >“ New Project ”,然后在“新建项目”对话框中选择“ PHP应用程序” ,然后单击“下一步”。 由于您仅创建虚拟项目,因此可以在下一个屏幕上单击“完成”。 NetBeans创建新项目并打开新的index.php文件供您编辑。

Code Templates is NetBeans’ code snippets functionality. Using templates allows you to write an abbreviation for a common piece of code and then press a trigger key to replace the abbreviation with a code snippet. Let’s try one of the templates that are already defined…

代码模板是NetBeans的代码片段功能。 使用模板,您可以为一段通用代码编写缩写,然后按触发键以代码片段替换该缩写。 让我们尝试一个已经定义的模板...

In index.php which was opened after creating the new project, place the cursor after the comment in the PHP block and press Enter to insert a new line. Then type the word if and, instead of pressing the space bar, press the Tab key. NetBeans will add the beginnings of an if statement to your script.

在创建新项目后打开的index.php中,将光标放在PHP块中的注释之后,然后按Enter以插入新行。 然后输入单词if和,而不是按空格键,而是按Tab键。 NetBeans会将if语句的开头添加到脚本中。

Now let’s create a new code snippet. Select Tools > Options from the main menu, and click on the Editor icon at the top of the Options window. Choose the Code Templates tab and then select HTML from the Language drop down. Click on the New button, type pre in the Abbreviation field, and click OK. In the Expanded Text field, type the following:

现在,让我们创建一个新的代码片段。 从主菜单中选择“ Tools >“ Options ”,然后单击“选项”窗口顶部的“编辑器”图标。 选择“代码模板”选项卡,然后从“语言”下拉列表中选择“ HTML ”。 单击“新建”按钮,在“缩写”字段中键入pre ,然后单击“确定”。 在扩展文本字段中,键入以下内容:

<pre> <?php print_r(${cursor}); ?> </pre>

I’ve chosen this as a simple example because it’s a common method to view the contents of arrays and objects in PHP. NetBeans is code aware, so because you created the snippet in the HTML templates it won’t work if you’re inside a PHP block. The ${cursor} variable in the snippet tells NetBeans where to place the cursor after the trigger key is pressed. After you’ve typed the code, click OK and test it out.

我选择这个作为一个简单的示例,因为它是查看PHP中数组和对象内容的常用方法。 NetBeans可以识别代码,因此,因为您是在HTML模板中创建的代码段,所以如果您位于PHP块中,则该代码段将无效。 片段中的${cursor}变量告诉NetBeans在按下触发键后将光标放置在何处。 键入代码后,单击“确定”并进行测试。

自动热键 (AutoHotKey)

Now that you know how to define Code Templates in NetBeans, let’s move on to another piece of helpful software– a program called AutoHotKey. AutoHotKey is a scripting language to automate tasks on Windows. You can download it from www.autohotkey.com/download.

既然您已经知道如何在NetBeans中定义代码模板,那么让我们继续研究另一个有用的软件-一个名为AutoHotKey的程序。 AutoHotKey是一种脚本语言,可以在Windows上自动执行任务。 您可以从www.autohotkey.com/download下载。

Once AutoHotKey is installed, right click on the desktop and select New > AutoHotKey script and name the file anything you like. Open the AHK file in a text editor like notepad, and type the following:

安装AutoHotKey后,右键单击桌面,然后选择“ New >“ AutoHotKey脚本,然后根据需要命名文件。 在文本编辑器(如记事本)中打开AHK文件,然后键入以下内容:

#^p:: Send pre{tab} Return

The pound sign tells AutoHotKey to listen for the Windows key, the caret signifies the CTRL key, and P is just the P key. The two colons afterwards tell AutoHotKey this is a batch of commands. The next line sends the keystrokes p, r, e, and a Tab to whichever window is active at the moment. Those keystrokes also match your Code Template abbreviation and the trigger Key in NetBeans. Return tells AutoHotKey to stop processing lines and start listening for other keys.

井号指示AutoHotKey侦听Windows键,插入号指示CTRL键,而P只是P键。 之后,两个冒号告诉AutoHotKey这是一批命令。 下一行将击键p , r , e和一个Tab发送到当前处于活动状态的任何窗口。 这些击键还与您的代码模板缩写和NetBeans中的触发键相匹配。 Return告诉AutoHotKey停止处理行并开始侦听其他键。

Save the file, then right-click on the AHK file and select Run script. Go back to NetBeans and delete the previous pre block you inserted using Code Templates. Now on your keyboard press CTRL + WIN + P and the pre block should be inserted and the cursor placed in the correct location.

保存文件,然后右键单击AHK文件并选择Run script 。 返回NetBeans并删除使用代码模板插入的上一个前置块。 现在,在键盘上按CTRL + WIN + P,应该插入pre块,并将光标放置在正确的位置。

AutoHotKey has a simple but effective syntax for scripting Windows. You can read more about its hotkey and see more sample scripts at www.autohotkey.com/docs/Hotkeys.htm.

AutoHotKey具有用于编写Windows脚本的简单但有效的语法。 您可以在www.autohotkey.com/docs/Hotkeys.htm上阅读有关其热键的更多信息,以及更多示例脚本。

同上 (Ditto)

The final piece of software I’d like to bring to your attention is Ditto. Ditto is a clipboard manager, and is one of the better ones I’ve tried as it is unobtrusive and has a small memory footprint. You can download it from ditto-cp.sourceforge.net. Once Ditto is downloaded, extract the contents of the archive file to any location of your choosing. Open the folder and double-click on the Ditto.exe file. This runs Ditto and places it in the System Tray. Right click on the Ditto icon and click on Options.

我想提请您注意的最后一个软件是Ditto。 Ditto是剪贴板管理器,并且是我尝试过的更好的应用程序之一,因为它不引人注目并且占用的内存很小。 您可以从ditto-cp.sourceforge.net下载它。 下载Ditto之后,将存档文件的内容提取到您选择的任何位置。 打开文件夹,然后双击Ditto.exe文件。 这将运行Ditto并将其放置在系统托盘中。 右键单击Ditto图标,然后单击Options 。

I set a couple of options but most of the defaults have proven to be sufficient in the time I’ve been using it. For example, Ditto defaults to a storage size of 500 items which is plenty in my opinion, but you can change this number to suit your needs. The options I have changed however are to start Ditto on system startup and its shortcut key to CTRL + SHIFT + V. This allows me to use Ditto by pressing one additional key to paste.

我设置了两个选项,但是事实证明,大多数默认设置在使用我的时候就足够了。 例如,Ditto的默认存储大小为500个项目,我认为这足够了,但是您可以更改此数字以满足您的需求。 但是,我更改的选项是在系统启动时启动Ditto,其快捷键为CTRL + SHIFT +V。这使我可以通过按一个附加键进行粘贴来使用Ditto。

Let’s operate under the assumption you’ve copied and pasted several variable names for arrays, and now you want to see the contents of one of them. In NetBeans, press CTRL + WIN + P to insert your pre block, and then press CTRL + SHIFT + V until the variable name you wanted to use is shown. You’ve cut down a lot of your keystrokes and have managed to keep your hands in similar positions on the keyboard!

让我们假设您已经为数组复制并粘贴了多个变量名,现在您要查看其中一个的内容。 在NetBeans中,按CTRL + WIN + P插入您的前置块,然后按CTRL + SHIFT + V直到显示要使用的变量名。 您已经减少了很多击键,并设法将手放在键盘上的相似位置!

Ditto allows you to sync your clipboards across multiple computers, which is great if you can forward ports with your router, adjust your firewall settings, etc. Unfortunately I don’t have access the port forwarding settings in the router at the office. To get around this limitation, I copied the ditto.db from the Ditto folder and placed it in a Ditto directory in my Dropbox account. Then I change the database path in Ditto’s options to point to the Dropbox folder. I just have to remember to close Ditto before I leave my office computer and the database stays synced between my other computers. I also make use of Dropbox to store my AutoHotKey scripts.

Ditto允许您在多台计算机之间同步剪贴板,如果您可以使用路由器转发端口,调整防火墙设置等,这非常有用。不幸的是,我无法在办公室的路由器中访问端口转发设置。 为了解决此限制,我从Ditto文件夹复制了ditto.db并将其放置在我的Dropbox帐户的Ditto目录中。 然后,在Ditto的选项中更改数据库路径,以指向Dropbox文件夹。 我只需要记住在离开办公室计算机之前关闭Ditto,数据库将在其他计算机之间保持同步。 我还利用Dropbox来存储我的AutoHotKey脚本。

摘要 (Summary)

In this article I’ve shown you how I use NetBeans’ Code Templates with AutoHotKey and Ditto to boost my productivity when programming. If you weren’t familiar with these programs before, I hope you find them useful and helpful. If you have any productivity tips you’d like to share, feel free to do so in the comments section below.

在本文中,我向您展示了如何将NetBeans的代码模板与AutoHotKey和Ditto结合使用,以提高编程时的生产率。 如果您以前不熟悉这些程序,希望您对它们有所帮助。 如果您有任何要分享的工作效率提示,请在下面的评论部分中随意进行。

Image via Gunnar Pippel / Shutterstock

图片来自Gunnar Pippel / Shutterstock

翻译自: https://www.sitepoint.com/code-templates-autohotkey-and-ditto/

netbeans 代码对齐

最新回复(0)