Daftar Otomatis Kelanjutan dan Pemformatan untuk Neovim, Didukung oleh Lua
Pertanyaan ini dapat ditafsirkan dalam dua cara. Mengapa saya membuat Autolist, dan mengapa Anda harus menggunakan Autolist.
Kedengarannya sederhana, tapi yang saya inginkan hanyalah daftar plugin lanjutan di Lua. Bullets.vim bekerja, tetapi ditulis dalam Vimscript dan panjangnya lebih dari seribu baris. Tak perlu dikatakan, saya tidak dapat menemukan yang cocok, jadi saya memutuskan untuk membuat sendiri.
File autolist.nvim's
relatif kecil, dengan file lengkap dengan komentar dan pemformatan. Ini berusaha untuk menjadi minimal mungkin, sambil menerapkan fungsionalitas dasar daftar otomatis, dan mengimplementasikan penilaian ulang konteks/penandaan entri daftar, untuk mengalihkan pikiran dari pemformatan, dan membuatnya bekerja di latar belakang saat Anda menuliskan pikiran Anda.
Ini menggunakan Lazy.nvim, tetapi Anda dapat menyesuaikannya dengan manajer paket lainnya juga:
{
" gaoDean/autolist.nvim " ,
ft = {
" markdown " ,
" text " ,
" tex " ,
" plaintex " ,
" norg " ,
},
config = function ()
require ( " autolist " ). setup ()
vim . keymap . set ( " i " , " <tab> " , " <cmd>AutolistTab<cr> " )
vim . keymap . set ( " i " , " <s-tab> " , " <cmd>AutolistShiftTab<cr> " )
-- vim.keymap.set("i", "<c-t>", "<c-t><cmd>AutolistRecalculate<cr>") -- an example of using <c-t> to indent
vim . keymap . set ( " i " , " <CR> " , " <CR><cmd>AutolistNewBullet<cr> " )
vim . keymap . set ( " n " , " o " , " o<cmd>AutolistNewBullet<cr> " )
vim . keymap . set ( " n " , " O " , " O<cmd>AutolistNewBulletBefore<cr> " )
vim . keymap . set ( " n " , " <CR> " , " <cmd>AutolistToggleCheckbox<cr><CR> " )
vim . keymap . set ( " n " , " <C-r> " , " <cmd>AutolistRecalculate<cr> " )
-- cycle list types with dot-repeat
vim . keymap . set ( " n " , " <leader>cn " , require ( " autolist " ). cycle_next_dr , { expr = true })
vim . keymap . set ( " n " , " <leader>cp " , require ( " autolist " ). cycle_prev_dr , { expr = true })
-- if you don't want dot-repeat
-- vim.keymap.set("n", "<leader>cn", "<cmd>AutolistCycleNext<cr>")
-- vim.keymap.set("n", "<leader>cp", "<cmd>AutolistCycleNext<cr>")
-- functions to recalculate list on edit
vim . keymap . set ( " n " , " >> " , " >><cmd>AutolistRecalculate<cr> " )
vim . keymap . set ( " n " , " << " , " <<<cmd>AutolistRecalculate<cr> " )
vim . keymap . set ( " n " , " dd " , " dd<cmd>AutolistRecalculate<cr> " )
vim . keymap . set ( " v " , " d " , " d<cmd>AutolistRecalculate<cr> " )
end ,
},
-|+|*
atau 1.|2.|3.
)enter
/ return
dan entri daftar baru akan dibuat secara otomatisenter
/ return
pada entri daftar kosong akan menghapusnya, meninggalkan Anda dengan kalimat baru yang segar. - [x] checkboxes can be toggled with ` :AutolistToggleCheckbox ` , which is bound to ` return ` in normal mode if you used the default mappings
1 . [x] these can also be numbered
a) [ ] or these can work too
b) [ x ] see?
I. Roman numerals are also supported
II. Just press enter, and autolist will do the calculations for you
MX. All the way up
MXI. to infinity
MXII. It really will continue forever
MXIII. -I think
- you can cycle the type of the list with ` :AutolistCycleNext ` and ` :AutolistCyclePrev `
- below is a copy of this list after cycling twice
1 . you can cycle the type of the list with ` :AutolistCycleNext ` and ` :AutolistCyclePrev `
2 . below is a copy of this list after cycling twice
local list_patterns = {
neorg_1 = " %- " ,
neorg_2 = " %-%- " ,
neorg_3 = " %-%-%- " ,
neorg_4 = " %-%-%-%- " ,
neorg_5 = " %-%-%-%-%- " ,
unordered = " [-+*] " , -- - + *
digit = " %d+[.)] " , -- 1. 2. 3.
ascii = " %a[.)] " , -- a) b) c)
roman = " %u*[.)] " , -- I. II. III.
latex_item = " \ item " ,
}
local default_config = {
enabled = true ,
colon = { -- if a line ends in a colon
indent = true , -- if in list and line ends in `:` then create list
indent_raw = true , -- above, but doesn't need to be in a list to work
preferred = " - " , -- what the new list starts with (can be `1.` etc)
},
cycle = { -- Cycles the list type in order
" - " , -- whatever you put here will match the first item in your list
" * " , -- for example if your list started with a `-` it would go to `*`
" 1. " , -- this says that if your list starts with a `*` it would go to `1.`
" 1) " , -- this all leverages the power of recalculate.
" a) " , -- i spent many hours on that function
" I. " , -- try it, change the first bullet in a list to `a)`, and press recalculate
},
lists = { -- configures list behaviours
-- Each key in lists represents a filetype.
-- The value is a table of all the list patterns that the filetype implements.
-- See how to define your custom list below in the readme.
-- You must put the file name for the filetype, not the file extension
-- To get the "file name", it is just =:set filetype?= or =:se ft?=.
markdown = {
list_patterns . unordered ,
list_patterns . digit ,
list_patterns . ascii , -- for example this specifies activate the ascii list
list_patterns . roman , -- type for markdown files.
},
text = {
list_patterns . unordered ,
list_patterns . digit ,
list_patterns . ascii ,
list_patterns . roman ,
},
norg = {
list_patterns . neorg_1 ,
list_patterns . neorg_2 ,
list_patterns . neorg_3 ,
list_patterns . neorg_4 ,
list_patterns . neorg_5 ,
},
tex = { list_patterns . latex_item },
plaintex = { list_patterns . latex_item },
},
checkbox = {
left = " %[ " , -- the left checkbox delimiter (you could change to "%(" for brackets)
right = " %] " , -- the right checkbox delim (same customisation as above)
fill = " x " , -- if you do the above two customisations, your checkbox could be (x) instead of [x]
},
-- this is all based on lua patterns, see "Defining custom lists" for a nice article to learn them
}
Berikut semua fungsi publik:
AutolistNewBullet
: Menambahkan peluru baru di baris saat iniAutolistRecalculate
: Hitung ulang daftar yang dipesanAutolistToggleCheckbox
: Mengaktifkan kotak centang pada baris saat iniAutolistCycleNext
: Siklus Daftar Jenis ke depan sesuai dengan config.cycle
AutolistCyclePrev
: di atas, tapi mundurAutolistTab
: Memiliki casing penggunaan khusus di mana Anda ingin mengindir daftar saat Anda menekan tab. Jika Anda ingin menindas daftar saat Anda menekan <ct>
, Anda hanya perlu memetakan <ct>
ke AutolistRecalculate
karena AutolistTab
pada dasarnya mengubah tab menjadi <ct>
dengan penghitungan ulang.AutolistShiftTab
: Hal yang sama seperti di atas, digunakan untuk mereplikasi pengolah kata. Pemetaan <s-tab>
untuk ini akan mengubahnya menjadi <cd>AutolistRecalculate
. Semua fungsi yang dijelaskan di atas memiliki rekan LUA yang hanyalah versi kasus ular dari perintah kasus pascal. Misalnya AutolistNewBullet
memiliki case snake require("autolist").new_bullet()
Ada dua fungsi khusus untuk LUA: require("autolist").cycle_next_dr
dan require("autolist").cycle_prev_dr
, yang menyediakan versi AutolistCycleNext
dan AutolistCyclePrev
.
Singkatnya, yang perlu Anda lakukan adalah membuat kecocokan pola LUA yang memungkinkan Autolist untuk menemukan penanda daftar baru Anda.
Berikut adalah artikel yang tidak banyak tentang pola LUA, tetapi Anda dapat menemukan contoh untuk pola-pola ini di bagian Pola yang dimuat sebelumnya.
Inilah cara menentukan daftar kustom Anda:
local my_list_patterns = {
test = " %a[.)] "
}
require ( ' autolist ' ). setup ({
lists = {
markdown = {
" %a[.)] " , -- insert your custom lua pattern here
my_list_patterns . test , -- or use a variable
},
},
}
})
Sekarang pola LUA Anda (dalam hal ini %a[.)]
Yang cocok dengan daftar ASCII) akan diterapkan pada file penurunan harga.
Apakah ia memiliki pemetaan untuk mengotak -atik kotak centang seperti Bullets.VIM memiliki? Ya.
Apakah itu mendukung daftar kotak centang? Ya.
Menemukan bahwa plugin pecah saat Anda menggunakan Autolist? Lihat #43. Pada dasarnya Anda perlu memastikan bahwa autolist memuat setelah semua plugin lainnya. Jika itu tidak berhasil, jangan ragu untuk membuat masalah baru. Juga, pastikan bahwa kapitalisasi pemetaan Anda benar, atau Autolist tidak akan mendeteksi plugin lain ( <cr>
harus <CR>
).
terinspirasi oleh intisari ini
"Semua perangkat lunak menambahkan fitur sampai sangat rumit. Kemudian digantikan oleh solusi" lebih sederhana "yang menambahkan fitur sampai persis sama rumitnya."
Mencari kontributor karena saya memiliki pekerjaan sekolah yang berarti saya terkadang tidak dapat mengimbangi masalah