Skip to content

Commit

Permalink
Add assert & optional completion support
Browse files Browse the repository at this point in the history
Additions:
- Added support for adding an assert statement
- Added support to create Optional.ofNullable(..) call.
  • Loading branch information
MartinWitt committed Jun 10, 2023
1 parent 20e5b1b commit 8a1e173
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@
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),
FOR(PostfixPreferences.FOR_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.FOR_CONTENT, PostfixPreferences.FOR_DESCRIPTION),
FORI(PostfixPreferences.FORI_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.FORI_CONTENT, PostfixPreferences.FORI_DESCRIPTION),
FORR(PostfixPreferences.FORR_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.FORR_CONTENT, PostfixPreferences.FORR_DESCRIPTION),
NNULL(PostfixPreferences.NNULL_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.NNULL_CONTENT, PostfixPreferences.NNULL_DESCRIPTION),
NULL(PostfixPreferences.NULL_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.NULL_CONTENT, PostfixPreferences.NULL_DESCRIPTION),
NULL(PostfixPreferences.NULL_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.NULL_CONTENT,PostfixPreferences.NULL_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),
SYSERR(PostfixPreferences.SYSERR_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.SYSERR_CONTENT, PostfixPreferences.SYSERR_DESCRIPTION),
THROW(PostfixPreferences.THROW_ID, JavaPostfixContextType.ID_ALL, PostfixPreferences.THROW_CONTENT, PostfixPreferences.THROW_DESCRIPTION),
Expand Down Expand Up @@ -55,6 +57,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 @@ -63,13 +66,15 @@ class PostfixPreferences {
public static final String IF_ID = "org.eclipse.jdt.ls.postfixcompletion.if";
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 OPT_ID = "org.eclipse.jdt.postfixcompletion.opt";
public static final String SYSOUT_ID = "org.eclipse.jdt.postfixcompletion.sysout";
public static final String SYSERR_ID = "org.eclipse.jdt.postfixcompletion.syserr";
public static final String THROW_ID = "org.eclipse.jdt.postfixcompletion.throw";
public static final String VAR_ID = "org.eclipse.jdt.postfixcompletion.var";
public static final String WHILE_ID = "org.eclipse.jdt.postfixcompletion.while";

// Default Contents
public static final String ASSERT_CONTENT = "assert ${i:inner_expression(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 @@ -92,6 +97,7 @@ class PostfixPreferences {
public static final String NULL_CONTENT = "if (${i:inner_expression(java.lang.Object,array)} == null) {\n" +
"\t$${0}\n" +
"}";
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 SYSERR_CONTENT = "System.err.println(${i:inner_expression(java.lang.Object)}${});$${0}";
public static final String THROW_CONTENT = "throw ${true:inner_expression(java.lang.Throwable)};";
Expand All @@ -101,6 +107,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 @@ -109,6 +116,7 @@ class PostfixPreferences {
public static final String IF_DESCRIPTION = "Creates a if statement";
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 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 SYSERR_DESCRIPTION = "Sends the affected object to a System.err.println(..) call";
public static final String THROW_DESCRIPTION = "Throws the given Exception";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,33 @@ 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 +350,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_sysout() throws JavaModelException {
//@formatter:off
Expand Down

0 comments on commit 8a1e173

Please sign in to comment.