1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 17:26:01 +02:00

Bug 488904 - Added a preference for enabling/disabling argument guessing

Change-Id: Id39454da16638d5f11c95e419c2385dc16cdb53a
This commit is contained in:
Sergey Prigogin 2016-03-02 20:35:39 -08:00
parent 86dabd3d00
commit 70fe6cbbde
6 changed files with 70 additions and 57 deletions

View file

@ -12,6 +12,7 @@
* Kirk Beitz (Nokia) * Kirk Beitz (Nokia)
* Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion) * Jens Elmenthaler - http://bugs.eclipse.org/173458 (camel case completion)
* Thomas Corbat * Thomas Corbat
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.preferences; package org.eclipse.cdt.internal.ui.preferences;
@ -48,6 +49,7 @@ public class CodeAssistPreferencePage extends AbstractPreferencePage {
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, ContentAssistPreference.AUTOACTIVATION_DELAY)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, ContentAssistPreference.AUTOACTIVATION_DELAY));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ContentAssistPreference.AUTOINSERT)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ContentAssistPreference.AUTOINSERT));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ContentAssistPreference.PREFIX_COMPLETION)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ContentAssistPreference.PREFIX_COMPLETION));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.BOOLEAN, ContentAssistPreference.GUESS_ARGUMENTS));
// overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, ContentAssistPreference.TIMEOUT_DELAY)); // overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.INT, ContentAssistPreference.TIMEOUT_DELAY));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, ContentAssistPreference.AUTOACTIVATION_TRIGGERS_DOT)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, ContentAssistPreference.AUTOACTIVATION_TRIGGERS_DOT));
overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, ContentAssistPreference.AUTOACTIVATION_TRIGGERS_ARROW)); overlayKeys.add(new OverlayPreferenceStore.OverlayKey(OverlayPreferenceStore.STRING, ContentAssistPreference.AUTOACTIVATION_TRIGGERS_ARROW));
@ -106,6 +108,9 @@ public class CodeAssistPreferencePage extends AbstractPreferencePage {
label = PreferencesMessages.CEditorPreferencePage_ContentAssistPage_insertCommonProposalAutomatically; label = PreferencesMessages.CEditorPreferencePage_ContentAssistPage_insertCommonProposalAutomatically;
addCheckBox(insertionGroup, label, ContentAssistPreference.PREFIX_COMPLETION, 0); addCheckBox(insertionGroup, label, ContentAssistPreference.PREFIX_COMPLETION, 0);
label = PreferencesMessages.CEditorPreferencePage_ContentAssistPage_guessArguments;
addCheckBox(insertionGroup, label, ContentAssistPreference.GUESS_ARGUMENTS, 0);
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// sorting and filtering // sorting and filtering
label = PreferencesMessages.CEditorPreferencePage_ContentAssistPage_sortingSection_title; label = PreferencesMessages.CEditorPreferencePage_ContentAssistPage_sortingSection_title;
@ -138,7 +143,7 @@ public class CodeAssistPreferencePage extends AbstractPreferencePage {
addCheckBox(enableGroup, label, ContentAssistPreference.AUTOACTIVATION_TRIGGERS_REPLACE_DOT_WITH_ARROW, 0); addCheckBox(enableGroup, label, ContentAssistPreference.AUTOACTIVATION_TRIGGERS_REPLACE_DOT_WITH_ARROW, 0);
label = PreferencesMessages.CEditorPreferencePage_ContentAssistPage_autoActivationDelay; label = PreferencesMessages.CEditorPreferencePage_ContentAssistPage_autoActivationDelay;
addTextField(enableGroup, label, ContentAssistPreference.AUTOACTIVATION_DELAY, 4, 0, true); addTextField(enableGroup, label, ContentAssistPreference.AUTOACTIVATION_DELAY, 4, 4, true);
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
// The following items are grouped for Default Arguments // The following items are grouped for Default Arguments
@ -181,6 +186,7 @@ public class CodeAssistPreferencePage extends AbstractPreferencePage {
store.setDefault(ContentAssistPreference.AUTOINSERT, true); store.setDefault(ContentAssistPreference.AUTOINSERT, true);
store.setDefault(ContentAssistPreference.PREFIX_COMPLETION, true); store.setDefault(ContentAssistPreference.PREFIX_COMPLETION, true);
store.setDefault(ContentAssistPreference.GUESS_ARGUMENTS, true);
store.setDefault(ContentAssistPreference.ORDER_PROPOSALS, false); store.setDefault(ContentAssistPreference.ORDER_PROPOSALS, false);
store.setDefault(ContentAssistPreference.PROPOSALS_FILTER, store.setDefault(ContentAssistPreference.PROPOSALS_FILTER,
ProposalFilterPreferencesUtil.getProposalFilternamesAsString()); ProposalFilterPreferencesUtil.getProposalFilternamesAsString());

View file

@ -42,6 +42,7 @@ public final class PreferencesMessages extends NLS {
public static String CEditorPreferencePage_ContentAssistPage_insertionGroupTitle; public static String CEditorPreferencePage_ContentAssistPage_insertionGroupTitle;
public static String CEditorPreferencePage_ContentAssistPage_insertSingleProposalAutomatically; public static String CEditorPreferencePage_ContentAssistPage_insertSingleProposalAutomatically;
public static String CEditorPreferencePage_ContentAssistPage_insertCommonProposalAutomatically; public static String CEditorPreferencePage_ContentAssistPage_insertCommonProposalAutomatically;
public static String CEditorPreferencePage_ContentAssistPage_guessArguments;
public static String CEditorPreferencePage_ContentAssistPage_showProposalsInAlphabeticalOrder; public static String CEditorPreferencePage_ContentAssistPage_showProposalsInAlphabeticalOrder;
public static String CEditorPreferencePage_ContentAssistPage_showCamelCaseMatches; public static String CEditorPreferencePage_ContentAssistPage_showCamelCaseMatches;
public static String CEditorPreferencePage_ContentAssistPage_autoActivationGroupTitle; public static String CEditorPreferencePage_ContentAssistPage_autoActivationGroupTitle;

View file

@ -26,8 +26,9 @@ CEditorPreferencePage_colors=Synta&x
#CEditorPreferencePage_ContentAssistPage_searchGroupCurrentFileOption=S&earch current file and included files #CEditorPreferencePage_ContentAssistPage_searchGroupCurrentFileOption=S&earch current file and included files
#CEditorPreferencePage_ContentAssistPage_searchGroupCurrentProjectOption=Search current &project #CEditorPreferencePage_ContentAssistPage_searchGroupCurrentProjectOption=Search current &project
CEditorPreferencePage_ContentAssistPage_insertionGroupTitle=Insertion CEditorPreferencePage_ContentAssistPage_insertionGroupTitle=Insertion
CEditorPreferencePage_ContentAssistPage_insertSingleProposalAutomatically=&Insert single proposals automatically CEditorPreferencePage_ContentAssistPage_insertSingleProposalAutomatically=&Insert a single proposal automatically
CEditorPreferencePage_ContentAssistPage_insertCommonProposalAutomatically=I&nsert common prefixes automatically CEditorPreferencePage_ContentAssistPage_insertCommonProposalAutomatically=I&nsert common prefixes automatically
CEditorPreferencePage_ContentAssistPage_guessArguments=&Fill function arguments and show guessed arguments
CEditorPreferencePage_ContentAssistPage_showProposalsInAlphabeticalOrder=Present proposals in a&lphabetical order CEditorPreferencePage_ContentAssistPage_showProposalsInAlphabeticalOrder=Present proposals in a&lphabetical order
CEditorPreferencePage_ContentAssistPage_showCamelCaseMatches=Show ca&mel case matches CEditorPreferencePage_ContentAssistPage_showCamelCaseMatches=Show ca&mel case matches
CEditorPreferencePage_ContentAssistPage_autoActivationGroupTitle=Auto-Activation CEditorPreferencePage_ContentAssistPage_autoActivationGroupTitle=Auto-Activation

View file

@ -10,6 +10,7 @@
* Anton Leherbauer (Wind River Systems) * Anton Leherbauer (Wind River Systems)
* Kirk Beitz (Nokia) * Kirk Beitz (Nokia)
* Thomas Corbat (IFS) * Thomas Corbat (IFS)
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist; package org.eclipse.cdt.internal.ui.text.contentassist;

View file

@ -124,21 +124,22 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
private static final String TEMPLATE_PARAMETER_PATTERN = "template<{0}> class"; //$NON-NLS-1$; private static final String TEMPLATE_PARAMETER_PATTERN = "template<{0}> class"; //$NON-NLS-1$;
private static final String TYPENAME = "typename"; //$NON-NLS-1$; private static final String TYPENAME = "typename"; //$NON-NLS-1$;
private static final String ELLIPSIS = "..."; //$NON-NLS-1$; private static final String ELLIPSIS = "..."; //$NON-NLS-1$;
private String fPrefix; private String fPrefix = ""; //$NON-NLS-1$
private boolean fGuessArguments;
private List<IBinding> fAvailableElements; private List<IBinding> fAvailableElements;
/** /**
* Default constructor is required (executable extension). * Default constructor is required (executable extension).
*/ */
public DOMCompletionProposalComputer() { public DOMCompletionProposalComputer() {
fPrefix = ""; //$NON-NLS-1$
} }
@Override @Override
protected List<ICompletionProposal> computeCompletionProposals(CContentAssistInvocationContext context, protected List<ICompletionProposal> computeCompletionProposals(CContentAssistInvocationContext context,
IASTCompletionNode completionNode, String prefix) { IASTCompletionNode completionNode, String prefix) {
fPrefix = prefix; fPrefix = prefix;
initializeDefinedElements(context); fGuessArguments = getPreferenceStore().getBoolean(ContentAssistPreference.GUESS_ARGUMENTS);
fAvailableElements = fGuessArguments ? getDefinedElements(context) : Collections.<IBinding>emptyList();
List<ICompletionProposal> proposals = new ArrayList<>(); List<ICompletionProposal> proposals = new ArrayList<>();
if (inPreprocessorDirective(context)) { if (inPreprocessorDirective(context)) {
@ -622,7 +623,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
if (function.getParameters() != null && function.getParameters().length != 0 if (function.getParameters() != null && function.getParameters().length != 0
&& isBeforeParameters(context)) { && isBeforeParameters(context)) {
proposals.add(ParameterGuessingProposal.createProposal(context, fAvailableElements, proposal, proposals.add(ParameterGuessingProposal.createProposal(context, fAvailableElements, proposal,
function, fPrefix)); function, fPrefix, fGuessArguments));
} else { } else {
proposals.add(proposal); proposals.add(proposal);
} }
@ -659,9 +660,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
/** /**
* Initializes the list of variables accessible at the start of the current statement. * Initializes the list of variables accessible at the start of the current statement.
*/ */
private void initializeDefinedElements(CContentAssistInvocationContext context) { private List<IBinding> getDefinedElements(CContentAssistInvocationContext context) {
fAvailableElements = Collections.emptyList();
// Get all variables accessible at the start of the statement. // Get all variables accessible at the start of the statement.
// ex1: int a = foo( // ex1: int a = foo(
// ^ --> We don't want 'a' as a suggestion. // ^ --> We don't want 'a' as a suggestion.
@ -672,7 +671,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
// ^ --> If this offset is used, the only defined name will be "bar(char*)". // ^ --> If this offset is used, the only defined name will be "bar(char*)".
IASTCompletionNode node = context.getCompletionNode(); IASTCompletionNode node = context.getCompletionNode();
if (node == null) if (node == null)
return; return Collections.emptyList();
// Find the enclosing statement at the point of completion. // Find the enclosing statement at the point of completion.
IASTStatement completionStatement = null; IASTStatement completionStatement = null;
@ -687,8 +686,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
} }
} }
if (completionStatement == null) if (completionStatement == null)
return; return Collections.emptyList();
// Get content assist results for an empty prefix at the start of the statement. // Get content assist results for an empty prefix at the start of the statement.
final int statementOffset = getNodeOffset(completionStatement); final int statementOffset = getNodeOffset(completionStatement);
@ -701,7 +699,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
IBinding[] bindings = findBindingsForContextAssist(name, ast); IBinding[] bindings = findBindingsForContextAssist(name, ast);
if (bindings.length == 0) if (bindings.length == 0)
return; return Collections.emptyList();
// Get all variables declared in the translation unit. // Get all variables declared in the translation unit.
final Set<IBinding> declaredVariables = new HashSet<>(); final Set<IBinding> declaredVariables = new HashSet<>();
@ -732,7 +730,7 @@ public class DOMCompletionProposalComputer extends ParsingBasedProposalComputer
elementsMap.put(binding.getName(), binding); elementsMap.put(binding.getName(), binding);
} }
} }
fAvailableElements = new ArrayList<>(elementsMap.values()); return new ArrayList<>(elementsMap.values());
} }
private IBinding[] findBindingsForContextAssist(IASTName name, IASTTranslationUnit ast) { private IBinding[] findBindingsForContextAssist(IASTName name, IASTTranslationUnit ast) {

View file

@ -9,6 +9,7 @@
* Andrew McCullough - initial API and implementation * Andrew McCullough - initial API and implementation
* IBM Corporation - general improvement and bug fixes, partial reimplementation * IBM Corporation - general improvement and bug fixes, partial reimplementation
* Mohamed Azab (Mentor Graphics) - Bug 438549. Add mechanism for parameter guessing. * Mohamed Azab (Mentor Graphics) - Bug 438549. Add mechanism for parameter guessing.
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.text.contentassist; package org.eclipse.cdt.internal.ui.text.contentassist;
@ -44,6 +45,7 @@ import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IFunction; import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IParameter; import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ITranslationUnit; import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.CUIPlugin; import org.eclipse.cdt.ui.CUIPlugin;
@ -66,10 +68,11 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal {
private char[][] fParametersNames; private char[][] fParametersNames;
private IType[] fParametersTypes; private IType[] fParametersTypes;
private List<IBinding> fAssignableElements; private List<IBinding> fAssignableElements;
private final boolean fGuessArguments;
public static ParameterGuessingProposal createProposal(CContentAssistInvocationContext context, public static ParameterGuessingProposal createProposal(CContentAssistInvocationContext context,
List<IBinding> availableElements, CCompletionProposal proposal, IFunction function, List<IBinding> availableElements, CCompletionProposal proposal, IFunction function,
String prefix) { String prefix, boolean guessArguments) {
String replacement = getParametersList(function); String replacement = getParametersList(function);
String fullPrefix = function.getName() + "("; //$NON-NLS-1$ String fullPrefix = function.getName() + "("; //$NON-NLS-1$
int replacementOffset = proposal.getReplacementOffset(); int replacementOffset = proposal.getReplacementOffset();
@ -106,7 +109,7 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal {
ParameterGuessingProposal ret = new ParameterGuessingProposal(replacement, replacementOffset, ParameterGuessingProposal ret = new ParameterGuessingProposal(replacement, replacementOffset,
replacementLength, proposal.getImage(), proposal.getDisplayString(), proposal.getIdString(), replacementLength, proposal.getImage(), proposal.getDisplayString(), proposal.getIdString(),
proposal.getRelevance(), context.getViewer(), function, invocationOffset, parseOffset, proposal.getRelevance(), context.getViewer(), function, invocationOffset, parseOffset,
context.getTranslationUnit(), document); context.getTranslationUnit(), document, guessArguments);
ret.setContextInformation(proposal.getContextInformation()); ret.setContextInformation(proposal.getContextInformation());
ret.fFullPrefix = fullPrefix; ret.fFullPrefix = fullPrefix;
ret.fCEditor = getCEditor(context.getEditor()); ret.fCEditor = getCEditor(context.getEditor());
@ -135,9 +138,10 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal {
public ParameterGuessingProposal(String replacementString, int replacementOffset, int replacementLength, public ParameterGuessingProposal(String replacementString, int replacementOffset, int replacementLength,
Image image, String displayString, String idString, int relevance, ITextViewer viewer, Image image, String displayString, String idString, int relevance, ITextViewer viewer,
IFunction function, int invocationOffset, int parseOffset, ITranslationUnit tu, IFunction function, int invocationOffset, int parseOffset, ITranslationUnit tu,
IDocument document) { IDocument document, boolean guessArguments) {
super(replacementString, replacementOffset, replacementLength, image, displayString, idString, super(replacementString, replacementOffset, replacementLength, image, displayString, idString,
relevance, viewer, function, invocationOffset, parseOffset, tu, document); relevance, viewer, function, invocationOffset, parseOffset, tu, document);
this.fGuessArguments = guessArguments;
} }
/** /**
@ -166,12 +170,14 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal {
fParametersNames = getFunctionParametersNames(fFunctionParameters); fParametersNames = getFunctionParametersNames(fFunctionParameters);
fParametersTypes = getFunctionParametersTypes(fFunctionParameters); fParametersTypes = getFunctionParametersTypes(fFunctionParameters);
if (fGuessArguments) {
try { try {
guessParameters(); guessParameters();
} catch (Exception e) { } catch (Exception e) {
CUIPlugin.log(e); CUIPlugin.log(e);
return; return;
} }
}
int baseOffset = getReplacementOffset(); int baseOffset = getReplacementOffset();
String replacement = getReplacementString(); String replacement = getReplacementString();
@ -250,7 +256,7 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal {
return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength()); return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength());
} }
private void guessParameters() throws Exception { private void guessParameters() throws CModelException {
int count = fParametersNames.length; int count = fParametersNames.length;
fPositions = new Position[count]; fPositions = new Position[count];
fChoices = new ICompletionProposal[count][]; fChoices = new ICompletionProposal[count][];
@ -269,11 +275,11 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal {
fPositions[i] = position; fPositions[i] = position;
fChoices[i] = argumentProposals; fChoices[i] = argumentProposals;
} }
updateProposalsPossitions(); updateProposalsPositions();
} }
private void updateProposalsPossitions() throws Exception { private void updateProposalsPositions() {
StringBuffer buffer = new StringBuffer(); StringBuilder buffer = new StringBuilder();
buffer.append(fFullPrefix); buffer.append(fFullPrefix);
setCursorPosition(buffer.length()); setCursorPosition(buffer.length());