📜 ⬆️ ⬇️

We write macros for TODO and FIXME in Sublime Text, or how a bit of code saves a lot of time

The great master fought with chaos. And the harder he fought, the more thoughts came to him. When thoughts of intelligent came, he wrote them down, preceding the magic word TODO. He also wrote down thoughts about a senseless one, but for such thoughts he had another magic word - FIXME. And I must say that from the Beginning of Time there was no more powerful spells for defeating Chaos than these two.



I propose to speculate on how to make your life easier and get a simple tool with hot keys to insert comments into the TODO and FIXME code in the popular Sublime Text editor.


What we want to get


We need to immediately determine what we want to get after calling our insert command. Suppose that the cursor is on some line of code for which we want to insert a TODO comment . You can add a comment to the end of our line. But this solution has two significant drawbacks. First, the line may be too long, and by inserting a comment, we can easily break the agreement on the maximum length of the line. Even if there is no such agreement, it will be inconvenient to work with such a string. Secondly, if we want to use the automatic generator of the TODO file , which contains a list of all the comments he encountered in the TODO and FIXME project with links to files and line numbers in them, in the process of building the application, it may be that he demands that the comments were at the beginning of the lines, as the todo-webpack-plugin does, which I use.


So, the comment should appear above the line to which we want to add the TODO . That is, after the launch, our team should:


  1. move the cursor to the beginning of the line
  2. insert blank line
  3. move the cursor to the beginning of this new line
  4. insert commentary characters for this type of file
    • // JS, SCSS .
    • # python .
    • <!-- HTML, XML -->
    • Etc. - somewhere and ';' is used.
  5. insert instruction TODO: and a space after it.

After completing these steps, the cursor will appear exactly where the description is appropriate.


We write a macro


To create our tool, it is logical to use the macro creation tools available in the editor. We simply turn on the recording of the macro, perform some actions in the editor, stop the recording, save it to a file and assign hotkeys for which our recorded sequence of actions — our macro — will be launched again when we need it.


Open any file with code and put the cursor on the line for which we want (or pretend that we want, and in fact just write a macro) add TODO . Turn on the record: Tools> Record Macro or Ctrl + Alt + Q. Dale act on the points discussed above.


  1. Home - translate the cursor to the beginning of the line.
  2. Enter - insert an empty string.
  3. Up arrow - put the cursor at the beginning of this empty line.
  4. Ctrl + / - insert a comment, using the command available in the editor, which correctly puts a comment according to the content of the code.
  5. Enter the text TODO: and the space after it.

Stop recording with the same keyboard shortcut Ctrl + Alt + Q or Tools> Stop Recording Macro . Further on Tools> Save Macro ... we open dialogue of record of a macro in a file. Set the file name InsertTodo , leaving the proposed extension .sublime-macro . Assign a hot key. To do this, open Preferences> Key Bindings . The Key Bindings - Default tab contains a list of all default keyboard shortcuts and commands associated with them. We need it to find an unallocated keyboard shortcut that would suit us. It would be convenient to use Ctrl + Shift + T , but this hotkey in Linux is intercepted by the system and opens the terminal. The combination of Ctrl + Shift + D is also used - duplicates the string. I stopped at Ctrl + Shift + = - it turned out to be free.


The “Key Bindings - User” tab is used to define custom keyboard shortcuts. There we prescribe our new combination:


 [ { "keys": ["Ctrl+shift+="], "command": "run_macro_file", "args": {"file": "Packages/User/InsertTodo.sublime-macro"} } ] 

This is if the file was empty. If it already contains something other than brackets, we put a comma there after the last curly brace, and from the next line add our object:


 [ // -... { //   }, //    { "keys": ["Ctrl+shift+="], "command": "run_macro_file", "args": {"file": "Packages/User/InsertTodo.sublime-macro"} } ] 

I think that the meaning of this record is quite clear. The key key stores the key combination. command defines the command that will be launched by this combination. In our case, it is run_macro_file , which can run macros. args stores the arguments passed to the command. In our case, the path to the file of our macro is passed.


Save, close the window with the settings files and test what happened. We are convinced that everything works correctly both in Python , and in JS and in HTML .


What's inside


Now you can be curious and look at the InsertTodo.sublime-macro file itself :


 [ { "args": { "extend": false, "to": "bol" }, "command": "move_to" }, { "args": { "characters": "\n" }, "command": "insert" }, { "args": { "by": "lines", "forward": false }, "command": "move" }, { "args": { "block": false }, "command": "toggle_comment" }, { "args": { "characters": "T" }, "command": "insert" }, { "args": { "characters": "O" }, "command": "insert" }, { "args": { "characters": "D" }, "command": "insert" }, { "args": { "characters": "O" }, "command": "insert" }, { "args": { "characters": ":" }, "command": "insert" }, { "args": { "characters": " " }, "command": "insert" } ] 

Ay - not good! Each character of the string "TODO:" is output by its own command. Correct:


 [ { "args": { "extend": false, "to": "bol" }, "command": "move_to" }, { "args": { "characters": "\n" }, "command": "insert" }, { "args": { "by": "lines", "forward": false }, "command": "move" }, { "args": { "block": false }, "command": "toggle_comment" }, { "args": { "characters": "TODO: " }, "command": "insert" } ] 

We save and check that nothing is broken. Everything is working.


And now fixme


Build a similar tool to insert FIXME. To do this, create the InsertFixme.sublime-macro file in the same directory, and copy the contents of the InsertTodo.sublime-macro into it , transferring the last command "TODO:" to "FIXME:" :


 ... { "args": { "characters": "FIXME: " }, "command": "insert" } ] 

Bind hotkeys. The combination of Ctrl + Shift + - I was free.


 [ ... { "keys": ["Ctrl+shift+="], "command": "run_macro_file", "args": {"file": "Packages/User/InsertTodo.sublime-macro"} }, { "keys": ["Ctrl+shift+-"], "command": "run_macro_file", "args": {"file": "Packages/User/InsertFixme.sublime-macro"} } ] 

Everything. Now we are testing and rejoicing that we have such unpretentious tools at hand that, despite their simplicity, will help us to save a lot of our precious time!


References:




')

Source: https://habr.com/ru/post/349076/


All Articles