LSP server and emacs LSP client glue for eglot and lsp-mode #80

Open
pkulev wants to merge 3 commits from pkulev/sex:feature/lsp into main
Collaborator

Summary

sexc can index a unit without stopping at the first bad form. sex-lsp serves that index over LSP. sex-lsp.el registers the same server with lsp-mode and eglot. sex-mode.el stays the editing mode.

@alex-eg, specs, realizations, and closure helpers are not indexed, as you have plans to rework them substantially.

Compiler

A long-running query resets the compiler before it reads. reset-name-tables! clears the name, type, and tag tables and then the sums, ops, specs, and realizations that reset-declarations! already cleared. Macros, the module caches, form locations, and closure tables are cleared too. One file is analyzed at a time. The prelude is still prepended, but those forms were never read from a file, so u8 and ƛenv do not show up as the unit's names.

sex-error still aborts, and the message is still file:line: text plus the same arguments. It also records a diagnostic first, and the condition carries a sex mark so the catcher does not record it twice. Warnings go into that same list instead of stderr while a query is running. A normal sexc build is unchanged: the list is off, the first error still ends the process.

Recovery is one toplevel form at a time. process-rec catches only when diagnostics are being collected. The bad form adds nothing to the output. Later forms are still walked, so a function after (nope) is still indexed. There is no statement-level recovery.

While walking, plain declarations are recorded:

  • fn, with the written prototype (fn name args ret) and the docstring
  • var, global or local, and parameters
  • struct, union, enum
  • typedef
  • enum constants, as const
  • sum, plus each constructor as an fn

A local's scope is the line range of the fn, do, for, while, if, switch, or case that binds it. It is visible from its own line through the end of that range. Lookup takes the tightest range that contains the point. Same span: the later declaration wins, and a struct beats the typedef of the same name. A global has no range and is visible through the whole unit. Imported pub forms keep the defining file's location, so a jump lands in that file. Names containing ƛ, ǂ, or ː are dropped. A sum records itself and then suppresses the structs and typedefs it generates underneath.

Locations are still (file line end-line) on forms. The identifier under the cursor is taken from the buffer text.

sexc --check prints (diagnostics ...). Exit status is non-zero only when the file cannot be read. Semantic errors stay in the list and the process exits 0. sexc --symbols prints the table. sexc --expand-lines=START:END emits C with statement #line directives and keeps the chunks whose Sex line falls in that inclusive range. START alone, or :, is the whole buffer.

Server

sex-lsp is a second program: it speaks LSP on stdio and calls the query in-process.
Medea is used to work with json, that part publishes diagnostics, and answers hover, definition, completion, and document symbols. sex/expand takes a 1-based line range and returns the C. A change reanalyzes on a 150 ms quiet wait. file-select is always given a timeout. Without one it does not wake, and the stdin port is unbuffered so a short message is not stuck behind a 1024-byte fill.

clangd is off unless --c-backend=clangd. It is not shown a translation of the buffer. The server forks it onto pipes and asks it about a probe: the unit's (include ...) forms, the prelude headers, and NAME(0) inside a function. Hyphens become underscores except in ->. Results come back kebab-case. The probe sits for about a second and a half so clangd can read the headers before it answers. A half-typed name is completed. Hover and definition are requested on a finished name. Flags are a one-line sex-cflags file or --cflags.

Emacs

sex-mode.el defines sex-mode, syntax highlight and navigation, sex-lsp.el adds LSP on top.

Test plan

(include stdio.h)

(pub fn bar ((a int)) void
  "Add once."
  (return))

(fn main () void
  (bar 1)
  (pri)
  (nope))
What How You should see
Hover Point at bar (fn bar ((a int)) void) and Add once.
Definition M-. on the call bar Jumps to the pub fn. M-, comes back
Completion C-M-i after (ba bar
Diagnostics Leave (nope) in unknown top level form. lsp-mode: M-x flycheck-list-errors
Symbols M-x imenu bar, main, and their parameters
sexpand C-c C-e on a sexp, C-c C-d on a defun, C-c C-r on a region, C-c C-b for the buffer *sexpand* in a C mode, with the C for that range
C completion sex-c-backend is clangd, then complete (pri printf
C hover / definition Point at (printf "hi") The header prototype. M-. opens stdio.h

(pri is only long enough for completion. Hover and definition need the whole name. make check covers the server side of hover, diagnostics, and sex/expand.

## Summary `sexc` can index a unit without stopping at the first bad form. `sex-lsp` serves that index over LSP. `sex-lsp.el` registers the same server with lsp-mode and eglot. `sex-mode.el` stays the editing mode. @alex-eg, specs, realizations, and closure helpers are not indexed, as you have plans to rework them substantially. ## Compiler A long-running query resets the compiler before it reads. `reset-name-tables!` clears the name, type, and tag tables and then the sums, ops, specs, and realizations that `reset-declarations!` already cleared. Macros, the module caches, form locations, and closure tables are cleared too. One file is analyzed at a time. The prelude is still prepended, but those forms were never read from a file, so `u8` and `ƛenv` do not show up as the unit's names. `sex-error` still aborts, and the message is still `file:line: text` plus the same arguments. It also records a diagnostic first, and the condition carries a `sex` mark so the catcher does not record it twice. Warnings go into that same list instead of stderr while a query is running. A normal `sexc` build is unchanged: the list is off, the first error still ends the process. Recovery is one toplevel form at a time. `process-rec` catches only when diagnostics are being collected. The bad form adds nothing to the output. Later forms are still walked, so a function after `(nope)` is still indexed. There is no statement-level recovery. While walking, plain declarations are recorded: - `fn`, with the written prototype `(fn name args ret)` and the docstring - `var`, global or local, and parameters - `struct`, `union`, `enum` - `typedef` - enum constants, as `const` - `sum`, plus each constructor as an `fn` A local's scope is the line range of the `fn`, `do`, `for`, `while`, `if`, `switch`, or `case` that binds it. It is visible from its own line through the end of that range. Lookup takes the tightest range that contains the point. Same span: the later declaration wins, and a struct beats the typedef of the same name. A global has no range and is visible through the whole unit. Imported `pub` forms keep the defining file's location, so a jump lands in that file. Names containing `ƛ`, `ǂ`, or `ː` are dropped. A sum records itself and then suppresses the structs and typedefs it generates underneath. Locations are still `(file line end-line)` on forms. The identifier under the cursor is taken from the buffer text. `sexc --check` prints `(diagnostics ...)`. Exit status is non-zero only when the file cannot be read. Semantic errors stay in the list and the process exits 0. `sexc --symbols` prints the table. `sexc --expand-lines=START:END` emits C with statement `#line` directives and keeps the chunks whose Sex line falls in that inclusive range. `START` alone, or `:`, is the whole buffer. ## Server `sex-lsp` is a second program: it speaks LSP on stdio and calls the query in-process. Medea is used to work with json, that part publishes diagnostics, and answers hover, definition, completion, and document symbols. `sex/expand` takes a 1-based line range and returns the C. A change reanalyzes on a 150 ms quiet wait. `file-select` is always given a timeout. Without one it does not wake, and the stdin port is unbuffered so a short message is not stuck behind a 1024-byte fill. clangd is off unless `--c-backend=clangd`. It is not shown a translation of the buffer. The server forks it onto pipes and asks it about a probe: the unit's `(include ...)` forms, the prelude headers, and `NAME(0)` inside a function. Hyphens become underscores except in `->`. Results come back kebab-case. The probe sits for about a second and a half so clangd can read the headers before it answers. A half-typed name is completed. Hover and definition are requested on a finished name. Flags are a one-line `sex-cflags` file or `--cflags`. ## Emacs `sex-mode.el` defines sex-mode, syntax highlight and navigation, `sex-lsp.el` adds LSP on top. ## Test plan ```sex (include stdio.h) (pub fn bar ((a int)) void "Add once." (return)) (fn main () void (bar 1) (pri) (nope)) ``` | What | How | You should see | |---|---|---| | Hover | Point at `bar` | `(fn bar ((a int)) void)` and `Add once.` | | Definition | `M-.` on the call `bar` | Jumps to the `pub fn`. `M-,` comes back | | Completion | `C-M-i` after `(ba` | `bar` | | Diagnostics | Leave `(nope)` in | `unknown top level form`. lsp-mode: `M-x flycheck-list-errors` | | Symbols | `M-x imenu` | `bar`, `main`, and their parameters | | sexpand | `C-c C-e` on a sexp, `C-c C-d` on a defun, `C-c C-r` on a region, `C-c C-b` for the buffer | `*sexpand*` in a C mode, with the C for that range | | C completion | `sex-c-backend` is `clangd`, then complete `(pri` | `printf` | | C hover / definition | Point at `(printf "hi")` | The header prototype. `M-.` opens `stdio.h` | `(pri` is only long enough for completion. Hover and definition need the whole name. `make check` covers the server side of hover, diagnostics, and `sex/expand`.
pkulev added the feature label 2026-11-11 06:10:22 +01:00
pkulev self-assigned this 2026-11-11 06:10:22 +01:00
pkulev added 3 commits 2026-11-11 06:10:22 +01:00
Record plain declarations, with location and docstring, and keep going
when one toplevel form is wrong. sexc --check and --symbols print
that. Specs, realizations, and closure helpers stay out of the index
for future rework (@alex-eg).
sex-lsp answers:
- hover,
- definition,
- completion,
- symbols,
- diagnostics.

--expand-lines and sex/expand return the C for a line
range. clangd is off unless --c-backend=clangd, and it is asked about
names the compiler never parsed.
emacs: load the server beside sex-mode
All checks were successful
Sex CI / build-linux (pull_request) Successful in 6m4s
ac6daa748e
pkulev requested review from alex-eg 2026-11-11 06:10:22 +01:00
pkulev force-pushed feature/lsp from ac6daa748e to e2f71d23a0 2026-11-14 22:06:49 +01:00 Compare
All checks were successful
Sex CI / build-linux (pull_request) Successful in 5m54s
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u feature/lsp:pkulev-feature/lsp
git checkout pkulev-feature/lsp
Sign in to join this conversation.
No Reviewers
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: alex-eg/sex#80