From c9f5ab56b88a32b894702e78a263416a5e69a003 Mon Sep 17 00:00:00 2001 From: Steve Vermeulen Date: Thu, 19 Sep 2024 16:32:54 +0800 Subject: [PATCH] Changed 10 files. --- src/teal_language_server/asserts.tl | 6 ++-- src/teal_language_server/class.d.tl | 2 +- src/teal_language_server/document.tl | 31 +++++++------------ src/teal_language_server/env_updater.tl | 16 ++++++++-- .../lsp_events_manager.tl | 2 +- src/teal_language_server/misc_handlers.tl | 9 ++++-- src/teal_language_server/path.tl | 2 +- src/teal_language_server/server_state.tl | 6 ++-- .../teal_project_config.tl | 4 +-- src/teal_language_server/tracing.tl | 12 +++---- 10 files changed, 49 insertions(+), 41 deletions(-) diff --git a/src/teal_language_server/asserts.tl b/src/teal_language_server/asserts.tl index 148f74f..0a0d7a6 100644 --- a/src/teal_language_server/asserts.tl +++ b/src/teal_language_server/asserts.tl @@ -18,13 +18,13 @@ end function asserts.fail(format:string, ...:any) _raise(format, ...) end -function asserts.that(condition:boolean, format:string, ...:any) +function asserts.that(condition:boolean, format?:string, ...:any) if not condition then _raise(format, ...) end end -function asserts.is_nil(value:any, format:string, ...:any) +function asserts.is_nil(value:any, format?:string, ...:any) if value ~= nil then if format == nil then _raise("Expected nil value but instead found '{}'", value) @@ -34,7 +34,7 @@ function asserts.is_nil(value:any, format:string, ...:any) end end -function asserts.is_not_nil(value:any, format:string, ...:any) +function asserts.is_not_nil(value:any, format?:string, ...:any) if value == nil then if format == nil then _raise("Expected non-nil value") diff --git a/src/teal_language_server/class.d.tl b/src/teal_language_server/class.d.tl index d0d0d6e..03def13 100644 --- a/src/teal_language_server/class.d.tl +++ b/src/teal_language_server/class.d.tl @@ -16,7 +16,7 @@ local record SetupOptions end local record Class - setup:function(rec:any, name:string, options:SetupOptions) + setup:function(rec:any, name:string, options?:SetupOptions) get_name:function(any):string try_get_name:function(any):string diff --git a/src/teal_language_server/document.tl b/src/teal_language_server/document.tl index 68e57c5..bb4b475 100644 --- a/src/teal_language_server/document.tl +++ b/src/teal_language_server/document.tl @@ -28,9 +28,6 @@ local record Cache parse_errors: {tl.Error} result: tl.Result - - type_report: tl.TypeReport - type_report_env: tl.TypeReportEnv end local record Document @@ -109,7 +106,7 @@ end function Document:get_tokens(): {Token}, {tl.Error} local cache = self._cache if not cache.tokens then - cache.tokens, cache.err_tokens = tl.lex(self._content) as ({Token}, {tl.Error}) + cache.tokens, cache.err_tokens = tl.lex(self._content, self._uri.path) as ({Token}, {tl.Error}) if not cache.err_tokens then cache.err_tokens = {} end @@ -117,7 +114,7 @@ function Document:get_tokens(): {Token}, {tl.Error} return cache.tokens, cache.err_tokens end -local parse_prog = tl.parse_program as function({Token}, {tl.Error}, string): Node, {string} +local parse_prog = tl.parse_program as function({Token}, {tl.Error}, ?string): Node, {string} function Document:get_ast(): Node, {tl.Error} local tks, err_tks = self:get_tokens() if #err_tks > 0 then @@ -150,15 +147,11 @@ function Document:get_result(): tl.Result, boolean return cache.result, found_errors end -function Document:get_type_report(): tl.TypeReport, tl.TypeReportEnv, boolean - local result, has_errors = self:get_result() - - local cache = self._cache - if not cache.type_report then - cache.type_report, cache.type_report_env = tl.get_types(result) - end +function Document:get_type_report(): tl.TypeReport, tl.TypeReporter, boolean + local _result, has_errors = self:get_result() -- TODO - is this necessary? + local env = self._server_state:get_env() - return cache.type_report, cache.type_report_env, has_errors + return env.reporter:get_report(), env.reporter, has_errors end local function _strip_trailing_colons(text: string): string @@ -239,7 +232,7 @@ local function insert_errs(fname: string, diags: {lsp.Diagnostic}, tks: {Token}, end end -function Document:_publish_diagnostics(diagnostics: {lsp.Diagnostic}, version: number) +function Document:_publish_diagnostics(diagnostics: {lsp.Diagnostic}, version?: number) tracing.debug(_module_name, "Publishing diagnostics for {}...", {self._uri.path}) self._lsp_reader_writer:send_rpc_notification("textDocument/publishDiagnostics", { uri = Uri.tostring(self._uri), @@ -248,7 +241,7 @@ function Document:_publish_diagnostics(diagnostics: {lsp.Diagnostic}, version: n } as lsp.Method.Params) end -local function imap(t: {V}, fn: function(V): (T), start: integer, finish: integer): {T} +local function imap(t: {V}, fn: function(V): (T), start?: integer, finish?: integer): {T} local new: {T} = {} for i = start or 1, finish or #t do new[i] = fn(t[i]) @@ -307,7 +300,7 @@ end function Document:get_type_info_for_symbol(identifier:string, where: lsp.Position): tl.TypeInfo local tr , _ = self:get_type_report() - local symbols = tl.symbols_in_scope(tr, where.line + 1, where.character + 1) + local symbols = tl.symbols_in_scope(tr, where.line + 1, where.character + 1, self._uri.path) local type_id = symbols[identifier] local result:tl.TypeInfo = nil @@ -331,7 +324,7 @@ end function Document:type_information_for_token(token: Token): tl.TypeInfo local tr , _ = self:get_type_report() - local symbols = tl.symbols_in_scope(tr, token.y, token.x) + local symbols = tl.symbols_in_scope(tr, token.y, token.x, self._uri.path) local type_id = symbols[token.tk] local local_type_info = tr.types[type_id] @@ -481,7 +474,7 @@ function Document:type_information_at(where: lsp.Position): tl.TypeInfo return type_info end -local function indent(n: number): string +local function indent(n: integer): string return (" "):rep(n) end local function ti(list: {string}, ...: string) @@ -490,7 +483,7 @@ local function ti(list: {string}, ...: string) end end -function Document:show_type(info: tl.TypeInfo, depth: number): string +function Document:show_type(info: tl.TypeInfo, depth: integer): string if not info then return "???" end depth = depth or 1 if depth > 4 then diff --git a/src/teal_language_server/env_updater.tl b/src/teal_language_server/env_updater.tl index e7ebb05..e211b3b 100644 --- a/src/teal_language_server/env_updater.tl +++ b/src/teal_language_server/env_updater.tl @@ -65,7 +65,7 @@ function EnvUpdater:_init_env_from_config(cfg: TealProjectConfig): tl.Env, strin return "%" .. c end - local function str_esc(s: string, sub: string | function(string): string | {string:string}): string, integer + local function str_esc(s: string, sub?: string | function(string): string | {string:string}): string, integer return s:gsub( "[%^%$%(%)%%%.%[%]%*%+%-%?]", sub as function(string): string @@ -77,8 +77,18 @@ function EnvUpdater:_init_env_from_config(cfg: TealProjectConfig): tl.Env, strin self._substitutions[source_dir] = "^" .. str_esc(mod_name) end - local function init_teal_env(gen_compat: boolean | tl.CompatMode, gen_target: tl.TargetMode, env_def: string): tl.Env, string - return tl.init_env(false, gen_compat, gen_target, {env_def}) + local function init_teal_env(gen_compat: tl.GenCompat, gen_target: tl.GenTarget, env_def: string): tl.Env, string + local opts:tl.EnvOptions = { + defaults = { + gen_compat = gen_compat, + gen_target = gen_target, + }, + predefined_modules = {env_def}, + } + + local env = tl.new_env(opts) + env.report_types = true + return env end cfg = cfg or {} diff --git a/src/teal_language_server/lsp_events_manager.tl b/src/teal_language_server/lsp_events_manager.tl index 181e000..9eff2d6 100644 --- a/src/teal_language_server/lsp_events_manager.tl +++ b/src/teal_language_server/lsp_events_manager.tl @@ -44,7 +44,7 @@ function LspEventsManager:_trigger(method:lsp.Method.Name, params:lsp.Method.Par if ok then tracing.debug(_module_name, "Successfully handled request with method {}", {method}) else - tracing.error(_module_name, "Error in handler for request with method {}: {}", {method, err}) + tracing.error(_module_name, "Error in handler for request with method {method}: {error}", {method, err}) end else tracing.warning(_module_name, "No handler found for event with method {}", {method}) diff --git a/src/teal_language_server/misc_handlers.tl b/src/teal_language_server/misc_handlers.tl index 2fbd60d..70fccbb 100644 --- a/src/teal_language_server/misc_handlers.tl +++ b/src/teal_language_server/misc_handlers.tl @@ -304,9 +304,14 @@ function MiscHandlers:_on_hover(params:lsp.Method.Params, id:integer):nil tracing.trace(_module_name, "Successfully found type_info: {}", {type_info}) - local type_str = doc:show_type(type_info) + -- show_type is too much info + -- this causes everything to be much slower + -- this also seems to be inconsistent with other lsps which just show type name + -- local type_str = doc:show_type(type_info) + local type_str = type_info.str + self._lsp_reader_writer:send_rpc(id, { - contents = { tk.tk .. ":", type_str }, + contents = { type_str }, range = { start = lsp.position(token_pos.line, token_pos.character), ["end"] = lsp.position(token_pos.line, token_pos.character + #tk.tk), diff --git a/src/teal_language_server/path.tl b/src/teal_language_server/path.tl index 4fb005c..e3d61ac 100644 --- a/src/teal_language_server/path.tl +++ b/src/teal_language_server/path.tl @@ -284,7 +284,7 @@ function Path:delete_file() tracing.trace(_module_name, "Deleted file at path '{path}'", {self._value}) end -function Path:create_directory(args:Path.CreateDirectoryArgs) +function Path:create_directory(args?:Path.CreateDirectoryArgs) if args and args.exist_ok and self:exists() then asserts.that(self:is_directory()) return diff --git a/src/teal_language_server/server_state.tl b/src/teal_language_server/server_state.tl index 0fe2830..440e109 100644 --- a/src/teal_language_server/server_state.tl +++ b/src/teal_language_server/server_state.tl @@ -49,7 +49,7 @@ local capabilities = { function ServerState:_validate_config(c:TealProjectConfig) asserts.that(type(c) == "table", "Expected table, got {}", type(c)) - local function sort_in_place(t: {Value}, fn: function(Value, Value): boolean): {Value} + local function sort_in_place(t: {Value}, fn?: function(Value, Value): boolean): {Value} table.sort(t, fn) return t end @@ -78,7 +78,7 @@ function ServerState:_validate_config(c:TealProjectConfig) end end - local function get_types_in_array(val: {any}, typefn: function(any): string): {string} + local function get_types_in_array(val: {any}, typefn?: function(any): string): {string} typefn = typefn or type local set = {} for _, v in ipairs(val) do @@ -98,7 +98,7 @@ function ServerState:_validate_config(c:TealProjectConfig) return "{" .. table.concat(ts, "|") .. "}" end - local function get_map_type(val: any, default_key: string, default_value: string): string + local function get_map_type(val: any, default_key: string, default_value?: string): string if type(val) ~= "table" then return type(val) end diff --git a/src/teal_language_server/teal_project_config.tl b/src/teal_language_server/teal_project_config.tl index e636993..008a4b3 100644 --- a/src/teal_language_server/teal_project_config.tl +++ b/src/teal_language_server/teal_project_config.tl @@ -11,8 +11,8 @@ local record TealProjectConfig module_name: string scripts: {string:{string}} - gen_compat: tl.CompatMode - gen_target: tl.TargetMode + gen_compat: tl.GenCompat + gen_target: tl.GenTarget disable_warnings: {tl.WarningKind} warning_error: {tl.WarningKind} diff --git a/src/teal_language_server/tracing.tl b/src/teal_language_server/tracing.tl index 06971a4..9ba9608 100644 --- a/src/teal_language_server/tracing.tl +++ b/src/teal_language_server/tracing.tl @@ -160,7 +160,7 @@ local function create_entry(module:string, level:TraceEntry.Level, message_templ } end -function tracing.log(module:string, level:TraceEntry.Level, message:string, fields:any) +function tracing.log(module:string, level:TraceEntry.Level, message:string, fields?:any) asserts.is_not_nil(message, "Must provide a non nil value for message") asserts.that(fields == nil or type(fields) == "table", "Invalid value for fields") @@ -173,31 +173,31 @@ function tracing.log(module:string, level:TraceEntry.Level, message:string, fiel end end -function tracing.trace(module:string, message:string, fields:{any:any}) +function tracing.trace(module:string, message:string, fields?:any) if tracing._is_level_enabled(module, _level_trace) then tracing.log(module, "TRACE", message, fields) end end -function tracing.debug(module:string, message:string, fields:{any:any}) +function tracing.debug(module:string, message:string, fields?:any) if tracing._is_level_enabled(module, _level_debug) then tracing.log(module, "DEBUG", message, fields) end end -function tracing.info(module:string, message:string, fields:{any:any}) +function tracing.info(module:string, message:string, fields?:any) if tracing._is_level_enabled(module, _level_info) then tracing.log(module, "INFO", message, fields) end end -function tracing.warning(module:string, message:string, fields:{any:any}) +function tracing.warning(module:string, message:string, fields?:any) if tracing._is_level_enabled(module, _level_warning) then tracing.log(module, "WARNING", message, fields) end end -function tracing.error(module:string, message:string, fields:{any:any}) +function tracing.error(module:string, message:string, fields?:any) if tracing._is_level_enabled(module, _level_error) then tracing.log(module, "ERROR", message, fields) end