Ⅰ. 插件描述
Run javascript code directly in VimⅡ. 基本信息
|
Ⅲ. 安装方法
使用Vundle管理器安装
在你的.vimrc下添加:Plugin 'michalliu/jsruntime-vim'
… 然后在Vim中运行以下命令:
:source %
:PluginInstall
对于Vundle版本 < 0.10.2,请用上面的Bundle替换Plugin。
使用NeoBundle管理器安装
在你的.vimrc下添加:NeoBundle 'michalliu/jsruntime-vim'
… 然后在Vim中运行以下命令:
:source %
:NeoBundleInstall
使用VimPlug管理器安装
在你的.vimrc下添加:Plug 'michalliu/jsruntime-vim'
… 然后在Vim中运行以下命令:
:source %
:PlugInstall
使用Pathogen管理器安装
在终端中运行以下命令:cd ~/.vim/bundle
git clone https://github.com/michalliu/jsruntime.vim
Ⅳ. 文档说明
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
- sourcebeautify.vim source code beautifier
- 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
javascript#runtime#evalScript({script}, {renew_context})
:echo javascript#runtime#evalScript('1+2') // output 3renew_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 undefinedrenew_context is not guaranteed support, if not support renew_context will be simply ignored, please
read javascript#runtime#supportLivingContext for more.
- 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
- javascript#runtime#isSupportLivingContext
you can use javascript#runtime#isSupportLivingContext to check whether living_context is supported by jsruntime