dotfiles icon indicating copy to clipboard operation
dotfiles copied to clipboard

Neovim: Add .luarc.json to special directories to fix LSP "undefined global vim" warnings

Open Copilot opened this issue 4 months ago • 2 comments

問題

ftplugin/.lua、ftdetect/.lua、syntax/.lua、plugin/.luaなどの特殊ディレクトリ内のLuaファイルを編集する際、Lua Language Server(LSP)がvimグローバル変数について「undefined global vim」という警告を表示していました:

vim.opt_local.tabstop = 4      -- ⚠️ LSP Warning: "undefined global 'vim'"
vim.opt_local.shiftwidth = 4   -- ⚠️ LSP Warning: "undefined global 'vim'"

local augroup = vim.api.nvim_create_augroup('MyGroup', { clear = true })
                -- ⚠️ LSP Warning: "undefined global 'vim'"

これにより、Neovim Lua設定ファイルの編集が困難になり、LSPが適切な自動補完、型チェック、ドキュメント表示を提供できませんでした。

根本原因

ルートの.config/nvim/.luarc.jsonは正しく設定されており、init.luaやlua/**/*内のファイルでは正常に動作していました。しかし、LSPが特殊ディレクトリ(after/ftplugin、ftdetect、syntax、plugin)内のファイルを編集する際にこのルート設定を認識していませんでした。

解決策

各特殊ディレクトリに個別の.luarc.jsonファイルを追加しました。これにより、これらのディレクトリ内のLuaファイルを編集する際にもLSPが適切に動作します:

追加したファイル:

  • .config/nvim/after/ftplugin/.luarc.json (102個のLuaファイル用)
  • .config/nvim/ftdetect/.luarc.json (44個のLuaファイル用)
  • .config/nvim/syntax/.luarc.json (17個のLuaファイル用)
  • .config/nvim/plugin/.luarc.json (2個のLuaファイル用)
  • .config/nvim/after/syntax/.luarc.json (1個のLuaファイル用)

.luarc.jsonの内容:

{
  "$schema": "https://raw.githubusercontent.com/LuaLS/vscode-lua/master/setting/schema.json",
  "Lua.runtime.version": "LuaJIT",
  "Lua.diagnostics.globals": ["vim"],
  "Lua.workspace.library": ["/home/linuxbrew/.linuxbrew/share/nvim/runtime"],
  "Lua.workspace.checkThirdParty": false,
  "Lua.completion.callSnippet": "Replace"
}

影響範囲

この変更により、特殊ディレクトリ内の166個のLuaファイルでLSPサポートが提供されます:

  • 102個のftpluginファイル
  • 44個のftdetectファイル
  • 17個のsyntaxファイル
  • 2個のpluginファイル
  • 1個のafter/syntaxファイル

init.luaやlua/**/*内のカスタムLuaモジュール(171個)は引き続きルートの.luarc.jsonを使用して正常に動作します。

修正後

vim.opt_local.tabstop = 4      -- ✅ 警告なし - 完全に型付け
vim.opt_local.shiftwidth = 4   -- ✅ 自動補完が動作
vim.opt_local.expandtab = true -- ✅ ホバーでドキュメント表示

local augroup = vim.api.nvim_create_augroup('MyGroup', { clear = true })
                -- ✅ 完全な型安全性とIntelliSense

特殊ディレクトリ内のLuaファイル編集時にも、適切な自動補完、型チェック、ホバードキュメント、リファクタリングサポートが利用できるようになりました。

Fixes aiya000/dotfiles#90

Original prompt

This section details on the original issue you should resolve

<issue_title>Neovim: ftplugin/*.luaなどをeditしていると、lspがundefined global vimと言ってくる</issue_title> <issue_description></issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes aiya000/dotfiles#90


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar Oct 16 '25 23:10 Copilot