2014-10-19 60 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

相關問題