Giter Site home page Giter Site logo

tomarrell / vim-npr Goto Github PK

View Code? Open in Web Editor NEW
23.0 3.0 2.0 37 KB

Sensible 'gf' for Node Path Relative JS module resolution per project ๐Ÿฟ

License: GNU General Public License v3.0

Vim Script 100.00%
vim goto defintion nodejs configure package

vim-npr's Introduction

Vim-NPR ๐Ÿ”Ž๐Ÿ“‘

A plugin for sensible Node Path Relative module resolution in Javascript on a project-by-project basis. This will allow Vim to resolve modules using gf, even when they're using Node Path Relative, or custom resolution directories in your webpack configuration.

Installation

Supports Vim-Plug, Vundle, and likely any other vim plugin manager that uses a similar format.

Add the following line to your .vimrc file:

Plug 'tomarrell/vim-npr'

Configuration

Simply add a resolve key to your project's package.json file with an array of directories you would like the file to potentially be resolved relative to. The plugin will find this key and resolve your files as per the directories listed.

If a package.json file can be found, however no resolve key is present, the plugin will default to the following directories for resolution:

{
  // ...
  "resolve": ["src", "lib", "test", "public", "node_modules"],
  // ...
}

If these directories don't exist, or the plugin cannot find the file under the cursor, the plugin will simply fail to resolve the file.

By default, the plugin will resolve the package.json by traversing up 5 directories, this number is configurable using the g:vim_npr_max_levels variable.

The plugin will be active whenever you enter a buffer with the extension .js, .jsx, .css or .coffee.

Finally, if the exact file name with extension is not provided in the path, the plugin will attempt a list of defaults. These are appended to the path for each match attempt. The default list is:

let g:vim_npr_file_names = ["", ".js", "/index.js"]

Note that "" (empty string) and ".js" (plain .js extension) are important to resolve exact files and paths simply omitting the extension respectively.

Usage

Works with ES, AMD, and CommonJS module definitions.

An example of the possible module resolution types are below, provided correct configuration. {NPR} represents a configured or default relative directory, which can be specified in your package.json as the resolve array:

import Header from 'Header';            // will resolve {NPR}/Header/index.js
import Header from 'Header/index.js';   // will resolve {NPR}/Header/index.js
import Header from 'Header/style.css';  // will resolve {NPR}/Header/style.css
import Button from 'Header/Button';     // will resolve {NPR}/Header/Button/index.js

import Header from '~/components/Header';          // will resolve {NPR}/components/Header/index.js
import Header from '~/components/Header/index.js'; // will resolve {NPR}/components/Header/index.js

import React from 'react';              // will resolve {NPR}/react/index.js
import { connect } from 'react-redux';  // will resolve {NPR}/react-redux/lib/

It even works throughout your CSS.

@import 'variables.css';  /* will resolve {NPR}/variables.css */
@import 'Home/style.css'; /* will resolve {NPR}/Home/style.css */

It functions the same as the traditional vim gf command, without getting in the way of your other language file resolvers.

License

Licensed under the GNU GPL v3.0 license. Please see the extended license terms here.

vim-npr's People

Contributors

kwikenkwak avatar lucerion avatar pmaoui avatar tomarrell avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

pmaoui kwikenkwak

vim-npr's Issues

Fallback to node vim?

Hi Tom! I wanted to submit this as a PR but my vimscript is abysmal. What I've ended up doing is pasting vim-npr into my node-vim plugin so that when node-vim can't find the file it falls back to vim-npr. It's a pretty hacky solution and I'm sure there's a proper way to do it.

Is it possible to detect if node-vim is installed and fallback to its file resolution if vim-npr can't resolve the file? It looks like node-vim exposes a NodeGotoFile function that could be used.

Also, I think the default g:vim_npr_file_names should be ["", ".js", "/index.js", "/index.jsx"]
It seems logical since the the plugin activates inside jsx files anyway.

Globally configure dirs through vim_npr_default_dirs in .vimrc

Hello,

Thanks for the great plugin.

Is this the correct way to add 'src/screens' to the lookup path? When I do so in package.json it works fine, but most of my projects have 'src/screens' and I'm trying to configure it globally through .vimrc.

let g:vim_npr_default_dirs = ["src", "lib", "test", "public", "node_modules", "src/screens"]

@ prefixed directories not working ??

I just noticed, that @Material for example is not correctly resolved.

Looks like "expand("")" is just dropping the "@" char ... tried some workarounds, but it does not seem to help.

Any hints or suggestions?

Fails to resolve './components/counter'

I have a react app with a /src folder and /src/components folder.
In my /src/index.js I have the following line:
import Counter from './components/counter'
(importing the jsx file /src/components/counter.jsx)
However, vim-npr is failing to resolve the file in the path. I have tried with and without the "resolve" key in my package.json. I am using the latest version.
2019-07-11-094400_956x526_scrot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.