Skip to content

Commit

Permalink
Merge pull request #1 from takapa/takapa-patch-1
Browse files Browse the repository at this point in the history
Support non-linked template values
  • Loading branch information
ainslec committed Jun 2, 2015
2 parents 8cc2082 + 4a0c45e commit 6390e7f
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions src/main/java/org/fife/ui/autocomplete/TemplateCompletion.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,32 +291,66 @@ private void parse(String template) {
char next = template.charAt(offs+1);
switch (next) {
case '$': // "$$" => escaped single dollar sign
addTemplatePiece(new TemplatePiece.Text(
template.substring(lastOffs, offs+1)));
addTemplatePiece(new TemplatePiece.Text(template.substring(lastOffs, offs+1)));
lastOffs = offs + 2;
break;
case '{': // "${...}" => variable
int closingCurly = template.indexOf('}', offs+2);
if (closingCurly>-1) {
addTemplatePiece(new TemplatePiece.Text(
template.substring(lastOffs, offs)));
String varName = template.substring(offs+2, closingCurly);
if (!"cursor".equals(varName) && isParamDefined(varName)) {
addTemplatePiece(new TemplatePiece.ParamCopy(varName));
int closingCurly = -2;

// Allow an escaped closing brace character in the 'varname' part of the template
// Here we check that a closing brace is not prefixed by a backslash
// NOTE :: It doesn't deal with \\ as a prefix to a non escaped '}' currently.

int scanStart = offs+2;
while (closingCurly == -2) {
closingCurly = template.indexOf('}', scanStart);
if (closingCurly != -1) {
if (template.charAt(closingCurly-1) == '\\') {
scanStart = closingCurly+1;
closingCurly = -2;
}
}
}

if (closingCurly > -1) {
final String textPriorToTemplateBlock = template.substring(lastOffs, offs);
final TemplatePiece.Text piece = new TemplatePiece.Text(textPriorToTemplateBlock);
addTemplatePiece(piece);

// Replace escaped rbrace (backslash rbrace) with just rbrace (in varname)
String varName = template.substring(offs+2, closingCurly).replace("\\}", "}");

// Use a colon as a signifier that the varname is a non-unified 'varname', so we
// can use the same initial text in autocomplete without unifying the text input
// based on the same initial key. Any varname which starts with colon is essentially
// marked as 'not a variable', and it means that we can substitute freeform text
// without the autocompleter linking user input based on the values matching (which
// is the current behaviour).

boolean isTabStopNotVarname = varName.startsWith(":");
varName = isTabStopNotVarname ? varName.substring(1) : varName;

if ( (!"cursor".equals(varName)) && isParamDefined(varName) && (!isTabStopNotVarname)) {
final TemplatePiece.ParamCopy piece2 = new TemplatePiece.ParamCopy(varName);
addTemplatePiece(piece2);
}
else {
addTemplatePiece(new TemplatePiece.Param(varName));
final TemplatePiece.Param piece2 = new TemplatePiece.Param(varName);
addTemplatePiece(piece2);
}
lastOffs = closingCurly + 1;
}


break;
}

}

if (lastOffs<template.length()) {
String text = template.substring(lastOffs);
addTemplatePiece(new TemplatePiece.Text(text));
final TemplatePiece.Text piece = new TemplatePiece.Text(text);
addTemplatePiece(piece);
}

}
Expand Down Expand Up @@ -381,4 +415,4 @@ public String toString() {
}


}
}

0 comments on commit 6390e7f

Please sign in to comment.