Skip to content

Commit 9f6cf53

Browse files
committed
fix #452 fix matching gitignore of root
1 parent 5c39b8f commit 9f6cf53

File tree

3 files changed

+48
-27
lines changed

3 files changed

+48
-27
lines changed

Diff for: changelog.md

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ f( -- view comments of `1` and `2` in completion
4343
* `CHG` completion: improve `then .. end`
4444
* `CHG` improve performance
4545
* `FIX` missed syntax error `function m['x']() end`
46+
* `FIX` [#452](https://github.com/sumneko/lua-language-server/issues/452)
4647

4748
## 1.18.1
4849
`2021-3-10`

Diff for: script/glob/gitignore.lua

+45-27
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function mt:checkDirectory(catch, path, matcher)
116116
end
117117

118118
function mt:simpleMatch(path)
119-
path = path:gsub('^[/\\]+', '')
119+
path = self:getRelativePath(path)
120120
for i = #self.matcher, 1, -1 do
121121
local matcher = self.matcher[i]
122122
local catch = matcher(path)
@@ -148,48 +148,66 @@ function mt:finishMatch(path)
148148
return false
149149
end
150150

151-
function mt:scan(root, callback)
151+
function mt:getRelativePath(path)
152+
local root = self.options.root or ''
153+
if self.options.ignoreCase then
154+
path = path:lower()
155+
root = root:lower()
156+
end
157+
path = path:gsub('^[/\\]+', ''):gsub('[/\\]+', '/')
158+
root = root:gsub('^[/\\]+', ''):gsub('[/\\]+', '/')
159+
if path:sub(1, #root) == root then
160+
path = path:sub(#root + 1)
161+
path = path:gsub('^[/\\]+', '')
162+
end
163+
return path
164+
end
165+
166+
function mt:scan(path, callback)
152167
local files = {}
153168
if type(callback) ~= 'function' then
154169
callback = nil
155170
end
156-
local list = { root }
171+
local list = {}
172+
173+
local function check(current)
174+
local fileType = self:callInterface('type', current)
175+
if fileType == 'file' then
176+
if callback then
177+
callback(current)
178+
end
179+
files[#files+1] = current
180+
elseif fileType == 'directory' then
181+
local result = self:callInterface('list', current)
182+
if type(result) == 'table' then
183+
for _, path in ipairs(result) do
184+
local filename = path:match '([^/\\]+)[/\\]*$'
185+
if filename
186+
and filename ~= '.'
187+
and filename ~= '..' then
188+
list[#list+1] = path
189+
end
190+
end
191+
end
192+
end
193+
end
194+
check(path)
157195
while #list > 0 do
158196
local current = list[#list]
159197
if not current then
160198
break
161199
end
162200
list[#list] = nil
163-
if not self:simpleMatch(current) then
164-
local fileType = self:callInterface('type', current)
165-
if fileType == 'file' then
166-
if callback then
167-
callback(current)
168-
end
169-
files[#files+1] = current
170-
elseif fileType == 'directory' then
171-
local result = self:callInterface('list', current)
172-
if type(result) == 'table' then
173-
for _, path in ipairs(result) do
174-
local filename = path:match '([^/\\]+)[/\\]*$'
175-
if filename
176-
and filename ~= '.'
177-
and filename ~= '..' then
178-
list[#list+1] = path
179-
end
180-
end
181-
end
182-
end
201+
local stem = self:getRelativePath(current)
202+
if not self:simpleMatch(stem) then
203+
check(current)
183204
end
184205
end
185206
return files
186207
end
187208

188209
function mt:__call(path)
189-
if self.options.ignoreCase then
190-
path = path:lower()
191-
end
192-
path = path:gsub('^[/\\]+', '')
210+
path = self:getRelativePath(path)
193211
return self:finishMatch(path)
194212
end
195213

Diff for: script/workspace/workspace.lua

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ function m.getNativeMatcher()
129129
end
130130

131131
m.nativeMatcher = glob.gitignore(pattern, m.matchOption, globInteferFace)
132+
m.nativeMatcher:setOption('root', m.path)
132133

133134
m.nativeVersion = config.version
134135
return m.nativeMatcher
@@ -155,6 +156,7 @@ function m.getLibraryMatchers()
155156
if fs.exists(fs.path(path)) then
156157
local nPath = fs.absolute(fs.path(path)):string()
157158
local matcher = glob.gitignore(true, m.matchOption, globInteferFace)
159+
matcher:setOption('root', path)
158160
if platform.OS == 'Windows' then
159161
matcher:setOption 'ignoreCase'
160162
end

0 commit comments

Comments
 (0)