Crafting a Language Server in Golang: A VSCode LSP for Lama2
Tags: webdev, api, go, programming
Published: 2023-11-07 15:30:00

This post narrates our journey to developing advanced functions in our VSCode extension for Lama2, a plain-text free API client manager. We will see how our thinking and implementation evolved through three stages over a period of a year.


## The Genesis of Lama2 🦙

[Lama2](https://hexmos.com/lama2/index.html) is a plain-text oriented API manager optimized for team collaboration via Git.

My team stores all our APIs in a Git repository, ensuring easy collaboration and regular updates.
Whenever an API is created or updated, the link to the l2 file's Git repo is mentioned as part of the corresponding PR/MR.

The below demonstration shows how an API request can be made from a `.l2` file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wd5pif1hb6mwj2ocs034.gif)

The below demonstration shows how an API request can be made from the extension [Lama2Code](https://marketplace.visualstudio.com/items?itemName=hexmos.Lama2) which is written in Typescript.
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utvjnwkxnrb3ywuujkkj.gif)

Running an l2 file is simple:

```bash
l2  /home/lovestaco/apihub/oneLogin/login.l2
```

The diagram depicts the flow of an API call, the L2 binary gets the env from [`l2.env`](https://hexmos.com/lama2/tutorials/examples.html#case-1-l2env-adjacent-to-an-api-file). It then uses [`httpie-go`](https://pkg.go.dev/github.com/HexmosTech/httpie-go@v1.0.8) to make the request and outputs the results to Stdout.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/46hxo95bjv3g552srmp1.png)

The `l2.env` file sits in the same folder as the l2 API file. It contains environment variables(env) like:

```javascript
export FABI_PROD="http://httpbin.org/"
export FABI_LOCAL="http://0.0.0.0:8000/"
```

### Early Steps: Naive Implementation of Auto-Completion

#### Elevating User Experience: Auto-Completion for api.l2

Manually referring to env variables from `l2.env` every time wasn't efficient. So, to make things easier, we added a feature: the extension checks `l2.env` for auto-completion.

Retrieving envs became a repetitive task, necessitating an efficient solution.

The diagram illustrates the flow of an API request. The extension runs the l2 command, after which the L2 binary fetches the env from `l2.env`, executes the request, and then outputs to stdout. The extension then reads it out and displays it out using webview.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05hkrppraplf4l3me0hg.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p56pkjunqr38u2t5i0tt.png) 

This picture showcases the auto-completion of an env using Lama2Code for the first iteration.

#### The Stage 1 Hurdle

While the initial problem was addressed, the solution wasn't optimal because it was not reusable and was not standardized. Clearly, a more efficient approach was required.

As a remedy, we introduced an argument: `l2 -e`.
It gathered all the envs, allowing editors to invoke the command and use the [registerCompletionItemProvider](https://code.visualstudio.com/api/references/vscode-api#languages.registerCompletionItemProvider) API for auto-completing the env.

### Leveling Up: The CLI Switch

We developed the `l2 -e` command, this cool command can fetch envs from both `l2.env` and ` l2config.env`. And when an env is declared in both files, we prioritized the values in `l2.env`. This decision not only helped in avoiding conflicts but also improved the efficiency of our VSCode extension.

#### Why We Wanted a Project-Level env(l2config.env) File:
Continue reading the full article [https://journal.hexmos.com/lama2-lsp-journey/](https://journal.hexmos.com/lama2-lsp-journey/)