Live Templates - GavriYashar/Matlab-Editor-Plugin GitHub Wiki

Live Templates are editor commands you can design to insert predefined code. Here's an example for the command %this% (delivered within the package). When you type a command into the editor the string gets replaced by the predefined text. This predefined text may include variables depending on what you want to achieve. %this% was designed to insert the fully qualified name of the current class you're in (or function, or script).

LiveTemplates

Setup

If you where running the installer live templates should be inside your installation folder of MEP. If not open MEP with any archive viewer (e.g. 7zip) and copy Replacements folder in the same directory as MEP*.jar. Also make sure that feature.enableReplacements is set to true (see preferences).

If the startup doesn't add the pathes to the properties you have to set path.mepr.var=D\:\\<INSTALLDIR>\\Replacements\\Variables and path.mepr.rep=D\:\\<INSTALLDIR>\\Replacements

V1.38 / Matlab R2020a

In your default.properties-File you should change the charset.mepr to the desired characterset, use the Status-Bar to see what character encoding your LiveTemplates have. (matlab release notes)

How to use Live Templates

I've delivered some basic templates for you as examples. You can either insert them via the Viewer (see Viewer) or typing the command directly e.g. %THIS%.

Quick Search

CTRL + SPACE quick searches live templates when writing an command. When writing %TEMPL + CTRL + SPACE it will insert %MYTEMPLATE if exactly one template is found otherwise it will display matches in the command window like this:

    ======== found templates ========
	        MYTEMPLATE_ONE
	        MYTEMPLATE_TWO
    =================================

Viewer

The viewer is opened via ALT + INSERT command. You can search by text, or by tags you've given. Pressing Enter, or the "Insert" button will insert text at cursor position in currently active editor. UP or DOWN will select previous/next element in list.

Live Templates Viewer

The "Edit" button will open up the selected template in Matlab. The "New Action" button will create a new template.

Creating Templates

Open up the TemplateViewer (ALT + INSERT) and press the "New Action" button, and insert a name for your template (note templates are case insensitive). Pressing ok will create the Template with following content.

  %% MEPRBEGIN
  % One line description here please.
  %
  % Tags: Tag1, Tag2
  %
  % V1.0 | ${DATE} | ${AUTHORLONG} | Creation
  %% MEPREND
  %% FUNCHANDLEBEGIN
  $VAR_1$ ${@()questdlg('question?)','title','answer1','answer2','answer3')}
  %% FUNCHANDLEEND
  
  %% custom variables inside this live templates
  var1 = $VAR_1$

Block Comment

%% MEPRBEGIN and %% MEPREND are block comments, everything between these two lines will be ignored and not inserted. % One line description here please. the first line represents a short comment displayed in the LiveTemplateViewer to make it easier for you to find desired templates % Tags: defines a line to give the templates some tags to filter them in the LiveTemplateViewer

Function block

%% FUNCHANDLEBEGIN and %% FUNCHANDLEEND defines a function handle block. to work properly a function handle has to return a string (char). $VAR_1$ defines a variable (only for this template) for the return value of the declared function handle.

Variables

Variables are simple Matlab functions (they have to be executable to work properly). They also need to have exactly one input argument and one output argument as string (char).

As example: ${DATE} calls a variable found in <install dir of MEP>/Replacements/MEPV_DATE.m

    function txt = MESRV_DATE(txt)
    % returns date string as yyyy-mm-dd format
    %% VERSIONING
    %             Author: Andreas Justin,
    %      Creation date: 2014-06-11
    %             Matlab: 8.3.0.532 (R2014a)
    %  Required Products: -
    %
    %% REVISIONS
    % V1.0 | 2014-06-11 | Andreas Justin    | Ersterstellung
    %
    % See also MESRV_DATUM
    expr = '\$\{(DATE|DATUM)\}';
    if ~isempty(regexp(txt,expr,'once'))
        txt = regexprep(txt, expr, datestr(now,'yyyy-mm-dd'));
    end

You can even pass in variables when typing %MYCOMMAND(VAR1)%, all you have to do is extracting them as seen in MEPV_REV.m

expr = '\$\{(REV)\(?(\d*\.?\d*)\)?}'; % this will extract one variable passed in as ####.####
m = regexp(txt,expr,'tokens','once');
⚠️ **GitHub.com Fallback** ⚠️