Skip to content

Commit

Permalink
Updating Crystal LSP (#4156)
Browse files Browse the repository at this point in the history
* fix: crystal lsp server

* feat: add crystal-guide

* feat: add crystal-guide to mkdocs.yml
  • Loading branch information
lanjoni authored Sep 1, 2023
1 parent f8ed844 commit 271525a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 10 deletions.
14 changes: 7 additions & 7 deletions clients/lsp-crystal.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@

(require 'lsp-mode)

(defgroup lsp-scry nil
"LSP support for Crystal via scry."
(defgroup lsp-crystalline nil
"LSP support for Crystal via crystalline."
:group 'lsp-mode
:link '(url-link "https://github.com/crystal-lang-tools/scry"))
:link '(url-link "https://github.com/elbywan/crystalline"))

(defcustom lsp-clients-crystal-executable '("scry" "--stdio")
"Command to start the scry language server."
:group 'lsp-scry
(defcustom lsp-clients-crystal-executable '("crystalline" "--stdio")
"Command to start the crystalline language server."
:group 'lsp-crystalline
:risky t
:type 'file)

(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection lsp-clients-crystal-executable)
:major-modes '(crystal-mode)
:server-id 'scry))
:server-id 'crystalline))

(lsp-consistency-check lsp-crystal)

Expand Down
6 changes: 3 additions & 3 deletions docs/lsp-clients.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,9 @@
{
"name": "crystal",
"full-name": "Crystal",
"server-name": "scry",
"server-url": "https://github.com/crystal-lang-tools/scry",
"installation-url": "https://github.com/crystal-lang-tools/scry#installation",
"server-name": "crystalline",
"server-url": "https://github.com/elbywan/crystalline",
"installation-url": "https://github.com/elbywan/crystalline#installation",
"debugger": "Not available"
},
{
Expand Down
74 changes: 74 additions & 0 deletions docs/tutorials/crystal-guide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
---
author: lanjoni
template: comment.html
root_file: docs/tutorials/crystal-guide.md
---

# Configuring `Emacs` as a `Crystal` IDE

Author: [@lanjoni](https://github.com/lanjoni)

In this guide, I'll show you how to configure `lsp-mode` for Crystal development, using `GNU Emacs`. By the end of this tutorial you will be able to use the code editor and all its features for Crystal so far.

For all `lsp-mode` features, check
[here](https://emacs-lsp.github.io/lsp-mode/page/main-features/).

## Installing the language server

`lsp-mode` is a client-server application with `Emacs` acting as the client. In
order for it to work, you have to install a separate _language server_ that
understands the specifics of your target language.

For Crystal, we will be using [crystalline](https://github.com/elbywan/crystalline), which is currently the only [LSP](https://microsoft.github.io/language-server-protocol/implementors/servers/) server for Crystal that is still receiving updates and community support. Previously, [scry](https://github.com/crystal-lang-tools/scry) was used as an LSP server.

### Via lsp-mode

`lsp-mode` ships with automatic installation scripts for some of the language servers and crystal-lsp
is one of the supported servers, you can install it using <kbd>M-x</kbd> `lsp-install-server` <kbd>RET</kbd> `crystal-lsp`.
After installed, `lsp-mode` will automatically initialize it when opening Crystal files.

### Manually

To install manually first make sure the binary is already in your PATH. You can download an already compiled version or compile from the original code with Crystal. Check [here](https://github.com/elbywan/crystalline#installation) for more information on how to build it manually.

After installed, you can add in your settings the definitions to use Crystalline with lsp-mode by default with:

```elisp
(with-eval-after-load 'lsp-mode
(add-to-list 'lsp-language-id-configuration
'(crystal-mode . "crystal"))
(lsp-register-client
(make-lsp-client :new-connection (lsp-stdio-connection '("crystalline"))
:activation-fn (lsp-activate-on "crystal")
:priority '1
:server-id 'crystalline)))
```

For a custom path, you can set the path of the server location with:

```elisp
(setq lsp-crystal-custom-server-command '("bash" "-c" "/path/to/crystal-lsp"))
```

## LSP features

This guide will focus on the features of `lsp-mode` that are most relevant to Crystal development. You can view documentation of all features at the official [lsp-mode site](https://emacs-lsp.github.io/lsp-mode/page/main-features/) or [crystalline features section](https://github.com/elbywan/crystalline#features).

### Syntax check

This automatically checks your code for errors and warnings as you write it.

There is syntax checking, functions/variables that are not used or do not exist, among other types of checks.

![syntax-checker](images/crystal-syntax-checker.png "syntax-checker")

### Formatting

For code formatting, crystal-lsp together with Crystalline provide code formatting following the respective [code conventions (coding style)](https://crystal-lang.org/reference/1.9/conventions/coding_style.html) of the Crystal programming language.

## Performance

The first time `crystal-lsp` starts your project, it will scan all the settings in your `shard.yml` file, which may take a while depending on the size of your project.

## See also
- [Crystal Book](https://crystal-lang.org/reference/1.9/index.html)
Binary file added docs/tutorials/images/crystal-syntax-checker.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ nav:
- 'Configuring Emacs as a Clojure IDE': tutorials/clojure-guide.md
- 'Debugging Clojurescript': tutorials/debugging-clojure-script.md
- 'Python, debbuging with poetry + pyenv': https://emacs-lsp.github.io/dap-mode/page/python-poetry-pyenv/
- 'Configuring Emacs as a Crystal IDE': tutorials/crystal-guide.md
- Blog:
- 2020:
- '7.0 Release': blog/2020/7.0-release.md
Expand Down

0 comments on commit 271525a

Please sign in to comment.