Skip to content

Commit

Permalink
Separate rendering logic, update virtualtext behaviour (#24)
Browse files Browse the repository at this point in the history
Currently, most of the rendering logic is present in the main file (https://github.com/yardnsm/vim-import-cost/blob/404e9647b31a20940d09c943ba2c69e31d68e50d/autoload/import_cost.vim), both the scratch buffer logic and the virtual text logic. This PR separates these into their on files.

### Also in this PR

The main change was the refactoring, but more nice stuff were updated.

* Updated virtual text method to be more stable.
* Added an `:ImportCostClear` command to clean the virtual text in a given range.
* Dropped the "auto" mode for the virtual text. The reason behind this is due to make this plugin non-interruptible and making it available only on demand. Users who still want this behaviour can take a snippet from the README.
* Update the documentation a bit.
* Added a custom highlight group for the virtual text called `ImportCostVirtualText`, whose default value if `NonText` (which is more appropriate than `LineNr` previously). The `g:import_cost_virtualtext_hl_group` option was removed.
* Change default virtualtext prefix to ` > ` .

This PR also updates the script to emit events on the fly, meaning you don't have to wait for the script to finish in order to view the imports, similarly to the VS Code plugin!

When running the plugin, This will only affect when the virtual text feature is on. See it for yourself!

![ezgif-4-30d9b6b2a33b](https://user-images.githubusercontent.com/11786506/51088864-cb3a9900-176d-11e9-8f13-9deaaadb8fb5.gif)
  • Loading branch information
yardnsm authored Jan 24, 2019
1 parent 404e964 commit e98ba9d
Show file tree
Hide file tree
Showing 11 changed files with 490 additions and 331 deletions.
Binary file added .github/assets/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed .github/assets/screenshot.png
Binary file not shown.
39 changes: 31 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

[![Build Status](https://travis-ci.org/yardnsm/vim-import-cost.svg?branch=master)](https://travis-ci.org/yardnsm/vim-import-cost)

![](.github/assets/screenshot.png)
![](.github/assets/demo.gif)

This plugin is a (very rough) port of the [import-cost](https://github.com/wix/import-cost) plugin
for Visual Studio Code. In a nutshell, it calculates the bundle size for each `import` or `require`
statement. However, unlike the original plugin, this plugins does not show the calculated size as
inline text. Instead, it exposes commands that allows you to calculate the package's size on demand.
for Visual Studio Code. In a nutshell, it calculates the bundle size for each `import` statement or
`require` calls. Thanks to Neovim's virtual text feature, this plugin can show the calculated size
next to the `import` statements, with a fallback to showing the calculated size in a
scratch buffer.

This plugin exposes commands that allows you to calculate the package's size on demand. By default
it'll try to use Neovim's virtual text feature.

This plugin uses the
[`import-cost`](https://github.com/wix/import-cost/tree/master/packages/import-cost) module (also
part of the origin import-cost plugin) that uses `webpack` to calculate the size of the imported
part of the original import-cost plugin) that uses `webpack` to calculate the size of the imported
module.

You can find more information about how the size calculation process works in [this blog
Expand All @@ -33,10 +37,12 @@ the plugin directory, so the required dependencies will be installed.

## Usage

Use the `:ImportCost` command when inside a JavaScript / TypeScript buffer. This commands also
support ranges, so you can initiate it from VISUAL mode.
You can initiate the plugin by demand; it does not offer any automatic calculation out of the box.

Use the `:ImportCostSingle` command if you wish to process the current line only.
- Use the `:ImportCost` command when inside a JavaScript / TypeScript buffer. This commands also
support ranges, so you can initiate it from VISUAL mode.
- Use the `:ImportCostSingle` command if you wish to process the current line only.
- Use the `:ImportCostClear` command to clear the virtual text set by the plugin.

## Configuration

Expand All @@ -46,6 +52,23 @@ Find the available configuration methods in the plugin's [help file](./doc/impor
:help import_cost
```

## FAQ

### How can I automatically run this plugin on buffer updates?

You can all the `:ImportCost` command within an `autocmd`, so it can be triggered when the buffer is
updated. **This is not recommended to use when the virtual text feature is not supported.**

```vim
" Put this in your .vimrc
augroup import_cost_auto_run
autocmd!
autocmd InsertLeave *.js,*.jsx,*.ts,*.tsx ImportCost
autocmd BufEnter *.js,*.jsx,*.ts,*.tsx ImportCost
autocmd CursorHold *.js,*.jsx,*.ts,*.tsx ImportCost
augroup END
```

---

## License
Expand Down
Loading

0 comments on commit e98ba9d

Please sign in to comment.