|
| 1 | +## Architecture |
| 2 | + |
| 3 | +This document describes the high-level architecture of postgres_lsp. If you want to familiarize yourself with the code base, you are just in the right place! |
| 4 | + |
| 5 | +> Since the project still evolves rapidly, this document may not be up-to-date. If you find any inconsistency, please let us know by creating an issue. |
| 6 | +
|
| 7 | +### Bird's Eye View |
| 8 | + |
| 9 | +On the highest level, the postgres language server is a thing which accepts input source code, cuts it into individual sql statements and parses and analyses each. In addition, it connects to a postgres database and stores an im-memory schema cache with all required type information such as tables, columns and functions. The result of the parsing is used alongside the schema cache to answer queries about a statement. |
| 10 | + |
| 11 | +The client can submit a delta of input data (typically, a change to a single file), and the server will update the affected statements and their analysis accordingly. The underlying engine makes sure that we only re-parse and re-analyse what is necessary. |
| 12 | + |
| 13 | +### Entry Points |
| 14 | + |
| 15 | +The main entry point is as of now the `pg_lsp` crate, especially the `main.rs` function. It spawns the language server and starts listening for incoming messages. The server is implemented in the `server` module. |
| 16 | + |
| 17 | +There might be an additional entry point for a CLI tool in the future. |
| 18 | + |
| 19 | +### Code Map |
| 20 | + |
| 21 | +This section talks briefly about various important directories and data structures. |
| 22 | + |
| 23 | +#### `lib/` |
| 24 | + |
| 25 | +Independent libraries that are used by the project but are not specific for postgres. |
| 26 | + |
| 27 | +#### `crates/pg_lsp` |
| 28 | + |
| 29 | +The main entry point of the language server. It contains the server implementation and the main loop. |
| 30 | + |
| 31 | +#### `crates/pg_workspace` |
| 32 | + |
| 33 | +> This crate will grow significantly in near future. The current implementation just contains the base data structures and stores the diagnostic results from various features. |
| 34 | +
|
| 35 | +The main API for consumers of the IDE. It stores the internal state of the workspace, such as the schema cache and the parsed statements and their analysis. |
| 36 | + |
| 37 | +#### `crates/pg_lexer` |
| 38 | + |
| 39 | +Simple lexer that tokenizes the input source code. Enhances the output of the `pg_query` tokenizer with the missing whitespace tokens. |
| 40 | + |
| 41 | +#### `crates/pg_statement_splitter` |
| 42 | + |
| 43 | +Implements the statement splitter, which cuts the input source code into individual statements. |
| 44 | + |
| 45 | +#### `crates/pg_base_db` |
| 46 | + |
| 47 | +Implements the base data structures and defines how documents and statements are stored and updated efficiently. |
| 48 | + |
| 49 | +#### `crates/pg_schema_cache` |
| 50 | + |
| 51 | +We store an in-memory representation of the database schema to efficiently resolve types. |
| 52 | + |
| 53 | +#### `crates/pg_query_ext` |
| 54 | + |
| 55 | +Simple wrapper crate for `pg_query` to expose types and a function to get the root node for an SQL statement. It also host any "extensions" to the `pg_query` crate that are not yet contributed upstream. Once all extensions are contributed upstream, this crate will be removed. |
| 56 | + |
| 57 | +#### `crates/pg_query_proto_parser` |
| 58 | + |
| 59 | +We use procedural macros a lot to generate repetitive code from the protobuf definition provided by `libg_query`. The `pg_query_proto_parser` crate is used to parse the proto file into a more usable data structure. |
| 60 | + |
| 61 | +#### `crates/pg_syntax` |
| 62 | + |
| 63 | +Implements the CST parser and AST enhancer. The CST parser is what is described in [this blog post](https://supabase.com/blog/postgres-language-server-implementing-parser). The AST enhancer takes in the CST and enriches the AST returned by `pg_query` with a range for each node. |
| 64 | + |
| 65 | +#### `crates/pg_type_resolver` |
| 66 | + |
| 67 | +Utility crate used by the feature crates listed below to resolve the source types to the actual types in the schema cache. |
| 68 | + |
| 69 | +#### `crates/pg_commands`, `crates/pg_completions`, `crates/pg_hover`, `crates/pg_inlay_hints`, `crates/pg_lint`, `crates/pg_typecheck` |
| 70 | + |
| 71 | +These crates implement the various features of the language server. They are all independent of each other and always operate on the schema cache and a single statement and its parse results. They are intentionally implemented in separate creates and without any language server flavour to make them reusable eg in a later cli. |
0 commit comments