Skip to content

Commit bfd14d3

Browse files
authored
Merge pull request #5 from jdrupal-dev/feature/ft-config
Allow configuring file types
2 parents e44d847 + 048ae60 commit bfd14d3

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
Install this plugin as a dependency to `hrsh7th/nvim-cmp`.
1212

13+
Default configuration can be found in `lua/css-vars/default_config.lua`.
14+
1315
### [lazy.nvim](https://github.com/folke/lazy.nvim)
1416
```lua
1517
{
@@ -18,7 +20,13 @@ Install this plugin as a dependency to `hrsh7th/nvim-cmp`.
1820
-- other dependencies...
1921
{
2022
"jdrupal-dev/css-vars.nvim",
21-
opts = {},
23+
opts = {
24+
-- If you use CSS-in-JS, you can add the autocompletion to JS/TS files.
25+
cmp_filetypes = { "javascript", "javascriptreact", "typescript", "typescriptreact" },
26+
-- WARNING: The search is not optimized to look for variables in JS files.
27+
-- If you change the search_extensions you might get false positives and weird completion results.
28+
search_extensions = { ".js", ".ts", ".jsx", ".tsx" }
29+
},
2230
},
2331
},
2432
config = function()

lua/css-vars/default_config.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
return {
2+
-- A list of filetypes to load the cmp source in.
3+
cmp_filetypes = { "css", "scss", "less" },
4+
-- A list of file extensions to search for CSS variables in.
5+
search_extensions = { ".css", ".scss", ".less" },
6+
}

lua/css-vars/init.lua

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,32 @@ local Job = require("plenary.job")
33
local registered = false
44

55
local M = {}
6-
M.setup = function()
6+
M.setup = function(cfg)
7+
M.__conf = vim.tbl_deep_extend("keep", cfg or {}, require("css-vars.default_config"))
78
if registered then
89
return
910
end
1011
registered = true
1112

13+
local args = {
14+
"-e",
15+
"[^\\w](--[^:)]*):",
16+
"-r",
17+
"'$1'",
18+
"-o",
19+
"--no-filename",
20+
}
21+
22+
-- Only search in files that are listed in the "search_extensions" config.
23+
for _, extension in pairs(M.__conf.search_extensions) do
24+
table.insert(args, "-g")
25+
table.insert(args, "*" .. extension)
26+
end
27+
table.insert(args, vim.loop.cwd())
28+
1229
Job:new({
1330
command = "rg",
14-
args = {
15-
"-e",
16-
"[^\\w](--[^:)]*):",
17-
"-r",
18-
"'$1'",
19-
"-o",
20-
"--no-filename",
21-
"-g",
22-
"*.css",
23-
"-g",
24-
"*.less",
25-
"-g",
26-
"*.scss",
27-
vim.loop.cwd(),
28-
},
31+
args = args,
2932
env = { PATH = vim.env.PATH },
3033
on_exit = function(j)
3134
local result = j:result()
@@ -46,11 +49,7 @@ M.setup = function()
4649
end
4750

4851
source.complete = function(_, request, callback)
49-
if
50-
request.context.filetype ~= "css"
51-
and request.context.filetype ~= "less"
52-
and request.context.filetype ~= "scss"
53-
then
52+
if not vim.tbl_contains(M.__conf.cmp_filetypes, request.context.filetype) then
5453
callback({ isIncomplete = true })
5554
return
5655
end

0 commit comments

Comments
 (0)