Skip to content

Commit

Permalink
Escape special characters within LSP snippets
Browse files Browse the repository at this point in the history
Signed-off-by: David Kwon <dakwon@redhat.com>
  • Loading branch information
dkwon17 authored and fbricon committed Aug 28, 2020
1 parent 55ac697 commit dc3db25
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static void placeholders(int index, String text, StringBuilder snippets)
snippets.append("${");
snippets.append(index);
snippets.append(":");
snippets.append(text);
snippets.append(escape(text));
snippets.append("}");
}

Expand Down Expand Up @@ -70,4 +70,8 @@ public static void choice(int index, Collection<String> values, StringBuilder sn
snippets.append("}");
}

private static String escape(String text) {
return text.replace("$", "\\$").replace("}", "\\}");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.eclipse.lsp4mp.services.MicroProfileAssert.testCompletionFor;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.eclipse.lsp4mp.commons.MicroProfileProjectInfo;
Expand Down Expand Up @@ -211,13 +212,13 @@ public void noCompletionForExistingProperties() throws BadLocationException {

projectInfo.setProperties(properties);

testCompletionFor(value, false, null, 2, projectInfo, c("quarkus.http.cors", "quarkus.http.cors=", r(0, 0, 0)),
testCompletionFor(value, false, 2, projectInfo, c("quarkus.http.cors", "quarkus.http.cors=", r(0, 0, 0)),
c("quarkus.application.name", "quarkus.application.name=", r(0, 0, 0)));

value = "quarkus.http.cors=false\r\n" + //
"|";

testCompletionFor(value, false, null, 1, projectInfo,
testCompletionFor(value, false, 1, projectInfo,
c("quarkus.application.name", "quarkus.application.name=", r(1, 0, 0)));

}
Expand All @@ -240,7 +241,7 @@ public void noCompletionForExistingPropertiesWithProfile() throws BadLocationExc

projectInfo.setProperties(properties);

testCompletionFor(value, false, null, 1, projectInfo,
testCompletionFor(value, false, 1, projectInfo,
c("quarkus.http.cors", "%prod.quarkus.http.cors=", r(1, 0, 6)));
}

Expand All @@ -260,21 +261,21 @@ public void completionForExistingPropertiesDifferentProfile() throws BadLocation

projectInfo.setProperties(properties);

testCompletionFor(value, false, null, 2, projectInfo, c("quarkus.http.cors", "quarkus.http.cors=", r(0, 0, 0)),
testCompletionFor(value, false, 2, projectInfo, c("quarkus.http.cors", "quarkus.http.cors=", r(0, 0, 0)),
c("quarkus.application.name", "quarkus.application.name=", r(0, 0, 0)));

value = "quarkus.http.cors=false\r\n" + //
"%dev.|";

testCompletionFor(value, false, null, 2, projectInfo,
testCompletionFor(value, false, 2, projectInfo,
c("quarkus.http.cors", "%dev.quarkus.http.cors=", r(1, 0, 5)),
c("quarkus.application.name", "%dev.quarkus.application.name=", r(1, 0, 5)));

value = "quarkus.http.cors=false\r\n" + //
"%dev.quarkus.application.name\r\n" + //
"%prod.|";

testCompletionFor(value, false, null, 2, projectInfo,
testCompletionFor(value, false, 2, projectInfo,
c("quarkus.http.cors", "%prod.quarkus.http.cors=", r(2, 0, 6)),
c("quarkus.application.name", "%prod.quarkus.application.name=", r(2, 0, 6)));

Expand All @@ -297,4 +298,42 @@ public void completionSpacingSurroundingEquals() throws BadLocationException {
c("quarkus.http.cors", "quarkus.http.cors = ${1|false,true|}", r(0, 0, 0)));
}

}
@Test
public void completionDefaultValueContainsDollarSign() throws BadLocationException {
MicroProfileProjectInfo projectInfo = new MicroProfileProjectInfo();
ItemMetadata metadata = new ItemMetadata();
metadata.setName("price.string");
metadata.setDefaultValue("Price: $10");
projectInfo.setProperties(Collections.singletonList(metadata));

String value = "|";
testCompletionFor(value, true, 1, projectInfo, c("price.string", "price.string=${0:Price: \\$10}", r(0, 0, 0)));
testCompletionFor(value, false, 1, projectInfo, c("price.string", "price.string=Price: $10", r(0, 0, 0)));
}

@Test
public void completionDefaultValueContainsBraces() throws BadLocationException {
MicroProfileProjectInfo projectInfo = new MicroProfileProjectInfo();
ItemMetadata metadata = new ItemMetadata();
metadata.setName("price.string");
metadata.setDefaultValue("Price: {10}");
projectInfo.setProperties(Collections.singletonList(metadata));

String value = "|";
testCompletionFor(value, true, 1, projectInfo, c("price.string", "price.string=${0:Price: {10\\}}", r(0, 0, 0)));
testCompletionFor(value, false, 1, projectInfo, c("price.string", "price.string=Price: {10}", r(0, 0, 0)));
}

@Test
public void completionDefaultValueContainsDollarSignAndBraces() throws BadLocationException {
MicroProfileProjectInfo projectInfo = new MicroProfileProjectInfo();
ItemMetadata metadata = new ItemMetadata();
metadata.setName("price.string");
metadata.setDefaultValue("Price: ${price}");
projectInfo.setProperties(Collections.singletonList(metadata));

String value = "|";
testCompletionFor(value, true, 1, projectInfo, c("price.string", "price.string=${0:Price: \\${price\\}}", r(0, 0, 0)));
testCompletionFor(value, false, 1, projectInfo, c("price.string", "price.string=Price: ${price}", r(0, 0, 0)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ public static void testCompletionFor(String value, boolean snippetSupport, boole
getDefaultMicroProfileProjectInfo(), expectedItems);
}

public static void testCompletionFor(String value, boolean snippetSupport, String fileURI, Integer expectedCount,
public static void testCompletionFor(String value, boolean snippetSupport, Integer expectedCount,
MicroProfileProjectInfo projectInfo, CompletionItem... expectedItems) throws BadLocationException {
testCompletionFor(value, snippetSupport, false, null, expectedCount, projectInfo, expectedItems);
}

public static void testCompletionFor(String value, boolean snippetSupport, String fileURI, Integer expectedCount,
MicroProfileProjectInfo projectInfo, CompletionItem... expectedItems) throws BadLocationException {
testCompletionFor(value, snippetSupport, false, fileURI, expectedCount, projectInfo, expectedItems);
}

public static void testCompletionFor(String value, boolean snippetSupport, boolean insertSpacing, String fileURI,
Integer expectedCount, MicroProfileProjectInfo projectInfo, CompletionItem... expectedItems)
throws BadLocationException {
Expand Down

0 comments on commit dc3db25

Please sign in to comment.