Download
Getting Started
Members
Projects
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
More
Community
Marketplace
Events
Planet Eclipse
Newsletter
Videos
Participate
Report a Bug
Forums
Mailing Lists
Wiki
IRC
How to Contribute
Working Groups
Automotive
Internet of Things
LocationTech
Long-Term Support
PolarSys
Science
OpenMDM
Toggle navigation
Bugzilla – Attachment 222245 Details for
Bug 70631
[content assist] Parameter Hints should infer the overloaded method version
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Requests
|
Help
|
Log In
[x]
|
Terms of Use
|
Copyright Agent
[patch]
Fixed version of previous patch. Counts parameters in call to sort method list.
countMethodParamsFix.patch (text/plain), 17.63 KB, created by
Tristan Hume
on 2012-10-12 14:03:51 EDT
(
hide
)
Description:
Fixed version of previous patch. Counts parameters in call to sort method list.
Filename:
MIME Type:
Creator:
Tristan Hume
Created:
2012-10-12 14:03:51 EDT
Size:
17.63 KB
patch
obsolete
>diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/JavaHeuristicScanner.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/JavaHeuristicScanner.java >index f4084a7..ee4c53e 100644 >--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/JavaHeuristicScanner.java >+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/JavaHeuristicScanner.java >@@ -7,6 +7,7 @@ > * > * Contributors: > * IBM Corporation - initial API and implementation >+ * Tristan Hume <trishume@gmail.com> - [content assist] Parameter Hints should infer the overloaded method version - https://bugs.eclipse.org/bugs/show_bug.cgi?id=70631 > *******************************************************************************/ > package org.eclipse.jdt.internal.ui.text; > >@@ -635,6 +636,229 @@ > } > > /** >+ * Finds the number of parameters in the method call at the cursor >+ * @param offset an offset in a parameters list. >+ * @return the number of parameters in the call or -1 if the number >+ * could not be found or the offset was not in a parameter list. >+ */ >+ public int countParameters(int offset) { >+ int closing = findClosingPeer(offset, offset+200, '(', ')'); >+ if(closing == JavaHeuristicScanner.NOT_FOUND) >+ return -1; >+ int opening = findOpeningPeer(offset, Math.max(-1, offset-200), '(', ')'); >+ if(opening == JavaHeuristicScanner.NOT_FOUND) >+ return -1; >+ int count; >+ try { >+ count = getCharCount(opening+1, closing, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$ >+ } catch (BadLocationException e) { >+ return -1; >+ } >+ // the number of parameters is the number of commas plus one >+ return count+1; >+ } >+ >+ /** >+ * Find the end of a block comment >+ * @param pos the position in the document to start from. Must be in a block comment. >+ * @param end the position at which to stop searching. >+ * @return the offset immediately past the end of the comment >+ * @throws BadLocationException thrown if pos or end is past the end of the document. >+ */ >+ public int getCommentEnd(int pos, int end) throws BadLocationException { >+ while (pos < end) { >+ char curr= fDocument.getChar(pos); >+ pos++; >+ if (curr == '*') { >+ if (pos < end && fDocument.getChar(pos) == '/') { >+ return pos + 1; >+ } >+ } >+ } >+ return end; >+ } >+ >+ /** >+ * Finds the end of a string literal >+ * @param pos the position in the document to start from. Must be in a block comment. >+ * @param end the position at which to stop searching. >+ * @param ch the string delimiter. Usually ' or " >+ * @return the offset of the character past the closing quote >+ * @throws BadLocationException thrown if pos or end is past the end of the document. >+ */ >+ public int getStringEnd(int pos, int end, char ch) throws BadLocationException { >+ while (pos < end) { >+ char curr= fDocument.getChar(pos); >+ pos++; >+ if (curr == '\\') { >+ // ignore escaped characters >+ pos++; >+ } else if (curr == ch) { >+ return pos; >+ } >+ } >+ return end; >+ } >+ >+ /** >+ * Counts characters in a specific span. Can consider nesting. >+ * >+ * For example: getCharCount(start, end, ",", "", true) >+ * will count the commas between start and end except those that are in nested brackets. >+ * If that span of the document contained "one, two, three(four,[six,seven]), eight" >+ * it would return 3. >+ * @param start The start of the range to search >+ * @param end The end of the range >+ * @param increments The characters on which to increment the count. I.E "<(" >+ * @param decrements The characters on which to decrement the count. I.E ")>" >+ * @param considerNesting Should nesting be considered. If true characters in nested brackets are not considered. >+ * @return the count at the end of the range. >+ * @throws BadLocationException thrown if the start or end are not valid locations in the document. >+ */ >+ public int getCharCount(final int start, final int end, String increments, String decrements, boolean considerNesting) throws BadLocationException { >+ >+ Assert.isTrue((increments.length() != 0 || decrements.length() != 0) && !increments.equals(decrements)); >+ >+ final int NONE= 0; >+ final int BRACKET= 1; >+ final int BRACE= 2; >+ final int PAREN= 3; >+ final int ANGLE= 4; >+ >+ int nestingMode= NONE; >+ int nestingLevel= 0; >+ >+ int charCount= 0; >+ int offset= start; >+ while (offset < end) { >+ char curr= fDocument.getChar(offset++); >+ switch (curr) { >+ case '/': >+ if (offset < end) { >+ char next= fDocument.getChar(offset); >+ if (next == '*') { >+ // a comment starts, advance to the comment end >+ offset= getCommentEnd(offset + 1, end); >+ } else if (next == '/') { >+ // '//'-comment: nothing to do anymore on this line >+ int nextLine= fDocument.getLineOfOffset(offset) + 1; >+ if (nextLine == fDocument.getNumberOfLines()) >+ offset= end; >+ else >+ offset= fDocument.getLineOffset(nextLine); >+ } >+ } >+ break; >+ case '*': >+ if (offset < end) { >+ char next= fDocument.getChar(offset); >+ if (next == '/') { >+ // we have been in a comment: forget what we read before >+ charCount= 0; >+ ++ offset; >+ } >+ } >+ break; >+ case '"': >+ case '\'': >+ offset= getStringEnd(offset, end, curr); >+ break; >+ case '[': >+ if (considerNesting) { >+ if (nestingMode == BRACKET || nestingMode == NONE) { >+ nestingMode= BRACKET; >+ nestingLevel++; >+ } >+ break; >+ } >+ //$FALL-THROUGH$ >+ case ']': >+ if (considerNesting) { >+ if (nestingMode == BRACKET) >+ if (--nestingLevel == 0) >+ nestingMode= NONE; >+ break; >+ } >+ //$FALL-THROUGH$ >+ case '(': >+ if (considerNesting) { >+ if (nestingMode == ANGLE) { >+ // generics heuristic failed >+ nestingMode=PAREN; >+ nestingLevel= 1; >+ } >+ if (nestingMode == PAREN || nestingMode == NONE) { >+ nestingMode= PAREN; >+ nestingLevel++; >+ } >+ break; >+ } >+ //$FALL-THROUGH$ >+ case ')': >+ if (considerNesting) { >+ if (nestingMode == PAREN) >+ if (--nestingLevel == 0) >+ nestingMode= NONE; >+ break; >+ } >+ //$FALL-THROUGH$ >+ case '{': >+ if (considerNesting) { >+ if (nestingMode == ANGLE) { >+ // generics heuristic failed >+ nestingMode=BRACE; >+ nestingLevel= 1; >+ } >+ if (nestingMode == BRACE || nestingMode == NONE) { >+ nestingMode= BRACE; >+ nestingLevel++; >+ } >+ break; >+ } >+ //$FALL-THROUGH$ >+ case '}': >+ if (considerNesting) { >+ if (nestingMode == BRACE) >+ if (--nestingLevel == 0) >+ nestingMode= NONE; >+ break; >+ } >+ //$FALL-THROUGH$ >+ case '<': >+ if (considerNesting) { >+ if (nestingMode == ANGLE || nestingMode == NONE && looksLikeClassInstanceCreationBackward(offset - 1, start - 1)) { >+ nestingMode= ANGLE; >+ nestingLevel++; >+ } >+ break; >+ } >+ //$FALL-THROUGH$ >+ case '>': >+ if (considerNesting) { >+ if (nestingMode == ANGLE) >+ if (--nestingLevel == 0) >+ nestingMode= NONE; >+ break; >+ } >+ //$FALL-THROUGH$ >+ default: >+ if (nestingLevel != 0) >+ continue; >+ >+ if (increments.indexOf(curr) >= 0) { >+ ++ charCount; >+ } >+ >+ if (decrements.indexOf(curr) >= 0) { >+ -- charCount; >+ } >+ } >+ } >+ >+ return charCount; >+ } >+ >+ /** > * Computes the surrounding block around <code>offset</code>. The search is started at the > * beginning of <code>offset</code>, i.e. an opening brace at <code>offset</code> will not be > * part of the surrounding block, but a closing brace will. >diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/ContentAssistProcessor.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/ContentAssistProcessor.java >index ab46c95..ea147e3 100644 >--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/ContentAssistProcessor.java >+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/ContentAssistProcessor.java >@@ -9,6 +9,7 @@ > * IBM Corporation - initial API and implementation > * Paul Fullbright <paul.fullbright@oracle.com> - content assist category enablement - http://bugs.eclipse.org/345213 > * Marcel Bruch <bruch@cs.tu-darmstadt.de> - [content assist] Allow to re-sort proposals - https://bugs.eclipse.org/bugs/show_bug.cgi?id=350991 >+ * Tristan Hume <trishume@gmail.com> - [content assist] Parameter Hints should infer the overloaded method version - https://bugs.eclipse.org/bugs/show_bug.cgi?id=70631 > *******************************************************************************/ > package org.eclipse.jdt.internal.ui.text.java; > >@@ -73,6 +74,7 @@ > import org.eclipse.jdt.internal.ui.JavaPlugin; > import org.eclipse.jdt.internal.ui.JavaUIMessages; > import org.eclipse.jdt.internal.ui.dialogs.OptionalMessageDialog; >+import org.eclipse.jdt.internal.ui.text.JavaHeuristicScanner; > > > /** >@@ -365,7 +367,8 @@ > List<IContextInformation> proposals= collectContextInformation(viewer, offset, monitor); > > monitor.subTask(JavaTextMessages.ContentAssistProcessor_sorting_contexts); >- List<IContextInformation> filtered= filterAndSortContextInformation(proposals, monitor); >+ int paramCount = new JavaHeuristicScanner(viewer.getDocument()).countParameters(offset); >+ List<IContextInformation> filtered= filterAndSortContextInformation(proposals, monitor, paramCount); > fNumberOfComputedResults= filtered.size(); > > IContextInformation[] result= filtered.toArray(new IContextInformation[filtered.size()]); >@@ -397,10 +400,28 @@ > * @param contexts the list of collected proposals (element type: > * {@link IContextInformation}) > * @param monitor a progress monitor >+ * @param paramCount the parameter count to favor, or -1 if none. > * @return the list of filtered and sorted proposals, ready for > * display (element type: {@link IContextInformation}) > */ >- protected List<IContextInformation> filterAndSortContextInformation(List<IContextInformation> contexts, IProgressMonitor monitor) { >+ protected List<IContextInformation> filterAndSortContextInformation(List<IContextInformation> contexts, IProgressMonitor monitor, int paramCount) { >+ if(paramCount >= 0) { >+ List<IContextInformation> newList = new ArrayList<IContextInformation>(4); >+ List<IContextInformation> addLater = new ArrayList<IContextInformation>(3); >+ // look through all the overloaded parameter lists looking for >+ // ones with the right length. Put those first and the rest after. >+ for(IContextInformation info : contexts) { >+ String params = info.getInformationDisplayString(); >+ int count = countOccurrences(params, ',') + 1; >+ if(count == paramCount) { >+ newList.add(info); >+ } else { >+ addLater.add(info); >+ } >+ } >+ newList.addAll(addLater); >+ return newList; >+ } > return contexts; > } > >@@ -459,6 +480,16 @@ > protected IProgressMonitor createProgressMonitor() { > return new NullProgressMonitor(); > } >+ >+ private static int countOccurrences(String haystack, char needle) { >+ int count = 0; >+ for (int i=0,l=haystack.length(); i < l; i++) { >+ if (haystack.charAt(i) == needle) { >+ count++; >+ } >+ } >+ return count; >+ } > > /** > * Creates the context that is passed to the completion proposal >diff --git a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaParameterListValidator.java b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaParameterListValidator.java >index 25830d6..4e16890 100644 >--- a/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaParameterListValidator.java >+++ b/org.eclipse.jdt.ui/ui/org/eclipse/jdt/internal/ui/text/java/JavaParameterListValidator.java >@@ -7,6 +7,7 @@ > * > * Contributors: > * IBM Corporation - initial API and implementation >+ * Tristan Hume <trishume@gmail.com> - [content assist] Parameter Hints should infer the overloaded method version - https://bugs.eclipse.org/bugs/show_bug.cgi?id=70631 > *******************************************************************************/ > package org.eclipse.jdt.internal.ui.text.java; > >@@ -16,8 +17,6 @@ > > import org.eclipse.swt.SWT; > import org.eclipse.swt.custom.StyleRange; >- >-import org.eclipse.core.runtime.Assert; > > import org.eclipse.jface.text.BadLocationException; > import org.eclipse.jface.text.IDocument; >@@ -57,181 +56,6 @@ > fCurrentParameter= -1; > } > >- private int getCommentEnd(IDocument d, int pos, int end) throws BadLocationException { >- while (pos < end) { >- char curr= d.getChar(pos); >- pos++; >- if (curr == '*') { >- if (pos < end && d.getChar(pos) == '/') { >- return pos + 1; >- } >- } >- } >- return end; >- } >- >- private int getStringEnd(IDocument d, int pos, int end, char ch) throws BadLocationException { >- while (pos < end) { >- char curr= d.getChar(pos); >- pos++; >- if (curr == '\\') { >- // ignore escaped characters >- pos++; >- } else if (curr == ch) { >- return pos; >- } >- } >- return end; >- } >- >- private int getCharCount(IDocument document, final int start, final int end, String increments, String decrements, boolean considerNesting) throws BadLocationException { >- >- Assert.isTrue((increments.length() != 0 || decrements.length() != 0) && !increments.equals(decrements)); >- >- final int NONE= 0; >- final int BRACKET= 1; >- final int BRACE= 2; >- final int PAREN= 3; >- final int ANGLE= 4; >- >- int nestingMode= NONE; >- int nestingLevel= 0; >- >- int charCount= 0; >- int offset= start; >- while (offset < end) { >- char curr= document.getChar(offset++); >- switch (curr) { >- case '/': >- if (offset < end) { >- char next= document.getChar(offset); >- if (next == '*') { >- // a comment starts, advance to the comment end >- offset= getCommentEnd(document, offset + 1, end); >- } else if (next == '/') { >- // '//'-comment: nothing to do anymore on this line >- int nextLine= document.getLineOfOffset(offset) + 1; >- if (nextLine == document.getNumberOfLines()) >- offset= end; >- else >- offset= document.getLineOffset(nextLine); >- } >- } >- break; >- case '*': >- if (offset < end) { >- char next= document.getChar(offset); >- if (next == '/') { >- // we have been in a comment: forget what we read before >- charCount= 0; >- ++ offset; >- } >- } >- break; >- case '"': >- case '\'': >- offset= getStringEnd(document, offset, end, curr); >- break; >- case '[': >- if (considerNesting) { >- if (nestingMode == BRACKET || nestingMode == NONE) { >- nestingMode= BRACKET; >- nestingLevel++; >- } >- break; >- } >- //$FALL-THROUGH$ >- case ']': >- if (considerNesting) { >- if (nestingMode == BRACKET) >- if (--nestingLevel == 0) >- nestingMode= NONE; >- break; >- } >- //$FALL-THROUGH$ >- case '(': >- if (considerNesting) { >- if (nestingMode == ANGLE) { >- // generics heuristic failed >- nestingMode=PAREN; >- nestingLevel= 1; >- } >- if (nestingMode == PAREN || nestingMode == NONE) { >- nestingMode= PAREN; >- nestingLevel++; >- } >- break; >- } >- //$FALL-THROUGH$ >- case ')': >- if (considerNesting) { >- if (nestingMode == PAREN) >- if (--nestingLevel == 0) >- nestingMode= NONE; >- break; >- } >- //$FALL-THROUGH$ >- case '{': >- if (considerNesting) { >- if (nestingMode == ANGLE) { >- // generics heuristic failed >- nestingMode=BRACE; >- nestingLevel= 1; >- } >- if (nestingMode == BRACE || nestingMode == NONE) { >- nestingMode= BRACE; >- nestingLevel++; >- } >- break; >- } >- //$FALL-THROUGH$ >- case '}': >- if (considerNesting) { >- if (nestingMode == BRACE) >- if (--nestingLevel == 0) >- nestingMode= NONE; >- break; >- } >- //$FALL-THROUGH$ >- case '<': >- if (considerNesting) { >- if (nestingMode == ANGLE || nestingMode == NONE && checkGenericsHeuristic(document, offset - 1, start - 1)) { >- nestingMode= ANGLE; >- nestingLevel++; >- } >- break; >- } >- //$FALL-THROUGH$ >- case '>': >- if (considerNesting) { >- if (nestingMode == ANGLE) >- if (--nestingLevel == 0) >- nestingMode= NONE; >- break; >- } >- //$FALL-THROUGH$ >- default: >- if (nestingLevel != 0) >- continue; >- >- if (increments.indexOf(curr) >= 0) { >- ++ charCount; >- } >- >- if (decrements.indexOf(curr) >= 0) { >- -- charCount; >- } >- } >- } >- >- return charCount; >- } >- >- private boolean checkGenericsHeuristic(IDocument document, int end, int bound) { >- JavaHeuristicScanner scanner= new JavaHeuristicScanner(document); >- return scanner.looksLikeClassInstanceCreationBackward(end, bound); >- } >- > /** > * @see IContextInformationValidator#isContextInformationValid(int) > */ >@@ -240,14 +64,14 @@ > try { > if (position < fPosition) > return false; >- >+ > IDocument document= fViewer.getDocument(); > IRegion line= document.getLineInformationOfOffset(fPosition); > > if (position < line.getOffset() || position >= document.getLength()) > return false; > >- return getCharCount(document, fPosition, position, "(<", ")>", false) >= 0; //$NON-NLS-1$ //$NON-NLS-2$ >+ return new JavaHeuristicScanner(document).getCharCount(fPosition, position, "(<", ")>", false) >= 0; //$NON-NLS-1$ //$NON-NLS-2$ > > } catch (BadLocationException x) { > return false; >@@ -262,7 +86,7 @@ > int currentParameter= -1; > > try { >- currentParameter= getCharCount(fViewer.getDocument(), fPosition, position, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$ >+ currentParameter= new JavaHeuristicScanner(fViewer.getDocument()).getCharCount(fPosition, position, ",", "", true); //$NON-NLS-1$//$NON-NLS-2$ > } catch (BadLocationException x) { > return false; > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Flags:
daniel_megert
:
review-
Actions:
View
|
Diff
Attachments on
bug 70631
:
222193
|
222194
|
222245