From ee776fecd97fbfbca7446aa526df8065299a582d Mon Sep 17 00:00:00 2001 From: mitchell <70453897+667e-11@users.noreply.github.com> Date: Thu, 17 Feb 2011 19:27:32 -0500 Subject: Support type-inference through variable assignment. --- modules/lua/adeptsense.lua | 16 ++++++---------- modules/textadept/adeptsense.lua | 32 ++++++++++++++++++++++++++------ 2 files changed, 32 insertions(+), 16 deletions(-) (limited to 'modules') diff --git a/modules/lua/adeptsense.lua b/modules/lua/adeptsense.lua index 0d651bcc..47218e61 100644 --- a/modules/lua/adeptsense.lua +++ b/modules/lua/adeptsense.lua @@ -9,20 +9,16 @@ module('_m.lua.adeptsense', package.seeall) sense = _m.textadept.adeptsense.new('lua') sense.syntax.class_definition = 'module%s*%(?%s*[\'"]([%w_%.]+)' sense.syntax.symbol_chars = '[%w_%.:]' +sense.syntax.type_declarations = {} +sense.syntax.type_assignments = { + ["^'"] = 'string', -- foo = 'bar' + ['^"'] = 'string', -- foo = "bar" + ['^([%w_%.]+)'] = '%1' -- foo = _m.textadept.adeptsense +} sense.api_files = { _HOME..'/modules/lua/api' } sense:add_trigger('.') sense:add_trigger(':', false, true) ---- --- Returns the current module's name (if any) for showing module completions in --- addition to global completions. Otherwise returns nil so only global --- completions are shown. --- @param symbol Must be the empty string (''). -function sense:get_class(symbol) - if symbol ~= '' then return nil end -- no such thing - return self.super.get_class(self, symbol) -- try to get current module -end - -- script/update_doc generates a fake set of ctags used for autocompletion. sense.ctags_kinds = { f = 'functions', diff --git a/modules/textadept/adeptsense.lua b/modules/textadept/adeptsense.lua index 5612725f..865bb376 100644 --- a/modules/textadept/adeptsense.lua +++ b/modules/textadept/adeptsense.lua @@ -46,21 +46,35 @@ function get_class(sense, symbol) local completions = sense.completions local symbol_chars = sense.syntax.symbol_chars local type_declarations = sense.syntax.type_declarations - local class + local type_assignments = sense.syntax.type_assignments + local assignment_patt = symbol..'%s*=%s*([^\r\n]+)' + local class, assignment for i = buffer:line_from_position(buffer.current_pos), 0, -1 do local s, e if symbol == self or symbol == '' then - -- Determine classname from the class declaration. + -- Determine type from the class declaration. s, e, class = buffer:get_line(i):find(class_definition) if class and not completions[class] then class = nil end else - -- Search for a type declaration. + -- Search for a type declaration or type assignment. local line = buffer:get_line(i) if line:find(symbol) then for _, patt in ipairs(type_declarations) do s, e, class = line:find(patt:gsub('%%_', symbol)) if class then break end end + s, e, assignment = line:find(assignment_patt) + if assignment then + for patt, type in pairs(type_assignments) do + local captures = { assignment:match(patt) } + if #captures > 0 then + class = type:gsub('%%(%d+)', function(n) + return captures[tonumber(n)] + end) + end + if class then break end + end + end end end if class then @@ -469,8 +483,13 @@ api_files = {}, -- @field symbol_chars A Lua pattern of characters allowed in a symbol, -- including member operators. Default is '[%w_%.]'. -- @field type_declarations A list of Lua patterns used for determining the --- class of a symbol. The first capture returned must be the class name. Use --- '%_' to match the symbol. Defaults to '(%u[%w_%.]+)%s+%_'. +-- class type of a symbol. The first capture returned must be the class name. +-- Use '%_' to match the symbol. Defaults to '(%u[%w_%.]+)%s+%_'. +-- @field type_assignments A map of Lua patterns to class types for variable +-- assignments. This is typically used for dynamically typed languages. For +-- example, `sense.type_assignments['^"'] = 'string'` would recognize string +-- assignments in Lua so the `foo` in `foo = "bar"` would be recognized as +-- type `string`. The class type value can contain pattern captures. -- @class table -- @name syntax -- @see get_class @@ -480,7 +499,8 @@ syntax = { symbol_chars = '[%w_%.]', type_declarations = { '(%u[%w_%.]+)%s+%_', -- Foo bar - } + }, + type_assignments = {} }, super = setmetatable({}, { __index = _M }) -- cgit v1.2.3