---
# System prepended metadata

title: Language Server
tags: [shaping-archive]

---

# Language Server

###### tags: `shaping-archive`

Appetite: 1/2 cycle

## Goal

Provide a minimal language server for GT4Py field view with basic safety features.

## Status

A first unsafe implementation exists in https://github.com/havogt/gt4py/tree/lsp which provides
- diagnostics for field view errors
- type hints for fields

It is unsafe because it executes files on file open and on edit to extract required information. Malicious code (even by accident) might get executed without the user being aware.

The current mechanism works as follows
- search the AST of the opened/edited buffer for `@field_operator` or `@program` decorator
- if it has at least one of them we proceed
- `compile()` and `exec()` the current buffer (unsafe!)
- get the foast and past nodes
- if a `FieldOperatorTypeDeductionError` is raised in this process we publish diagnostics
- hover: we search for the current node under the cursor (not efficient) and publish the field type (if the node under cursor is of FieldType)

## Tasks

### Implement a safe mechanism to extract externals

#### Alternative 1: beniget use-def-chain to extract externals from AST

In https://github.com/havogt/gt4py/tree/lsp_beniget a first prototype is implemented using `beniget`'s use-def-chain. The strategy is to extract all required information from the AST without executing (or execute only field_operators + minimal set of white-listed external nodes)

#### Alternative 2: Use `RestrictedPython`

This approach would white-list certain modules and specific functions. (Maybe in combination with alternative 1)

#### MyPy hooks

Needs to be explored.

### Features

#### Clean implementation of current features

##### FieldType

Possibly extend TypeDeduction pass 

##### Diagnostics

Extend diagnostics to other capture other errors (in addition to `FieldOperatorTypeDeductionError`)

#### Extents information

E.g. on hover of an argument of a field_op, provide information on the chain of shifts. Information is available from `CollectShifts` pass.

### Clients

- Publish vscode extension
- Document how to use with neovim
- Optional: PyCharm plugin

