From 1e413fd038ccb764efacc40bd8314cfa81b27fee Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 11 Dec 2023 13:25:28 +0000 Subject: [PATCH] Initial commit --- .bundle/config | 2 + .gitignore | 2 + .rspec | 1 + Gemfile | 4 ++ autoload/zkvim.vim | 49 +++++++++++++++++++++++ doc/tags | 0 doc/zkvim.txt | 2 + plugin/zkvim.vim | 18 +++++++++ spec/spec_helper.rb | 98 +++++++++++++++++++++++++++++++++++++++++++++ spec/zk.vim_spec.rb | 44 ++++++++++++++++++++ 10 files changed, 220 insertions(+) create mode 100644 .bundle/config create mode 100644 .gitignore create mode 100644 .rspec create mode 100644 Gemfile create mode 100644 autoload/zkvim.vim create mode 100644 doc/tags create mode 100644 doc/zkvim.txt create mode 100644 plugin/zkvim.vim create mode 100644 spec/spec_helper.rb create mode 100644 spec/zk.vim_spec.rb diff --git a/.bundle/config b/.bundle/config new file mode 100644 index 0000000..2369228 --- /dev/null +++ b/.bundle/config @@ -0,0 +1,2 @@ +--- +BUNDLE_PATH: "vendor/bundle" diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fc0674a --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +Gemfile.lock +vendor/ diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..c99d2e7 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..708d971 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source "https://rubygems.org" + +gem "rspec", '~> 3.12' +gem "vimrunner", '~> 0.3.4' diff --git a/autoload/zkvim.vim b/autoload/zkvim.vim new file mode 100644 index 0000000..3d0459b --- /dev/null +++ b/autoload/zkvim.vim @@ -0,0 +1,49 @@ +function! zkvim#create_link(zk_title, zk_id) + return '[' . a:zk_title . ']' . '<' . a:zk_id . '>' +endfunction + +function! zkvim#create_append_link_command(zk_title, zk_id) + return 'normal! a' . zkvim#create_link(a:zk_title, a:zk_id) +endfunction + +function! zkvim#get_new_zettel_id() + let zk_cmd = 'NOZKEDIT=true zk new' + return system(zk_cmd) +endfunction + +function! zkvim#get_zettel_id(selection) + return trim(matchstr(a:selection, '[0-9]*\ ')) " TODO: Find out why this requires a space at the end +endfunction + +function! zkvim#get_zettel_title(selection) + return trim(matchstr(a:selection, '#\zs.*\ze')) +endfunction + +function! zkvim#znew() + let zk_filename = zkvim#get_new_zettel_id() + execute 'edit ' . zk_filename +endfunction + +function! zkvim#zinsertnew(zk_title) + let zk_filename = zkvim#get_new_zettel_id() + let zk_id = zkvim#get_zettel_id(zk_filename) + execute zkvim#create_append_link_command(a:zk_title, zk_id) + execute 'write' + execute 'edit ' . zk_filename + execute 'normal! a# ' . a:zk_title +endfunction + +function! zkvim#sink(selection) abort + let zk_id = zkvim#get_zettel_id(a:selection) + let zk_title = zkvim#get_zettel_title(a:selection) + execute zkvim#create_append_link_command(zk_title, zk_id) +endfunction + +function! zkvim#zsearch() + let zk_cmd = 'zk list' + let fzf_options = #{ + \source: zk_cmd, + \sink: function('zkvim#sink')} + + call fzf#run(fzf_options) +endfunction diff --git a/doc/tags b/doc/tags new file mode 100644 index 0000000..e69de29 diff --git a/doc/zkvim.txt b/doc/zkvim.txt new file mode 100644 index 0000000..79a644b --- /dev/null +++ b/doc/zkvim.txt @@ -0,0 +1,2 @@ +The zkvim plugin is used to interact with the `zk` Zettelkasten tool from +within Vim. diff --git a/plugin/zkvim.vim b/plugin/zkvim.vim new file mode 100644 index 0000000..0db295e --- /dev/null +++ b/plugin/zkvim.vim @@ -0,0 +1,18 @@ +" Title: zkvim +" Description: Allows usage of the `zk` command from within Vim. + +if exists('g:loaded_zk_vim') + finish +endif +let g:loaded_zk_vim = 1 + +if ! exists('g:loaded_fzf') + echo 'fzf is not loaded. zk.vim relies on fzf, please ensure it is installed and loaded correctly.' + finish +endif + +" Zsearch allows you to search for a Zettel and creates a link to the selected +" Zettel at your cursor +command! -nargs=0 Zsearch call zkvim#zsearch() +command! -nargs=0 Znew call zkvim#znew() +command! -nargs=1 Zinsertnew call zkvim#zinsertnew() diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..c80d44b --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,98 @@ +# This file was generated by the `rspec --init` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See https://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # https://rspec.info/features/3-12/rspec-core/configuration/zero-monkey-patching-mode/ + config.disable_monkey_patching! + + # This setting enables warnings. It's recommended, but in some cases may + # be too noisy due to issues in dependencies. + config.warnings = true + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end diff --git a/spec/zk.vim_spec.rb b/spec/zk.vim_spec.rb new file mode 100644 index 0000000..b0816ba --- /dev/null +++ b/spec/zk.vim_spec.rb @@ -0,0 +1,44 @@ +require 'vimrunner' + +vim = Vimrunner.start +vim.add_plugin(File.expand_path('../..', __FILE__), 'plugin/zkvim.vim') + +describe "zk.vim" do + after(:all) do + vim.kill + end + + describe "zkvim#create_link" do + it "returns a correctly-formatted link when given a title and id" do + expect(vim.command('echo zkvim#create_link("Title", "id")')).to eq("[Title]") + end + end + + describe "zkvim#create_append_link_command" do + it "inserts a link using a NORMAL mode append command" do + expect(vim.command('echo zkvim#create_append_link_command("Title", "id")')).to eq("normal! a[Title]") + end + end + + describe "zkvim#get_new_zettel_id" do + it "returns the pathname of a new zettel" do + expect(vim.command('echo zkvim#get_new_zettel_id()')).to match("/home/viciouscirce/dox/zettelkasten//[[:digit:]]{12}") + end + end + + describe "zkvim#get_zettel_title" do + it "returns the title of the zettel when given a fzf selection" do + expect(vim.command('echo zkvim#get_zettel_title("/home/viciouscirce/dox/zettelkasten//20230527091457 | # Ruby")')).to eq("Ruby") + end + end + + describe "zkvim#get_zettel_id" do + it "returns the id of the zettel when given a fzf selection" do + expect(vim.command('echo zkvim#get_zettel_id("/home/viciouscirce/dox/zettelkasten//20230527091457 | # Ruby")')).to eq("20230527091457") + end + + it "returns the id of the zettel when given a zettel filepath" do + expect(vim.command('echo zkvim#get_zettel_id("/home/viciouscirce/dox/zettelkasten//20230527091457 ")')).to eq("20230527091457") + end + end +end