object.vim
An object-oriented framework for Vimscript.
__ _ __
____ / /_ (_)__ _____/ /_
/ __ \/ __ \ / / _ \/ ___/ __/
/ /_/ / /_/ / / / __/ /__/ /_
\____/_.___/_/ /\___/\___/\__/
/___/
An object-oriented framework for Vimscript that looks and feels like Python and strives for maximum convenience and compatibility with built-in functions.
Read: Vim and Python are friends, let's do both of them more!
Examples
- Defining classes and creating instances.
let MyClass = object#class('MyClass')
let var = object#new(MyClass)- Calling methods of parents and siblings with
super().
" MyClass as above.
function! MyClass.__init__()
call object#super(MyClass, self).__init__()
let self.data = 1
endfunction- Creating Lambdas.
let x = reverse(range(3))
echo sort(x, object#lambda('x y', 'x - y'))
[0, 1, 2]- Iterator and for loop construct.
echo object#list(object#zip('abc', range(3)))
[['a', 0], ['b', 1], ['c', 2]]
let dict = {'foo': 1, 'bar': 2}
call object#for('key val', items(dict), 'echo key val')
foo 1
bar 2- Open files for reading and writing.
let f = object#open('data.txt', 'r')
echo object#repr(f)
<open file 'data.txt', mode 'r'>
echo f.read()
'All the lines concatenated with newline.'- Wrappers for built-in types for inheritance.
let dict = object#dict_()
echo object#repr(dict)
<'dict' type>
let MyDict = object#class('MyDict', dict)
let var = object#new(MyDict, {'foo': 1})
echo var.values()
[['foo', 1]]- A bunch of hookable protocol functions.
echo object#len(range(10))
10
echo object#contains('', 'string')
1
let object = object#object_()
echo object#dir(object)
['__name__', '__bases__', '__base__', '__repr__', '__class__', '__init__', '__mro__']- Hashing arbitrary objects.
echo object#hash('this is a string')
197650594
echo object#hash(-1234)
2147482414
echo object#hash(function('tr'))
233815837- Class-based exceptions.
let except = object#except#builtins()
call object#class('MyException', except.Exception, g:)
call object#raise(MyException)
E605: Exception not caught: MyException:Features
- Shallow namespace
object#. - Multiple inheritance powered by C3 linearization.
- A complete set of Python-like built-in functions.
- True lambda with closure and named arguments.
- True iterator and
zip()andenumerate(). - Hash (nearly) arbitrary object.
- Read and write files with
open().
Future Work
- Increase coverage of
file.vimandlist.vim. - Finish
compare.vimfor hookable comparition. - Add
str.vimfor Python-like formatting. - Better documentation.
- Improve performance.
Installation
This plugin follows the standard runtime path structure, and as such it can be installed with a variety of plugin managers:
| Plugin Manager | Install with... |
|---|---|
| NeoBundle | NeoBundle 'cgsdfc/object.vim' |
| Vundle | Plugin 'cgsdfc/object.vim' |
| Plug | Plug 'cgsdfc/object.vim' |
| VAM | call vam#ActivateAddons(['vim-airline']) |
| Dein | call dein#add('cgsdfc/object.vim') |
| minpac | call minpac#add('cgsdfc/object.vim') |
Dependencies
- vim-maktaba for handling built-in type.
- vader.vim for unit tests.
- vimdoc for generating the help file from comments.
Documentation
:help object
License
MIT License. Copyright (c) 2018 cgsdfc.