Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Oct 16, 2025

問題

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 #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 #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.

Co-authored-by: aiya000 <4897842+aiya000@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix undefined global vim in Neovim ftplugin Neovim: Add vim.lua type definitions to fix LSP "undefined global vim" warnings Oct 17, 2025
Copilot AI requested a review from aiya000 October 17, 2025 00:09
@aiya000 aiya000 marked this pull request as ready for review October 18, 2025 04:43
@aiya000
Copy link
Owner

aiya000 commented Oct 18, 2025

@copilot
いや、例えば~/.config/nvimでinit.luaを編集するときはvim変数を参照できる…というか、~/.config/nvim/lua/**/*も編集できると思うので、できないのはftpluginや、他のスペシャルなケースなので、その対応は違うと思う。
あと報告は日本語でして欲しい。

Co-authored-by: aiya000 <4897842+aiya000@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Oct 18, 2025

@copilot
いや、例えば~/.config/nvimでinit.luaを編集するときはvim変数を参照できる…というか、~/.config/nvim/lua/**/*も編集できると思うので、できないのは...

ご指摘ありがとうございます。対応を修正しました。

以前のアプローチ(types/vim.luaの追加)を削除し、ftplugin、ftdetect、syntax、pluginなどの特殊ディレクトリに個別の.luarc.jsonを配置する方法に変更しました。

これにより、init.luaやlua/**/*は引き続き正常に動作し、特殊ディレクトリ内のLuaファイルでもvim変数が正しく認識されるようになります。

変更内容: コミット 104f25a

Copilot AI changed the title Neovim: Add vim.lua type definitions to fix LSP "undefined global vim" warnings Neovim: Add .luarc.json to special directories to fix LSP "undefined global vim" warnings Oct 18, 2025
@aiya000 aiya000 force-pushed the main branch 3 times, most recently from dd42220 to 17e6373 Compare October 30, 2025 14:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants