2014-10-19 109 views
0

我正在为Eclipse编写一个插件,我试图实现内容辅助。下面的代码适用于它覆盖现有文本并希望它插入的例外。如何使eclipse内容辅助插入而不是覆盖

任何帮助感激地收到。

的ContentAssistantProvider:

package astra.ide.editor.astra; 

import java.util.ArrayList; 
import java.util.HashSet; 
import java.util.List; 
import java.util.Set; 

import org.eclipse.core.resources.IFile; 
import org.eclipse.jdt.core.IMethod; 
import org.eclipse.jdt.core.IType; 
import org.eclipse.jdt.core.JavaModelException; 
import org.eclipse.jface.text.BadLocationException; 
import org.eclipse.jface.text.IDocument; 
import org.eclipse.jface.text.ITextViewer; 
import org.eclipse.jface.text.contentassist.CompletionProposal; 
import org.eclipse.jface.text.contentassist.ICompletionProposal; 
import org.eclipse.jface.text.contentassist.IContentAssistProcessor; 
import org.eclipse.jface.text.contentassist.IContextInformation; 
import org.eclipse.jface.text.contentassist.IContextInformationValidator; 
import org.eclipse.ui.IEditorPart; 
import org.eclipse.ui.IFileEditorInput; 
import org.eclipse.ui.PlatformUI; 

import astra.ast.core.ASTRACore; 
import astra.ast.core.ParseException; 
import astra.ast.jdt.JDTHelper; 

public class ASTRAContentAssistantProcessor implements IContentAssistProcessor { 
    static Set<Character> set = new HashSet<Character>(); 

    static { 
     set.add(' '); 
     set.add('('); 
     set.add(','); 
     set.add(')'); 
     set.add(';'); 
     set.add('{'); 
     set.add('}'); 
    } 

    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) { 
     IDocument doc = viewer.getDocument(); 

     String context = ""; 
     int i = offset-2; 
     try { 
      char ch = doc.getChar(i); 
      while (!set.contains(ch)) { 
       context = ch + context; 
       ch = doc.getChar(--i); 
      } 
     } catch (BadLocationException e) { 
      e.printStackTrace(); 
     } 

     String text = doc.get(); 
     String cls = ""; 
     int index = text.indexOf("package"); 
     if (index > -1) { 
      int index2 = text.indexOf(";", index); 
      cls = text.substring(index+8, index2-1).trim() + "."; 
     } 


     index = text.indexOf("agent", index); 
     int index2 = text.indexOf("extends", index); 
     if (index2 == -1) { 
      index2 = text.indexOf("{", index); 
     } 
     System.out.println("cls: " + text.substring(index+6, index2-1).trim()); 
     cls += text.substring(index+6, index2-1).trim(); 


     List<ICompletionProposal> list = new ArrayList<ICompletionProposal>(); 

     IEditorPart editorPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor(); 
     if(editorPart != null) { 
      IFileEditorInput input = (IFileEditorInput)editorPart.getEditorInput() ; 
      IFile file = input.getFile(); 

      JDTHelper helper = new JDTHelper(file.getProject()); 
      try { 
       String moduleClass = ASTRACore.getModuleClass(helper, cls, context); 
       IType type = helper.resolveClass(moduleClass); 
       for (IMethod mthd : type.getMethods()) { 
        String template = mthd.getElementName() + "()"; 
        list.add(new CompletionProposal(template, offset, template.length(), template.length())); 
       } 

      } catch (ParseException e) { 
       e.printStackTrace(); 
      } catch (JavaModelException e) { 
       e.printStackTrace(); 
      } 
     } 

     return list.toArray(new ICompletionProposal[list.size()]); 
    } 

    public IContextInformation[] computeContextInformation(ITextViewer viewer, 
      int offset) { 
     return null; 
    } 

    public char[] getCompletionProposalAutoActivationCharacters() { 
     return new char[] { '.' }; 
    } 

    public char[] getContextInformationAutoActivationCharacters() { 
     return null; 
    } 

    public String getErrorMessage() { 
     return null; 
    } 

    public IContextInformationValidator getContextInformationValidator() { 
     return null; 
    } 
} 

我加入SourceConfigurationViewer的代码是:

public IContentAssistant getContentAssistant(ISourceViewer sourceViewer) { 
    ContentAssistant assistant = new ContentAssistant(); 
    assistant.setInformationControlCreator(getInformationControlCreator(sourceViewer)); 
    assistant.setContentAssistProcessor(new ASTRAContentAssistantProcessor(),IDocument.DEFAULT_CONTENT_TYPE); 
    assistant.enableAutoActivation(true); 

    return assistant; 
} 

回答

0

在你CompletionProposal创建:

new CompletionProposal(template, offset, template.length(), template.length()) 

第三个参数是要替换的文本的长度为offset,因此使用template.length()会导致模板被覆盖。要插入使用:

new CompletionProposal(template, offset, 0, template.length()) 

,使用0作为第三个参数使要插入的文本。

+0

谢谢!像梦一样工作! – Rem 2014-10-19 23:33:26

相关问题