fix(lsp): Fix LSP server attachment and JSON parsing issues

- Replace lspconfig.scl_lsp registration with vim.lsp.start() + FileType autocmd
  for Neovim 0.11+ compatibility
- Fix Lua reserved keyword 'end' in tables (use bracket notation ['end'])
- Fix JSON decoder pattern matching bugs (s:find -> s:sub:match)
- Add LSP method name conversion (textDocument/didOpen -> textDocument_didOpen)
- Handle request vs notification correctly (only respond to requests with id)
- Strip CRLF line endings for Windows compatibility
- Add semantic token handler aliases
- Update documentation with implementation notes
This commit is contained in:
2026-02-18 13:03:14 +01:00
parent 4429280524
commit 96c0c4584f
6 changed files with 201 additions and 91 deletions
+44
View File
@@ -104,6 +104,19 @@ function M.clear_cache()
end
```
## Lua Reserved Keywords
Lua reserved keywords (like `end`, `for`, `in`, etc.) cannot be used as table keys directly. Use bracket notation:
```lua
-- WRONG: causes syntax error
local range = { start = pos1, end = pos2 }
-- CORRECT: use bracket notation
local range = { start = pos1, ["end"] = pos2 }
```
This is especially important for LSP range objects which require an `end` field per the LSP specification.
## Error Handling
### Return Pattern
@@ -166,3 +179,34 @@ Find the last `:=`, `=>`, `=`, `<>`, `>=`, `<=`, `>`, `<`, `AND`, `OR`, `NOT` an
| `:SCLMultilineParams` | Fill multiline parameters for FB/Function call |
| `:LspSCLFormat` | Format current SCL file |
| `:SCLGeneratePlcJson` | Generate plc.data.json from data_types/ |
## LSP Server Implementation Notes
### Method Name Conversion
LSP methods like `textDocument/didOpen` are converted to handler names like `textDocument_didOpen`:
```lua
local method_name = message.method:gsub("/", "_")
local handler = handlers[method_name]
```
### Request vs Notification
- Requests have `id` field and require a response
- Notifications have no `id` and should not receive a response
```lua
if message.id then
-- Only send response for requests
return { id = message.id, result = result }
end
```
### Line Ending Handling
The LSP server strips CRLF (`\r\n`) line endings for Windows compatibility:
```lua
line = line:gsub("\r$", "")
```
### JSON Parser
The standalone JSON parser in `src/json.lua` handles:
- Objects, arrays, strings, numbers, booleans, null
- Escape sequences in strings
- Whitespace skipping