Skip to content

Commit e4dba10

Browse files
committed
Added convenience functions for config file checking.
1 parent 7d2f21b commit e4dba10

2 files changed

Lines changed: 45 additions & 7 deletions

File tree

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,28 @@ Fields:
3636
- `lua`:
3737
- `cpp`:
3838
- `go`:
39+
- `dart`:
40+
41+
<a id="format.config_file_contains"></a>
42+
## `format.config_file_contains`(*filename*, *text*)
43+
44+
Returns whether or not the given config file exists in the current or a parent directory of
45+
the current buffer's filename, and whether or not it contains the given text.
46+
47+
Parameters:
48+
- *filename*: String config filename.
49+
- *text*: String text to look for.
50+
51+
<a id="format.config_file_exists"></a>
52+
## `format.config_file_exists`(*filename*)
53+
54+
Returns whether or not the given config file exists in the current or a parent directory of
55+
the current buffer's filename.
56+
57+
Also returns the config file's filename.
58+
59+
Parameters:
60+
- *filename*: String config filename.
3961

4062
<a id="format.ignore_file_patterns"></a>
4163
## `format.ignore_file_patterns`

init.lua

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,39 @@
1919
-- @module format
2020
local M = {}
2121

22-
--- Helper function that returns whether or not the given config file exists in the current or
23-
-- a parent directory of the current buffer's filename.
24-
local function has_config_file(filename)
22+
--- Returns whether or not the given config file exists in the current or a parent directory of
23+
-- the current buffer's filename.
24+
-- Also returns the config file's filename.
25+
-- @param filename String config filename.
26+
function M.config_file_exists(filename)
2527
if not buffer.filename then return false end
2628
local dir = buffer.filename:match('^(.+)[/\\]')
2729
while dir do
28-
if lfs.attributes(dir .. '/' .. filename) then return true end
30+
local config_file = dir .. '/' .. filename
31+
if lfs.attributes(config_file) then return true, config_file end
2932
dir = dir:match('^(.+)[/\\]')
3033
end
31-
return false
34+
return false, nil
35+
end
36+
37+
--- Returns whether or not the given config file exists in the current or a parent directory of
38+
-- the current buffer's filename, and whether or not it contains the given text.
39+
-- @param filename String config filename.
40+
-- @param text String text to look for.
41+
function M.config_file_contains(filename, text)
42+
local exists, config_file = M.config_file_exists(filename)
43+
if not exists then return false end
44+
local f<close> = io.open(filename)
45+
return f:read('a'):find(text, 1, true) ~= nil
3246
end
3347

3448
--- Map of lexer languages to string code formatter commands or functions that return such
3549
-- commands.
3650
M.commands = {
37-
lua = function() return has_config_file('.lua-format') and 'lua-format' or nil end,
38-
cpp = function() return has_config_file('.clang-format') and 'clang-format -style=file' or nil end,
51+
lua = function() return M.config_file_exists('.lua-format') and 'lua-format' or nil end,
52+
cpp = function()
53+
return M.config_file_exists('.clang-format') and 'clang-format -style=file' or nil
54+
end, --
3955
go = 'gofmt', dart = 'dart format'
4056
}
4157
M.commands.c = M.commands.cpp

0 commit comments

Comments
 (0)