Skip to content

Commit

Permalink
Fix for issue #410: constructor and method completion prefs enhancement
Browse files Browse the repository at this point in the history
- use Java Content Assist preferences for parameter insert and guessing
- fix constructor and method context display (parameter list above text)
  • Loading branch information
eric-milles committed Feb 4, 2018
1 parent 612b3f3 commit 4d90e3e
Show file tree
Hide file tree
Showing 24 changed files with 607 additions and 969 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ 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.eclipse.jface.text.contentassist.ICompletionProposalExtension2
import org.junit.After
import org.junit.AfterClass
import org.junit.Before
Expand Down Expand Up @@ -90,7 +91,9 @@ abstract class CompletionTestSuite extends GroovyEclipseTestSuite {
protected ICompletionProposal[] performContentAssist(ICompilationUnit unit, int offset, Class<? extends IJavaCompletionProposalComputer> computerClass) {
JavaEditor editor = openInEditor(unit)
SynchronizationUtils.waitForIndexingToComplete(unit)
JavaSourceViewer viewer = (JavaSourceViewer) editor.viewer
JavaSourceViewer viewer = editor.viewer
viewer.setSelectedRange(offset, 0)

JavaContentAssistInvocationContext context = new JavaContentAssistInvocationContext(viewer, offset, editor)
SimpleProgressMonitor monitor = new SimpleProgressMonitor("Create completion proposals for $unit.elementName")
List<ICompletionProposal> proposals = computerClass.newInstance().computeCompletionProposals(context, monitor)
Expand Down Expand Up @@ -162,7 +165,7 @@ abstract class CompletionTestSuite extends GroovyEclipseTestSuite {
int i = indexOfProposal(proposals, name, 0, isType)
if (i != -1)
return proposals[i]
fail("Expected at least one proposal that matches '$name', but found none")
fail("Expected at least one proposal that matches '$name', but found none.")
}

protected void applyProposalAndCheck(IDocument document, ICompletionProposal proposal, String expected) {
Expand All @@ -172,6 +175,21 @@ abstract class CompletionTestSuite extends GroovyEclipseTestSuite {
assertEquals('Completion proposal applied but different results found.', expect, actual)
}

/**
* Applies the specified completion proposal to the active editor and checks
* against the expected result. Assumes performContentAssist(...) was called
* by some means to get {@code proposal}.
*/
protected void applyProposalAndCheck(ICompletionProposal proposal, String expected, char trigger = 0, int stateMask = 0) {
assert proposal instanceof ICompletionProposalExtension2
JavaContentAssistInvocationContext context = proposal.@fInvocationContext
proposal.apply(context.viewer, trigger, stateMask, context.invocationOffset)

String expect = expected.normalize()
String actual = context.document.get().normalize()
assertEquals('Completion proposal applied but different results found.', expect, actual)
}

protected void checkReplacementRegexp(ICompletionProposal[] proposals, String expectedReplacement, int expectedCount) {
int foundCount = 0
for (proposal in proposals) {
Expand Down Expand Up @@ -308,7 +326,7 @@ abstract class CompletionTestSuite extends GroovyEclipseTestSuite {

private String elementsToNames(IJavaElement[] visibleElements) {
String[] names = new String[visibleElements.length]
for (int i = 0; i < names.length; i++) {
for (int i = 0; i < names.length; i += 1) {
names[i] = visibleElements[i].elementName
}
return Arrays.toString(names)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,8 +32,9 @@ final class ConstructorCompletionTests extends CompletionTestSuite {
// filter some legacy packages
setJavaPreference(PreferenceConstants.TYPEFILTER_ENABLED, 'sun.*;com.sun.*;org.omg.*')

setJavaPreference(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES, 'true')
setJavaPreference(PreferenceConstants.CODEASSIST_GUESS_METHOD_ARGUMENTS, 'true')
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.CLOSURE_NOPARENS, false)
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.PARAMETER_GUESSING, true)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import groovy.transform.NotYetImplemented

import org.eclipse.jdt.internal.codeassist.impl.AssistOptions
import org.eclipse.jdt.ui.PreferenceConstants
import org.eclipse.jface.text.Document
import org.eclipse.jface.text.contentassist.ICompletionProposal
import org.junit.Test

Expand Down Expand Up @@ -404,7 +403,7 @@ final class FieldCompletionTests extends CompletionTestSuite {
meth(B)
'''.stripIndent()
ICompletionProposal proposal = checkUniqueProposal(contents, 'B', 'BLACK')
applyProposalAndCheck(new Document(contents), proposal, '''\
applyProposalAndCheck(proposal, '''\
|import static tree.node.Color.BLACK
|
|def meth(tree.node.Color c) { }
Expand All @@ -426,7 +425,7 @@ final class FieldCompletionTests extends CompletionTestSuite {
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'BL'))
proposalExists(proposals, 'BLACK', 2)

applyProposalAndCheck(new Document(contents), orderByRelevance(proposals)[0], '''\
applyProposalAndCheck(orderByRelevance(proposals)[0], '''\
|import static a.b.c.D.BLACK
|
|import tree.node.Color
Expand Down Expand Up @@ -496,7 +495,7 @@ final class FieldCompletionTests extends CompletionTestSuite {
'''.stripIndent()
ICompletionProposal proposal = checkUniqueProposal(contents, 'DOT', 'DOTALL')

applyProposalAndCheck(new Document(contents), proposal, '''\
applyProposalAndCheck(proposal, '''\
|import static java.util.regex.Pattern.DOTALL
|
|DOTALL
Expand All @@ -512,7 +511,7 @@ final class FieldCompletionTests extends CompletionTestSuite {
'''.stripIndent()
ICompletionProposal proposal = checkUniqueProposal(contents, 'DOT', 'DOTALL')

applyProposalAndCheck(new Document(contents), proposal, '''\
applyProposalAndCheck(proposal, '''\
|import static java.util.regex.Pattern.DOTALL
|
|DOTALL
Expand All @@ -529,7 +528,7 @@ final class FieldCompletionTests extends CompletionTestSuite {
'''.stripIndent()
ICompletionProposal proposal = checkUniqueProposal(contents, 'DOT', 'DOTALL')

applyProposalAndCheck(new Document(contents), proposal, '''\
applyProposalAndCheck(proposal, '''\
|import java.util.regex.Pattern
|
|Pattern.DOTALL
Expand All @@ -549,7 +548,7 @@ final class FieldCompletionTests extends CompletionTestSuite {
'''.stripIndent()
ICompletionProposal proposal = checkUniqueProposal(contents, 'DOT', 'DOTALL')

applyProposalAndCheck(new Document(contents), proposal, '''\
applyProposalAndCheck(proposal, '''\
java.util.regex.Pattern.DOTALL
'''.stripIndent())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ final class GroovyLikeCompletionTests extends CompletionTestSuite {
groovyPrefs.setValue(GroovyContentAssist.CLOSURE_BRACKETS, true)
groovyPrefs.setValue(GroovyContentAssist.CLOSURE_NOPARENS, true)
groovyPrefs.setValue(GroovyContentAssist.NAMED_ARGUMENTS, false)
groovyPrefs.setValue(GroovyContentAssist.PARAMETER_GUESSING, false)
setJavaPreference(PreferenceConstants.TYPEFILTER_ENABLED, 'com.sun.*;sun.*')
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
package org.codehaus.groovy.eclipse.codeassist.tests

import org.codehaus.groovy.eclipse.codeassist.GroovyContentAssist
import org.eclipse.jface.text.Document
import org.eclipse.jface.text.IDocument
import org.eclipse.jdt.ui.PreferenceConstants
import org.eclipse.jface.text.contentassist.ICompletionProposal
import org.junit.Assert
import org.junit.Before
Expand All @@ -27,9 +26,10 @@ final class GuessingCompletionTests extends CompletionTestSuite {

@Before
void setUp() {
setJavaPreference(PreferenceConstants.CODEASSIST_FILL_ARGUMENT_NAMES, 'true')
setJavaPreference(PreferenceConstants.CODEASSIST_GUESS_METHOD_ARGUMENTS, 'true')
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.CLOSURE_BRACKETS, true)
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.CLOSURE_NOPARENS, true)
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.PARAMETER_GUESSING, true)
}

@Test
Expand Down Expand Up @@ -83,22 +83,14 @@ final class GuessingCompletionTests extends CompletionTestSuite {
// GroovyExtendedCompletionContext.computeVisibleElements(String)
String contents = '''\
Closure yyy
def zzz = { }
def zzz = { -> }
def xxx(Closure c) { }
xxx
'''.stripIndent()
String[][] expectedChoices = [
['zzz', 'yyy', '{ }'] as String[]
]
try {
checkProposalChoices(contents, 'xxx', 'xxx {', expectedChoices)
} catch (AssertionError e) {
try {
checkProposalChoices(contents, 'xxx', 'xxx yyy', expectedChoices)
} catch (AssertionError e2) {
checkProposalChoices(contents, 'xxx', 'xxx zzz', expectedChoices)
}
}
checkProposalChoices(contents, 'xxx', 'xxx { }', expectedChoices)
}

@Test
Expand All @@ -116,11 +108,10 @@ final class GuessingCompletionTests extends CompletionTestSuite {
|
|pack.Util.ut
|'''.stripMargin()
IDocument document = new Document(contents)
ICompletionProposal proposal = checkUniqueProposal(contents, 'ut', 'util', 'util(MILLIS)')

// apply initial proposal to generate parameter proposals
applyProposalAndCheck(document, proposal, '''\
applyProposalAndCheck(proposal, '''\
|import static java.util.concurrent.TimeUnit.MILLISECONDS as MILLIS
|
|pack.Util.util(MILLIS)
Expand All @@ -132,7 +123,7 @@ final class GuessingCompletionTests extends CompletionTestSuite {
choices*.displayString.join('\n'))

// TODO: Something below is not matching the real editor's application of the parameter proposal
/*applyProposalAndCheck(document, choices[1], '''\
/*applyProposalAndCheck(choices[1], '''\
|import static java.util.concurrent.TimeUnit.DAYS
|import static java.util.concurrent.TimeUnit.MILLISECONDS as MILLIS
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,16 @@ import org.codehaus.groovy.ast.MethodNode
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.GroovyContentAssist
import org.codehaus.groovy.eclipse.codeassist.proposals.GroovyMethodProposal
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit
import org.eclipse.jdt.core.compiler.CharOperation
import org.eclipse.jdt.internal.codeassist.impl.AssistOptions
import org.eclipse.jdt.ui.PreferenceConstants
import org.eclipse.jface.text.Document
import org.eclipse.jface.text.contentassist.ICompletionProposal
import org.junit.Before
import org.junit.Test

final class MethodCompletionTests extends CompletionTestSuite {

@Before
void setUp() {
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.PARAMETER_GUESSING, false)
}

private List<MethodNode> delegateTestParameterNames(GroovyCompilationUnit unit) {
waitForIndex()
List<MethodNode> methods = extract(unit).getMethods('m')
Expand Down Expand Up @@ -404,7 +396,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'comp'))
proposalExists(proposals, 'compile', 2)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'compile(String regex)'), '''\
applyProposalAndCheck(findFirstProposal(proposals, 'compile(String regex)'), '''\
|import static java.util.regex.Pattern.compile
|
|compile(regex)
Expand Down Expand Up @@ -433,7 +425,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'comp'))
proposalExists(proposals, 'compile', 2)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'compile(String regex)'), '''\
applyProposalAndCheck(findFirstProposal(proposals, 'compile(String regex)'), '''\
|import static java.util.regex.Pattern.compile
|
|compile(regex)
Expand All @@ -451,7 +443,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'comp'))
proposalExists(proposals, 'compile', 2)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'compile(String regex)'), '''\
applyProposalAndCheck(findFirstProposal(proposals, 'compile(String regex)'), '''\
|import java.util.regex.Pattern
|
|Pattern.compile(regex)
Expand All @@ -472,7 +464,7 @@ final class MethodCompletionTests extends CompletionTestSuite {
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'comp'))
proposalExists(proposals, 'compile', 2)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'compile(String regex)'), '''\
applyProposalAndCheck(findFirstProposal(proposals, 'compile(String regex)'), '''\
java.util.regex.Pattern.compile(regex)
'''.stripIndent())
}
Expand All @@ -483,27 +475,16 @@ final class MethodCompletionTests extends CompletionTestSuite {
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'isE'))
proposalExists(proposals, 'isEmpty', 1)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'isEmpty'), 'String.&isEmpty')
applyProposalAndCheck(findFirstProposal(proposals, 'isEmpty'), 'String.&isEmpty')
}

@Test
void testMethodPointer2() {
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.PARAMETER_GUESSING, true)

String contents = 'String.&isE'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'isE'))
proposalExists(proposals, 'isEmpty', 1)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'isEmpty'), 'String.&isEmpty')
}

@Test
void testMethodPointer3() {
String contents = 'String.& isE'
ICompletionProposal[] proposals = createProposalsAtOffset(contents, getLastIndexOf(contents, 'isE'))
proposalExists(proposals, 'isEmpty', 1)

applyProposalAndCheck(new Document(contents), findFirstProposal(proposals, 'isEmpty'), 'String.& isEmpty')
applyProposalAndCheck(findFirstProposal(proposals, 'isEmpty'), 'String.& isEmpty')
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -28,7 +28,6 @@ final class OtherCompletionTests extends CompletionTestSuite {
@Before
void setUp() {
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.CLOSURE_NOPARENS, false)
GroovyContentAssist.default.preferenceStore.setValue(GroovyContentAssist.PARAMETER_GUESSING, false)
}

@Test // GRECLIPSE-414
Expand Down
Loading

0 comments on commit 4d90e3e

Please sign in to comment.