[IMPLEMENTATION DISCUSSION] Snippets
Dear all, I'd like to start a discussion where we can discuss the implementation detail of snippets in Zettlr. As there are some approaches to it, I would like to ignite a discussion on how to actually do it for the use cases Zettlr solves.
Here's my own first draft:
- Add a dialog just as for
Custom CSSor the
Tags, in the Zettlr or File menu respectively.
- In there you can add, remove, and update snippets. The snippets' content is powered by an instance of the main application's CodeMirror with the respective syntax highlighting. The names should be only lowercase letters [a-z0-9] w/ no special chars (makes it easier to reference them during writing and storing them). Have a look at Question 3 for why this would save me a lot of pain in implementing.
- In these snippets, you can use an ID variable, because this is obviously something necessary for integrating nicely with the Zettelkasten functionality.
- Apart from that, you can use tab-positions that work in the way as in popular code editors (VSCode, Atom, etc.) — these will be indicated by
$3, etc. and are highlighted appropriately. These enable you to press Tab to switch to the next position.
- Everytime a user types something, a plugin checks if it's a colon (
:). If it is, a dropdown opens with all snippets the user has created. Once the user picks a selection, the
ZettlrEditorwill pull in the corresponding snippets' contents. Having a certain character trigger the snippets rather than a fuzzy search within the snippet database will prevent hint-dropdowns popping up everywhere you type, which is a huge distraction.
- This content will be parsed to remove all tab-positions, which are then written to an array.
- The contents (with potential IDs replaced) will then be added to the cursor position. Now, whenever a user presses TAB, the cursor jumps to the next tab-position, until all tab-positions are removed from the array. Then TAB works normal again.
- Anything missing from this initial implementation? Please bear in mind that implementing this feature will indeed take a lot of time initially, with more easier addendums later on, so we shouldn't overload it in 1.5.
- Which actions abort going through all the tab-positions, clearing the full array immediately? (Such as using an arrow-key in Atom or VSCode)
- Do we save each snippet as a Markdown file in the format
<name>.mdinside the user folder, or all in one file? This might depend upon the expected sizes of these snippets. The former would make it easy to share the snippets with other users (maybe via this forum?).