Skip to content

valenyala/cmp-forge-remappings

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

cmp-forge-remappings

nvim-cmp source for Solidity imports in Foundry projects.

Features

Import path completion through forge's remappings, segment by segment:

import "▊                              →  @openzeppelin, forge-std, src, lib, …
import "@openzeppelin/▊                →  contracts
import "@openzeppelin/contracts/token/▊ →  ERC20, ERC721, ERC777, ERC1155
import "src/▊                          →  your contracts

Symbol auto-import — type import { and pick any contract, interface or library declared in your project or its dependencies; accepting the entry completes the whole statement with the remapped path:

import {ERC72▊     ⇒  import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
  • Remappings come from forge remappings (so the ones forge infers from lib/ are included), with a remappings.txt fallback when forge isn't on $PATH. src/ and lib/ are offered as root-relative imports too.
  • Everything runs asynchronously and is cached per project root; caches refresh in the background, so a new remapping or contract shows up on the next completion without ever blocking a keystroke.
  • Plays nice with autopairs: if your pairs plugin already inserted the closing }, the completed } from "..."; replaces it instead of duplicating it.
  • Selecting an entry shows the full import statement in cmp's documentation window.

Requirements

  • Neovim ≥ 0.10 (vim.system, vim.fs.root)
  • Foundry's forge on $PATH (optional but recommended; falls back to parsing remappings.txt)
  • ripgrep (optional; falls back to grep for the symbol index)

Installation

lazy.nvim:

{
  "hrsh7th/nvim-cmp",
  dependencies = {
    "valenyala/cmp-forge-remappings",
  },
}

packer.nvim:

use { "valenyala/cmp-forge-remappings", after = "nvim-cmp" }

Then add the source to your cmp setup:

require("cmp").setup({
  sources = {
    { name = "forge_remappings" },
    -- your other sources …
  },
})

Configuration

Options are passed per source, the nvim-cmp way. Defaults shown:

sources = {
  {
    name = "forge_remappings",
    option = {
      -- Project dirs offered as root-relative imports (only when they
      -- exist in the project root).
      project_dirs = { "src/", "lib/" },
      -- Set to false to disable symbol completion inside `import {`.
      symbols = true,
    },
  },
}

The source only activates in solidity buffers that live under a project root containing foundry.toml or remappings.txt, and only on import lines — it stays silent everywhere else.

Notes

  • The symbol index greps for declarations at the start of a line, which is what forge fmt (and every sane formatter) produces. Free-standing top-level struct/enum/error definitions are not indexed.
  • Files under a remapped target are offered under their remapped path (@openzeppelin/...), never the raw lib/... path; anything unmapped falls back to a root-relative path, which foundry also resolves.

License

MIT

About

nvim-cmp source for Solidity imports in Foundry projects — remapping-aware path completion and symbol auto-import

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages