Skip to content

Commit 7fbae99

Browse files
committed
Initial OSS commit
1 parent f065187 commit 7fbae99

29 files changed

+1975
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
/*.xcodeproj
5+
compile_commands.json
6+
*.pyc

LICENSE

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Copyright (c) 2018, swift-vim contributors
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
7+
1. Redistributions of source code must retain the above copyright notice, this
8+
list of conditions and the following disclaimer.
9+
2. Redistributions in binary form must reproduce the above copyright notice,
10+
this list of conditions and the following disclaimer in the documentation
11+
and/or other materials provided with the distribution.
12+
13+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
14+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
16+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
17+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
18+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
19+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
20+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23+
24+
The views and conclusions contained in the software and documentation are those
25+
of the authors and should not be interpreted as representing official policies,
26+
either expressed or implied, of the swift-vim project.

Makefile

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
SHELL=bash
2+
3+
LAST_LOG=.build/last_build.log
4+
PWD=$(shell pwd)
5+
6+
PLUGIN_NAME=example
7+
8+
# Generate a plugin setup to work with VimKit
9+
.PHONY: generate
10+
generate:
11+
@./PluginGenerator/plugin_generator.sh
12+
13+
default: debug
14+
15+
.PHONY: release
16+
release: CONFIG=release
17+
release: SWIFT_OPTS=--product VimKit \
18+
-Xcc -I$(PYTHON_INCLUDE) \
19+
-Xcc -fvisibility=hidden \
20+
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
21+
-Xlinker $(PYTHON_LINKED_LIB)
22+
release: build_impl
23+
24+
.PHONY: debug
25+
debug: CONFIG=debug
26+
debug: SWIFT_OPTS=--product VimKit \
27+
-Xcc -I$(PYTHON_INCLUDE) \
28+
-Xcc -fvisibility=hidden \
29+
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
30+
-Xlinker $(PYTHON_LINKED_LIB)
31+
debug: build_impl
32+
33+
# Dynamically find python vars
34+
# Note, that this is OSX specific
35+
# We will pass this directly to the linker command line
36+
# Whatever dylib was used i.e. Py.framework/SOMEPYTHON
37+
.PHONY: py_vars
38+
py_vars:
39+
@source Utils/make_lib.sh; python_info
40+
$(eval PYTHON_LINKED_LIB=$(shell source Utils/make_lib.sh; linked_python))
41+
$(eval PYTHON_INCLUDE=$(shell source Utils/make_lib.sh; python_inc_dir))
42+
43+
44+
# SPM Build
45+
.PHONY: build_impl
46+
# Careful: assume we need to depend on this here
47+
build_impl: py_vars
48+
build_impl:
49+
@echo "Building.."
50+
@mkdir -p .build/$(CONFIG)
51+
@swift build -c $(CONFIG) $(SWIFT_OPTS) \
52+
-Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.12" \
53+
| tee $(LAST_LOG)
54+
55+
# Running tests with custom versions of Python
56+
# USE_PYTHON=/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/Python make test
57+
.PHONY: test
58+
test_b: CONFIG=debug
59+
test_b: SWIFT_OPTS= \
60+
-Xcc -DSPMVIM_LOADSTUB_RUNTIME \
61+
-Xcc -I$(PYTHON_INCLUDE) \
62+
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
63+
-Xlinker $(PYTHON_LINKED_LIB) \
64+
--build-tests
65+
test_b: build_impl
66+
test: CONFIG=debug
67+
test: py_vars test_b
68+
@echo "Testing.."
69+
@mkdir -p .build/$(CONFIG)
70+
@swift test --skip-build -c $(CONFIG) $(SWIFT_OPTS) | tee $(LAST_LOG)
71+
72+
.PHONY: test_generate
73+
test_generate:
74+
# We use the HEAD ref in the test
75+
git diff --quiet || (echo 'Dirty tree' && exit 1)
76+
rm -rf ~/Desktop/Swiftvimexample || true
77+
plugin_path=~/Desktop/Swiftvimexample make generate
78+
cd ~/Desktop/Swiftvimexample && make
79+
80+
clean:
81+
rm -rf .build/debug/*
82+
rm -rf .build/release/*
83+
84+
# Build compile_commands.json
85+
# Unfortunately, we need to clean.
86+
# Use the last installed product incase we messed something up during
87+
# coding.
88+
compile_commands.json: SWIFT_OPTS=-Xswiftc -parseable-output \
89+
-Xcc -I$(PYTHON_INCLUDE) \
90+
-Xlinker $(PYTHON_LINKED_LIB)
91+
compile_commands.json: CONFIG=debug
92+
compile_commands.json: clean build_impl
93+
cat $(LAST_LOG) | /usr/local/bin/spm-vim compile_commands
94+

Package.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// swift-tools-version:4.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "Vim",
8+
products: [
9+
// VimKit is a Library for VimPlugin development
10+
.library(
11+
name: "VimKit",
12+
type: .static,
13+
targets: ["VimKit"]),
14+
15+
.library(
16+
name: "VimInterface",
17+
type: .static,
18+
targets: ["VimInterface"]),
19+
20+
.library(
21+
name: "VimAsync",
22+
type: .static,
23+
targets: ["VimAsync"]),
24+
25+
.library(
26+
name: "Example",
27+
type: .dynamic,
28+
targets: ["Example"]),
29+
],
30+
31+
targets: [
32+
.target(name: "VimKit",
33+
dependencies: ["Vim"]),
34+
.target(name: "Vim",
35+
dependencies: ["VimInterface"]),
36+
37+
.target(name: "VimInterface",
38+
dependencies: []),
39+
40+
// Async Support for Vim. Note, that this is OSX only and
41+
// depends on Foundation
42+
.target(name: "VimAsync",
43+
dependencies: ["VimKit"]),
44+
45+
// Tests
46+
.testTarget(
47+
name: "VimInterfaceTests",
48+
dependencies: ["VimInterface", "Example"]),
49+
.testTarget(
50+
name: "VimKitTests",
51+
dependencies: ["VimKit", "Example"]),
52+
53+
// Example
54+
.target(name: "Example",
55+
dependencies: ["VimKit", "VimAsync"]),
56+
]
57+
)

PluginGenerator/Makefile.tpl

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
SHELL=bash
2+
3+
LAST_LOG=.build/last_build.log
4+
PWD=$(shell pwd)
5+
6+
PLUGIN_NAME=__VIM_PLUGIN_NAME__
7+
8+
default: debug
9+
10+
.PHONY: release
11+
release: CONFIG=release
12+
release: SWIFT_OPTS= \
13+
-Xcc -I$(PYTHON_INCLUDE) \
14+
-Xcc -fvisibility=hidden \
15+
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
16+
-Xlinker $(PYTHON_LINKED_LIB)
17+
release: build_impl
18+
19+
.PHONY: debug
20+
debug: CONFIG=debug
21+
debug: SWIFT_OPTS= \
22+
-Xcc -I$(PYTHON_INCLUDE) \
23+
-Xcc -fvisibility=hidden \
24+
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
25+
-Xlinker $(PYTHON_LINKED_LIB)
26+
debug: build_impl
27+
28+
# Dynamically find python vars
29+
# Note, that this is OSX specific
30+
# We will pass this directly to the linker command line
31+
# Whatever dylib was used i.e. Py.framework/SOMEPYTHON
32+
.PHONY: py_vars
33+
py_vars:
34+
@source VimUtils/make_lib.sh; python_info
35+
$(eval PYTHON_LINKED_LIB=$(shell source VimUtils/make_lib.sh; linked_python))
36+
$(eval PYTHON_INCLUDE=$(shell source VimUtils/make_lib.sh; python_inc_dir))
37+
38+
39+
# SPM Build
40+
.PHONY: build_impl
41+
# Careful: assume we need to depend on this here
42+
build_impl: py_vars
43+
build_impl:
44+
@echo "Building.."
45+
@mkdir -p .build/$(CONFIG)
46+
@swift build -c $(CONFIG) $(SWIFT_OPTS) \
47+
-Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.12" \
48+
| tee $(LAST_LOG)
49+
50+
.PHONY: test
51+
test_b: CONFIG=debug
52+
test_b: SWIFT_OPTS= \
53+
-Xcc -I$(PYTHON_INCLUDE) \
54+
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
55+
-Xlinker $(PYTHON_LINKED_LIB) \
56+
--build-tests
57+
test_b: build_impl
58+
test: CONFIG=debug
59+
test: py_vars test_b
60+
@echo "Testing.."
61+
@mkdir -p .build/$(CONFIG)
62+
@swift test --skip-build -c $(CONFIG) $(SWIFT_OPTS) | tee $(LAST_LOG)
63+

PluginGenerator/Package.tpl.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// swift-tools-version:4.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "__VIM_PLUGIN_NAME__",
8+
products: [
9+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
10+
.library(
11+
name: "__VIM_PLUGIN_NAME__",
12+
targets: ["__VIM_PLUGIN_NAME__"]),
13+
],
14+
dependencies: [
15+
// Dependencies declare other packages that this package depends on.
16+
// SwiftForVim is moving fast,
17+
// master for the latest and greatest
18+
.package(url: "https://github.com/swift-vim/SwiftForVim.git",
19+
.revision("__GIT_REVISION__"))
20+
],
21+
targets: [
22+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
23+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
24+
.target(
25+
name: "__VIM_PLUGIN_NAME__",
26+
dependencies: ["VimKit", "VimAsync"]),
27+
.testTarget(
28+
name: "__VIM_PLUGIN_NAME__Tests",
29+
dependencies: ["__VIM_PLUGIN_NAME__"]),
30+
]
31+
)

PluginGenerator/PluginMain.tpl.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// VimKit Plugin initialization
2+
import VimKit
3+
import VimAsync
4+
5+
public final class __VIM_PLUGIN_NAME__Plugin: VimPlugin {
6+
// Handle callbacks from Vim
7+
public func event(event id: Int, context: String) -> String? {
8+
return nil
9+
}
10+
}
11+
12+
// Core bootstrap for the plugin
13+
@_cdecl("__VIM_PLUGIN_NAME___plugin_init")
14+
public func plugin_init(context: UnsafePointer<Int8>) -> Int {
15+
// Setup the plugin conforming to <VimPlugin> here
16+
VimKit.setPlugin(__VIM_PLUGIN_NAME__Plugin())
17+
return 0
18+
}
19+
20+
@_cdecl("__VIM_PLUGIN_NAME___plugin_event")
21+
public func plugin_event(event: Int, context: UnsafePointer<Int8>) -> UnsafePointer<Int8>? {
22+
return VimKit.event(event: event, context: context)
23+
}
24+
25+
@_cdecl("__VIM_PLUGIN_NAME___plugin_runloop_callback")
26+
public func plugin_runloop_callback() {
27+
// RunLoop callbacks for threading.
28+
// Note: VimTask is tested on OSX only as of now.
29+
// Comment out the next line for use on other platforms.
30+
VimKit.runLoopCallback()
31+
}
32+

PluginGenerator/plugin.tpl.vim

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
" This is basic vim plugin boilerplate
2+
let s:save_cpo = &cpo
3+
set cpo&vim
4+
5+
" BEGIN_SWIFTVIM
6+
" COMPILED_FOR_SWIFTVIM_VERSION 0.1
7+
8+
" The API uses Python internally
9+
function! s:UsingPython3()
10+
if has('python3')
11+
return 1
12+
endif
13+
return 0
14+
endfunction
15+
16+
" Prefer py3
17+
let s:using_python3 = s:UsingPython3()
18+
let s:python_until_eof = s:using_python3 ? "python3 << EOF" : "python << EOF"
19+
let s:python_command = s:using_python3 ? "py3 " : "py "
20+
21+
let s:path = fnamemodify(expand('<sfile>:p:h'), ':h')
22+
23+
" SwiftVimEval:
24+
" Eval commands against the VimPlugin instance
25+
"
26+
" The Vim API exposes a single method:
27+
" pluginName.event(Int, String)
28+
"
29+
" This corresponds to the Swift protocol
30+
" protocol VimPlugin {
31+
" func event(id: Int, context: String) -> String
32+
" }
33+
"
34+
" ex:
35+
" myvimplugin.event(42, 'MeaningOfLife')
36+
"
37+
function! s:SwiftVimEval( eval_string )
38+
" Run some python
39+
if s:using_python3
40+
return py3eval( a:eval_string )
41+
endif
42+
return pyeval( a:eval_string )
43+
endfunction
44+
45+
function! s:SwiftVimSetupPlugin() abort
46+
exec s:python_until_eof
47+
import vim
48+
import os
49+
import sys
50+
51+
# Directory of the plugin
52+
plugin_dir = vim.eval('s:path')
53+
54+
# Bootstrap Swift Plugin
55+
sys.path.insert(0, os.path.join(plugin_dir, '.build'))
56+
import __VIM_PLUGIN_NAME__
57+
__VIM_PLUGIN_NAME__.load()
58+
59+
vim.command('return 1')
60+
EOF
61+
endfunction
62+
63+
if s:SwiftVimSetupPlugin() != 1
64+
echom "Setting up python failed..." . s:path
65+
endif
66+
67+
" Internal, VimRunLoop integration
68+
fun s:SwiftVimRunLoopTimer(timer)
69+
call s:SwiftVimEval("__VIM_PLUGIN_NAME__.runloop_callback()")
70+
endf
71+
72+
let s:SwiftVimRunLoopTimer = timer_start(100, function('s:SwiftVimRunLoopTimer'), {'repeat':-1})
73+
74+
" END_SWIFTVIM
75+
76+
" This is basic vim plugin boilerplate
77+
let &cpo = s:save_cpo
78+
unlet s:save_cpo
79+

0 commit comments

Comments
 (0)