Skip to content

Example: Code Templates

bobbylight edited this page Sep 5, 2021 · 2 revisions

A "code template" is a kind of macro for commonly-typed code. It associates a short identifier with a longer code snippet. When code templates are enabled and a short identifier is typed, followed by Ctrl+Shift+Space, it is replaced with the corresponding longer code snippet. Code templates are shared among all RSyntaxTextAreas in an application. Adding and removing of code templates is done through the installed CodeTemplateManager.

RSyntaxTextArea comes with a StaticCodeTemplate concrete implementation that inserts static text before and/or after the current caret position. You can also create and use your own custom CodeTemplate implementations.

Note that, for a more powerful version of this feature, consider using TemplateCompletions from the AutoComplete library (todo: write an example of that in that project).

Below is an example of using code templates:

import java.awt.*;
import javax.swing.*;

import org.fife.ui.rtextarea.*;
import org.fife.ui.rsyntaxtextarea.*;
import org.fife.ui.rsyntaxtextarea.templates.*;

/**
 * A simple example showing how to add and use Code Templates to an
 * RSyntaxTextArea.<p>
 * 
 * A "code template" is a kind of macro for commonly-typed code. It associates a
 * short identifier with a longer code snippet. When code templates are enabled
 * and a short identifier is typed, it is replaced with the corresponding longer
 * code snippet. Type Ctrl+Shift+Space to insert the template.<p>
 * 
 * This example uses RSyntaxTextArea 2.5.6.
 */
public class CodeTemplateDemo extends JFrame {

   private static final long serialVersionUID = 1L;

   public CodeTemplateDemo() {

      JPanel cp = new JPanel(new BorderLayout());

      RSyntaxTextArea textArea = new RSyntaxTextArea(20, 60);
      textArea.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_JAVA);
      textArea.setCodeFoldingEnabled(true);
      RTextScrollPane sp = new RTextScrollPane(textArea);
      cp.add(sp);

      // Whether templates are enabled is a global property affecting all
      // RSyntaxTextAreas, so this method is static.
      RSyntaxTextArea.setTemplatesEnabled(true);

      // Code templates are shared among all RSyntaxTextAreas. You add and
      // remove templates through the shared CodeTemplateManager instance.
      CodeTemplateManager ctm = RSyntaxTextArea.getCodeTemplateManager();

      // StaticCodeTemplates are templates that insert static text before and
      // after the current caret position. This template is basically shorthand
      // for "System.out.println(".
      CodeTemplate ct = new StaticCodeTemplate("sout", "System.out.println(", null);
      ctm.addTemplate(ct);

      // This template is for a for-loop. The caret is placed at the upper
      // bound of the loop.
      ct = new StaticCodeTemplate("fb", "for (int i=0; i<", "; i++) {\n\t\n}\n");
      ctm.addTemplate(ct);

      setContentPane(cp);
      setTitle("CodeTemplate Demo");
      setDefaultCloseOperation(EXIT_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);

   }

   public static void main(String[] args) {
      // Start all Swing applications on the EDT.
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            new CodeTemplateDemo().setVisible(true);
         }
      });
   }

}

When running the above program, if you type "sout", then press Ctrl+Shift+space, "sout" will be replaced by "System.out.println(". Similarly, typing "fb" followed by Ctrl+Shift+space will change "fb" into a basic for-loop structure.