Created
March 13, 2011 05:28
-
-
Save ynkdir/867896 to your computer and use it in GitHub Desktop.
nsexample.vim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 基本アイデア: 名前空間を実行時の環境に合わせることでファイルの変更なし | |
" にコピーできるようにする。 | |
let s:ns = expand('<sfile>:p:r:gs?[\\/]?#?:s?^.*#autoload#??:s?$?#?') | |
function {s:ns}func() | |
echo "func()" | |
endfunction | |
" その他なんとなく作法てきな | |
" | |
" 例: | |
" foolib パッケージ | |
" autoload/foolib/util.vim | |
" autoload/foolib/net.vim | |
" | |
" 使う側: | |
" let s:util = foolib#util#import() | |
" もしくはあらかじめ自分の名前空間の下にコピーしておいて | |
" let s:util = myplugin#foolib#util#import() | |
" or | |
" let s:myns = expand('<sfile>:p:r:gs?[\\/]?#?:s?^.*#autoload#??:s?$?#?') | |
" let s:util = {s:myns}foolib#util#import() | |
" パッケージの名前空間 (必要なら) | |
let s:foolib = expand('<sfile>:p:h:gs?[\\/]?#?:s?^.*#autoload\(#\|$\)??') | |
" 自分の名前空間 | |
" {s:foolib}util#import() とかでも可 | |
let s:ns = expand('<sfile>:p:r:gs?[\\/]?#?:s?^.*#autoload#??:s?$?#?') | |
" モジュールインポート | |
function {s:ns}import() | |
return s:util | |
endfunction | |
" 一つのクラスのみ提供するモジュールは new() をエクスポートしたり | |
function {s:ns}new() | |
return s:util.SomeClass.new() | |
endfunction | |
" 関数を直接エクスポートしてもいい | |
function {s:ns}print(str) | |
call s:util.print(a:str) | |
endfunction | |
" 以下モジュール実装 | |
" 他のモジュールをインポートする場合 | |
let s:net = {s:foolib}net#import() | |
let s:util = {} | |
function s:util.print(str) | |
echo a:str | |
endfunction | |
let s:util.SomeClass = {} | |
function s:util.SomeClass.new() | |
return deepcopy(self) | |
endfunction | |
function s:util.SomeClass.method() | |
echo "foolib::util::SomeClass::method()" | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment