|
| 1 | +# Semcode LSP Server |
| 2 | + |
| 3 | +A Language Server Protocol (LSP) server that provides navigation features for C/C++ codebases indexed by semcode. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Go to Definition**: Jump to definitions of functions, macros, types, and typedefs using semcode's semantic database |
| 8 | +- **Find References**: Show all places where a symbol is referenced (callers) |
| 9 | +- **Git-Aware Lookups**: Automatically finds the correct version at your current commit |
| 10 | +- **Configuration Support**: Configurable database path through LSP client settings |
| 11 | + |
| 12 | +## Building |
| 13 | + |
| 14 | +```bash |
| 15 | +cargo build --release --bin semcode-lsp |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +The LSP server communicates over stdin/stdout using the JSON-RPC 2.0 protocol as specified by the Language Server Protocol. |
| 21 | + |
| 22 | +### Prerequisites |
| 23 | + |
| 24 | +1. A semcode-indexed codebase: |
| 25 | + ```bash |
| 26 | + semcode-index --source /path/to/your/code |
| 27 | + ``` |
| 28 | + |
| 29 | +2. The resulting `.semcode.db` database in your workspace directory |
| 30 | + |
| 31 | +## Neovim Configuration |
| 32 | + |
| 33 | +### Using nvim-lspconfig |
| 34 | + |
| 35 | +Add this to your Neovim configuration (`~/.config/nvim/init.lua` or similar): |
| 36 | + |
| 37 | +```lua |
| 38 | +-- Ensure you have nvim-lspconfig installed |
| 39 | +-- Using lazy.nvim: |
| 40 | +-- { 'neovim/nvim-lspconfig' } |
| 41 | + |
| 42 | +-- Configure semcode LSP |
| 43 | +local lspconfig = require('lspconfig') |
| 44 | +local configs = require('lspconfig.configs') |
| 45 | + |
| 46 | +-- Define semcode-lsp if it's not already defined |
| 47 | +if not configs.semcode_lsp then |
| 48 | + configs.semcode_lsp = { |
| 49 | + default_config = { |
| 50 | + cmd = { '/path/to/semcode/target/release/semcode-lsp' }, |
| 51 | + filetypes = { 'c', 'cpp', 'cc', 'h', 'hpp' }, |
| 52 | + root_dir = function(fname) |
| 53 | + -- Look for .semcode.db or use git root |
| 54 | + return lspconfig.util.find_git_ancestor(fname) or |
| 55 | + lspconfig.util.root_pattern('.semcode.db')(fname) or |
| 56 | + vim.fn.getcwd() |
| 57 | + end, |
| 58 | + settings = { |
| 59 | + semcode = { |
| 60 | + database_path = nil -- Uses workspace/.semcode.db by default |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | +end |
| 66 | + |
| 67 | +-- Setup the LSP |
| 68 | +lspconfig.semcode_lsp.setup({ |
| 69 | + -- Optional: custom database path |
| 70 | + settings = { |
| 71 | + semcode = { |
| 72 | + database_path = "/custom/path/to/.semcode.db" -- Optional |
| 73 | + } |
| 74 | + } |
| 75 | +}) |
| 76 | + |
| 77 | +-- Optional: Set up keybindings |
| 78 | +vim.api.nvim_create_autocmd('LspAttach', { |
| 79 | + group = vim.api.nvim_create_augroup('UserLspConfig', {}), |
| 80 | + callback = function(ev) |
| 81 | + -- Enable completion triggered by <c-x><c-o> |
| 82 | + vim.bo[ev.buf].omnifunc = 'v:lua.vim.lsp.omnifunc' |
| 83 | + |
| 84 | + local opts = { buffer = ev.buf } |
| 85 | + vim.keymap.set('n', 'gd', vim.lsp.buf.definition, opts) -- Go to definition |
| 86 | + vim.keymap.set('n', 'gr', vim.lsp.buf.references, opts) -- Find references (callers) |
| 87 | + vim.keymap.set('n', 'K', vim.lsp.buf.hover, opts) |
| 88 | + vim.keymap.set('n', 'gi', vim.lsp.buf.implementation, opts) |
| 89 | + vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, opts) |
| 90 | + vim.keymap.set('n', '<space>rn', vim.lsp.buf.rename, opts) |
| 91 | + vim.keymap.set('n', '<space>ca', vim.lsp.buf.code_action, opts) |
| 92 | + end, |
| 93 | +}) |
| 94 | +``` |
| 95 | + |
| 96 | +### Manual Configuration |
| 97 | + |
| 98 | +If you prefer manual configuration without nvim-lspconfig: |
| 99 | + |
| 100 | +```lua |
| 101 | +vim.lsp.start({ |
| 102 | + name = 'semcode-lsp', |
| 103 | + cmd = { '/path/to/semcode/target/release/semcode-lsp' }, |
| 104 | + root_dir = vim.fs.dirname(vim.fs.find({'.semcode.db', '.git'}, { upward = true })[1]), |
| 105 | + settings = { |
| 106 | + semcode = { |
| 107 | + database_path = nil -- Optional custom path |
| 108 | + } |
| 109 | + } |
| 110 | +}) |
| 111 | +``` |
| 112 | + |
| 113 | +## Configuration Options |
| 114 | + |
| 115 | +The LSP server accepts the following configuration through the `semcode` section: |
| 116 | + |
| 117 | +- `database_path` (string, optional): Custom path to the semcode database. If not specified, the server will: |
| 118 | + 1. Look for `.semcode.db` in the workspace directory |
| 119 | + 2. Fall back to `./.semcode.db` in the current directory |
| 120 | + |
| 121 | +## Testing |
| 122 | + |
| 123 | +To test the LSP server manually, you can run it and send JSON-RPC messages: |
| 124 | + |
| 125 | +```bash |
| 126 | +# Build the server |
| 127 | +cargo build --release --bin semcode-lsp |
| 128 | + |
| 129 | +# Run the server (it will read from stdin and write to stdout) |
| 130 | +./target/release/semcode-lsp |
| 131 | +``` |
| 132 | + |
| 133 | +Example initialization message: |
| 134 | +```json |
| 135 | +Content-Length: 246 |
| 136 | + |
| 137 | +{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"processId":null,"rootUri":"file:///path/to/your/workspace","capabilities":{},"initializationOptions":{},"workspaceFolders":null}} |
| 138 | +``` |
| 139 | + |
| 140 | +## How It Works |
| 141 | + |
| 142 | +1. **Database Connection**: The server connects to the semcode database (`.semcode.db`) in your workspace |
| 143 | +2. **Git Awareness**: The server detects your current git commit (HEAD) to ensure it finds the correct version of all symbols |
| 144 | +3. **Go to Definition**: When you request "go to definition" (`gd`) on an identifier, the server: |
| 145 | + - Extracts the identifier name at the cursor position |
| 146 | + - Uses git-aware lookup to query the database at your current commit |
| 147 | + - Checks in priority order: function > macro > type > typedef |
| 148 | + - Returns the file path and line number where it is defined |
| 149 | + - Your editor jumps to the definition location |
| 150 | +4. **Find References**: When you request "find references" (`gr`) on an identifier, the server: |
| 151 | + - Extracts the identifier name at the cursor position |
| 152 | + - Queries the database for all symbols that reference this identifier (callers) |
| 153 | + - Uses git-aware lookup to find references that exist at your current commit |
| 154 | + - Returns a list of all locations where it is referenced |
| 155 | + - Your editor displays the list of references |
| 156 | + |
| 157 | +### Git-Aware Lookups |
| 158 | + |
| 159 | +The LSP server uses **git-aware lookups** to ensure you always jump to the correct version of a symbol: |
| 160 | + |
| 161 | +- On initialization, it determines your current git commit (`HEAD`) |
| 162 | +- When looking up symbols, it finds the version that exists at your current commit |
| 163 | +- If you have indexed multiple versions of your codebase, it intelligently selects the right one |
| 164 | +- Falls back to non-git-aware lookup if not in a git repository |
| 165 | + |
| 166 | +## Supported Languages |
| 167 | + |
| 168 | +- C (`.c`, `.h`) |
| 169 | +- C++ (`.cpp`, `.cc`, `.cxx`, `.hpp`, `.hxx`) |
| 170 | + |
| 171 | +## Troubleshooting |
| 172 | + |
| 173 | +### Database Not Found |
| 174 | +If you see "Semcode database not found" messages: |
| 175 | +1. Ensure you've run `semcode-index` on your codebase |
| 176 | +2. Check that `.semcode.db` exists in your workspace |
| 177 | +3. Verify the database path configuration |
| 178 | + |
| 179 | +### Symbol Not Found |
| 180 | +If "go to definition" doesn't work for a symbol: |
| 181 | +1. Ensure the symbol (function, macro, type, or typedef) was indexed by semcode |
| 182 | +2. Try using the `semcode` CLI tool to verify the symbol exists in the database |
| 183 | +3. Check that you're using the correct identifier name (no typos) |
| 184 | +4. **Git-related issues:** |
| 185 | + - Make sure your working directory is at a git commit that has been indexed |
| 186 | + - If you've checked out a different commit, restart the LSP server to refresh the git SHA |
| 187 | + - Try running `semcode-index` on your current commit if it hasn't been indexed yet |
| 188 | + |
| 189 | +## Limitations |
| 190 | + |
| 191 | +- Supports functions, macros, types, and typedefs (other symbols may be added in the future) |
| 192 | +- Requires a pre-indexed semcode database |
| 193 | +- Does not support real-time indexing of file changes |
| 194 | +- References show symbol-level locations, not exact usage sites within function bodies |
0 commit comments