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 assert & optional completion support #2697

Merged
merged 1 commit into from
Jun 13, 2023
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 @@ -17,6 +17,7 @@
public enum PostfixTemplate {

//@formatter:on
ASSERT(PostfixPreferences.ASSERT_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.ASSERT_CONTENT, PostfixPreferences.ASSERT_DESCRIPTION),
CAST(PostfixPreferences.CAST_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.CAST_CONTENT, PostfixPreferences.CAST_DESCRIPTION),
IF(PostfixPreferences.IF_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.IF_CONTENT, PostfixPreferences.IF_DESCRIPTION),
ELSE(PostfixPreferences.ELSE_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.ELSE_CONTENT, PostfixPreferences.ELSE_DESCRIPTION),
Expand All @@ -29,6 +30,7 @@ public enum PostfixTemplate {
NULL(PostfixPreferences.NULL_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.NULL_CONTENT, PostfixPreferences.NULL_DESCRIPTION),
NOT(PostfixPreferences.NOT_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.NOT_CONTENT,
PostfixPreferences.NOT_DESCRIPTION),
OPT(PostfixPreferences.OPT_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.OPT_CONTENT, PostfixPreferences.OPT_DESCRIPTION),
SYSOUT(PostfixPreferences.SYSOUT_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.SYSOUT_CONTENT, PostfixPreferences.SYSOUT_DESCRIPTION),
SYSOUF(PostfixPreferences.SYSOUF_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.SYSOUF_CONTENT,
PostfixPreferences.SYSOUF_DESCRIPTION),
Expand Down Expand Up @@ -65,6 +67,7 @@ public String getId() {

class PostfixPreferences {
// IDs
public static final String ASSERT_ID = "org.eclipse.jdt.postfixcompletion.assert";
public static final String CAST_ID = "org.eclipse.jdt.postfixcompletion.cast";
public static final String ELSE_ID = "org.eclipse.jdt.ls.postfixcompletion.else";
public static final String FOR_ID = "org.eclipse.jdt.postfixcompletion.for";
Expand All @@ -75,6 +78,7 @@ class PostfixPreferences {
public static final String NNULL_ID = "org.eclipse.jdt.postfixcompletion.nnull";
public static final String NULL_ID = "org.eclipse.jdt.postfixcompletion.null";
public static final String NOT_ID = "org.eclipse.jdt.postfixcompletion.not";
public static final String OPT_ID = "org.eclipse.jdt.postfixcompletion.opt";
public static final String SYSOUT_ID = "org.eclipse.jdt.postfixcompletion.sysout";
public static final String SYSOUTV_ID = "org.eclipse.jdt.postfixcompletion.sysoutv";
public static final String SYSOUF_ID = "org.eclipse.jdt.postfixcompletion.sysouf";
Expand All @@ -85,6 +89,7 @@ class PostfixPreferences {
public static final String WHILE_ID = "org.eclipse.jdt.postfixcompletion.while";

// Default Contents
public static final String ASSERT_CONTENT = "assert ${i:inner_expression(boolean,java.lang.Boolean)};";
public static final String CAST_CONTENT = "(($${1})${inner_expression})$${0}";
public static final String ELSE_CONTENT = "if (!${i:inner_expression(boolean)}) {\n" +
"\t$${0}\n" +
Expand All @@ -109,6 +114,7 @@ class PostfixPreferences {
"\t$${0}\n" +
"}";
public static final String NOT_CONTENT = "!${i:inner_expression(boolean)}${}";
public static final String OPT_CONTENT = "Optional.ofNullable(${i:inner_expression(java.lang.Object)}${})";
public static final String SYSOUT_CONTENT = "System.out.println(${i:inner_expression(java.lang.Object)}${});$${0}";
public static final String SYSOUTV_CONTENT = "System.out.println(\"${i:inner_expression(java.lang.Object)}${} = \" + ${i:inner_expression(java.lang.Object)}${});$${0}";
public static final String SYSOUF_CONTENT = "System.out.printf(\"\", ${i:inner_expression(java.lang.Object)}${});$${0}";
Expand All @@ -121,6 +127,7 @@ class PostfixPreferences {
"}";

// Descriptions
public static final String ASSERT_DESCRIPTION = "Creates an assert statement";
public static final String CAST_DESCRIPTION = "Casts the expression to a new type";
public static final String ELSE_DESCRIPTION = "Creates a negated if statement";
public static final String FOR_DESCRIPTION = "Creates a for statement";
Expand All @@ -131,6 +138,7 @@ class PostfixPreferences {
public static final String NNULL_DESCRIPTION = "Creates an if statement and checks if the expression does not resolve to null";
public static final String NULL_DESCRIPTION = "Creates an if statement which checks if expression resolves to null";
public static final String NOT_DESCRIPTION = "Negates the expression";
public static final String OPT_DESCRIPTION = "Creates an Optional.ofNullable(..) call";
public static final String SYSOUT_DESCRIPTION = "Sends the affected object to a System.out.println(..) call";
public static final String SYSOUTV_DESCRIPTION = "Sends the affected object to a System.out.println(..) call";
public static final String SYSOUF_DESCRIPTION = "Sends the affected object to a System.out.printf(..) call";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,32 @@ public void testCastLazyResolve() throws JavaModelException {
}
}

@Test
public void test_assert() throws JavaModelException {
//@formatter:off
ICompilationUnit unit = getWorkingCopy(
"src/org/sample/Test.java",
"package org.sample;\n" +
"public class Test {\n" +
" public void testMethod(Boolean identifier) {\n" +
" identifier.assert" +
" }\n" +
"}"
);
//@formatter:on
CompletionList list = requestCompletions(unit, "identifier.assert");

assertNotNull(list);

List<CompletionItem> items = new ArrayList<>(list.getItems());
CompletionItem item = items.get(0);
assertEquals("assert", item.getLabel());
assertEquals(item.getInsertText(), "assert identifier;");
assertEquals(item.getInsertTextFormat(), InsertTextFormat.Snippet);
Range range = item.getAdditionalTextEdits().get(0).getRange();
assertEquals(new Range(new Position(3, 2), new Position(3, 19)), range);
}

@Test
public void test_cast() throws JavaModelException {
when(preferenceManager.getClientPreferences().isCompletionItemLabelDetailsSupport()).thenReturn(true);
Expand Down Expand Up @@ -323,6 +349,32 @@ public void test_null() throws JavaModelException {
assertEquals(new Range(new Position(3, 2), new Position(3, 8)), range);
}

@Test
public void test_opt() throws JavaModelException {
//@formatter:off
ICompilationUnit unit = getWorkingCopy(
"src/org/sample/Test.java",
"package org.sample;\n" +
"public class Test {\n" +
" public void testMethod(Object identifier) {\n" +
" identifier.opt" +
" }\n" +
"}"
);
//@formatter:on
CompletionList list = requestCompletions(unit, "identifier.opt");

assertNotNull(list);

List<CompletionItem> items = new ArrayList<>(list.getItems());
CompletionItem item = items.get(0);
assertEquals("opt", item.getLabel());
assertEquals(item.getInsertText(), "Optional.ofNullable(identifier)");
assertEquals(item.getInsertTextFormat(), InsertTextFormat.Snippet);
Range range = item.getAdditionalTextEdits().get(0).getRange();
assertEquals(new Range(new Position(3, 2), new Position(3, 16)), range);
}

@Test
public void test_not() throws JavaModelException {
//@formatter:off
Expand Down