Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape special characters within LSP snippets #29

Merged
merged 1 commit into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,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 @@ -67,4 +67,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