View | Details | Raw Unified | Return to bug 81403 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/cdt/internal/ui/text/contentassist/CCompletionProcessor.java (-9 / +109 lines)
Lines 10-15 Link Here
10
import java.util.Iterator;
10
import java.util.Iterator;
11
import java.util.LinkedList;
11
import java.util.LinkedList;
12
import java.util.List;
12
import java.util.List;
13
import java.util.regex.Pattern;
13
14
14
import org.eclipse.cdt.core.model.ICElement;
15
import org.eclipse.cdt.core.model.ICElement;
15
import org.eclipse.cdt.core.model.ITranslationUnit;
16
import org.eclipse.cdt.core.model.ITranslationUnit;
Lines 32-44 Link Here
32
import org.eclipse.cdt.ui.CUIPlugin;
33
import org.eclipse.cdt.ui.CUIPlugin;
33
import org.eclipse.cdt.ui.IFunctionSummary;
34
import org.eclipse.cdt.ui.IFunctionSummary;
34
import org.eclipse.cdt.ui.IWorkingCopyManager;
35
import org.eclipse.cdt.ui.IWorkingCopyManager;
35
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
36
import org.eclipse.cdt.ui.text.ICCompletionProposal;
36
import org.eclipse.cdt.ui.text.ICCompletionProposal;
37
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
37
import org.eclipse.core.resources.IProject;
38
import org.eclipse.core.resources.IProject;
38
import org.eclipse.jface.preference.IPreferenceStore;
39
import org.eclipse.jface.preference.IPreferenceStore;
39
import org.eclipse.jface.text.BadLocationException;
40
import org.eclipse.jface.text.BadLocationException;
40
import org.eclipse.jface.text.IDocument;
41
import org.eclipse.jface.text.IDocument;
41
import org.eclipse.jface.text.ITextViewer;
42
import org.eclipse.jface.text.ITextViewer;
43
import org.eclipse.jface.text.ITypedRegion;
44
import org.eclipse.jface.text.TypedRegion;
42
import org.eclipse.jface.text.contentassist.ContextInformation;
45
import org.eclipse.jface.text.contentassist.ContextInformation;
43
import org.eclipse.jface.text.contentassist.ICompletionProposal;
46
import org.eclipse.jface.text.contentassist.ICompletionProposal;
44
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
47
import org.eclipse.jface.text.contentassist.IContentAssistProcessor;
Lines 54-65 Link Here
54
 */
57
 */
55
public class CCompletionProcessor implements IContentAssistProcessor {
58
public class CCompletionProcessor implements IContentAssistProcessor {
56
59
60
    
57
	private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
61
	private static class ContextInformationWrapper implements IContextInformation, IContextInformationExtension {
58
		
62
		
59
		private final IContextInformation fContextInformation;
63
		private final IContextInformation fContextInformation;
60
		private int fPosition;
64
		private int fPosition;
61
		
65
		
62
		public ContextInformationWrapper(IContextInformation contextInformation) {
66
		public ContextInformationWrapper(IContextInformation contextInformation) {            
63
			fContextInformation= contextInformation;
67
			fContextInformation= contextInformation;
64
		}
68
		}
65
		
69
		
Lines 122-128 Link Here
122
	
126
	
123
	public CCompletionProcessor(IEditorPart editor) {
127
	public CCompletionProcessor(IEditorPart editor) {
124
		fEditor = (CEditor) editor;
128
		fEditor = (CEditor) editor;
125
	
126
		// Needed for search
129
		// Needed for search
127
		searchResultCollector = new BasicSearchResultCollector ();
130
		searchResultCollector = new BasicSearchResultCollector ();
128
		resultCollector = new ResultCollector();
131
		resultCollector = new ResultCollector();
Lines 327-338 Link Here
327
		if (fCurrentCompletionNode != null) {
330
		if (fCurrentCompletionNode != null) {
328
			addProposalsFromSearch(fCurrentCompletionNode, completions);
331
			addProposalsFromSearch(fCurrentCompletionNode, completions);
329
			addProposalsFromCompletionContributors(fCurrentCompletionNode, completions);
332
			addProposalsFromCompletionContributors(fCurrentCompletionNode, completions);
330
			addProposalsFromTemplates(viewer, fCurrentCompletionNode, completions);		
333
			addProposalsFromTemplates(viewer, fCurrentCompletionNode, completions);
331
			return order( (ICCompletionProposal[]) completions.toArray(new ICCompletionProposal[0]));
334
            ICCompletionProposal fastProposal = null;
335
            if (completions.size() > 1) {
336
                fastProposal = checkForFastCompletion(completions);
337
            }
338
            if (fastProposal == null) {
339
                return order((ICCompletionProposal[]) completions.toArray(new ICCompletionProposal[0]));
340
            }
341
            return new ICCompletionProposal[] { fastProposal };
332
		}
342
		}
333
		return null;			
343
		return null;			
334
	}
344
	}
335
	
345
346
    /**
347
     * Determines if character matches separator in fast completion postfix end.
348
     * 
349
     * @param ch
350
     *            Character to check.
351
     * @return <code>true</code> if matches.
352
     */
353
    private static boolean isProposalEnd(char ch) {
354
        return ch == '(' || Character.isWhitespace(ch);
355
    }
356
357
    /**
358
     * Retrieves matching proposals. These which matches proposals.
359
     * @param proposals Proposals to check.
360
     * @param prefixChars Prefix to match.
361
     * @return List of proposals which match to prefix.
362
     */
363
    private static List getMatchingProposals(List proposals, char[] prefixChars)
364
    {
365
        final List matchingCompletions = new LinkedList();
366
        final int size = proposals.size();
367
368
        // Retrieves only matching proposals.
369
        for (int i = 0; i < size; i++) {
370
            final ICCompletionProposal proposal = (ICCompletionProposal) proposals.get(i);
371
            final char[] proposalChars = proposal.getDisplayString().toCharArray();
372
            boolean skip = false;
373
            for (int j = 0; j < prefixChars.length; j++) {
374
                if (prefixChars[j] != proposalChars[j]) {
375
                    skip = true;
376
                    break;
377
                }
378
            }
379
            if (!skip) {
380
                matchingCompletions.add(proposal);                    
381
            }
382
        }
383
        return matchingCompletions;
384
    }
385
    /**
386
     * Checks if completions have their common part at the begining.
387
     * If yes, it creates proposal with usage of this part.
388
     * It is case sensitive.
389
390
     * @param completions Completions for check.
391
     * @return Found completion or <code>null</code>
392
     */
393
    private ICCompletionProposal checkForFastCompletion(List completions)
394
    {
395
        final char[] prefixChars = fCurrentCompletionNode.getCompletionPrefix().toCharArray();
396
        final List matching = getMatchingProposals(completions, prefixChars);
397
398
        // If no matching, all proposals should be shown.        
399
        final int size = matching.size();
400
        if (size == 0) {
401
            return null;
402
        }
403
404
        final StringBuffer resultProposal = new StringBuffer();
405
        final char[] current = ((ICCompletionProposal) matching.get(0)).getDisplayString().toCharArray();
406
        for (int currentCharIdx = prefixChars.length; currentCharIdx < current.length; currentCharIdx++) {
407
            boolean end = false;
408
            final char ch = current[currentCharIdx];
409
            if (isProposalEnd(ch))
410
            {
411
                break;
412
            }
413
            for (int i = 0; i < size; i++) {
414
                final char[] proposalChars = ((ICCompletionProposal) matching.get(i)).getDisplayString().toCharArray();
415
                if (currentCharIdx < proposalChars.length) {
416
                    if (proposalChars[currentCharIdx] != ch) {
417
                        end = true;
418
                        break;
419
                    }
420
                }
421
                else {
422
                    end = true;
423
                    break;
424
                }
425
            }
426
            if (end) {
427
                break;
428
            }
429
            resultProposal.append(ch);
430
        }
431
        return resultProposal.length() == 0
432
            ? null
433
            : new CCompletionProposal(resultProposal.toString(), fCurrentOffset, resultProposal.length(), null, null, 0);
434
    }    
435
    
336
	private void addProposalsFromTemplates(ITextViewer viewer, IASTCompletionNode completionNode, List completions){
436
	private void addProposalsFromTemplates(ITextViewer viewer, IASTCompletionNode completionNode, List completions){
337
		if (completionNode == null)
437
		if (completionNode == null)
338
			return;
438
			return;
Lines 343-351 Link Here
343
		IASTCompletionNode.CompletionKind kind = completionNode.getCompletionKind();
443
		IASTCompletionNode.CompletionKind kind = completionNode.getCompletionKind();
344
444
345
		
445
		
346
//		if ((kind == IASTCompletionNode.CompletionKind.VARIABLE_TYPE) ||
446
// if ((kind == IASTCompletionNode.CompletionKind.VARIABLE_TYPE) ||
347
//				(kind == IASTCompletionNode.CompletionKind.CLASS_REFERENCE)
447
// (kind == IASTCompletionNode.CompletionKind.CLASS_REFERENCE)
348
//			|| (kind == IASTCompletionNode.CompletionKind.SINGLE_NAME_REFERENCE)) {
448
// || (kind == IASTCompletionNode.CompletionKind.SINGLE_NAME_REFERENCE)) {
349
			addProposalsFromTemplateEngine(viewer, fTemplateEngine, completions);
449
			addProposalsFromTemplateEngine(viewer, fTemplateEngine, completions);
350
//		}
450
//		}
351
	}
451
	}

Return to bug 81403