Skip to content

Commit

Permalink
Changed 10 files.
Browse files Browse the repository at this point in the history
  • Loading branch information
svermeulen committed Sep 19, 2024
1 parent 2734fbd commit c9f5ab5
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 41 deletions.
6 changes: 3 additions & 3 deletions src/teal_language_server/asserts.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion src/teal_language_server/class.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 12 additions & 19 deletions src/teal_language_server/document.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -109,15 +106,15 @@ 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
end
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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -248,7 +241,7 @@ function Document:_publish_diagnostics(diagnostics: {lsp.Diagnostic}, version: n
} as lsp.Method.Params)
end

local function imap<V, T>(t: {V}, fn: function(V): (T), start: integer, finish: integer): {T}
local function imap<V, T>(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])
Expand Down Expand Up @@ -307,7 +300,7 @@ end

function Document:get_type_info_for_symbol(identifier:string, where: lsp.Position): tl.TypeInfo
local tr <const>, _ = 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 <const> = symbols[identifier]
local result:tl.TypeInfo = nil

Expand All @@ -331,7 +324,7 @@ end
function Document:type_information_for_token(token: Token): tl.TypeInfo
local tr <const>, _ = self:get_type_report()

local symbols <const> = tl.symbols_in_scope(tr, token.y, token.x)
local symbols <const> = tl.symbols_in_scope(tr, token.y, token.x, self._uri.path)
local type_id <const> = symbols[token.tk]
local local_type_info = tr.types[type_id]

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
16 changes: 13 additions & 3 deletions src/teal_language_server/env_updater.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {}
Expand Down
2 changes: 1 addition & 1 deletion src/teal_language_server/lsp_events_manager.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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})
Expand Down
9 changes: 7 additions & 2 deletions src/teal_language_server/misc_handlers.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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 <const> = 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 <const> = doc:show_type(type_info)
local type_str <const> = 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),
Expand Down
2 changes: 1 addition & 1 deletion src/teal_language_server/path.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/teal_language_server/server_state.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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<Value>(t: {Value}, fn: function(Value, Value): boolean): {Value}
local function sort_in_place<Value>(t: {Value}, fn?: function(Value, Value): boolean): {Value}
table.sort(t, fn)
return t
end
Expand Down Expand Up @@ -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 <const> = {}
for _, v in ipairs(val) do
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/teal_language_server/teal_project_config.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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}

Expand Down
12 changes: 6 additions & 6 deletions src/teal_language_server/tracing.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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
Expand Down

0 comments on commit c9f5ab5

Please sign in to comment.