From 141d4a2510dd4fa54fb9a61145567c065dc67ccc Mon Sep 17 00:00:00 2001 From: Doug Schaefer Date: Mon, 25 Apr 2005 17:07:51 +0000 Subject: [PATCH] Hooked up search to the new content assist. --- core/org.eclipse.cdt.ui/plugin.xml | 4 + .../SearchCompletionContributor.java | 99 +++++++++++++++++++ 2 files changed, 103 insertions(+) create mode 100644 core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/SearchCompletionContributor.java diff --git a/core/org.eclipse.cdt.ui/plugin.xml b/core/org.eclipse.cdt.ui/plugin.xml index 994e884dc86..54060e4d2f3 100644 --- a/core/org.eclipse.cdt.ui/plugin.xml +++ b/core/org.eclipse.cdt.ui/plugin.xml @@ -1361,6 +1361,10 @@ class="org.eclipse.cdt.internal.ui.text.template.TemplateEngine" id="CodeTemplates" priority="2"/> + diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/SearchCompletionContributor.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/SearchCompletionContributor.java new file mode 100644 index 00000000000..06f8f70dffe --- /dev/null +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/contentassist/SearchCompletionContributor.java @@ -0,0 +1,99 @@ +/********************************************************************** + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Common Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/cpl-v10.html + **********************************************************************/ +package org.eclipse.cdt.internal.ui.text.contentassist; + +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; +import org.eclipse.cdt.core.dom.ast.IASTFieldReference; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.filetype.ICFileType; +import org.eclipse.cdt.core.filetype.ICFileTypeConstants; +import org.eclipse.cdt.core.model.ICElement; +import org.eclipse.cdt.core.model.IWorkingCopy; +import org.eclipse.cdt.core.search.BasicSearchMatch; +import org.eclipse.cdt.core.search.BasicSearchResultCollector; +import org.eclipse.cdt.core.search.ICSearchConstants; +import org.eclipse.cdt.core.search.ICSearchPattern; +import org.eclipse.cdt.core.search.ICSearchScope; +import org.eclipse.cdt.core.search.SearchEngine; +import org.eclipse.cdt.internal.ui.viewsupport.CElementImageProvider; +import org.eclipse.cdt.ui.CUIPlugin; +import org.eclipse.cdt.ui.text.contentassist.ICompletionContributor; +import org.eclipse.jface.text.ITextViewer; +import org.eclipse.swt.graphics.Image; + +public class SearchCompletionContributor implements ICompletionContributor { + + public void contributeCompletionProposals(ITextViewer viewer, int offset, + IWorkingCopy workingCopy, ASTCompletionNode completionNode, + List proposals) + { + ICFileType fileType = CCorePlugin.getDefault().getFileType(workingCopy.getCProject().getProject(), workingCopy.getElementName()); + // and only for C source files + if (fileType.isHeader() || ! fileType.getLanguage().getId().equals(ICFileTypeConstants.LANG_C)) + return; + + if (completionNode != null) { + IASTName[] names = completionNode.getNames(); + for (int i = 0; i < names.length; i++) { + IASTName name = names[i]; + + // not hooked up, ignore + if (name.getTranslationUnit() == null) + continue; + + // ignore if this is a member access + if (name.getParent() instanceof IASTFieldReference) + continue; + + // Create search engine + SearchEngine searchEngine = new SearchEngine(); + searchEngine.setWaitingPolicy( ICSearchConstants.FORCE_IMMEDIATE_SEARCH ); + + // Create search scope + ICElement[] projects = new ICElement[] { workingCopy.getCProject() }; + ICSearchScope scope = SearchEngine.createCSearchScope(projects, true); + + // Create the pattern + String prefix = new String(name.toCharArray()) + "*"; + ICSearchPattern pattern = SearchEngine.createSearchPattern(prefix, ICSearchConstants.FUNCTION, ICSearchConstants.DEFINITIONS, false); + + // Run the search + BasicSearchResultCollector collector = new BasicSearchResultCollector(); + try { + searchEngine.search(CUIPlugin.getWorkspace(), pattern, scope, collector, false); + } catch (InterruptedException e) { + return; + } + + Set results = collector.getSearchResults(); + Iterator iResults = results.iterator(); + while (iResults.hasNext()) { + BasicSearchMatch match = (BasicSearchMatch)iResults.next(); + handleFunction(match.getName(), viewer, completionNode, offset, proposals); + } + } + } + // TODO else search the prefix text + } + + private void handleFunction(String name, ITextViewer viewer, ASTCompletionNode completionNode, int offset, List proposals) { + int repLength = completionNode.getLength(); + int repOffset = offset - repLength; + Image image = CUIPlugin.getImageDescriptorRegistry().get(CElementImageProvider.getFunctionImageDescriptor()); + String repString = name + "()"; + CCompletionProposal proposal = new CCompletionProposal(repString, repOffset, repLength, image, repString, 1, viewer); + proposal.setCursorPosition(repString.length() - 1); + proposals.add(proposal); + } + +}