aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Vigouroux <tomvig38@gmail.com>2021-04-02 16:50:28 +0200
committerGitHub <noreply@github.com>2021-04-02 16:50:28 +0200
commit22ef4071f0dedc344ddc48dcc7f463ee4cd84e44 (patch)
tree116e19eee3c547d21ea9be2e5398eb43dd49551a
parentf2df01900e4b981c88e0ab9fa738167a250ae22a (diff)
parent43eb22d4c92a3e61580098ec2a0c23435b8c4b67 (diff)
downloadrneovim-22ef4071f0dedc344ddc48dcc7f463ee4cd84e44.tar.gz
rneovim-22ef4071f0dedc344ddc48dcc7f463ee4cd84e44.tar.bz2
rneovim-22ef4071f0dedc344ddc48dcc7f463ee4cd84e44.zip
Merge pull request #14197 from theHamsta/file-handles-modeline
Fix #14192: Handle IO errors and close files in query.lua
-rw-r--r--runtime/lua/vim/treesitter/query.lua14
1 files changed, 12 insertions, 2 deletions
diff --git a/runtime/lua/vim/treesitter/query.lua b/runtime/lua/vim/treesitter/query.lua
index 188ec94a6a..f40e1d5294 100644
--- a/runtime/lua/vim/treesitter/query.lua
+++ b/runtime/lua/vim/treesitter/query.lua
@@ -22,6 +22,16 @@ local function dedupe_files(files)
return result
end
+local function safe_read(filename, read_quantifier)
+ local file, err = io.open(filename, 'r')
+ if not file then
+ error(err)
+ end
+ local content = file:read(read_quantifier)
+ io.close(file)
+ return content
+end
+
function M.get_query_files(lang, query_name, is_included)
local query_path = string.format('queries/%s/%s.scm', lang, query_name)
local lang_files = dedupe_files(a.nvim_get_runtime_file(query_path, true))
@@ -38,7 +48,7 @@ function M.get_query_files(lang, query_name, is_included)
local MODELINE_FORMAT = "^;+%s*inherits%s*:?%s*([a-z_,()]+)%s*$"
for _, file in ipairs(lang_files) do
- local modeline = io.open(file, 'r'):read('*l')
+ local modeline = safe_read(file, '*l')
if modeline then
local langlist = modeline:match(MODELINE_FORMAT)
@@ -73,7 +83,7 @@ local function read_query_files(filenames)
local contents = {}
for _,filename in ipairs(filenames) do
- table.insert(contents, io.open(filename, 'r'):read('*a'))
+ table.insert(contents, safe_read(filename, '*a'))
end
return table.concat(contents, '')