Skip to content

Commit

Permalink
Fix for issue #288: editor focus and selection after method completion
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed May 7, 2017
1 parent be25d6f commit 0426dfb
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 389 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,25 @@ import org.eclipse.jdt.internal.ui.javaeditor.JavaEditor
import org.eclipse.jdt.internal.ui.javaeditor.JavaSourceViewer
import org.eclipse.jdt.internal.ui.text.java.AbstractJavaCompletionProposal
import org.eclipse.jdt.internal.ui.text.java.LazyGenericTypeProposal
import org.eclipse.jdt.ui.PreferenceConstants
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposal
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext
import org.eclipse.jface.text.Document
import org.eclipse.jface.text.IDocument
import org.eclipse.jface.text.contentassist.ICompletionProposal
import org.junit.After

/**
* Includes utilities to help with all Content assist tests.
*/
abstract class CompletionTestSuite extends GroovyEclipseTestSuite {

@After
final void tearDownCompletionTestCase() {
setJavaPreference(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS, '')
}

protected ICompletionProposal[] performContentAssist(ICompilationUnit unit, int offset, Class<? extends IJavaCompletionProposalComputer> computerClass) {
waitForIndex()
JavaEditor editor = openInEditor(unit)
Expand Down Expand Up @@ -242,7 +249,7 @@ abstract class CompletionTestSuite extends GroovyEclipseTestSuite {

protected ICompletionProposal[] createProposalsAtOffset(String contents, String javaContents, int completionOffset) {
if (javaContents != null) {
addJavaSource("public class JavaClass { }\n" + javaContents, "JavaClass", "")
addJavaSource("public class JavaClass { }\n" + javaContents, "JavaClass")
}
def gunit = addGroovySource(contents, nextUnitName())
return createProposalsAtOffset(gunit, completionOffset)
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,91 +23,90 @@ import org.junit.Test
*/
final class InnerTypeCompletionTests extends CompletionTestSuite {

private static final String XXX = "xxx"
private static final String HTML = "HTML"
private static final String HTML_PROPOSAL = "HTML - javax.swing.text.html"
private static final String HTML = 'HTML'
private static final String HTML_PROPOSAL = 'HTML - javax.swing.text.html'

@Test
void testCompletionInInnerClass1() {
String contents = "class Outer { class Inner { \nHTML\n } }"
void testInInnerClass1() {
String contents = 'class Outer { class Inner { \nHTML\n } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, HTML))
proposalExists(proposals, HTML_PROPOSAL, 1)
}

@Test
void testCompletionInInnerClass2() {
String contents = "class Outer { class Inner { def x(HTML) { } } }"
void testInInnerClass2() {
String contents = 'class Outer { class Inner { def x(HTML) { } } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, HTML))
proposalExists(proposals, HTML_PROPOSAL, 1)
}

@Test
void testCompletionInInnerClass3() {
String contents = "class Outer { class Inner extends HTML { } }"
void testInInnerClass3() {
String contents = 'class Outer { class Inner extends HTML { } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, HTML))
proposalExists(proposals, HTML_PROPOSAL, 1)
}

@Test
void testCompletionInInnerClass4() {
String contents = "class Outer { class Inner { def x() { HTML } } }"
void testInInnerClass4() {
String contents = 'class Outer { class Inner { def x() { HTML } } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, HTML))
proposalExists(proposals, HTML_PROPOSAL, 1)
}

@Test
void testCompletionOfInnerClass1() {
String contents = "class Outer { class Inner { Inner f } } "
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, "Inner"))
proposalExists(proposals, "Inner", 1)
void testInnerClass1() {
String contents = 'class Outer { class Inner { Inner f } } '
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'Inner'))
proposalExists(proposals, 'Inner', 1)
}

@Test
void testCompletionOfInnerClass2() {
String contents = "class Outer { class Inner { Inner f } } "
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, "Inner"))
proposalExists(proposals, "Inner", 1)
void testInnerClass2() {
String contents = 'class Outer { class Inner { Inner f } } '
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'Inner'))
proposalExists(proposals, 'Inner', 1)
}

@Test
void testCompletionOFInnerMember1() {
String contents = "class Outer { class Inner { \n def y() { xxx } \n def xxx } } "
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, XXX))
proposalExists(proposals, XXX, 1)
void testInnerMember1() {
String contents = 'class Outer { class Inner { \n def y() { xxx } \n def xxx } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'xxx'))
proposalExists(proposals, 'xxx', 1)
}

@Test
void testCompletionOFInnerMember2() {
String contents = "class Outer { Inner i\ndef y() { i.xxx } \nclass Inner { \n def xxx } } "
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, XXX))
proposalExists(proposals, XXX, 1)
String contents = 'class Outer { Inner i\ndef y() { i.xxx } \nclass Inner { \n def xxx } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'xxx'))
proposalExists(proposals, 'xxx', 1)
}

@Test
void testCompletionOFInnerMember3() {
String contents = "def y(Outer.Inner i) { i.xxx } \nclass Outer { class Inner { \n def xxx } } "
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, XXX))
proposalExists(proposals, XXX, 1)
void testInnerMember3() {
String contents = 'def y(Outer.Inner i) { i.xxx } \nclass Outer { class Inner { \n def xxx } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'xxx'))
proposalExists(proposals, 'xxx', 1)
}

@Test
void testCompletionOFInnerMember4() {
String contents = "def y(Outer.Inner i) { i.xxx } \nclass Outer { class Inner { \n def getXxx() {} } } "
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, XXX))
proposalExists(proposals, XXX, 1)
void testInnerMember4() {
String contents = 'def y(Outer.Inner i) { i.xxx } \nclass Outer { class Inner { \n def getXxx() {} } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'xxx'))
proposalExists(proposals, 'xxx', 1)
}

@Test
void testCompletionOFInnerMember5() {
String contents = "Outer.Inner i\ni.xxx\nclass Outer { class Inner { \n def xxx } } "
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, XXX))
proposalExists(proposals, XXX, 1)
void testInnerMember5() {
String contents = 'Outer.Inner i\ni.xxx\nclass Outer { class Inner { \n def xxx } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'xxx'))
proposalExists(proposals, 'xxx', 1)
}

@Test
void testCompletionOFInnerMember6() {
String contents = "Outer.Inner i\ni.xxx\nclass Outer { class Inner { \n def getXxx() {} } } "
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, XXX))
proposalExists(proposals, XXX, 1)
void testInnerMember6() {
String contents = 'Outer.Inner i\ni.xxx\nclass Outer { class Inner { \n def getXxx() {} } }'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'xxx'))
proposalExists(proposals, 'xxx', 1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,15 @@ import org.codehaus.groovy.ast.stmt.ExpressionStatement
import org.codehaus.groovy.ast.stmt.ReturnStatement
import org.codehaus.groovy.ast.stmt.Statement
import org.codehaus.groovy.eclipse.codeassist.proposals.GroovyMethodProposal
import org.codehaus.groovy.eclipse.codeassist.requestor.GroovyCompletionProposalComputer
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit
import org.eclipse.jdt.core.compiler.CharOperation
import org.eclipse.jdt.ui.PreferenceConstants
import org.eclipse.jface.text.Document
import org.eclipse.jface.text.contentassist.ICompletionProposal
import org.junit.After
import org.junit.Test

final class MethodCompletionTests extends CompletionTestSuite {

@After
void tearDown() {
setJavaPreference(PreferenceConstants.CODEASSIST_FAVORITE_STATIC_MEMBERS, '')
}

private List<MethodNode> delegateTestParameterNames(GroovyCompilationUnit unit) {
waitForIndex()
List<MethodNode> methods = extract(unit).getMethods('m')
Expand Down Expand Up @@ -87,7 +80,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
HttpRetryException f() { null }
f().
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, 'f().'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'f().'))
proposalExists(proposals, 'cause', 1)
}

Expand All @@ -97,7 +90,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
HttpRetryException f() { null }
this.f().
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, 'f().'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'f().'))
proposalExists(proposals, 'cause', 1)
}

Expand All @@ -107,7 +100,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
class Super { HttpRetryException f() { null } }
new Super().f().
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, 'f().'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'f().'))
proposalExists(proposals, 'cause', 1)
}

Expand All @@ -118,7 +111,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
class Sub extends Super { }
new Sub().f().
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, 'f().'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'f().'))
proposalExists(proposals, 'cause', 1)
}

Expand All @@ -129,7 +122,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
def s = new Super()
s.f(null).
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, 'f(null).'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'f(null).'))
proposalExists(proposals, 'cause', 1)
}

Expand All @@ -140,7 +133,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
def s = new Super()
s.f().
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, 'f().'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, 'f().'))
proposalExists(proposals, 'cause', 1)
}

Expand Down Expand Up @@ -228,7 +221,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
(1).
def u
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, '(1).'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '(1).'))
proposalExists(proposals, 'abs', 1)
}

Expand All @@ -238,37 +231,37 @@ final class MethodCompletionTests extends CompletionTestSuite {
(((1))).
def u
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, '(((1))).'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '(((1))).'))
proposalExists(proposals, 'abs', 1)
}

@Test // GRECLIPSE-1374
void testParensExprs3() {
String contents = '(((1))).abs()'
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getIndexOf(contents, '(((1))).a'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getIndexOf(contents, '(((1))).a'))
proposalExists(proposals, 'abs', 1)
}

@Test // GRECLIPSE-1528
void testGetterSetter1() {
String contents = 'class A { private int value\n}'
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, '\n'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, '\n'))
proposalExists(proposals, 'getValue', 1)
proposalExists(proposals, 'setValue', 1)
}

@Test
void testGetterSetter2() {
String contents = 'class A { private final int value\n}'
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, '\n'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, '\n'))
proposalExists(proposals, 'getValue', 1)
proposalExists(proposals, 'setValue', 0)
}

@Test
void testGetterSetter3() {
String contents = 'class A { private boolean value\n}'
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, '\n'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, '\n'))
proposalExists(proposals, 'isValue', 1)
proposalExists(proposals, 'setValue', 1)
}
Expand All @@ -283,7 +276,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
}
}
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, 'A.'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'A.'))
proposalExists(proposals, 'util', 1)
}

Expand All @@ -300,7 +293,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
}
}
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, 'A.'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'A.'))
proposalExists(proposals, 'util', 1)
}

Expand All @@ -314,7 +307,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
}
}
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, 'A.class.'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'A.class.'))
proposalExists(proposals, 'util', 1)
}

Expand All @@ -330,7 +323,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
A.class.
}
}'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, 'A.class.'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'A.class.'))
proposalExists(proposals, 'util', 1)
}

Expand All @@ -340,7 +333,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
import static java.util.regex.Pattern.compile
comp
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, 'comp'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'comp'))
proposalExists(proposals, 'compile', 2)
}

Expand All @@ -350,7 +343,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
import static java.util.regex.Pattern.*
comp
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, 'comp'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'comp'))
proposalExists(proposals, 'compile', 2)
}

Expand All @@ -361,7 +354,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
String contents = '''\
comp
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, 'comp'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'comp'))
proposalExists(proposals, 'compile', 2)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'compile', false), '''\
Expand All @@ -378,7 +371,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
String contents = '''\
comp
'''.stripIndent()
ICompletionProposal[] proposals = performContentAssist(addGroovySource(contents), getLastIndexOf(contents, 'comp'), GroovyCompletionProposalComputer)
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'comp'))
proposalExists(proposals, 'compile', 2)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'compile', false), '''\
Expand Down
Loading

0 comments on commit 0426dfb

Please sign in to comment.