Skip to content

Commit f6ed8fe

Browse files
committed
Named Vim callbacks, Namespace Vim
This patch adds named vim callbacks so users can add basically any function to be called back from Vim. ``` VimPlugin.setCallable("helloWorld") { _ in print("Saying Hello World, Vim") } ``` Vim then calls the Plugin function ``` call s:SwiftVimEval("MyAwesomePlugin('helloWorld', 42)") ``` It simply adds to a Dictionary: a string containing a function. The public API now consists of 1 function: `plugin_load`. The patch also adds namespacing at the build level. It still uses `swift package manager` but in a non conventional way. There needs to be more thought put into build tooling to handle namespacing transitive dependencies.
1 parent 039871f commit f6ed8fe

File tree

20 files changed

+379
-248
lines changed

20 files changed

+379
-248
lines changed

Makefile

Lines changed: 85 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,75 +3,108 @@ SHELL=bash
33
LAST_LOG=.build/last_build.log
44
PWD=$(shell pwd)
55

6-
PLUGIN_NAME=example
6+
PLUGIN_NAME=Example
7+
TRIPPLE=x86_64-apple-macosx10.12
8+
BUILD_DIR=$(PWD)/.build/$(CONFIG)
79

8-
# Generate a plugin setup to work with VimKit
10+
# Generate a plugin setup to work with Vim
911
.PHONY: generate
1012
generate:
1113
@./PluginGenerator/plugin_generator.sh
1214

13-
default: debug
15+
.PHONY: debug
16+
debug: CONFIG=debug
17+
debug: plugin_so
1418

1519
.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
20+
release: CONFIG=debug
21+
release: vim_lib plugin_so
2322

24-
.PHONY: debug
25-
debug: CONFIG=debug
26-
debug: SWIFT_OPTS=--product VimKit \
27-
-Xcc -I$(PYTHON_INCLUDE) \
28-
-Xcc -fvisibility=hidden \
23+
BASE_OPTS=-Xcc -I$(PYTHON_INCLUDE) \
2924
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
30-
-Xlinker $(PYTHON_LINKED_LIB)
31-
debug: build_impl
25+
-Xlinker $(PYTHON_LINKED_LIB) \
26+
-Xcc -fvisibility=hidden \
27+
-Xlinker -undefined -Xlinker dynamic_lookup \
28+
-Xlinker -all_load
29+
30+
.PHONY: vim_lib, renamed_vim_lib
31+
vim_lib: SWIFT_OPTS=--product Vim \
32+
-Xswiftc -module-name=$(PLUGIN_NAME)Vim \
33+
-Xswiftc -module-link-name=$(PLUGIN_NAME)Vim \
34+
$(BASE_OPTS)
35+
36+
# FIXME: this triggers rebuilds.
37+
renamed_vim_lib: vim_lib
38+
@ditto $(BUILD_DIR)/Vim.swiftmodule \
39+
$(BUILD_DIR)/$(PLUGIN_NAME)Vim.swiftmodule
40+
@ditto $(BUILD_DIR)/Vim.swiftdoc \
41+
$(BUILD_DIR)/$(PLUGIN_NAME)Vim.swiftdoc
42+
@ditto $(BUILD_DIR)/libVim.a \
43+
$(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.a
44+
45+
# FIXME: this triggers rebuilds.
46+
.PHONY: vim_async_lib, renamed_vim_lib_async
47+
vim_async_lib: SWIFT_OPTS=--product VimAsync \
48+
-Xswiftc -module-name=$(PLUGIN_NAME)VimAsync \
49+
-Xswiftc -module-link-name=$(PLUGIN_NAME)VimAsync \
50+
$(BASE_OPTS)
51+
renamed_vim_async_lib: vim_async_lib
52+
@ditto $(BUILD_DIR)/VimAsync.swiftmodule \
53+
$(BUILD_DIR)/$(PLUGIN_NAME)VimAsync.swiftmodule
54+
@ditto $(BUILD_DIR)/VimAsync.swiftdoc \
55+
$(BUILD_DIR)/$(PLUGIN_NAME)VimAsync.swiftdoc
56+
@ditto $(BUILD_DIR)/libVimAsync.a \
57+
$(BUILD_DIR)/lib$(PLUGIN_NAME)libVimAsync.a
58+
59+
# Main plugin lib
60+
.PHONY: plugin_lib
61+
plugin_lib: SWIFT_OPTS=--product $(PLUGIN_NAME) \
62+
$(BASE_OPTS)
63+
plugin_lib: renamed_vim_lib
64+
# To useadd VimAsync, add it following `renamed_vim_lib`
3265

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
66+
# Build the .so, which Vim dynamically links.
67+
.PHONY: plugin_so
68+
plugin_so: plugin_lib
69+
@clang -g \
70+
-Xlinker $(PYTHON_LINKED_LIB) \
71+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME).dylib \
72+
-shared -o .build/$(PLUGIN_NAME).so
73+
74+
# Build for the python dylib vim links
3775
.PHONY: py_vars
3876
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-
77+
@source VimUtils/make_lib.sh; python_info
78+
$(eval PYTHON_LINKED_LIB=$(shell source VimUtils/make_lib.sh; linked_python))
79+
$(eval PYTHON_INCLUDE=$(shell source VimUtils/make_lib.sh; python_inc_dir))
4380

4481
# SPM Build
45-
.PHONY: build_impl
46-
# Careful: assume we need to depend on this here
47-
build_impl: py_vars
48-
build_impl:
82+
vim_lib vim_async_lib plugin_lib test_b: py_vars
4983
@echo "Building.."
50-
@mkdir -p .build/$(CONFIG)
51-
@swift build -c $(CONFIG) $(SWIFT_OPTS) \
52-
-Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.12" \
84+
@mkdir -p .build
85+
@swift build -c $(CONFIG) \
86+
$(BASE_OPTS) $(SWIFT_OPTS) $(EXTRA_OPTS) \
87+
-Xswiftc -target -Xswiftc $(TRIPPLE) \
88+
-Xlinker $(BUILD_DIR)/libVim.a \
5389
| tee $(LAST_LOG)
54-
@clang -g \
55-
-Xlinker $(PYTHON_LINKED_LIB) \
56-
-Xlinker $(PWD)/.build/$(CONFIG)/lib$(PLUGIN_NAME).dylib \
57-
-shared -o .build/$(PLUGIN_NAME).so
5890

59-
# Running tests with custom versions of Python
91+
# Mark - Internal Utils:
92+
93+
# Overriding Python:
6094
# USE_PYTHON=/usr/local/Cellar/python/3.6.4_4/Frameworks/Python.framework/Versions/3.6/Python make test
6195
.PHONY: test
62-
test_b: CONFIG=debug
63-
test_b: SWIFT_OPTS= \
64-
-Xcc -DSPMVIM_LOADSTUB_RUNTIME \
65-
-Xcc -I$(PYTHON_INCLUDE) \
66-
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
67-
-Xlinker $(PYTHON_LINKED_LIB) \
68-
--build-tests
69-
test_b: build_impl
7096
test: CONFIG=debug
71-
test: py_vars test_b
97+
test: EXTRA_OPTS= \
98+
-Xcc -DSPMVIM_LOADSTUB_RUNTIME
99+
test: debug
72100
@echo "Testing.."
73-
@mkdir -p .build/$(CONFIG)
74-
@swift test --skip-build -c $(CONFIG) $(SWIFT_OPTS) | tee $(LAST_LOG)
101+
@mkdir -p .build
102+
@swift build --product VimPackageTests \
103+
$(BASE_OPTS) $(SWIFT_OPTS) $(EXTRA_OPTS) \
104+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME).dylib \
105+
-Xswiftc -target -Xswiftc $(TRIPPLE)
106+
@swift test --skip-build | tee $(LAST_LOG)
107+
75108

76109
.PHONY: test_generate
77110
test_generate:
@@ -85,6 +118,10 @@ clean:
85118
rm -rf .build/debug/*
86119
rm -rf .build/release/*
87120

121+
# Generate the example
122+
PluginGenerator/PluginMain.tpl.swift: Sources/Example/Example.swift
123+
sed "s,Example,__VIM_PLUGIN_NAME__,g" $< > $@
124+
88125
# Build compile_commands.json
89126
# Unfortunately, we need to clean.
90127
# Use the last installed product incase we messed something up during

Package.swift

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ import PackageDescription
66
let package = Package(
77
name: "Vim",
88
products: [
9-
// VimKit is a Library for VimPlugin development
109
.library(
11-
name: "VimKit",
10+
name: "VimInterface",
1211
type: .static,
13-
targets: ["VimKit"]),
12+
targets: ["VimInterface"]),
1413

1514
.library(
16-
name: "VimInterface",
15+
name: "Vim",
1716
type: .static,
18-
targets: ["VimInterface"]),
17+
targets: ["Vim"]),
18+
19+
.library(
20+
name: "VimPluginBootstrap",
21+
type: .static,
22+
targets: ["VimPluginBootstrap"]),
1923

2024
.library(
2125
name: "VimAsync",
@@ -29,29 +33,30 @@ let package = Package(
2933
],
3034

3135
targets: [
32-
.target(name: "VimKit",
33-
dependencies: ["Vim"]),
3436
.target(name: "Vim",
35-
dependencies: ["VimInterface"]),
37+
dependencies: ["VimInterface", "VimPluginBootstrap"]),
3638

3739
.target(name: "VimInterface",
3840
dependencies: []),
3941

42+
.target(name: "VimPluginBootstrap",
43+
dependencies: []),
44+
4045
// Async Support for Vim. Note, that this is OSX only and
4146
// depends on Foundation
4247
.target(name: "VimAsync",
43-
dependencies: ["VimKit"]),
48+
dependencies: []),
4449

4550
// Tests
4651
.testTarget(
4752
name: "VimInterfaceTests",
48-
dependencies: ["VimInterface", "Example"]),
53+
dependencies: ["VimInterface"]),
4954
.testTarget(
50-
name: "VimKitTests",
51-
dependencies: ["VimKit", "Example"]),
55+
name: "VimTests",
56+
dependencies: []),
5257

53-
// Example
58+
// Example:
5459
.target(name: "Example",
55-
dependencies: ["VimKit", "VimAsync"]),
60+
dependencies: []),
5661
]
5762
)

PluginGenerator/Makefile.tpl

Lines changed: 63 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,64 +4,81 @@ LAST_LOG=.build/last_build.log
44
PWD=$(shell pwd)
55

66
PLUGIN_NAME=__VIM_PLUGIN_NAME__
7+
TRIPPLE=x86_64-apple-macosx10.12
8+
BUILD_DIR=.build/$(CONFIG)
79

8-
default: debug
10+
.PHONY: debug
11+
debug: CONFIG=debug
12+
debug: plugin_so
913

1014
.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
15+
release: CONFIG=debug
16+
release: vim_lib plugin_so
1817

19-
.PHONY: debug
20-
debug: CONFIG=debug
21-
debug: SWIFT_OPTS= \
22-
-Xcc -I$(PYTHON_INCLUDE) \
23-
-Xcc -fvisibility=hidden \
18+
BASE_OPTS=-Xcc -I$(PYTHON_INCLUDE) \
2419
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
25-
-Xlinker $(PYTHON_LINKED_LIB)
26-
debug: build_impl
20+
-Xlinker $(PYTHON_LINKED_LIB) \
21+
-Xcc -fvisibility=hidden \
22+
-Xlinker -undefined -Xlinker dynamic_lookup \
23+
-Xlinker -all_load
24+
25+
.PHONY: vim_lib, renamed_vim_lib
26+
vim_lib: SWIFT_OPTS=--product Vim \
27+
-Xswiftc -module-name=$(PLUGIN_NAME)Vim \
28+
-Xswiftc -module-link-name=$(PLUGIN_NAME)Vim \
29+
$(BASE_OPTS)
30+
31+
# FIXME: this triggers rebuilds.
32+
renamed_vim_lib: vim_lib
33+
@ditto $(BUILD_DIR)/Vim.swiftmodule \
34+
$(BUILD_DIR)/$(PLUGIN_NAME)Vim.swiftmodule
35+
@ditto $(BUILD_DIR)/Vim.swiftdoc \
36+
$(BUILD_DIR)/$(PLUGIN_NAME)Vim.swiftdoc
37+
@ditto $(BUILD_DIR)/libVim.a \
38+
$(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.a
39+
40+
# FIXME: this triggers rebuilds.
41+
.PHONY: vim_async_lib, renamed_vim_lib_async
42+
vim_async_lib: SWIFT_OPTS=--product VimAsync \
43+
-Xswiftc -module-name=$(PLUGIN_NAME)VimAsync \
44+
-Xswiftc -module-link-name=$(PLUGIN_NAME)VimAsync \
45+
$(BASE_OPTS)
46+
renamed_vim_async_lib: vim_async_lib
47+
ditto $(BUILD_DIR)/VimAsync.swiftmodule \
48+
$(BUILD_DIR)/$(PLUGIN_NAME)VimAsync.swiftmodule
49+
ditto $(BUILD_DIR)/VimAsync.swiftdoc \
50+
$(BUILD_DIR)/$(PLUGIN_NAME)VimAsync.swiftdoc
51+
@ditto $(BUILD_DIR)/libVimAsync.a \
52+
$(BUILD_DIR)/lib$(PLUGIN_NAME)libVimAsync.a
53+
54+
# Main plugin lib
55+
.PHONY: plugin_lib
56+
plugin_lib: SWIFT_OPTS=--product $(PLUGIN_NAME) \
57+
$(BASE_OPTS) \
58+
-Xlinker $(BUILD_DIR)/libVim.a
59+
plugin_lib: renamed_vim_lib
60+
# To useadd VimAsync, add it following `renamed_vim_lib`
2761

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
62+
# Build the .so, which Vim dynamically links.
63+
.PHONY: plugin_so
64+
plugin_so: plugin_lib
65+
@clang -g \
66+
-Xlinker $(PYTHON_LINKED_LIB) \
67+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME).dylib \
68+
-shared -o .build/$(PLUGIN_NAME).so
69+
70+
# Build for the python dylib vim links
3271
.PHONY: py_vars
3372
py_vars:
3473
@source VimUtils/make_lib.sh; python_info
3574
$(eval PYTHON_LINKED_LIB=$(shell source VimUtils/make_lib.sh; linked_python))
3675
$(eval PYTHON_INCLUDE=$(shell source VimUtils/make_lib.sh; python_inc_dir))
3776

38-
3977
# SPM Build
40-
.PHONY: build_impl
41-
# Careful: assume we need to depend on this here
42-
build_impl: py_vars
43-
build_impl:
78+
vim_lib vim_async_lib plugin_lib test_b: py_vars
79+
@mkdir -p .build
4480
@echo "Building.."
45-
@mkdir -p .build/$(CONFIG)
46-
@swift build -c $(CONFIG) $(SWIFT_OPTS) \
47-
-Xswiftc "-target" -Xswiftc "x86_64-apple-macosx10.12" \
81+
swift build -c $(CONFIG) \
82+
$(BASE_OPTS) $(SWIFT_OPTS) $(EXTRA_OPTS) \
83+
-Xswiftc -target -Xswiftc $(TRIPPLE) \
4884
| tee $(LAST_LOG)
49-
@clang -g \
50-
-Xlinker $(PYTHON_LINKED_LIB) \
51-
-Xlinker $(PWD)/.build/$(CONFIG)/lib$(PLUGIN_NAME).dylib \
52-
-shared -o .build/$(PLUGIN_NAME).so
53-
54-
.PHONY: test
55-
test_b: CONFIG=debug
56-
test_b: SWIFT_OPTS= \
57-
-Xcc -I$(PYTHON_INCLUDE) \
58-
-Xcc -DVIM_PLUGIN_NAME=$(PLUGIN_NAME) \
59-
-Xlinker $(PYTHON_LINKED_LIB) \
60-
--build-tests
61-
test_b: build_impl
62-
test: CONFIG=debug
63-
test: py_vars test_b
64-
@echo "Testing.."
65-
@mkdir -p .build/$(CONFIG)
66-
@swift test --skip-build -c $(CONFIG) $(SWIFT_OPTS) | tee $(LAST_LOG)
67-

0 commit comments

Comments
 (0)