Skip to main content

ElixirLS Overview

ElixirLS is an Elixir language server that provides a wide range of features for code editing and development in the Elixir programming language.

It is built using the Language Server Protocol (LSP), which enables it to provide syntax highlighting, code completion, documentation lookup, and other useful functionalities within popular code editors.

In this tutorial, we will explore the history, features, and examples of ElixirLS to help you understand its capabilities and how it can enhance your Elixir development workflow.

History of ElixirLS

ElixirLS was initially created by Jake Becker in 2016 as a Visual Studio Code extension called vscode-elixir. Over time, it gained popularity and evolved into a standalone language server that supports various code editors, including Visual Studio Code, Atom, and Sublime Text.

Since its inception, ElixirLS has undergone significant development and improvements, thanks to the contributions from the Elixir community. It has become an essential tool for Elixir developers, providing them with a seamless development experience.

Features of ElixirLS

Now let's delve into the features provided by ElixirLS that make it a powerful tool for Elixir developers.

1. Syntax Highlighting

ElixirLS offers syntax highlighting, which helps in visually distinguishing different elements of the code. It highlights keywords, modules, functions, variables, and other language constructs, making the code more readable and understandable.

Here's an example of Elixir code with syntax highlighting:

defmodule HelloWorld do
def hello(name) do
IO.puts("Hello, #{name}!")
end
end

The syntax highlighting provided by ElixirLS makes it easier to identify the module definition (defmodule), function definition (def), and string interpolation (#{name}).

2. Code Completion

ElixirLS provides intelligent code completion, allowing developers to quickly write code by suggesting available functions, modules, and variables. It analyzes the code context and provides relevant suggestions based on the imported modules and defined functions.

Let's consider the following example:

defmodule Calculator do
def add(a, b) do
a + b
end
end

Calculator.add(1, )

When you type Calculator.add(1, ) and trigger the code completion feature provided by ElixirLS, it suggests the variable names and function calls available in the current context, making it easier to complete the code with the desired arguments.

3. Documentation Lookup

With ElixirLS, you can quickly access the documentation of different modules, functions, and macros within your code editor. By hovering over a function or module name, you can view its documentation in a tooltip or a separate panel.

The documentation lookup feature helps in understanding the purpose, usage, and parameters of various functions and modules, enabling you to write code with better clarity.

Here's an example of accessing documentation using ElixirLS:

defmodule Calculator do
@moduledoc """
A simple calculator module.
"""

@doc """
Adds two numbers.

## Examples

iex> Calculator.add(1, 2)
3

"""

def add(a, b) do
a + b
end
end

When you hover over the add function, ElixirLS displays the documentation for the function, including its purpose, examples, and usage information.

4. Code Navigation

ElixirLS enables seamless code navigation within your project. It provides features like "Go to Definition" and "Find References" to help you quickly jump to the definition of a module, function, or variable, or find all the references to a specific element.

By using these navigation features, you can easily explore the codebase, understand dependencies, and navigate between different parts of your project, enhancing your productivity as a developer.

5. Code Formatting

ElixirLS offers code formatting capabilities, allowing you to automatically format your Elixir code according to a defined coding style. It helps in maintaining a consistent code style across your project and saves you the effort of manually formatting the code.

By integrating ElixirLS with your code editor, you can format the code with a single command or configure automatic formatting on file save.

Examples of ElixirLS

To illustrate the features of ElixirLS further, let's consider a few examples that demonstrate its capabilities.

Example 1: Code Completion

defmodule Calculator do
def add(a, b) do
a + b
end

def subtract(a, b) do
a - b
end
end

# Trigger code completion after typing "Calculator."

In the above example, when you trigger the code completion feature after typing Calculator., ElixirLS suggests the available functions add/2 and subtract/2 defined within the Calculator module.

Example 2: Documentation Lookup

defmodule Calculator do
@doc """
Adds two numbers.

## Examples

iex> Calculator.add(1, 2)
3

"""

def add(a, b) do
a + b
end
end

# Hover over the `add` function to view documentation.

By hovering over the add function, ElixirLS displays the documentation for the function, including the purpose, examples, and usage information.

Example 3: Code Navigation

defmodule Calculator do
def add(a, b) do
a + b
end

def multiply(a, b) do
add(a, b) * b
end
end

# Use the "Go to Definition" feature to navigate to the definition of `add`.

When you use the "Go to Definition" feature on the add function, ElixirLS takes you to the definition of the add function within the Calculator module.

Conclusion

ElixirLS is a powerful language server that enhances the development experience for Elixir developers. Its features like syntax highlighting, code completion, documentation lookup, and code navigation help in writing clean, efficient, and maintainable Elixir code.

To learn more about ElixirLS and its installation, configuration, and usage, refer to the official ElixirLS documentation.