Skip to content

michalliu/jsruntime.vim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

76 Commits
 
 
 
 
 
 

Repository files navigation

jsruntime.vim (Run javascript code directly in Vim)

Latest Update

Supports Win/Linux/Mac up to Vim 8.0

What's jsruntime

It use PyV8 as javascript interpreter. if PyV8 not supported, it use node, cscript, spiderMonkey as fallbacks, it aslo create a browser-based context to execute javascript code if PyV8 is supported.

Installation

Copy everything inside autoload to autoload directory of your vim

Plugins powered by jsruntime.vim

  1. sourcebeautify.vim source code beautifier
  2. jsflakes.vim lint javascript code on the fly

Usage

It is a basic library. It does nothing if no other plugin calls it. If you are a vim plugin developer and want to use this library. You need to check if this library exist

    try
        call javascript#runtime#evalScript("")
        let jsruntimePluginAvailable = 1
    catch E117
        let jsruntimePluginAvailable = 0
    endtry
    
    if jsruntimePluginAvailable
        " your code
    endif

This library is often used with jsoncodecs.vim.

    javascript#runtime#evalScript(jsoncodecs#dump_string(getline(1,'$')))

Documentation

It provide the following functions

  1. javascript#runtime#evalScript({script}, {renew_context})

     :echo javascript#runtime#evalScript('1+2')
     // output 3
    

    renew_context is a flag to indicate whether keep the context created by script before

     :call javascript#runtime#evalScript('a=3')  // we create a context
     :echo javascript#runtime#evalScript('a;') // we eval this script in context created before
     // output 1
     :echo javascript#runtime#evalScript('a;',1) // we eval this script in a completely new context
     // output undefined
    

    renew_context is not guaranteed support, if not support renew_context will be simply ignored, please

    read javascript#runtime#supportLivingContext for more.

  2. javascript#runtime#evalScriptInBrowserContext

    because we only implement browser interface using PyV8, so if PyV8 is not supported, this function will not exist, check existence before use

     // vim script
     if exists('javascript#runtime#evalScriptInBrowserContext')
         // do what you like
     endif
    

    sample

     :call javascript#runtime#evalScriptInBrowserContext('<html><body onload="console.log(1+2);"><p></p></body></html>')
     //output 3
    
  3. javascript#runtime#isSupportLivingContext

    you can use javascript#runtime#isSupportLivingContext to check whether living_context is supported by jsruntime

About

Run javascript code directly in Vim

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published