LuaLS / LuaLS/lua-language-server
Feature: namespaced type definitions
まだ誰も着手していません。
- 主要言語
- 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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
この issue では、./my-library/db.lua、./my-library/db/migration.lua、./my-library/utils.lua にある Lua ファイルが例として示されていますが、リポジトリ内の実装ファイルやテストについては名前が示されていません。まず、言語サーバーが現在どのように型定義とアノテーションを解決しているかを追跡し、次に namespace 宣言とローカル名および完全修飾名をどのように表現すべきかを判断します。すべての例で、namespace を持つクラス、エイリアス、フィールド、参照が一貫して解決されれば完了です。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- lua
- 領域
- devtools
- issue の種類
- 機能追加
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100