ZeroBraneStudio icon indicating copy to clipboard operation
ZeroBraneStudio copied to clipboard

Code snippets manager

Open Witcher6 opened this issue 8 years ago • 5 comments

This feature is implemented in many IDEs. And Lua people need it too!

And there are 2 main use-case scenarios:

  1. Typing keyword and then pressing tab/space/enter like with auto-completion, but instead of variable whole chunk of code appears. :fn (TAB) ->
local function _ ()
end
  1. Shortcut like Ctrl-P with pop-uping window, where you can scroll list of snippets and do simple search among them.

In discussing with Paulk, he has mentioned cool stuff like jumping right to empty spaces to fill without manual cursor moving (i.e. function names , params and etc.) Also, it'd be good to have easy way to add new snippets. I.E. select chunk of code -> shortcut -> place cursor waypoints -> you are ready to use new snippet.

Witcher6 avatar Jul 24 '17 21:07 Witcher6

I see several additional functions that may be needed to handle of the aspects of snippets:

  1. Ability to specify variables and their default values; for example: do_something('${1:this}', ${2:foo}, ${3:100}, ${4:{'${3:that}', '${5:something-else}'}}); will have 5 variables with different default values and one variable (3) begin used twice
  2. Ability to use the same variable in several places. An example of this is variable $3 in the previous snippet; when its value is updated in one location, it's updated in others as well using multi-cursor, but only for the first time when visited.
  3. Ability to change values for variables before expanding. This can be used with abc var1 var2TAB to load snippet named abc and to set variables 1 and to to var1 and var2 values.
  4. Ability to specify current selection to wrap it into the code in the snippet. For example, abc$SELECTION$def will generate abcCODEdef if CODE is the current selection. Also see #257.
  5. Ability to specify where the cursor should be positioned after insertion.
  6. Ability to support TAB expansion when typed and commandbar search; the latter option allows searching for specific snippet in the command bar using fuzzy search, similar to file and symbol searches. The search results will show the name of the snippet and its code and will insert and expand it upon selection.
  7. Indentation support. Using tabulation (\t) in code snippets would replace it with the correct indentation currently used in the file; using spaces will keep things as is in the snippet. The indentation is always relative: all the lines in the code snippet get the indentation based on the existing indentation of the first line when the snippet is inserted.

Other nice-to-have features:

  • Ability to link a snippet to a shortcut (although less of a need if commandbar snippet search is provided)
  • Ability to designate scope for snippets: ifelse for Lua and for C should produce different results. This can be done by specifying a spec or extension that the snippet should be associated with.

pkulchenko avatar Jul 24 '17 22:07 pkulchenko

sounds like cool mini-language!

Witcher6 avatar Jul 24 '17 22:07 Witcher6

Just some thoughts on this based on other similar snippet engines I have seen.

$0 or ${0} is where the cursor ends up at the end

${1:this | or | that} would pop up a window and allow the user to choose from a list of predefined items. You could change it to ${choice: this | or | that} using the keyword choice to cause different functionality and it would be pretty obvious in the snippet itself what is going on. I write a lot of shell scripts and sometimes it's BASH and sometimes it's KSH or SH. So a snippet would look like this when I want a shebang line: #!/usr/bin/env ${choice: sh | ksh | bash}

A popup menu for snippet name completions. So if I have 3 snippets for if statements called if ife ifee and I typed if a menu would show up so I could choice one either after hitting tab to run the snippet or as I am typing. You could just have it pop up if a word meets a snippet name as well. I think VS Code gives you the name of the snippet and the short description.

sigzero avatar Oct 01 '20 14:10 sigzero

Just a thought on a snippet editor.

zerobrane_snippets

sigzero avatar Nov 17 '20 20:11 sigzero

Please checkout my snippets plugin Any feedbacks are welcome.

moteus avatar Sep 11 '21 14:09 moteus