Skip to content

Feature Request: Multi-line Environment Variable Support #5

Description

@ph1losof

Problem Description

Currently, cmp-dotenv does not support multi-line environment variables, which severely limits its usefulness for modern applications that require complex configurations like SSL certificates, JSON configurations, or lengthy connection strings spanning multiple lines.

Examining the current parsing logic in load.lua:

function M.load_data_from_text(content)
  local data_loaded = {}
  local lines_arr = get_lines(content)
  if next(lines_arr) ~= nil then
    local docs = nil
    for v in pairs(lines_arr) do
      local line = lines_arr[v]
      if not (line == nil or line == '') and string.sub(line, 1, 1) ~= '#' then
        local raw_values = split_str(line, '=')  -- Single line processing only
        local value = raw_values[2] and vim.trim(raw_values[2]) or ''
        data_loaded[raw_values[1]] = { value = value, docs = docs and vim.trim(docs) or nil }
        docs = nil
      else
        docs = (docs or '') .. vim.trim(string.sub(line, 2)) .. '\n'
      end
    end
  end
  return data_loaded
end

The parser processes each line independently using simple string splitting, making it impossible to handle values that span multiple lines or use complex quoting mechanisms.

Current Architecture Limitations

Core Parsing Issues

  1. Line-by-line processing: Uses split_str(line, '=') which only handles single-line values
  2. No state management: No tracking of parsing context between lines
  3. Basic string splitting: Simple split_str() function cannot handle:
    • Quoted values spanning multiple lines
    • Backslash continuation (\)
    • Escaped characters within values
    • Nested quotes or complex quoting scenarios
  4. No quote handling: Parser doesn't distinguish between quote types or handle unclosed quotes
  5. Comment interference: Line-by-line comment processing breaks multi-line values containing #

Completion System Limitations

-- Current completion building in utils.lua
function utils.build_completions(instance, opts)
  for key, v in pairs(instance.env_variables) do
    local docs = ''
    if opts.show_content_on_docs then
      docs = 'Content: ' .. v.value  -- Single-line value display only
    end
    -- ... rest of completion logic
  end
end

The completion system assumes all values are single-line strings, making it inadequate for displaying or handling multi-line content.

Use Cases for Multi-line Environment Variables

Multi-line environment variables are essential for:

  • SSL/TLS Certificates: Private keys and certificates spanning multiple lines
  • JSON Configurations: Complex configuration objects
  • API Documentation: Multi-line documentation strings
  • Database Connection Strings: Long URLs with multiple parameters
  • Email Templates: HTML/text templates
  • Shell Scripts: Complex command sequences
  • Base64 Encoded Data: Large payloads split for readability

Example Multi-line Formats That Should Be Supported

Quoted Multi-line Values

# SSL Certificate
SSL_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC7VJTUt9Us8cKB
xZfaimuldo53lCFHk7Tp0hHB0pKZMTn9nxQgWh8KFaGGJrN+8JHZN/TqYG9vOvnh
P4RtYkmp5G7S1A2FZjdYa+rGFZEb6MQJ1XdN5K4k5Y9fWGZdYa+rGFZEb6MQJ1X
-----END PRIVATE KEY-----"

# JSON Configuration
DATABASE_CONFIG="{
  \"host\": \"localhost\",
  \"port\": 5432,
  \"database\": \"myapp\",
  \"ssl\": {
    \"enabled\": true,
    \"cert_path\": \"/etc/ssl/certs/db.pem\"
  }
}"

# API Documentation
API_DOCS="# User Management API

## GET /users
Returns a list of all users in the system.

### Response Format
```json
{
  \"users\": [
    {\"id\": 1, \"name\": \"John Doe\", \"email\": \"john@example.com\"}
  ]
}
```"

Backslash Continuation

# Long database URL
DATABASE_URL=postgresql://user:password@localhost:5432/database?sslmode=require&\
application_name=myapp&\
connect_timeout=10&\
pool_timeout=5&\
max_connections=20

# Complex API endpoint
API_ENDPOINT=https://api.example.com/v1/users/search?include=profile,settings&\
filter=active&\
sort=created_at&\
limit=100

Alternative Solution

For users who need comprehensive multi-line support immediately, ecolog.nvim provides a mature, production-ready solution with:

  • Complete multi-line parsing with all quote types and continuation methods
  • Advanced completion integration with content-aware features
  • Rich integrations system supports all main pickers (snacks.nvim, fzf-lua, telescope), peek window, secrets-manager integrations, cmp integrations (blink.cmp and nvim-cmp), monorepo integration, preview masking for all pickers and many more
  • Native blink.cmp integration
  • Type system detect environment variable types
  • Security features including shelter mode and injection protection
  • Extensive ecosystem including LSP integration, pickers, and syntax highlighting
  • Real-time validation and intelligent caching

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions