Skip to content
Open
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 @@ -29,7 +29,9 @@
import javax.swing.text.JTextComponent;
import com.sun.source.tree.CaseTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.EnhancedForLoopTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.ForLoopTree;
import com.sun.source.util.SourcePositions;
import com.sun.source.util.TreePath;
import java.util.Set;
Expand Down Expand Up @@ -144,7 +146,8 @@ public void run(ResultIterator resultIterator) throws Exception {
}
case FOR_LOOP:
case ENHANCED_FOR_LOOP:
if (!isRightParenthesisOfLoopPresent(controller, so)) {
if (!isInLoopBody(controller, tree, so)
&& !isRightParenthesisOfLoopPresent(controller, so)) {
treeKindCtx = null;
}
break;
Expand Down Expand Up @@ -177,6 +180,18 @@ private boolean isRightParenthesisOfLoopPresent(CompilationController controller
}
return false;
}

private boolean isInLoopBody(CompilationController controller, Tree loop, int abbrevStartOffset) {
Tree statement;
if (loop.getKind() == Tree.Kind.FOR_LOOP) {
statement = ((ForLoopTree) loop).getStatement();
} else {
statement = ((EnhancedForLoopTree) loop).getStatement();
}
long statementStart = controller.getTrees().getSourcePositions()
.getStartPosition(controller.getCompilationUnit(), statement);
return statementStart >= 0 && abbrevStartOffset >= statementStart;
}

private TokenId skipNextWhitespaces(TokenSequence<?> tokenSequence) {
TokenId tokenId = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.netbeans.lib.editor.codetemplates.CodeTemplateInsertHandler;
import org.netbeans.lib.editor.codetemplates.api.CodeTemplate;
import org.netbeans.lib.editor.codetemplates.api.CodeTemplateManager;
import org.netbeans.lib.editor.codetemplates.spi.CodeTemplateFilter;
import org.netbeans.lib.editor.codetemplates.storage.CodeTemplateSettingsImpl.OnExpandAction;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
Expand Down Expand Up @@ -203,6 +204,60 @@ public void testCodeTemplatesShouldWorkInsideParenthesesOfWhileLoop() throws Exc
"}");
}

public void testSoutTemplateIsAcceptedInUnbracedForLoopBody() throws Exception {
assertSoutTemplateAccepted("public class Test {\n"
+ " private void test() {\n"
+ " for (int i = 0; i < 3; i++)\n"
+ " sout\n"
+ " }\n"
+ "}", true);
Comment on lines +208 to +213

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick: feel free to use multi line Strings. The editor has a hint to convert to it.

(same for the other new test cases you added)

}

public void testSoutTemplateIsAcceptedInUnbracedEnhancedForLoopBody() throws Exception {
assertSoutTemplateAccepted("public class Test {\n"
+ " private void test(String[] values) {\n"
+ " for (String value : values)\n"
+ " sout\n"
+ " }\n"
+ "}", true);
}

public void testSoutTemplateIsRejectedInIncompleteForLoopHeader() throws Exception {
assertSoutTemplateAccepted("public class Test {\n"
+ " private void test() {\n"
+ " for (int i = 0; sout\n"
+ " }\n"
+ "}", false);
}

private void assertSoutTemplateAccepted(String text, boolean expected) throws Exception {
clearWorkDir();
testFile = FileUtil.toFileObject(getWorkDir()).createData("Test.java");
EditorKit kit = new JavaKit();
JEditorPane pane = new JEditorPane();
SwingUtilities.invokeAndWait(() -> {
pane.setEditorKit(kit);
});
Document doc = pane.getDocument();
doc.putProperty(Document.StreamDescriptionProperty, testFile);
doc.putProperty(Language.class, JavaTokenId.language());
doc.putProperty("mimeType", "text/x-java");
pane.setText(text);
try (OutputStream out = testFile.getOutputStream();
Writer w = new OutputStreamWriter(out)) {
w.append(text);
}
CodeTemplate sout = CodeTemplateManager.get(doc).getCodeTemplates().stream()
.filter(template -> "sout".equals(template.getAbbreviation()))
.findFirst()
.orElseThrow();
int abbreviationOffset = text.indexOf("sout");
CodeTemplateFilter filter = new JavaCodeTemplateFilter.Factory()
.createFilter(doc, abbreviationOffset, -1);

assertEquals(expected, filter.accept(sout));
}

public void testInfiteLoop() throws Exception {
doTestTemplateInsert("for (${IT_TYPE rightSideType type=\"java.util.Iterator\" default=\"Iterator\" editable=false} ${IT newVarName default=\"it\"} = ${COL instanceof=\"java.util.Collection\" default=\"col\"}.iterator(); ${IT}.hasNext();) {\n" +
" ${TYPE rightSideType default=\"Object\"} ${ELEM newVarName default=\"elem\"} = ${TYPE_CAST cast default=\"\" editable=false}${IT}.next();\n" +
Expand Down
Loading