LuaLS / LuaLS/lua-language-server

Feature: namespaced type definitions

未关闭
#1,167 4 条评论 14 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

enhancement feat/LuaCats Annotations
主要语言
Lua
星标
4.4k
派生
442
PR 合并指标
30 天内没有已合并 PR

描述

This is yet another feature/enhancement idea to make annotations easier to read/write. Perhaps it's come up before, but I couldn't find any issues or discussions about it.

It's wise to namespace type definitions to avoid collisions with type definitions from other code. This can lead to painfully long type names (e.g. my-project.db.entities.Foo). This idea allows defining namespaces for utility and syntactic sugar.

Here is a semi-realistic example and application of this idea:

  • file: ./my-library/db.lua
--- Namespace declaration
---
--- All types declared in this file will be prefixed with the `my-library.db` namespace 
--- identifier when used elsewhere.
---
---@namespace my-library.db

--- Database connection Handle
-- Locally, this can be referenced as `connection`. Files/modules with different namespaces can
-- reference it by it's fully-qualified name, `my-library.db.connection`
-- 
---@class connection

--- Database Connection Options
--
---@class opts
---@field host string
---@field port number
---@field user string
---@field pass string

--- Establish a new DB connection
---@param opts opts
---@return connection? conn
---@return string? error
return function(opts) end
  • file: ./my-library/db/migration.lua

--- namespaces can be re-used
---@namespace my-library.db

--- usable locally as `migration.status`, exports as `my-library.db.migration.status`
---
---@alias migration.status
---| 0 # pending
---| 1 # executing
---| 2 # complete
---| 3 # error

--- exported as `my-library.db.migration.opts`
---
---@class migration.opts
---@field name string
---@field run_in_transaction boolean

---
---@class migration
---@field status migration.status
---@field name string
local migration = {}

--- Execute the migration
---@param db connection
---@return boolean ok
---@return string? error
function migration:exec(db) end

--- Check migration status
---@param db connection
---@return migration.status
function migration:status(db) end

local _M = {}

--- Create a new DB migration
---@param opts migration.opts
function _M.new(opts) end

--- List all migrations
---@param db connection
---@return migration[]
function _M.list(db) end

return _M
  • file: ./my-library/utils.lua
---@namespace my-library

local db = require "my-library.db"
local migrations = require "my-library.db.migrations"

local _M = {}

--- Check to see if any migrations need to be executed
---@param opts db.opts # options for connecting to the database server
function _M.check_migrations(opts)
  -- the displayed type for `conn` is `db.connection`, because the current namespace is `my-library`
  local conn = db(opts)

  for _, m in ipairs(migrations.list(conn)) do
    -- displayed type for `m` is `db.migration`
    -- displayed type for `status` is `db.migration.status`
    local status = m:status(conn)
    print("migration: ", m.name, ", status: ", status)
  end
end

return _M
  • namespaces are just labels/aliases, so they can be shared and re-used between files
  • types are resolved by checking the local namespace before searching the global namespace

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

该 issue 给出了位于 ./my-library/db.lua、./my-library/db/migration.lua 和 ./my-library/utils.lua 的 Lua 示例文件,但没有指出仓库中的实现文件或测试。首先跟踪语言服务器当前如何解析类型定义和注解,然后确定应如何表示 namespace 声明以及局部名称和完全限定名称。完成的标准是:所有示例中的带 namespace 的类、别名、字段和引用都能以一致的方式解析。

由索引模型根据 Issue 内容生成。

评估

技术栈
lua
领域
devtools
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。