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:
+91
-70
@@ -13,7 +13,7 @@ end
|
||||
|
||||
local function escape(s)
|
||||
s = s:gsub("\\", "\\\\")
|
||||
s = s:gsub("\"", "\\\"")
|
||||
s = s:gsub('"', '\\"')
|
||||
s = s:gsub("\n", "\\n")
|
||||
s = s:gsub("\r", "\\r")
|
||||
s = s:gsub("\t", "\\t")
|
||||
@@ -29,7 +29,7 @@ function json.encode(data)
|
||||
elseif t == "number" then
|
||||
return tostring(data)
|
||||
elseif t == "string" then
|
||||
return "\"" .. escape(data) .. "\""
|
||||
return '"' .. escape(data) .. '"'
|
||||
elseif t == "table" then
|
||||
local parts = {}
|
||||
if is_array(data) then
|
||||
@@ -39,7 +39,7 @@ function json.encode(data)
|
||||
return "[" .. table.concat(parts, ",") .. "]"
|
||||
else
|
||||
for k, v in pairs(data) do
|
||||
table.insert(parts, "\"" .. escape(k) .. "\":" .. json.encode(v))
|
||||
table.insert(parts, '"' .. escape(k) .. '":' .. json.encode(v))
|
||||
end
|
||||
return "{" .. table.concat(parts, ",") .. "}"
|
||||
end
|
||||
@@ -49,7 +49,7 @@ function json.encode(data)
|
||||
end
|
||||
|
||||
local function skip_whitespace(s, i)
|
||||
while i <= #s and s:find("^%s", i) do
|
||||
while i <= #s and s:sub(i, i):match("%s") do
|
||||
i = i + 1
|
||||
end
|
||||
return i
|
||||
@@ -60,7 +60,7 @@ local function parse_string(s, i)
|
||||
i = i + 1
|
||||
while i <= #s do
|
||||
local c = s:sub(i, i)
|
||||
if c == "\"" then
|
||||
if c == '"' then
|
||||
return table.concat(result), i + 1
|
||||
elseif c == "\\" and i < #s then
|
||||
local next_c = s:sub(i + 1, i + 1)
|
||||
@@ -70,9 +70,9 @@ local function parse_string(s, i)
|
||||
table.insert(result, "\r")
|
||||
elseif next_c == "t" then
|
||||
table.insert(result, "\t")
|
||||
elseif next_c == "\\\"" then
|
||||
table.insert(result, "\"")
|
||||
elseif next_c == "\\\\" then
|
||||
elseif next_c == '"' then
|
||||
table.insert(result, '"')
|
||||
elseif next_c == "\\" then
|
||||
table.insert(result, "\\")
|
||||
else
|
||||
table.insert(result, next_c)
|
||||
@@ -91,28 +91,85 @@ local function parse_number(s, i)
|
||||
if s:sub(i, i) == "-" then
|
||||
i = i + 1
|
||||
end
|
||||
while i <= #s and s:find("^%d", s:sub(i, i)) do
|
||||
while i <= #s and s:sub(i, i):match("%d") do
|
||||
i = i + 1
|
||||
end
|
||||
if s:sub(i, i) == "." then
|
||||
i = i + 1
|
||||
while i <= #s and s:find("^%d", s:sub(i, i)) do
|
||||
while i <= #s and s:sub(i, i):match("%d") do
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
if s:sub(i, i):find("^[eE]") then
|
||||
if s:sub(i, i):match("[eE]") then
|
||||
i = i + 1
|
||||
if s:sub(i, i):find("^[+-]") then
|
||||
if s:sub(i, i):match("[+-]") then
|
||||
i = i + 1
|
||||
end
|
||||
while i <= #s and s:find("^%d", s:sub(i, i)) do
|
||||
while i <= #s and s:sub(i, i):match("%d") do
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return tonumber(s:sub(start, i - 1)), i
|
||||
end
|
||||
|
||||
local function parse_value(s, i)
|
||||
local parse_value
|
||||
|
||||
local function parse_array(s, i)
|
||||
local arr = {}
|
||||
i = i + 1
|
||||
while i <= #s do
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) == "]" then
|
||||
return arr, i + 1
|
||||
end
|
||||
local val
|
||||
val, i = parse_value(s, i)
|
||||
table.insert(arr, val)
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) == "]" then
|
||||
return arr, i + 1
|
||||
end
|
||||
if s:sub(i, i) == "," then
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
return nil, i
|
||||
end
|
||||
|
||||
local function parse_object(s, i)
|
||||
local obj = {}
|
||||
i = i + 1
|
||||
while i <= #s do
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) == "}" then
|
||||
return obj, i + 1
|
||||
end
|
||||
if s:sub(i, i) == '"' then
|
||||
local key
|
||||
key, i = parse_string(s, i)
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) ~= ":" then
|
||||
return nil, i
|
||||
end
|
||||
i = i + 1
|
||||
local val
|
||||
val, i = parse_value(s, i)
|
||||
obj[key] = val
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) == "}" then
|
||||
return obj, i + 1
|
||||
end
|
||||
if s:sub(i, i) == "," then
|
||||
i = i + 1
|
||||
end
|
||||
else
|
||||
return nil, i
|
||||
end
|
||||
end
|
||||
return nil, i
|
||||
end
|
||||
|
||||
parse_value = function(s, i)
|
||||
i = skip_whitespace(s, i)
|
||||
if i > #s then
|
||||
return nil, i
|
||||
@@ -121,66 +178,30 @@ local function parse_value(s, i)
|
||||
local c = s:sub(i, i)
|
||||
|
||||
if c == "{" then
|
||||
local obj = {}
|
||||
i = i + 1
|
||||
while i <= #s do
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) == "}" then
|
||||
return obj, i + 1
|
||||
end
|
||||
if s:sub(i, i) == "\"" then
|
||||
local key
|
||||
key, i = parse_string(s, i)
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) ~= ":" then
|
||||
return nil, i
|
||||
end
|
||||
i = i + 1
|
||||
local val
|
||||
val, i = parse_value(s, i)
|
||||
obj[key] = val
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) == "}" then
|
||||
return obj, i + 1
|
||||
end
|
||||
if s:sub(i, i) == "," then
|
||||
i = i + 1
|
||||
end
|
||||
else
|
||||
return nil, i
|
||||
end
|
||||
end
|
||||
return nil, i
|
||||
return parse_object(s, i)
|
||||
elseif c == "[" then
|
||||
local arr = {}
|
||||
i = i + 1
|
||||
while i <= #s do
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) == "]" then
|
||||
return arr, i + 1
|
||||
end
|
||||
local val
|
||||
val, i = parse_value(s, i)
|
||||
table.insert(arr, val)
|
||||
i = skip_whitespace(s, i)
|
||||
if s:sub(i, i) == "]" then
|
||||
return arr, i + 1
|
||||
end
|
||||
if s:sub(i, i) == "," then
|
||||
i = i + 1
|
||||
end
|
||||
return parse_array(s, i)
|
||||
elseif c == '"' then
|
||||
return parse_string(s, i)
|
||||
elseif c == "t" then
|
||||
if s:sub(i, i + 3) == "true" then
|
||||
return true, i + 4
|
||||
end
|
||||
return nil, i
|
||||
elseif c == "\"" then
|
||||
return parse_string(s, i)
|
||||
elseif s:sub(i, i + 3) == "null" then
|
||||
return nil, i + 4
|
||||
elseif s:sub(i, i + 3) == "true" then
|
||||
return true, i + 4
|
||||
elseif s:sub(i, i + 4) == "false" then
|
||||
return false, i + 5
|
||||
else
|
||||
elseif c == "f" then
|
||||
if s:sub(i, i + 4) == "false" then
|
||||
return false, i + 5
|
||||
end
|
||||
return nil, i
|
||||
elseif c == "n" then
|
||||
if s:sub(i, i + 3) == "null" then
|
||||
return nil, i + 4
|
||||
end
|
||||
return nil, i
|
||||
elseif c == "-" or c:match("%d") then
|
||||
return parse_number(s, i)
|
||||
else
|
||||
return nil, i
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user