Using Editors and IDEs

You now know how to build and run a Crystal code file on the command line, but as a developer, you’ll probably want some conveniences in your program editor, such as syntax-highlighting and other features. Plugins[129] exist for Emacs, Vim, Textmate, Notepad++, IntellliJ, Sublime Text, Atom, and Visual Studio Code. Also have a look here.[130]

We’ll highlight three cross-platform and open-source editors that offer IDE functionality for Crystal. The choice is yours—they all offer more or less the same developer support.

Working with Sublime Text

The well-known programmer’s editor Sublime Text[131] has a nice plugin, Sublime-crystal,[132] that provides syntax highlighting and various forms of code completion. You can find it as Crystal in Sublime Text Package Control.

Install it with the menu Preferences / Package Control / Install Package. Then verify from the menu Tools / Build system that Crystal is checked.

After editing, you can build and execute a Crystal program with the keyboard combination CTRL+B. This saves your source code before you start to compile and run. But it also formats your source code by applying the crystal format tool, so your code is always in good shape!

In the code (hello_world_class.cr) in the screenshot that follows, I’ve defined a class HelloWorld with an initialize and a greet method. HelloWorld.new creates an object, hw, and this calls the greet method. This outputs the “Hello, world!” message. We’ll go over syntax details in Chapter 2, Crystal Foundations.

images/setting_up/Fig_5_Sublime_Text.png

Working with Atom

Atom[133] is also a very popular editor, and there are no less than four plugins being developed for Crystal to enhance your developing experience. ide-crystal gives you an all-round package for working with Crystal, but some of the others might be good complements.

  • ide-crystal[134] aims to provide all IDE functionality for Crystal, such as auto-completion, linting and formatting your code, and many more.

  • language-crystal-actual[135] provides general Crystal language support, such as syntax highlighting and snippets.

  • crystal-tools,[136] developed by Manas, enables the built-in Crystal compiler tools in Atom.

  • crystal-block,[137] highlights matching crystal blocks for certain keywords.

The following is a screenshot with the same code as before:

images/setting_up/Fig_6_Atom.png

Working with Visual Studio Code

Two Crystal plugins provide a nice IDE experience for Visual Studio Code:[138]

  • vscode-crystal,[139] which adds code snippets and syntax highlighting as well as other options

  • vscode-crystal-ide,[140] a Crystal IDE powered by Language Server Protocol, with syntax coloring and error checking, with many more features coming.

Here’s a screenshot with the same code:

images/setting_up/Fig_7_VSCode.png

Click the right mouse button and choose Run code from the menu to build and run a code file. The vscode-native-debugger[141] provides debugger support for both GDB and LLDB.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.218.45