Skip to content

Commit d823299

Browse files
authored
Bunch of changes/fixes after finally upgrading (#40)
* * Updated teal type definition file based on current state of teal master * Fixed to report all diagnostics correctly. For whatever reason this was fixed by passing in filename to tl.parse_program * Refactors logging to be simpler. No longer supports named fields. The string placeholders should always be {} or {formatting} where formatting is a string.format compatible formatting string or a few special cases like @ for serialize * Added a bunch more logging as necessary while I was debugging. Should all be disabled unless --verbose is provided * Fixed tree sitter issue. I was finding that 'hover' was incorrectly providing data for the previous column, so I changed "Document:_tree_sitter_token" to interpret the 'end_point' value tree sitter provides as exclusive. I was unable to confirm that this is expected from tree sitter docs but this fixes my issue * Fixed bug with completion request, where it would fail if called from the end of the line in neovim. I assume this was because neovim sends the cursor position, which is technically outside the range of the current line, so I subtraced character by one to account for this in MiscHandlers:_on_completion. * Fixed bug where some methods wouldn't be returned by completion when arg type == tl.typcodes.RECORD * Fixed to include lsp_formatter * Fix to rockspec to include tracing_util * Fixed tests to pass again * More test fixes * Fix signature help event which was broken due to new changes
1 parent f1f9117 commit d823299

33 files changed

+742
-514
lines changed

gen/teal_language_server.sh

-3
This file was deleted.

gen/teal_language_server/args_parser.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
local asserts = require("teal_language_server.asserts")
33

4-
local args_parser = {CommandLineArgs = {}, }
4+
local args_parser = { CommandLineArgs = {} }
55

66

77

gen/teal_language_server/document.lua

+60-59
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,7 @@ local tl = require("tl")
2323

2424

2525

26-
27-
28-
29-
30-
31-
32-
33-
34-
35-
36-
37-
38-
39-
40-
local Document = {NodeInfo = {}, }
26+
local Document = { NodeInfo = {} }
4127

4228

4329

@@ -126,29 +112,30 @@ function Document:_get_tokens()
126112
return cache.tokens, cache.err_tokens
127113
end
128114

129-
local parse_prog = tl.parse_program
130115
function Document:_get_ast(tokens)
131116
local cache = self._cache
132117
if not cache.ast then
133118
local _
134119
cache.parse_errors = {}
135-
cache.ast, _ = parse_prog(tokens, cache.parse_errors)
136-
tracing.debug(_module_name, "parse_prog errors: " .. #cache.parse_errors)
120+
cache.ast, _ = tl.parse_program(tokens, cache.parse_errors, self._uri.path)
121+
tracing.debug(_module_name, "parse_prog errors: {}", { #cache.parse_errors })
137122
end
138123
return cache.ast, cache.parse_errors
139124
end
140125

141-
local type_check = tl.type_check
142-
143126
function Document:_get_result(ast)
144127
local cache = self._cache
145128
if not cache.result then
146-
tracing.info(_module_name, "Type checking document {}", { self._uri.path })
147-
cache.result = type_check(ast, {
148-
lax = is_lua(self._uri.path),
149-
filename = self._uri.path,
150-
env = self._server_state:get_env(),
151-
})
129+
local lax = is_lua(self._uri.path)
130+
tracing.info(_module_name, "Type checking document{} {}", { lax and " (lax)" or "", self._uri.path })
131+
132+
local opts = {
133+
feat_lax = lax and "on" or "off",
134+
feat_arity = "on",
135+
}
136+
137+
cache.result = tl.check(
138+
ast, self._uri.path, opts, self._server_state:get_env())
152139
end
153140
return cache.result
154141
end
@@ -188,7 +175,7 @@ end
188175

189176
function Document:clear_cache()
190177
self._cache = {}
191-
tracing.debug(_module_name, "Cleared cache for document {}", { self._uri })
178+
tracing.debug(_module_name, "Cleared cache for document {@}", { self._uri })
192179
end
193180

194181
function Document:update_text(text, version)
@@ -209,10 +196,9 @@ function Document:update_text(text, version)
209196
self._tree_cursor = self._tree:root():create_cursor()
210197
end
211198

212-
local get_raw_token_at = tl.get_token_at
213199
local function make_diagnostic_from_error(tks, err, severity)
214200
local x, y = err.x, err.y
215-
local err_tk = get_raw_token_at(tks, y, x)
201+
local err_tk = tl.get_token_at(tks, y, x)
216202
return {
217203
range = {
218204
start = {
@@ -256,6 +242,7 @@ end
256242

257243
function Document:process_and_publish_results()
258244
local tks, err_tks = self:_get_tokens()
245+
tracing.debug(_module_name, "Detected {} lex errors", { #err_tks })
259246
if #err_tks > 0 then
260247
self:_publish_diagnostics(imap(err_tks, function(t)
261248
return {
@@ -271,6 +258,7 @@ function Document:process_and_publish_results()
271258
end
272259

273260
local ast, parse_errs = self:_get_ast(tks)
261+
tracing.debug(_module_name, "Detected {} parse errors", { #parse_errs })
274262
if #parse_errs > 0 then
275263
self:_publish_diagnostics(imap(parse_errs, function(e)
276264
return make_diagnostic_from_error(tks, e, "Error")
@@ -282,6 +270,8 @@ function Document:process_and_publish_results()
282270
local fname = self._uri.path
283271
local result = self:_get_result(ast)
284272

273+
tracing.debug(_module_name, "Detected {} type errors", { #result.type_errors })
274+
285275
local config = self._server_state.config
286276
local disabled_warnings = set(config.disable_warnings or {})
287277
local warning_errors = set(config.warning_error or {})
@@ -313,36 +303,37 @@ function Document:resolve_type_ref(type_number)
313303
end
314304
end
315305

316-
function Document:_quick_get(tr, last_token)
306+
function Document:type_information_for_tokens(tokens, y, x)
307+
local tr = self:get_type_report()
308+
309+
310+
311+
312+
313+
314+
315+
316+
317+
318+
319+
320+
317321

318-
local file = tr.by_pos[self._uri.path]
319-
if file == nil then
320-
tracing.warning(_module_name, "selfchecker: the file dissappeared?")
321-
return nil
322-
end
323322

324-
local line = file[last_token.y] or file[last_token.y - 1] or file[last_token.y + 1]
325-
if line == nil then
326-
tracing.warning(_module_name, "selfchecker: the file dissappeared?")
327-
return nil
328-
end
329323

330-
local type_ref = line[last_token.x] or line[last_token.x - 1] or line[last_token.x + 1]
331-
if type_ref == nil then
332-
tracing.warning(_module_name, "selfchecker: couldn't find the typeref")
333-
return nil
334-
end
335-
return self:resolve_type_ref(type_ref)
336-
end
337324

338-
function Document:type_information_for_tokens(tokens, y, x)
339-
local tr = self:get_type_report()
340325

341326

342-
local type_info
343327

328+
329+
330+
331+
332+
333+
local type_info
344334

345335
local scope_symbols = tl.symbols_in_scope(tr, y + 1, x + 1, self._uri.path)
336+
tracing.trace(_module_name, "Looked up symbols at {}, {} for file {} with result: {@}", { y + 1, x + 1, self._uri.path, scope_symbols })
346337
if #tokens == 0 then
347338
local out = {}
348339
for key, value in pairs(scope_symbols) do out[key] = value end
@@ -352,26 +343,35 @@ function Document:type_information_for_tokens(tokens, y, x)
352343
}
353344
return type_info
354345
end
355-
local type_id = scope_symbols[tokens[1]]
356-
tracing.warning(_module_name, "tokens[1]: " .. tokens[1])
346+
local raw_token = tokens[1]
347+
tracing.trace(_module_name, "Processing token {} (all: {@})", { raw_token, tokens })
348+
local type_id = scope_symbols[raw_token]
349+
if type_id == nil then
350+
tracing.warning(_module_name, "Failed to find type id for token {}", { raw_token })
351+
end
357352
if type_id ~= nil then
353+
tracing.trace(_module_name, "Matched token {} to type id {}", { raw_token, type_id })
358354
type_info = self:resolve_type_ref(type_id)
355+
356+
if type_info == nil then
357+
tracing.warning(_module_name, "Failed to resolve type ref for id {}", {})
358+
end
359359
end
360360

361361

362362
if type_info == nil then
363-
type_info = tr.types[tr.globals[tokens[1]]]
364-
end
363+
type_info = tr.types[tr.globals[raw_token]]
365364

366-
if type_info == nil then
367-
tracing.warning(_module_name, "Unable to find type info in global table as well..")
365+
if type_info == nil then
366+
tracing.warning(_module_name, "Unable to find type info in global table as well..")
367+
end
368368
end
369369

370-
tracing.warning(_module_name, "What is this type_info:" .. tostring(type_info))
370+
tracing.debug(_module_name, "Got type info: {@}", { type_info })
371371

372372
if type_info and #tokens > 1 then
373373
for i = 2, #tokens do
374-
tracing.trace(_module_name, "tokens[i]: " .. tokens[i])
374+
tracing.trace(_module_name, "tokens[i]: {}", { tokens[i] })
375375

376376
if type_info.fields then
377377
type_info = self:resolve_type_ref(type_info.fields[tokens[i]])
@@ -403,6 +403,7 @@ function Document:_tree_sitter_token(y, x)
403403

404404
if moved == false then
405405
self._tree_cursor:goto_parent()
406+
406407
local parent_node = self._tree_cursor:current_node()
407408

408409
local out = {
@@ -486,7 +487,7 @@ function Document:_tree_sitter_token(y, x)
486487
end_point = node:end_point()
487488

488489
if y == start_point.row and y == end_point.row then
489-
if x >= start_point.column and x <= end_point.column then
490+
if x >= start_point.column and x < end_point.column then
490491
return self:_tree_sitter_token(y, x)
491492
end
492493

gen/teal_language_server/lsp.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
local tl = require("tl")
77

8-
local lsp = {Message = {ResponseError = {}, }, Position = {}, Range = {}, Location = {}, Diagnostic = {}, Method = {}, TextDocument = {}, TextDocumentContentChangeEvent = {}, CompletionContext = {}, }
8+
local lsp = { Message = { ResponseError = {} }, Position = {}, Range = {}, Location = {}, Diagnostic = {}, Method = {}, TextDocument = {}, TextDocumentContentChangeEvent = {}, CompletionContext = {} }
99

1010

1111

gen/teal_language_server/lsp_events_manager.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function LspEventsManager:_trigger(method, params, id)
4444
if ok then
4545
tracing.debug(_module_name, "Successfully handled request with method {}", { method })
4646
else
47-
tracing.error(_module_name, "Error in handler for request with method {method}: {error}", { method, err })
47+
tracing.error(_module_name, "Error in handler for request with method {}: {}", { method, err })
4848
end
4949
else
5050
tracing.warning(_module_name, "No handler found for event with method {}", { method })

gen/teal_language_server/lsp_formatter.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 th
22

33
local Document = require("teal_language_server.document")
44

5-
local lsp_formatter = {Documentation = {}, SignatureHelp = {SignatureParameter = {}, Signature = {}, }, }
5+
local lsp_formatter = { Documentation = {}, SignatureHelp = { SignatureParameter = {}, Signature = {} } }
66

77

88

gen/teal_language_server/lsp_reader_writer.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function LspReaderWriter:_parse_header(lines)
4848

4949
asserts.that(key ~= nil and val ~= nil, "invalid header: " .. line)
5050

51-
tracing.trace(_module_name, "Request Header: {key}: {val}", { key, val })
51+
tracing.trace(_module_name, "Request Header: {}: {}", { key, val })
5252

5353
if key == "Content-Length" then
5454
asserts.is_nil(len)
@@ -106,9 +106,9 @@ end
106106
function LspReaderWriter:receive_rpc()
107107
local header_info = self:_decode_header()
108108

109-
tracing.trace(_module_name, "Successfully read LSP rpc header: {header_info}\nWaiting to receive body...", { header_info })
109+
tracing.trace(_module_name, "Successfully read LSP rpc header: {}\nWaiting to receive body...", { header_info })
110110
local body_line = self._stdin_reader:read(header_info.length)
111-
tracing.trace(_module_name, "Received request Body: {body_line}", { body_line })
111+
tracing.trace(_module_name, "Received request Body: {}", { body_line })
112112

113113
local data = json.decode(body_line)
114114

@@ -126,7 +126,7 @@ function LspReaderWriter:_encode(t)
126126
local content = "Content-Length: " .. tostring(#msg) .. "\r\n\r\n" .. msg
127127
assert(self._stdout:write(content))
128128

129-
tracing.trace(_module_name, "Sending data: {content}", { content })
129+
tracing.trace(_module_name, "Sending data: {}", { content })
130130
end
131131

132132
function LspReaderWriter:send_rpc(id, t)

gen/teal_language_server/main.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ local function main()
6565

6666
cached_entries = nil
6767

68-
tracing.info(_module_name, "Started new instance teal-language-server. Lua Version: {version}. Platform: {platform}", { _VERSION, util.get_platform() })
68+
tracing.info(_module_name, "Started new instance teal-language-server. Lua Version: {}. Platform: {}", { _VERSION, util.get_platform() })
6969
tracing.info(_module_name, "Received command line args: {}", { args })
70-
tracing.info(_module_name, "CWD = {cwd}", { uv.cwd() })
70+
tracing.info(_module_name, "CWD = {}", { uv.cwd() })
7171

7272
local disposables
7373

@@ -118,7 +118,7 @@ local function main()
118118
generate_debug_names = true,
119119
on_completed = function(err)
120120
if err ~= nil then
121-
tracing.error(_module_name, "Received on_completed request with error:\n{error}", { err })
121+
tracing.error(_module_name, "Received on_completed request with error:\n{}", { err })
122122
else
123123
tracing.info(_module_name, "Received on_completed request")
124124
end
@@ -145,7 +145,7 @@ local function main()
145145
uv.walk(function(handle)
146146
if not handle:is_closing() then
147147
local handle_type = handle:get_type()
148-
tracing.warning(_module_name, "Found unclosed handle of type '{handle_type}', closing it.", { handle_type })
148+
tracing.warning(_module_name, "Found unclosed handle of type '{}', closing it.", { handle_type })
149149
handle:close()
150150
end
151151
end)
@@ -162,7 +162,7 @@ local function main()
162162
util.try({
163163
action = run_luv,
164164
catch = function(err)
165-
tracing.error(_module_name, "Error: {error}", { err })
165+
tracing.error(_module_name, "Error: {}", { err })
166166
error(err)
167167
end,
168168
})

0 commit comments

Comments
 (0)