Skip to content

Automate adding import statement without LSP for Typescript/Javascript

License

Notifications You must be signed in to change notification settings

dorage/ts-manual-import.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ts-manual-import.nvim

Add TS/JS import statement manually in luasnip callbacks, other snippets in lua, any modules, keymaps.

Screencast.from.2024-07-14.23-01-16.webm

Installation

Install the plugin with your preferred package manager.

{ "dorage/ts-manual-import.nvim.git" }

Requirements

tree-sitter.nvim

require("nvim-treesitter.configs").setup({
    ensure_installed = {
        "javascript",
        "typescript",
        "tsx",
    },
    sync_install = true,
})

Configuration

It does not need to setup.

Usage

with LuaSnip

s(
	{ name = "React: useState", trig = "rehs" },
	fmt(
		[[
	const [<>, use<>] = useState(<>);
		]],
		{ i(1), i(2), i(3) },
		{ delimiters = "<>" }
	),
	{
		callbacks = require("ts-manual-import").luasnip_callback({
			{
				modules = { "useState" },
				source = "react",
			},
		}),
	}
)

-- **The above code is equivalent to the below code**

s(
	{ name = "React: useState", trig = "rehs" },
	fmt(
		[[
	const [<>, use<>] = useState(<>);
		]],
		{ i(1), i(2), i(3) },
		{ delimiters = "<>" }
	),
	{
		callbacks = {
            [event.eneter]=function()
                require("ts-manual-import").import({
                    {
                        modules = { "useState" },
                        source = "react",
                    }
                })
            end
        },
	}
)

normal usage

-- default usage
require("ts-manual-import").import({
	{ defalut = "React", modules = {}, source = "react" },
	{ modules = { "z" }, source = "zod" },
	{ modules = { "useState" }, source = "react" },
})

How does it work?

  • There has no import statement of the source, Then it would add import statement below the last import statement in the buffer.

  • There has a import statement of the source, Then it would add modules on the import statement.

  • There has a import statement of the source, But module is imported already, then do nothing

  • There has multiple import statements of the source, Then it would add modules on the last import statement.

Todo

  • unit test
  • Support import type ... statement in Typescript

References

It is the first made neovim plugin.

This plugin has been constructed based on nvim-plugin-template.

I referenced many parts in folke's plugins.

About

Automate adding import statement without LSP for Typescript/Javascript

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published