Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/artemave/vjs

Missing javascript refactoring tools for nvim
https://github.com/artemave/vjs

javascript vim vim-plugin

Last synced: 4 months ago
JSON representation

Missing javascript refactoring tools for nvim

Awesome Lists containing this project

README

        

# Vjs [![CircleCI](https://circleci.com/gh/artemave/vjs.svg?style=svg)](https://circleci.com/gh/artemave/vjs)

## What is this?

A Neovim plugin that adds a bunch of refactoring tools for Javascript/TypeScript. Namely:

- extract variable
- extract function/method
- extract class/function/variable declaration into a separate file
- declare undefined variable/method/function/class
- list imports for current file
- update imports when moving file (also works in NERDTree)
- autocomplete `require`/`import` paths
- turn a string into a template string once `${}` detected

## Installation

Requires [treesitter](https://github.com/nvim-treesitter/nvim-treesitter).

Use [a plugin manager](https://github.com/junegunn/vim-plug):

```vim script
Plug 'artemave/vjs'
```

## Usage

#### Complete import/require paths

Vjs comes with an `omnifunc` for `require`/`import` path completion:

Set it up:

```viml
autocmd FileType {javascript,typescript} setlocal omnifunc=vjs#ModuleComplete
```

#### `:VjsRenameFile`

Rename/move file and update imports. It updates both imports in current file and all files that import current file.

It's also possible to batch rename files by calling `vjs#imports#RenameFile` directly. For example, assuming there is a bunch of `.mjs` files in quickfix window that I want to rename to `.jsx`, the following command will perform batch rename and update all the imports:

```
:cdo call vjs#imports#RenameFile({ 'new_name': expand('%:r') . '.jsx' })
```

There is an experimental integration with [NERDTree](https://github.com/preservim/nerdtree) project explorer. Renaming/moving javascript/typescript files in NERDTree automatically updates imports ([watch demo](https://www.veed.io/view/823c1c54-fbaf-4c6e-97a4-5a89203a1e07?panel=share)).

#### `:'<,'>VjsExtractVariable`

Extracts selected code into a variable.

#### `:'<,'>VjsExtractFunctionOrMethod`

Extracts selected code into a global function.

#### `:ExtractDeclarationIntoFile`

Extracts enclosing function/class into a separate file. Inserts import into the original file.

#### `:VjsCreateDeclaration`

If a reference under cursor happens to be undefined, this will insert a declaration for it. The appropriate declaration - variable, class or function - is automatically picked.

In a similar manner, if a method/property is not defined for `this` in the current scope, this will insert its declaration.

#### `:VjsListDependents`

Shows list of modules that require/import current file in quickfix window.

#### Template strings

Vjs can automatically convert normal string to template string once the string contains `${}`. To enable this:

```viml
autocmd TextChanged * if &ft =~ 'javascript\|typescript' | call luaeval("require'vjs'.to_template_string()") | endif
autocmd InsertLeave * if &ft =~ 'javascript\|typescript' | call luaeval("require'vjs'.to_template_string()") | endif
```

#### npm workspaces support

Import update on rename, list dependants and `gf` follow package references. E.g, in the following example, pressing `gf` when the cursor is within `'abc'` in `./lib/index.js`, jumps to `./packages/abc/index.js`:

```js
// ./packages/abc/index.js
module.exports = 'abc'

// ./lib/index.js
const moduleA = require('abc')
```

### Example bindings

There are no default bindings. But you can use these:

```vim script
au FileType {javascript,javascript.jsx,typescript} vmap vv :VjsExtractVariable
au FileType {javascript,javascript.jsx,typescript} vmap vf :VjsExtractFunctionOrMethod
au FileType {javascript,javascript.jsx,typescript} nmap ve :VjsExtractDeclarationIntoFile
au FileType {javascript,javascript.jsx,typescript} nmap vd :VjsCreateDeclaration
au FileType {javascript,javascript.jsx,typescript} nmap vr :VjsRenameFile
au FileType {javascript,javascript.jsx,typescript} nmap vl :VjsListDependents
```

If you don't like binding explosion, then perhaps you could add those as code actions via [null-ls](https://github.com/jose-elias-alvarez/null-ls.nvim).

### Configuration

`g:vjs_es_modules_complete` - don't strip out file extension from autocompleted modules and also show `index` modules. Defaults to `0`.

`g:vjs_nerd_tree_overriden` - if truthy, disables nerdtree integration. Defaults to `0`.

## Development

```
git clone https://github.com/artemave/vjs.git
cd vjs
./run_tests
```