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

Add text selection support in code snippet #1222

Merged
merged 2 commits into from
Oct 22, 2019
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 @@ -20,13 +20,19 @@ public class SnippetUtils {

private static final String MARKDOWN_LANGUAGE = "java";

private static final String TM_SELECTED_TEXT = "\\$TM_SELECTED_TEXT";

private SnippetUtils() {
}

public static Either<String, MarkupContent> beautifyDocument(String raw) {
// remove the placeholder for the plain cursor like: ${0}, ${1:variable}
String escapedString = raw.replaceAll("\\$\\{\\d:?(.*?)\\}", "$1");

// Replace the reserved variable with empty string.
// See: https://github.com/eclipse/eclipse.jdt.ls/issues/1220
escapedString = escapedString.replaceAll(TM_SELECTED_TEXT, "");

if (JavaLanguageServerPlugin.getPreferencesManager() != null && JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences() != null
&& JavaLanguageServerPlugin.getPreferencesManager().getClientPreferences().isSupportsCompletionDocumentationMarkdown()) {
MarkupContent markupContent = new MarkupContent();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ class TemplatePreferences {
public static final String SYSOUT_CONTENT = "System.out.println($${0});";
public static final String SYSERR_CONTENT = "System.err.println($${0});";
public static final String SYSTRACE_CONTENT = "System.out.println(\"${enclosing_type}.${enclosing_method}()\");";
public static final String FOREACH_CONTENT = "for ($${1:${iterable_type}} $${2:${iterable_element}} : $${3:${iterable}}) {\n" + "\t$${0}\n" + "}";
public static final String FORI_CONTENT = "for ($${1:int} $${2:${index}} = $${3:0}; $${2:${index}} < $${4:${array}.length}; $${2:${index}}++) {\n" + "\t$${0}\n" + "}";
public static final String WHILE_CONTENT = "while ($${1:${condition:var(boolean)}}) {\n" + "\t$${0}\n" + "}";
public static final String DOWHILE_CONTENT = "do {\n" + "\t$${0}\n" + "} while ($${1:${condition:var(boolean)}});";
public static final String IF_CONTENT = "if ($${1:${condition:var(boolean)}}) {\n" + "\t$${0}\n" + "}";
public static final String FOREACH_CONTENT = "for ($${1:${iterable_type}} $${2:${iterable_element}} : $${3:${iterable}}) {\n" + "\t$$TM_SELECTED_TEXT$${0}\n" + "}";
public static final String FORI_CONTENT = "for ($${1:int} $${2:${index}} = $${3:0}; $${2:${index}} < $${4:${array}.length}; $${2:${index}}++) {\n" + "\t$$TM_SELECTED_TEXT$${0}\n" + "}";
public static final String WHILE_CONTENT = "while ($${1:${condition:var(boolean)}}) {\n" + "\t$$TM_SELECTED_TEXT$${0}\n" + "}";
public static final String DOWHILE_CONTENT = "do {\n" + "\t$$TM_SELECTED_TEXT$${0}\n" + "} while ($${1:${condition:var(boolean)}});";
public static final String IF_CONTENT = "if ($${1:${condition:var(boolean)}}) {\n" + "\t$$TM_SELECTED_TEXT$${0}\n" + "}";
public static final String IFELSE_CONTENT = "if ($${1:${condition:var(boolean)}}) {\n" + "\t$${2}\n" + "} else {\n" + "\t$${0}\n" + "}";
public static final String IFNULL_CONTENT = "if ($${1:${name:var}} == null) {\n" + "\t$${0}\n" + "}";
public static final String IFNOTNULL_CONTENT = "if ($${1:${name:var}} != null) {\n" + "\t$${0}\n" + "}";
public static final String IFNULL_CONTENT = "if ($${1:${name:var}} == null) {\n" + "\t$$TM_SELECTED_TEXT$${0}\n" + "}";
public static final String IFNOTNULL_CONTENT = "if ($${1:${name:var}} != null) {\n" + "\t$$TM_SELECTED_TEXT$${0}\n" + "}";

// Descriptions
public static final String SYSOUT_DESCRIPTION = "print to standard out";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,26 @@ public void testMultipleVariablesInput() {

assertEquals(result.getLeft(), expected);
}

@Test
public void testSelectedTextPlaceholder() {
ClientPreferences mockCapabilies = mock(ClientPreferences.class);
when(mockCapabilies.isSupportsCompletionDocumentationMarkdown()).thenReturn(Boolean.FALSE);
when(preferenceManager.getClientPreferences()).thenReturn(mockCapabilies);

//@formatter:off
String raw = "for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:args.length}; ${2:i}++) {\n" +
"\t$TM_SELECTED_TEXT${0}\n" +
"}";
//@formatter:on
Either<String, MarkupContent> result = SnippetUtils.beautifyDocument(raw);

//@formatter:off
String expected = "for (int i = 0; i < args.length; i++) {\n" +
"\t\n" +
"}";
//@formatter:on

assertEquals(result.getLeft(), expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,7 @@ public void testSnippet_array_foreach() throws JavaModelException {
CompletionItem item = items.get(0);
assertEquals("foreach", item.getLabel());
String insertText = item.getInsertText();
assertEquals("for (${1:String} ${2:string} : ${3:args}) {\n\t${0}\n}", insertText);
assertEquals("for (${1:String} ${2:string} : ${3:args}) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}

@Test
Expand All @@ -986,7 +986,7 @@ public void testSnippet_list_foreach() throws JavaModelException {
CompletionItem item = items.get(0);
assertEquals("foreach", item.getLabel());
String insertText = item.getInsertText();
assertEquals("for (${1:String} ${2:string} : ${3:args}) {\n\t${0}\n}", insertText);
assertEquals("for (${1:String} ${2:string} : ${3:args}) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}

@Test
Expand All @@ -1011,7 +1011,7 @@ public void testSnippet_array_fori() throws JavaModelException {
CompletionItem item = items.get(0);
assertEquals("fori", item.getLabel());
String insertText = item.getInsertText();
assertEquals("for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:args.length}; ${2:i}++) {\n\t${0}\n}", insertText);
assertEquals("for (${1:int} ${2:i} = ${3:0}; ${2:i} < ${4:args.length}; ${2:i}++) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}

@Test
Expand All @@ -1036,7 +1036,7 @@ public void testSnippet_while() throws JavaModelException {
CompletionItem item = items.get(1);
assertEquals("while", item.getLabel());
String insertText = item.getInsertText();
assertEquals("while (${1:con}) {\n\t${0}\n}", insertText);
assertEquals("while (${1:con}) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}

@Test
Expand All @@ -1061,7 +1061,7 @@ public void testSnippet_dowhile() throws JavaModelException {
CompletionItem item = items.get(0);
assertEquals("dowhile", item.getLabel());
String insertText = item.getInsertText();
assertEquals("do {\n\t${0}\n} while (${1:con});", insertText);
assertEquals("do {\n\t$TM_SELECTED_TEXT${0}\n} while (${1:con});", insertText);
}

@Test
Expand All @@ -1086,7 +1086,7 @@ public void testSnippet_if() throws JavaModelException {
CompletionItem item = items.get(5);
assertEquals("if", item.getLabel());
String insertText = item.getInsertText();
assertEquals("if (${1:con}) {\n\t${0}\n}", insertText);
assertEquals("if (${1:con}) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}

@Test
Expand Down Expand Up @@ -1136,7 +1136,7 @@ public void testSnippet_ifnull() throws JavaModelException {
CompletionItem item = items.get(0);
assertEquals("ifnull", item.getLabel());
String insertText = item.getInsertText();
assertEquals("if (${1:obj} == null) {\n\t${0}\n}", insertText);
assertEquals("if (${1:obj} == null) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}

@Test
Expand All @@ -1161,7 +1161,7 @@ public void testSnippet_ifnotnull() throws JavaModelException {
CompletionItem item = items.get(0);
assertEquals("ifnotnull", item.getLabel());
String insertText = item.getInsertText();
assertEquals("if (${1:obj} != null) {\n\t${0}\n}", insertText);
assertEquals("if (${1:obj} != null) {\n\t$TM_SELECTED_TEXT${0}\n}", insertText);
}

@Test
Expand Down