Skip to content

Add require() helper #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Add require() helper #4

wants to merge 1 commit into from

Conversation

nylen
Copy link

@nylen nylen commented Sep 24, 2013

Node.js require() helper

This code maps the commands :Require and :Unrequire to help with managing require() statements in Node.js code. Commands :R and :UR are also created if they don't already exist.

A brief usage example, starting in a blank node.js file:

Run these vim commands:

:Require async
:Require cp=child_process
:Require ./lib/somelib
:Require mongodb.MongoClient
:Unrequire mongodb
:Require mc=mongodb.MongoClient

The file will go through these states:

    var async = require('async');
    var async = require('async'),
        cp    = require('child_process');
    var async   = require('async'),
        cp      = require('child_process'),
        somelib = require('./lib/somelib');
    var async       = require('async'),
        cp          = require('child_process'),
        MongoClient = require('mongodb'),
        somelib     = require('./lib/somelib');
    var async   = require('async'),
        cp      = require('child_process'),
        somelib = require('./lib/somelib');
    var async   = require('async'),
        cp      = require('child_process'),
        mc      = require('mongodb').MongoClient,
        somelib = require('./lib/somelib');

Improvements

This has been very useful to me so far, but it's my first piece of serious vimscript, so changes and improvements welcome!

This code maps the commands :Require and :Unrequire to help with
managing require() statements in Node.js code.  Commands :R and :UR are
also created if they don't already exist.
@moll
Copy link
Owner

moll commented Sep 26, 2013

Hello, James!

First off, thank you immensely for sharing your code with everyone!

It looks like a useful refactoring tool for sure! I haven't, however, entirely decided which way Node.vim itself should go — what should it have in the core and what integration work should it do given other plugins. Refactoring tools are pretty large area themselves and they're often pretty subjective.

I think we both wouldn't want things to turn into too bloated kitchen sinks. What would you say if we'd set Node.vim to also be an API into which you could call for common functionality? Functionality such as having b:node_root available up to resolving module paths and so on. This way you could build and publish this require refactoring helper independently right away, yet still build upon what Node.vim has to offer.

Let me know what you think of that and what API functions would you need.

Two small things I spotted scrolling down the code:

  • Line 110 if module =~# '\v^\.' — Doesn't take absolute paths into account.
  • Line 112 let sortkey = split(module, '/')[-1] — You could use fnamemodify(module, ":t") to do that path splitting for you.

Oh, and one other thing regardless of how we proceed:
Tests! Writing software is hard in itself. Doing that in VimL is like a masochist challenge. Having automated tests gives you at least some confidence that what you wrote today will keep on working tomorrow, especially in a non-stable and a changing system like Vim.

Also, because we're writing software used by other people, any regression or recurring bug shows disrespect and sloppiness. I'll write a post about testing VimL and plugins in the near future, but till then you could check out Node.vim's tests as an example. As a general practice, writing your tests first helps you make sure every branch of your code is covered.

@nylen
Copy link
Author

nylen commented Sep 30, 2013

Andri, thank you for your tactful and well-written feedback. I agree on all points raised.

It looks like the only things I am using from your code are the list of core modules and b:node_root. The node#lib#deps function I added may also be generally useful.

It doesn't seem like there is a standard way of managing dependencies among vim plugins though - would we just tell users to install Node.vim, and pick from the list of available plugins?

I have very little experience with writing tests. I'll try to figure it out when I have time.

I write VBA for my day job, so I'm up for the occasional masochistic challenge, and Vimscript is refreshing by comparison.

@nylen nylen closed this Sep 30, 2013
@nylen nylen reopened this Sep 30, 2013
@moll
Copy link
Owner

moll commented Oct 8, 2013

So, let's do it. b:node_root will remain at your disposal. I'll then make the list of core modules public, too.

I'm not sure about those other Vim plugin managers — maybe one of them does support dependencies. But I guess just saying so for starters would do the trick. I can add a link to your plugin in the README, too.

@nylen
Copy link
Author

nylen commented Nov 8, 2013

I'm closing this pull request - I've submitted another one (#5) that has the changes I need to publish my plugin.

I looked at the Node.vim tests and could not figure out how to make them work, due to my lack of Ruby experience. Let me know when you write that post and I will give it another shot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants