perl editor のpluginを作る コードアシスト すごく単純なもの

eclipseなんだしコードアシストは必須要件!
ただperlはコード上で変数がオブジェクトなのかはたまたなんのオブジェクトなのかの判断が難しい。
そこでctagsを利用し(5.7以降) キーワードファイルを作成しそれを元にコードアシストすることにする。

まずコードアシスト(ctrl+space)を押したときに候補となるリストを返却するクラスを実装する。

package com.appspot.charisoku30km.editor.perl.editors;

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;

public class PerlAssistProcessor implements IContentAssistProcessor {

    
    public PerlAssistProcessor(){
    }  
    
    //IContentAssistProcessorのメソッドの実装
    //コードアシストの候補となるキーワード(ICompletionProposalオブジェクト)の配列を返します
    @Override
    public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer,
            int offset) {
        
        String currentWord = getCurrentWord(viewer.getDocument(), offset);
        CompletionProposal[] words = new CompletionProposal[1];
        //現在の対象の文字列の長さと候補のワードの文字列の長さをから
        //置き換えるべき長さなどを設定しています。
        words[0] = new CompletionProposal(
                "nurupo", 
                offset - currentWord.length(), 
                currentWord.length(), 
                "nurupo".length()
            );

        return words;
    }
    
    //現在対象となっている文字列を取得する
    private String getCurrentWord(IDocument document, int documentOffset) {
        StringBuffer currentWord = new StringBuffer();
        char ch;
        
        try {
            for (int offset = documentOffset - 1 ;
                offset >=0 && !Character.isWhitespace(ch = document.getChar(offset)) ;
                offset--) {
                currentWord.insert(0, ch);
            }
            return currentWord.toString();
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public IContextInformation[] computeContextInformation(ITextViewer viewer,
            int offset) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public char[] getCompletionProposalAutoActivationCharacters() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public char[] getContextInformationAutoActivationCharacters() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public IContextInformationValidator getContextInformationValidator() {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String getErrorMessage() {
        // TODO Auto-generated method stub
        return null;
    }    


}

nurupoという文字列をアシスト候補として返却します。
次のSourceViewerConfigurationを継承し、このエディタ用のSourceViewerConfigurationを実装します。

package com.appspot.charisoku30km.editor.perl.editors;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

public class PerlConfiguration extends SourceViewerConfiguration {


    public PerlConfiguration() {
    }    
    
    //コンテンツアシスト設定の実装
    //デフォルトドキュメントの区画に対しPerlAssistProcessorを設定します。
    //デフォルトドキュメントの区画は現在の実装ではコードのどこでもデフォルトドキュメントの区画となります。
    public IContentAssistant getContentAssistant
      (ISourceViewer sourceViewer) {
        ContentAssistant assistant = new ContentAssistant();

        PerlAssistProcessor processor = new PerlAssistProcessor();
        assistant.setContentAssistProcessor(processor,IDocument.DEFAULT_CONTENT_TYPE);

        assistant.install(sourceViewer);

        return assistant;
    }

}

コンテンツアシストの実装と設定クラスができたらPerlOreditorクラスにこの作成した設定クラスPerlConfigurationを設定してあげます

package com.appspot.charisoku30km.editor.perl.editors;

import com.appspot.charisoku30km.editor.perl.editors.PerlConfiguration;
import org.eclipse.ui.editors.text.TextEditor;

public class PerlOreditor extends TextEditor {

    public PerlOreditor() {
        super();
        setSourceViewerConfiguration(new PerlConfiguration());
    }
    
}

これで起動してctrl+spaceでnurupoと表示されたらOKだ!