Git Product home page Git Product logo

cmp-dotenv's Introduction

cmp-dotenv

2023-12-26_19-35-38.mp4
Wihtout cmp-dotenv With cmp-dotenv
image image

The plugin you need to have an autocomplete of the environment variables of your system and those of your .env* files

Important

This repository does not require constant updates but if you have any problems or feature suggestions open an issue and I will be happy to check it out.

Setup

require("cmp").setup {
    sources = {
        { name = "dotenv" }
    }
}

Or with configuration

local cmp = require("cmp")

cmp.setup {
    sources = {
        {
          name = "env",
          -- Defaults
          option = {
            path = '.',
            load_shell = true,
            item_kind = cmp.lsp.CompletionItemKind.Variable,
            eval_on_confirm = false,
            show_documentation = true,
            show_content_on_docs = true,
            documentation_kind = 'markdown',
            dotenv_environment = '.*',
            file_priority = function(a, b)
              -- Prioritizing local files
              return a:upper() < b:upper()
            end,
          }
        }
    }
}

Options

name default description
path '.' The path where to look for the files
load_shell true Do you want to load shell variables?
item_kind Variable What kind of suggestion will cmp make?
eval_on_confirm false When you confirm the completion, do you want the variable or the value?
show_documentation true Do you want to see the documentation that has the variable?
show_content_on_docs true Allows to preview the content in the documentation popup with the prefix of Content: .
documentation_kind 'markdown' What did you write the variable documentation on?
dotenv_environment '.*' Which variable environment do you want to load? .* by default loads all of them, this variable accepts regex, some suggestions I can give you are example, local or development.
file_priority function With this function you define the upload priority, by default it prioritizes the local variables, this responds to the callback of a computer for the found files.

Advanced Usage

If you are a developer or just want to do more things in your lua configuration, you can use the api provided here and the (recommended) functions available, are these

local dotenv = require('cmp-dotenv.dotenv')

-- Get the variable you want by name or
-- get the default value in case it does not exist.
dotenv.get_env_variable(name, default)

-- Get all variables that have been loaded
dotenv.get_all_env()

-- You can set a variable to the auto-completion system and
-- find it available throughout the Neovim environment.
-- You can pass it a documentation in the format
-- you have configured or not pass it at all.
dotenv.set_env_variable(name, value, docs or nil)

-- This loads all the environment variables according to the
-- configuration you have from the files or the shell,
-- I do not recommend calling this function as it is
-- usually called by default by cmp.
dotenv.load(force, options)

Contributing

All contributions are welcome! See CONTRIBUTING.md.

License

This template is licensed according to GPL version 2, with the following exception:

The license applies only to the Nix CI infrastructure provided by this template repository, including any modifications made to the infrastructure. Any software that uses or is derived from this template may be licensed under any OSI approved open source license, without being subject to the GPL version 2 license of this template.

cmp-dotenv's People

Contributors

sergioribera avatar

Stargazers

 avatar Shion Shimizu avatar Yuto Tanaka(Work) avatar Minh Tien avatar  avatar Mrinmoy avatar Slee Woo avatar PhenChua avatar  avatar Andis Spriņķis avatar  avatar Maksim Rozhkov avatar Gipo avatar Raffaele Mancuso avatar Kyle King avatar Uwe Sommerlatt avatar ik5 avatar Ramon Rodrigues avatar gabe avatar Fabio S. avatar Gio avatar Nogweii avatar Vera Rei avatar Laurent Seigneurie avatar L I N U X ( リナックス ) avatar Jocelyn Gaudette avatar  avatar Andy Shevchenko avatar Lalit Kumar avatar  avatar  avatar Yusuf Aktepe avatar Yi Ming avatar Alexis Quintero avatar Maddison Hellstrom avatar Scott McKendry avatar Matt avatar Benne avatar Miroslav avatar Emille Henry avatar Michael Benford avatar Yuta Katayama avatar Chaz avatar Phosphorus Moscu avatar Ofir Gal avatar

Watchers

 avatar

cmp-dotenv's Issues

Can't override default source configuration

Hello,
thank you for your very useful plugin. I've been waiting for something like this for a long time.
However, I can't find a way to customize the default configuration. Specifically, trying to disable loading of shell variables doesn't work.

I am using Lazy.nvim, and Nvim v0.9.5. Here's my config

{
		"hrsh7th/nvim-cmp",
		dependencies = {
			{ "L3MON4D3/LuaSnip" },
			{ "saadparwaiz1/cmp_luasnip" },
			{ "SergioRibera/cmp-dotenv" },
		},
		config = function()
			-- SNIPPETS
			require("luasnip").filetype_extend("telekasten", { "markdown" })
			require("luasnip").filetype_extend("fountain", { "markdown" })
			require("luasnip.loaders.from_vscode").lazy_load()
			require("luasnip.loaders.from_lua").lazy_load({ paths = "~/.config/nvim/lua/confs/luasnip/" })

			local cmp = require("cmp")
			local cmp_action = require("lsp-zero").cmp_action()
			local cmp_format = require("lsp-zero").cmp_format()
			local luasnip = require("luasnip")
			cmp.setup({
				sources = {
					{ name = "nvim_lsp" },
					{ name = "luasnip" },
					{
						name = "dotenv",
						option = {
							path = ".",
							load_shell = false,
							item_kind = cmp.lsp.CompletionItemKind.Variable,
							eval_on_confirm = false,
							show_documentation = true,
							show_content_on_docs = true,
							documentation_kind = "markdown",
							dotenv_environment = ".*",
							file_priority = function(a, b)
								-- Prioritizing local files
								return a:upper() < b:upper()
							end,
						},
					},
				},
				mapping = {
					["<CR>"] = cmp.mapping.confirm({ select = true }),
					["<C-l>"] = cmp_action.luasnip_jump_forward(),
					["<C-h>"] = cmp_action.luasnip_jump_backward(),
					["<C-u>"] = cmp.mapping.scroll_docs(-4),
					["<C-d>"] = cmp.mapping.scroll_docs(4),
					["<Tab>"] = cmp_action.luasnip_supertab(),
					["<S-Tab>"] = cmp_action.luasnip_shift_supertab(),
				},
				snippet = {
					expand = function(args)
						luasnip.lsp_expand(args.body)
					end,
				},
				formatting = cmp_format,
			})
		end,
	}

As you can see, setting load_shell to false does nothing. Same thing when I try to set path to a custom location.
Thanks in advance for any help.

Trigger character or manual trigger?

Hey there, i have been searching for a completion source like this.
I work in a codebase that contains 100+ envs, and almost any letter would be a hit. Is there a way to set this so that it only triggers on all snake case captialized chracters? Or is there a way to set a manual trigger?
Thanks!

Error occured when use `cmp-dotenv`

Just use cmp-dotenv with this a default configure will cause error:

Here is the configure part:

require("cmp").setup {
    sources = {
        { name = "dotenv" }
    }
}

And the error message:

Failed to source `/home/winterreise/.local/share/nvim/lazy/cmp-dotenv/after/plugin/cmp-dotenv.lua`

vim/_editor.lua:0: InsertEnter Autocommands for "*"..script nvim_exec2() called at InsertEnter Autocommands for "*":0../home/winterreise/.local/share/nvim/lazy/cmp-dotenv/after/plugin/cmp-dotenv.lua: Vim(source):E5113: Error while calling lua chunk: ...cal/share/nvim/lazy/cmp-dotenv/lua/cmp-dotenv/option.lua:26: attempt to index a nil value
stack traceback:
	...cal/share/nvim/lazy/cmp-dotenv/lua/cmp-dotenv/option.lua:26: in function 'get'
	...cal/share/nvim/lazy/cmp-dotenv/lua/cmp-dotenv/dotenv.lua:30: in function 'load'
	...l/share/nvim/lazy/cmp-dotenv/after/plugin/cmp-dotenv.lua:3: in main chunk
	[C]: in function 'nvim_exec2'
	vim/_editor.lua: in function 'cmd'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:481: in function <...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:480>
	[C]: in function 'xpcall'
	.../.local/share/nvim/lazy/lazy.nvim/lua/lazy/core/util.lua:113: in function 'try'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:480: in function 'source'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:439: in function 'source_runtime'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:410: in function 'packadd'
	...
	[C]: in function 'xpcall'
	.../.local/share/nvim/lazy/lazy.nvim/lua/lazy/core/util.lua:113: in function 'try'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:337: in function '_load'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:187: in function 'load'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:338: in function <...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:337>
	[C]: in function 'xpcall'
	.../.local/share/nvim/lazy/lazy.nvim/lua/lazy/core/util.lua:113: in function 'try'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:337: in function '_load'
	...local/share/nvim/lazy/lazy.nvim/lua/lazy/core/loader.lua:187: in function 'load'
	...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:83: in function <...hare/nvim/lazy/lazy.nvim/lua/lazy/core/handler/event.lua:72>

# stacktrace:
  - vim/_editor.lua:0 _in_ **cmd**

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.