Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
<!-- Add all new changes here. They will be moved under a version at release -->
* `FIX` Respect `.gitignore` exceptions when scanning workspace files, including match-all patterns and directory exceptions, and match literal commas and braces in standalone patterns. [#3503](https://github.com/LuaLS/lua-language-server/issues/3503)

## 3.19.2
`2026-10-13`
Expand Down
66 changes: 40 additions & 26 deletions script/glob/gitignore.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ local function expect(p, err)
return p + m.T(err)
end

local parser = m.P {
local grammar = {
'Main',
['Sp'] = m.S(' \t')^0,
['Slash'] = m.S('/')^1,
Expand All @@ -43,6 +43,13 @@ local parser = m.P {
['RangeUnit'] = m.Ct(- m.P']' * m.C(m.P(1)) * (m.P'-' * - m.P']' * m.C(m.P(1)))^-1),
}

local groupedParser = m.P(grammar)
-- A standalone pattern can contain literal commas and braces. Only the
-- explicit brace-group syntax uses them as delimiters between patterns.
grammar.Main = m.Ct(m.V'Pattern') + m.T'Main Failed'
grammar.SimpleChar = m.P(1) - m.S'[]*?/'
local parser = m.P(grammar)

---@class gitignore
---@field pattern string[]
---@field options table
Expand All @@ -62,7 +69,8 @@ function mt:addPattern(pat)
if self.options.ignoreCase then
pat = pat:lower()
end
local states, err = parser:match(pat)
local selectedParser = pat:match('^[ \t]*{') and groupedParser or parser
local states, err = selectedParser:match(pat)
if not states then
self.errors[#self.errors+1] = {
pattern = pat,
Expand All @@ -71,7 +79,7 @@ function mt:addPattern(pat)
return
end
for _, state in ipairs(states) do
self.matcher[#self.matcher+1] = matcher(state)
self.matcher[#self.matcher+1] = matcher(state, { fullMatch = true })
end
end

Expand Down Expand Up @@ -100,28 +108,27 @@ function mt:hasInterface(name)
return self.interface[name] ~= nil
end

function mt:checkDirectory(catch, path, matcher0)
function mt:checkDirectory(path, matcher0)
if not self:hasInterface 'type' then
return true
end
if not matcher0:isNeedDirectory() then
return true
end
if #catch < #path then
-- if path is 'a/b/c' and catch is 'a/b'
-- then the catch must be a directory
return true
else
return self:callInterface('type', path) == 'directory'
end
return self:callInterface('type', path) == 'directory'
end

function mt:simpleMatch(path)
path = self:getRelativePath(path)
function mt:matchPath(relative, path)
-- Patterns describe entries within the root, not the root itself.
if relative == '' then
return nil
end
if self.options.ignoreCase then
relative = relative:lower()
end
for i = #self.matcher, 1, -1 do
local matcher0 = self.matcher[i]
local catch = matcher0(path)
if catch and self:checkDirectory(catch, path, matcher0) then
if matcher0(relative) and self:checkDirectory(path, matcher0) then
if matcher0:isNegative() then
return false
else
Expand All @@ -132,34 +139,41 @@ function mt:simpleMatch(path)
return nil
end

function mt:simpleMatch(path)
return self:matchPath(self:getRelativePath(path), path)
end

function mt:finishMatch(path)
local paths = {}
for filename in path:gmatch '[^/\\]+' do
paths[#paths+1] = filename
end
for i = 1, #paths do
local newPath = table.concat(paths, '/', 1, i)
local passed = self:simpleMatch(newPath)
local root = self.options.root
local fullPath = root and root:gsub('[/\\]+$', '') .. '/' .. newPath or newPath
local passed = self:matchPath(newPath, fullPath)
if passed == true then
return true
elseif passed == false then
return false
end
end
return false
end

function mt:getRelativePath(path)
local root = self.options.root or ''
path = path:gsub('^[/\\]+', ''):gsub('[/\\]+', '/'):gsub('/$', '')
root = root:gsub('^[/\\]+', ''):gsub('[/\\]+', '/'):gsub('/$', '')
local comparedPath, comparedRoot = path, root
if self.options.ignoreCase then
path = path:lower()
root = root:lower()
comparedPath = path:lower()
comparedRoot = root:lower()
end
if comparedPath == comparedRoot then
return ''
end
path = path:gsub('^[/\\]+', ''):gsub('[/\\]+', '/')
root = root:gsub('^[/\\]+', ''):gsub('[/\\]+', '/')
if path:sub(1, #root) == root then
path = path:sub(#root + 1)
path = path:gsub('^[/\\]+', '')
if comparedPath:sub(1, #root + 1) == comparedRoot .. '/' then
return path:sub(#root + 2)
end
return path
end
Expand Down Expand Up @@ -193,7 +207,7 @@ function mt:scan(path, callback, hook)
end
end
end
if not self:simpleMatch(path) then
if not self(path) then
check(path)
end
while #list > 0 do
Expand Down
76 changes: 43 additions & 33 deletions script/glob/matcher.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local Symbol = m.S',{}[]*?/\\'
local Char = 1 - Symbol
local Path = (1 - m.S[[\/*?"<>|]])^1 * Slash
local NoWord = #(m.P(-1) + Symbol)
local EntryChar = 1 - m.S('/\\')
local EntryPath = EntryChar ^ 1 * Slash
local function whatHappened()
return m.Cmt(m.P(1)^1, function (...)
print(...)
Expand All @@ -15,85 +17,89 @@ local mt = {}
mt.__index = mt
mt.__name = 'matcher'

function mt:exp(state, index)
function mt:exp(state, index, ending)
local exp = state[index]
if not exp then
return
end
if exp.type == 'word' then
return self:word(exp, state, index + 1)
return self:word(exp, state, index + 1, ending)
elseif exp.type == 'char' then
return self:char(exp, state, index + 1)
return self:char(exp, state, index + 1, ending)
elseif exp.type == '**' then
return self:anyPath(exp, state, index + 1)
return self:anyPath(exp, state, index + 1, ending)
elseif exp.type == '*' then
return self:anyChar(exp, state, index + 1)
return self:anyChar(exp, state, index + 1, ending)
elseif exp.type == '?' then
return self:oneChar(exp, state, index + 1)
return self:oneChar(exp, state, index + 1, ending)
elseif exp.type == '[]' then
return self:range(exp, state, index + 1)
return self:range(exp, state, index + 1, ending)
elseif exp.type == '/' then
return self:slash(exp, state, index + 1)
return self:slash(exp, state, index + 1, ending)
end
end

function mt:word(exp, state, index)
local current = self:exp(exp.value, 1)
local after = self:exp(state, index)
function mt:word(exp, state, index, ending)
local after = self:exp(state, index, ending)
if after then
local current = self:exp(exp.value, 1, ending and #Slash)
return current * Slash * after
else
return current
return self:exp(exp.value, 1, ending)
end
end

function mt:char(exp, state, index)
function mt:char(exp, state, index, ending)
local current = m.P(exp.value)
local after = self:exp(state, index)
local after = self:exp(state, index, ending)
if after then
return current * after * NoWord
else
return current * NoWord
return current * (ending or NoWord)
end
end

function mt:anyPath(_, state, index)
local after = self:exp(state, index)
function mt:anyPath(_, state, index, ending)
local after = self:exp(state, index, ending)
if after then
return m.P {
'Main',
Main = after
+ Path * m.V'Main'
+ (ending and EntryPath or Path) * m.V'Main'
}
elseif ending then
return m.P(1) ^ 0 * ending
else
return Path^0
end
end

function mt:anyChar(_, state, index)
local after = self:exp(state, index)
function mt:anyChar(_, state, index, ending)
local after = self:exp(state, index, ending)
local char = ending and EntryChar or Char
if after then
return m.P {
'Main',
Main = after
+ Char * m.V'Main'
+ char * m.V'Main'
}
else
return Char^0
return char ^ 0 * (ending or m.P(true))
end
end

function mt:oneChar(_, state, index)
local after = self:exp(state, index)
function mt:oneChar(_, state, index, ending)
local after = self:exp(state, index, ending)
local char = ending and EntryChar or Char
if after then
return Char * after
return char * after
else
return Char
return char * (ending or m.P(true))
end
end

function mt:range(exp, state, index)
local after = self:exp(state, index)
function mt:range(exp, state, index, ending)
local after = self:exp(state, index, ending)
local ranges = {}
local selects = {}
for _, range in ipairs(exp.value) do
Expand All @@ -107,12 +113,12 @@ function mt:range(exp, state, index)
if after then
return current * after
else
return current
return current * (ending or m.P(true))
end
end

function mt:slash(_, state, index)
local after = self:exp(state, index)
function mt:slash(_, state, index, ending)
local after = self:exp(state, index, ending)
if after then
return after
else
Expand All @@ -125,15 +131,19 @@ function mt:pattern(state)
if not state[1] then
return nil
end
-- gitignore checks each entry independently before descending into it.
-- Keep the end assertion inside wildcard alternatives so they can try
-- a later occurrence of a pattern rather than committing to a prefix.
local ending = self.options and self.options.fullMatch and m.P(-1) or nil
if state.root then
local after = self:exp(state, 1)
local after = self:exp(state, 1, ending)
if after then
return m.C(after)
else
return nil
end
else
return m.C(self:anyPath(nil, state, 1))
return m.C(self:anyPath(nil, state, 1, ending))
end
end

Expand Down
2 changes: 1 addition & 1 deletion script/workspace/workspace.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ local globInteferFace = {
data[path] = 'file'
return nil
end
data[path] = true
data[path] = 'directory'
local paths = {}
pcall(function ()
for fullpath, status in fs.pairs(fullPath) do
Expand Down
Loading
Loading