From e98bf3e3e3331daf958a3072f4f75a2b15e98739 Mon Sep 17 00:00:00 2001 From: Sergey Prigogin Date: Wed, 4 Feb 2015 17:08:39 -0800 Subject: [PATCH] Cosmetics. --- .../org/eclipse/cdt/core/model/ILanguage.java | 3 +- .../cdt/core/model/ITranslationUnit.java | 3 +- .../dom/parser/AbstractCLikeLanguage.java | 2 +- .../SurroundWithTemplateMenuAction.java | 48 +++--------- .../CContentAssistInvocationContext.java | 32 ++++---- .../FunctionCompletionProposal.java | 31 ++++---- .../internal/ui/text/contentassist/Lazy.java | 8 +- .../text/contentassist/ParameterGuesser.java | 69 ++++++----------- .../ParameterGuessingProposal.java | 74 +++++++------------ .../ParsingBasedProposalComputer.java | 19 ++--- 10 files changed, 107 insertions(+), 182 deletions(-) diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguage.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguage.java index 53c13650a50..bcc2740c191 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguage.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ILanguage.java @@ -123,14 +123,13 @@ public interface ILanguage extends IAdaptable { throws CoreException; /** - * Return the AST completion node for the given offset. + * Returns the AST completion node for the given offset. * @since 5.2 */ public IASTCompletionNode getCompletionNode(FileContent reader, IScannerInfo scanInfo, IncludeFileContentProvider fileCreator, IIndex index, IParserLogService log, int offset) throws CoreException; - /** * Used to override the default model building behavior for a translation unit. * diff --git a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ITranslationUnit.java b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ITranslationUnit.java index edc3efee88e..c19e0644ddb 100644 --- a/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ITranslationUnit.java +++ b/core/org.eclipse.cdt.core/model/org/eclipse/cdt/core/model/ITranslationUnit.java @@ -468,11 +468,10 @@ public interface ITranslationUnit extends ICElement, IParent, IOpenable, ISource public IASTTranslationUnit getAST(IIndex index, int style) throws CoreException; /** - * Return the completion node using the given index and parsing style at the given offset. + * Returns the completion node using the given index and parsing style at the given offset. */ public IASTCompletionNode getCompletionNode(IIndex index, int style, int offset) throws CoreException; - /** * @deprecated use {@link #getSharedWorkingCopy(IProgressMonitor, IProblemRequestor)}, * or CDTUITools.getWorkingCopyManager() instead. diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/AbstractCLikeLanguage.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/AbstractCLikeLanguage.java index bb9ffe7f216..d572e73149e 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/AbstractCLikeLanguage.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/core/dom/parser/AbstractCLikeLanguage.java @@ -213,7 +213,7 @@ public abstract class AbstractCLikeLanguage extends AbstractLanguage implements } /** - * Create the parser. + * Creates the parser. * * @param scanner the IScanner to get tokens from * @param log the parser log service diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/SurroundWithTemplateMenuAction.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/SurroundWithTemplateMenuAction.java index 770375a7477..96dc6624cb3 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/SurroundWithTemplateMenuAction.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/actions/SurroundWithTemplateMenuAction.java @@ -57,7 +57,6 @@ import org.eclipse.cdt.internal.ui.text.contentassist.CContentAssistInvocationCo import org.eclipse.cdt.internal.ui.text.contentassist.TemplateCompletionProposalComputer; public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownDelegate2 { - public static final String SURROUND_WITH_QUICK_MENU_ACTION_ID= "org.eclipse.cdt.ui.edit.text.c.surround.with.quickMenu"; //$NON-NLS-1$ private static final String C_TEMPLATE_PREFERENCE_PAGE_ID= "org.eclipse.cdt.ui.preferences.TemplatePreferencePage"; //$NON-NLS-1$ @@ -67,14 +66,10 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD private static final String CONFIG_GROUP= "configGroup"; //$NON-NLS-1$ private static class ConfigureTemplatesAction extends Action { - public ConfigureTemplatesAction() { super(ActionMessages.SurroundWithTemplateMenuAction_ConfigureTemplatesActionName); } - /** - * {@inheritDoc} - */ @Override public void run() { PreferencesUtil.createPreferenceDialogOn(getShell(), C_TEMPLATE_PREFERENCE_PAGE_ID, new String[] {C_TEMPLATE_PREFERENCE_PAGE_ID}, null).open(); @@ -90,6 +85,7 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD public void run() { //Do nothing } + @Override public boolean isEnabled() { return false; @@ -99,7 +95,6 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD private Menu fMenu; private IPartService fPartService; private IPartListener fPartListener= new IPartListener() { - @Override public void partActivated(IWorkbenchPart part) { } @@ -135,9 +130,6 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD } } - /** - * {@inheritDoc} - */ @Override public Menu getMenu(Menu parent) { setMenu(new Menu(parent)); @@ -146,9 +138,6 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD return fMenu; } - /** - * {@inheritDoc} - */ @Override public Menu getMenu(Control parent) { setMenu(new Menu(parent)); @@ -171,9 +160,6 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD menu.add(new ConfigureTemplatesAction()); } - /** - * {@inheritDoc} - */ @Override public void dispose() { if (fPartService != null) { @@ -183,9 +169,6 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD setMenu(null); } - /** - * {@inheritDoc} - */ @Override public void init(IWorkbenchWindow window) { if (fPartService != null) { @@ -202,9 +185,6 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD } } - /** - * {@inheritDoc} - */ @Override public void run(IAction action) { IWorkbenchPart activePart= CUIPlugin.getActivePage().getActivePart(); @@ -221,9 +201,6 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD }.createMenu(); } - /** - * {@inheritDoc} - */ @Override public void selectionChanged(IAction action, ISelection selection) { // Default do nothing @@ -234,7 +211,6 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD * @param menu the menu to fill entries into it */ protected void fillMenu(Menu menu) { - IWorkbenchPart activePart= CUIPlugin.getActivePage().getActivePart(); if (!(activePart instanceof CEditor)) { ActionContributionItem item= new ActionContributionItem(NONE_APPLICABLE_ACTION); @@ -242,7 +218,7 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD return; } - CEditor editor= (CEditor)activePart; + CEditor editor= (CEditor) activePart; IAction[] actions= getTemplateActions(editor); if ( actions == null || actions.length <= 0) { @@ -267,9 +243,9 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD fMenu.addMenuListener(new MenuAdapter() { @Override public void menuShown(MenuEvent e) { - Menu m = (Menu)e.widget; + Menu m = (Menu) e.widget; MenuItem[] items = m.getItems(); - for (int i=0; i < items.length; i++) { + for (int i= 0; i < items.length; i++) { items[i].dispose(); } fillMenu(m); @@ -315,21 +291,19 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD return (ITextSelection)selection; } - private static IAction[] getActionsFromProposals(List proposals, final int offset, final ITextViewer viewer) { - List result= new ArrayList(); + private static IAction[] getActionsFromProposals(List proposals, final int offset, + final ITextViewer viewer) { + List result= new ArrayList<>(); int j = 1; for (Iterator it= proposals.iterator(); it.hasNext();) { final ICompletionProposal proposal= it.next(); - StringBuffer actionName= new StringBuffer(); + StringBuilder actionName= new StringBuilder(); if (j < 10) { actionName.append('&').append(j).append(' '); } actionName.append(proposal.getDisplayString()); Action action= new Action(actionName.toString()) { - /** - * {@inheritDoc} - */ @Override public void run() { applyProposal(proposal, viewer, (char)0, 0, offset); @@ -339,19 +313,19 @@ public class SurroundWithTemplateMenuAction implements IWorkbenchWindowPulldownD result.add(action); j++; } - if (result.size() == 0) + if (result.isEmpty()) return null; return result.toArray(new IAction[result.size()]); } - private static void applyProposal(ICompletionProposal proposal, ITextViewer viewer, char trigger, int stateMask, final int offset) { + private static void applyProposal(ICompletionProposal proposal, ITextViewer viewer, char trigger, + int stateMask, final int offset) { Assert.isTrue(proposal instanceof ICompletionProposalExtension2); IRewriteTarget target= null; IEditingSupportRegistry registry= null; IEditingSupport helper= new IEditingSupport() { - @Override public boolean isOriginator(DocumentEvent event, IRegion focus) { return focus.getOffset() <= offset && focus.getOffset() + focus.getLength() >= offset; diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CContentAssistInvocationContext.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CContentAssistInvocationContext.java index 71c09a3b9b0..17f6de74609 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CContentAssistInvocationContext.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/CContentAssistInvocationContext.java @@ -78,12 +78,17 @@ public class CContentAssistInvocationContext extends ContentAssistInvocationCont @Override protected IASTCompletionNode calculateValue() { int offset = getParseOffset(); - if (offset < 0) return null; + if (offset < 0) + return null; ICProject proj= getProject(); - if (proj == null) return null; + if (proj == null) + return null; try { + if (fIndex != null) + throw new IllegalStateException("The method should not be called multiple times."); //$NON-NLS-1$ + IIndexManager manager= CCorePlugin.getIndexManager(); fIndex = manager.getIndex(proj, IIndexManager.ADD_DEPENDENCIES | IIndexManager.ADD_EXTENSION_FRAGMENTS_CONTENT_ASSIST); @@ -201,18 +206,19 @@ public class CContentAssistInvocationContext extends ContentAssistInvocationCont * @param editor the editor that content assist is invoked in * @param isAutoActivated indicates whether content assist was auto-activated */ - public CContentAssistInvocationContext(ITextViewer viewer, int offset, IEditorPart editor, boolean isCompletion, boolean isAutoActivated) { + public CContentAssistInvocationContext(ITextViewer viewer, int offset, IEditorPart editor, + boolean isCompletion, boolean isAutoActivated) { super(viewer, offset); Assert.isNotNull(editor); fEditor= editor; fIsCompletion= isCompletion; fIsAutoActivated= isAutoActivated; fTU = new Lazy() { - @Override - protected ITranslationUnit calculateValue() { - return CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(fEditor.getEditorInput()); - } - }; + @Override + protected ITranslationUnit calculateValue() { + return CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(fEditor.getEditorInput()); + } + }; } /** @@ -258,13 +264,13 @@ public class CContentAssistInvocationContext extends ContentAssistInvocationCont @Override public IASTCompletionNode getCompletionNode() { - //for scalability + // For scalability. if (fEditor != null && fEditor instanceof CEditor) { - CEditor editor = (CEditor)fEditor; + CEditor editor = (CEditor) fEditor; - // check to make sure we should attempt local parsing completions... for remote projects - // we should not do this - if(!editor.shouldProcessLocalParsingCompletions()) { + // Check to make sure we should attempt local parsing completions... for remote projects + // we should not do this. + if (!editor.shouldProcessLocalParsingCompletions()) { return null; } if (editor.isEnableScalablilityMode()) { diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/FunctionCompletionProposal.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/FunctionCompletionProposal.java index 21e5e3aceb6..fdfa6477fbd 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/FunctionCompletionProposal.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/FunctionCompletionProposal.java @@ -37,15 +37,16 @@ import org.eclipse.cdt.ui.CUIPlugin; * exit policy. */ public class FunctionCompletionProposal extends CCompletionProposal { - private boolean fHasParametersComputed= false; + private boolean fHasParametersComputed; private boolean fHasParameters; - protected IParameter [] fFunctionParameters; + protected IParameter[] fFunctionParameters; protected CContentAssistInvocationContext fContext; public FunctionCompletionProposal(String replacementString, int replacementOffset, int replacementLength, - Image image, String displayString, String idString, int relevance, ITextViewer viewer, IFunction function, CContentAssistInvocationContext context) { - super(replacementString, replacementOffset, replacementLength, image, displayString, idString, relevance, - viewer); + Image image, String displayString, String idString, int relevance, ITextViewer viewer, + IFunction function, CContentAssistInvocationContext context) { + super(replacementString, replacementOffset, replacementLength, image, displayString, + idString, relevance, viewer); fFunctionParameters = function.getParameters(); fContext = context; } @@ -63,8 +64,8 @@ public class FunctionCompletionProposal extends CCompletionProposal { } /** - * @return true if the method has any parameters, false if it has - * no parameters + * Returns true if the method has any parameters, false if it has + * no parameters. */ protected final boolean hasParameters() { if (!fHasParametersComputed) { @@ -75,28 +76,24 @@ public class FunctionCompletionProposal extends CCompletionProposal { } private boolean computeHasParameters() { - return (fFunctionParameters != null && fFunctionParameters.length != 0); + return fFunctionParameters != null && fFunctionParameters.length != 0; } protected static class ExitPolicy implements IExitPolicy { - final char fExitCharacter; public ExitPolicy(char exitCharacter) { fExitCharacter = exitCharacter; } - /* - * @see org.eclipse.jdt.internal.ui.text.link.LinkedPositionUI.ExitPolicy#doExit(org.eclipse.jdt.internal.ui.text.link.LinkedPositionManager, org.eclipse.swt.events.VerifyEvent, int, int) - */ @Override public ExitFlags doExit(LinkedModeModel environment, VerifyEvent event, int offset, int length) { - if (event.character == fExitCharacter) { - if (environment.anyPositionContains(offset)) + if (environment.anyPositionContains(offset)) { return new ExitFlags(ILinkedModeListener.UPDATE_CARET, false); - else + } else { return new ExitFlags(ILinkedModeListener.UPDATE_CARET, true); + } } switch (event.character) { @@ -127,8 +124,8 @@ public class FunctionCompletionProposal extends CCompletionProposal { ui.setExitPosition(fTextViewer, exit, 0, Integer.MAX_VALUE); ui.setCyclingMode(LinkedModeUI.CYCLE_NEVER); ui.enter(); - } catch (BadLocationException x) { - CUIPlugin.log(x); + } catch (BadLocationException e) { + CUIPlugin.log(e); } } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/Lazy.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/Lazy.java index ff0e85f77ce..d6a57829ea5 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/Lazy.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/Lazy.java @@ -12,16 +12,16 @@ package org.eclipse.cdt.internal.ui.text.contentassist; /** - * This class represents a field or variable which shall be initialized lazily when accessed the - * first time. It's value is computed once by the calculateValue() method. The value is - * accessed by value(). + * This class represents a field or variable which shall be initialized lazily when accessed + * the first time. It's value is computed once by the {@code calculateValue()} method. The value is + * accessed by {@code value()}. * * This implementation is NOT thread-safe! * * @param The type of the lazy initialized variable. */ public abstract class Lazy { - private final static Object NOT_INITIALIZED = new Object(); + private static final Object NOT_INITIALIZED = new Object(); private Object value = NOT_INITIALIZED; /** diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParameterGuesser.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParameterGuesser.java index 58e83d9ea0f..9431b8e310e 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParameterGuesser.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParameterGuesser.java @@ -26,6 +26,7 @@ import org.eclipse.jface.text.contentassist.ICompletionProposal; import org.eclipse.swt.graphics.Image; import org.eclipse.cdt.core.dom.ast.DOMException; +import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory; import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit; import org.eclipse.cdt.core.dom.ast.IBinding; import org.eclipse.cdt.core.dom.ast.ICompositeType; @@ -36,7 +37,6 @@ import org.eclipse.cdt.core.dom.ast.IPointerType; import org.eclipse.cdt.core.dom.ast.IType; import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.IVariable; -import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase; import org.eclipse.cdt.core.dom.ast.cpp.ICPPBinding; import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate; @@ -61,10 +61,10 @@ import org.eclipse.cdt.internal.ui.util.StringMatcher; import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; /** - * This iAPI layout is copied from org.eclipse.jdt.internal.ui.text.java.ParameterGuesser + * This class is based on org.eclipse.jdt.internal.ui.text.java.ParameterGuesser * - * This class triggers a code-completion that will track all global, local and member variables and order them logically for later - * use as a parameter guessing proposal. + * This class triggers a code-completion that will track all global, local and member variables and + * order them logically for later use as a parameter guessing proposal. */ public class ParameterGuesser { private IASTTranslationUnit fTranslationUnit; @@ -72,9 +72,9 @@ public class ParameterGuesser { private final Set fAlreadyMatchedNames; private final static class Variable { - /** - * Variable type. Used to choose the best guess based on scope (Local beats instance beats inherited beats global). + * Variable type. Used to choose the best guess based on scope + * (Local beats instance beats inherited beats global). */ public static final int LOCAL = 0; public static final int FIELD = 1; @@ -92,7 +92,8 @@ public class ParameterGuesser { public boolean alreadyMatched; - public Variable(IType qualifiedTypeName, String name, int variableType, boolean isAutoboxMatch, int positionScore, char[] triggerChars, ImageDescriptor descriptor) { + public Variable(IType qualifiedTypeName, String name, int variableType, + boolean isAutoboxMatch, int positionScore, char[] triggerChars, ImageDescriptor descriptor) { this.qualifiedTypeName= qualifiedTypeName; this.name= name; this.variableType= variableType; @@ -103,20 +104,15 @@ public class ParameterGuesser { this.alreadyMatched= false; } - /* - * @see Object#toString() - */ @Override public String toString() { - - StringBuffer buffer= new StringBuffer(); + StringBuilder buffer= new StringBuilder(); buffer.append(qualifiedTypeName); buffer.append(' '); buffer.append(name); buffer.append(" ("); //$NON-NLS-1$ buffer.append(variableType); buffer.append(')'); - return buffer.toString(); } } @@ -129,8 +125,9 @@ public class ParameterGuesser { fTranslationUnit = translationUnit; } - private List evaluateVisibleMatches(IType expectedType, ArrayList suggestions) throws CModelException { - ArrayList res= new ArrayList(); + private List evaluateVisibleMatches(IType expectedType, List suggestions) + throws CModelException { + ArrayList res= new ArrayList<>(); int size = suggestions.size(); for (int i= 0; i < size; i++) { Variable variable= createVariable(suggestions.get(i), expectedType, i); @@ -172,7 +169,6 @@ public class ParameterGuesser { int variableType = Variable.GLOBAL; if (element instanceof ICPPField) { variableType = Variable.FIELD; - } else if (element instanceof IVariable) { try { if (element instanceof ICPPBinding && ((ICPPBinding) element).isGloballyQualified()) { @@ -185,11 +181,13 @@ public class ParameterGuesser { } // Handle reference case - if (isReferenceTo(enclosingType, elementType)) + if (isReferenceTo(enclosingType, elementType)) { elementName = "&" + elementName; //$NON-NLS-1$ - else if (isReferenceTo(elementType, enclosingType)) + } else if (isReferenceTo(elementType, enclosingType)) { elementName = "*" + elementName; //$NON-NLS-1$ - return new Variable(elementType, elementName, variableType, false, positionScore, NO_TRIGGERS, getImageDescriptor(element)); + } + return new Variable(elementType, elementName, variableType, false, positionScore, + NO_TRIGGERS, getImageDescriptor(element)); } return null; } @@ -294,9 +292,6 @@ public class ParameterGuesser { } /** - * - * Copied from JDT - * * Returns the matches for the type and name argument, ordered by match quality. * * @param expectedType - the qualified type of the parameter we are trying to match @@ -307,7 +302,7 @@ public class ParameterGuesser { * @param isLastParameter true iff this proposal is for the last parameter of a method * @return returns the name of the best match, or null if no match found */ - public ICompletionProposal[] parameterProposals(IType expectedType, String paramName, Position pos, ArrayList suggestions, boolean fillBestGuess, boolean isLastParameter) throws CModelException { + public ICompletionProposal[] parameterProposals(IType expectedType, String paramName, Position pos, List suggestions, boolean fillBestGuess, boolean isLastParameter) throws CModelException { List typeMatches= evaluateVisibleMatches(expectedType, suggestions); typeMatches = removeDuplicates(typeMatches); orderMatches(typeMatches, paramName); @@ -345,11 +340,7 @@ public class ParameterGuesser { return ret; } - /** - * Copied from JDT - */ private static class MatchComparator implements Comparator { - private String fParamName; MatchComparator(String paramName) { @@ -389,10 +380,7 @@ public class ParameterGuesser { } /** - * - * Copied from JDT - * - * Determine the best match of all possible type matches. The input into this method is all + * Determines the best match of all possible type matches. The input into this method is all * possible completions that match the type of the argument. The purpose of this method is to * choose among them based on the following simple rules: * @@ -415,10 +403,7 @@ public class ParameterGuesser { } /** - * - * Copied from JDT - * - * Remove the duplicates from the list if any. + * Removes the duplicates from the list if any. */ private static List removeDuplicates(List typeMatches) { HashSet set = new HashSet(); @@ -427,9 +412,6 @@ public class ParameterGuesser { } /** - * - * Copied from JDT - * * Returns the longest common substring of two strings. * * @param first the first string @@ -437,13 +419,12 @@ public class ParameterGuesser { * @return the longest common substring */ private static String getLongestCommonSubstring(String first, String second) { - - String shorter= (first.length() <= second.length()) ? first : second; + String shorter= first.length() <= second.length() ? first : second; String longer= shorter == first ? second : first; int minLength= shorter.length(); - StringBuffer pattern= new StringBuffer(shorter.length() + 2); + StringBuilder pattern= new StringBuilder(shorter.length() + 2); String longestCommonSubstring= ""; //$NON-NLS-1$ for (int i= 0; i < minLength; i++) { @@ -466,11 +447,7 @@ public class ParameterGuesser { return longestCommonSubstring; } - /** - * Copied from JDT - */ private Image getImage(ImageDescriptor descriptor) { - return (descriptor == null) ? null : CUIPlugin.getImageDescriptorRegistry().get(descriptor); + return descriptor == null ? null : CUIPlugin.getImageDescriptorRegistry().get(descriptor); } - } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParameterGuessingProposal.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParameterGuessingProposal.java index e05a8c2528a..7e252eebf40 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParameterGuessingProposal.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParameterGuessingProposal.java @@ -14,6 +14,7 @@ package org.eclipse.cdt.internal.ui.text.contentassist; import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import org.eclipse.jface.text.BadLocationException; import org.eclipse.jface.text.BadPositionCategoryException; @@ -54,7 +55,7 @@ import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.EditorHighlightingSynchronizer; /** - * This API layout is copied from org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal + * This class is based on org.eclipse.jdt.internal.ui.text.java.ParameterGuessingProposal * * Extents the basic Function Compilation Proposal to add a linked mode for each of * the function parameters with a list of suggestions for each parameter. @@ -62,15 +63,16 @@ import org.eclipse.cdt.internal.ui.editor.EditorHighlightingSynchronizer; public class ParameterGuessingProposal extends FunctionCompletionProposal { private ICompletionProposal[][] fChoices; // initialized by guessParameters() private Position[] fPositions; // initialized by guessParameters() - private boolean fReplacementStringComputed = false; + private boolean fReplacementStringComputed; private IRegion fSelectedRegion; // initialized by apply() private IPositionUpdater fUpdater; private String fPrefix; // The string from the start of the statement to the parse offset. private String fFullPrefix; // The string from the start of the statement to the invocation offset. private char[][] fParametersNames; - private IType [] fParametersTypes; + private IType[] fParametersTypes; - public static ParameterGuessingProposal createProposal(CContentAssistInvocationContext context, CCompletionProposal proposal, IFunction function, String prefix) { + public static ParameterGuessingProposal createProposal(CContentAssistInvocationContext context, + CCompletionProposal proposal, IFunction function, String prefix) { String replacement = getParametersList(function); String fullPrefix = function.getName() + "("; //$NON-NLS-1$ int replacementOffset = proposal.getReplacementOffset(); @@ -89,10 +91,10 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { try { fullPrefix = context.getDocument().get(replacementOffset, context.getInvocationOffset() - replacementOffset); replacement = fullPrefix + replacement + ")"; //$NON-NLS-1$ - } catch (BadLocationException e1) { + } catch (BadLocationException e) { } try { - // remove ')' from the replacement string if it is auto appended. + // Remove ')' from the replacement string if it is auto appended. if (context.getDocument().getChar(context.getInvocationOffset()) == ')') replacement = replacement.substring(0, replacement.length() - 1); } catch (BadLocationException e) { @@ -103,7 +105,9 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { } replacementLength = replacement.length(); - ParameterGuessingProposal ret = new ParameterGuessingProposal(replacement, replacementOffset, replacementLength, proposal.getImage(), proposal.getDisplayString(), proposal.getIdString(), proposal.getRelevance(), context.getViewer(), function, context); + ParameterGuessingProposal ret = new ParameterGuessingProposal(replacement, replacementOffset, + replacementLength, proposal.getImage(), proposal.getDisplayString(), + proposal.getIdString(), proposal.getRelevance(), context.getViewer(), function, context); ret.setContextInformation(proposal.getContextInformation()); ret.fPrefix = prefix; ret.fFullPrefix = fullPrefix; @@ -155,9 +159,6 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { return super.getPrefixCompletionText(document, completionOffset); } - /** - * Copied from JDT - */ @Override public void apply(final IDocument document, char trigger, int offset) { try { @@ -235,9 +236,6 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { } } - /** - * Copied from JDT - */ @Override public Point getSelection(IDocument document) { if (fSelectedRegion == null) @@ -246,9 +244,6 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { return new Point(fSelectedRegion.getOffset(), fSelectedRegion.getLength()); } - /** - * Copied from JDT - */ @Override public String getReplacementString() { if (!fReplacementStringComputed) { @@ -259,9 +254,6 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { return super.getReplacementString(); } - /** - * Copied from JDT - */ private String computeReplacementString() { if (!hasParameters()) return super.getReplacementString(); @@ -269,21 +261,18 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { String replacement; try { replacement = computeGuessingCompletion(); - } catch (Exception x) { + } catch (Exception e) { fPositions = null; fChoices = null; - CUIPlugin.log(x); + CUIPlugin.log(e); return super.getReplacementString(); } return replacement; } - /** - * Copied from JDT with replacing JDT types with CDT types - */ private String computeGuessingCompletion() throws Exception { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); buffer.append(fFullPrefix); setCursorPosition(buffer.length()); @@ -316,28 +305,26 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { return buffer.toString(); } - /** - * Copied from JDT with replacing JDT types with CDT types - */ private ICompletionProposal[][] guessParameters(char[][] parameterNames) throws Exception { int count= parameterNames.length; fPositions= new Position[count]; fChoices= new ICompletionProposal[count][]; ParameterGuesser guesser= new ParameterGuesser(fContext.getCompletionNode().getTranslationUnit()); - ArrayList assignableElements = getAssignableElements(); + List assignableElements = getAssignableElements(); for (int i= count - 1; i >= 0; i--) { String paramName= new String(parameterNames[i]); - Position position= new Position(0,0); + Position position= new Position(0, 0); boolean isLastParameter= i == count - 1; - ArrayList allProposals = new ArrayList(); + List allProposals = new ArrayList<>(); CCompletionProposal proposal= new CCompletionProposal(paramName, 0, paramName.length(), null, paramName, 0); if (isLastParameter) proposal.setTriggerCharacters(new char[] { ',' }); allProposals.add(proposal); - ICompletionProposal[] argumentProposals= guesser.parameterProposals(fParametersTypes[i], paramName, position, assignableElements, true, isLastParameter); + ICompletionProposal[] argumentProposals= + guesser.parameterProposals(fParametersTypes[i], paramName, position, assignableElements, true, isLastParameter); allProposals.addAll(Arrays.asList(argumentProposals)); fPositions[i]= position; fChoices[i]= allProposals.toArray(new ICompletionProposal[allProposals.size()]); @@ -346,8 +333,8 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { return fChoices; } - private static IType[] getFunctionParametersTypes(IParameter [] functionParameters) { - IType [] ret = new IType[functionParameters.length]; + private static IType[] getFunctionParametersTypes(IParameter[] functionParameters) { + IType[] ret = new IType[functionParameters.length]; for (int i = 0; i < functionParameters.length; i++) { ret[i] = functionParameters[i].getType(); } @@ -355,7 +342,7 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { } - private static char[][] getFunctionParametersNames(IParameter [] functionParameters) { + private static char[][] getFunctionParametersNames(IParameter[] functionParameters) { char[][] parameterNames = new char[functionParameters.length][]; for (int i = 0; i < functionParameters.length; i++) { parameterNames[i] = functionParameters[i].getNameCharArray(); @@ -374,12 +361,12 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { * Returns a list of functions and variables that are defined in current context. * @return a list of assignable elements. */ - private ArrayList getAssignableElements() { + private List getAssignableElements() { int i = getStatementStartOffset(fContext.getDocument(), getStatementStartOffset()); CContentAssistInvocationContext c = new CContentAssistInvocationContext(fTextViewer, i, getCEditor(), true, false); IASTCompletionNode node = c.getCompletionNode(); - IASTName [] names = node.getNames(); - ArrayList allBindings = new ArrayList(); + IASTName[] names = node.getNames(); + List allBindings = new ArrayList<>(); for (IASTName name : names) { IASTCompletionContext astContext = name.getCompletionContext(); if (astContext != null) { @@ -421,9 +408,6 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { return offset; } - /** - * Copied from JDT - */ private void ensurePositionCategoryInstalled(final IDocument document, LinkedModeModel model) { if (!document.containsPositionCategory(getCategory())) { document.addPositionCategory(getCategory()); @@ -445,9 +429,6 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { } } - /** - * Copied from JDT - */ private void ensurePositionCategoryRemoved(IDocument document) { if (document.containsPositionCategory(getCategory())) { try { @@ -459,16 +440,11 @@ public class ParameterGuessingProposal extends FunctionCompletionProposal { } } - /** - * Copied from JDT - */ private String getCategory() { return "ParameterGuessingProposal_" + toString(); //$NON-NLS-1$ } /** - * Copied from JDT with replacing JDT types with CDT types. - * * Returns the currently active C/C++ editor, or null if it * cannot be determined. * diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParsingBasedProposalComputer.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParsingBasedProposalComputer.java index e61a675eb61..50a8d6c4d6a 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParsingBasedProposalComputer.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/ParsingBasedProposalComputer.java @@ -6,11 +6,10 @@ * http://www.eclipse.org/legal/epl-v10.html * * Contributors: - * QNX - Initial API and implementation - * Anton Leherbauer (Wind River Systems) - * Mentor Graphics (Mohamed Azab) - added the API to CDT + * QNX - Initial API and implementation + * Anton Leherbauer (Wind River Systems) + * Mentor Graphics (Mohamed Azab) - added the API to CDT *******************************************************************************/ - package org.eclipse.cdt.internal.ui.text.contentassist; import java.util.ArrayList; @@ -36,8 +35,7 @@ import org.eclipse.cdt.ui.text.contentassist.ICompletionProposalComputer; * @author Bryan Wilkinson */ public abstract class ParsingBasedProposalComputer implements ICompletionProposalComputer { - - private String fErrorMessage = null; + private String fErrorMessage; @Override public List computeCompletionProposals( @@ -73,8 +71,7 @@ public abstract class ParsingBasedProposalComputer implements ICompletionProposa public List computeContextInformation( ContentAssistInvocationContext context, IProgressMonitor monitor) { Collection proposals= computeCompletionProposals(context, monitor); - // remove duplicates - + // Remove duplicates proposals= (new LinkedHashSet(proposals)); List result= new ArrayList(); for (ICompletionProposal proposal : proposals) { @@ -102,11 +99,11 @@ public abstract class ParsingBasedProposalComputer implements ICompletionProposa } /** - * Compute base relevance depending on quality of name / prefix match. + * Computes base relevance depending on quality of name / prefix match. * - * @param prefix the completion pefix + * @param prefix the completion prefix * @param match the matching identifier - * @return a relevance value inidicating the quality of the name match + * @return a relevance value indicating the quality of the name match */ protected int computeBaseRelevance(String prefix, String match) { boolean caseMatch= prefix.length() > 0 && match.startsWith(prefix);