7

Eclipseで、Tabキーを使用してメソッド呼び出しのパラメーター間をジャンプできるのが好きです。プラグインにも同様の機能を提供したいと思います。正確には、エディターにテキストを挿入しています。特定の構文を強調表示し、プログラマーがTabキーを使用して次の一致にジャンプできるようにします。

これが例です。次のスニペットを動的に作成したとしましょう。

String a = "bogus string";
int i = a.[?]

それをエディターに挿入し、[?]強調表示して変更できるようにします(ユーザーが入力する場合がありますlength())。また、フラグメントが多い場合は、[?]Tabキーを使って次のフラグメントに移動してほしい。

少し調べてみると、テンプレートを使用して実行できる可能性があることがわかりました。しかし、Web上で関連する例を見つけることができません。誰かがこれを経験したことがありますか?

アップデート:

私はまだ解決策を思い付くことができませんが、役に立つかもしれない2つのリンクを見つけました。

リンク1

リンク2

4

3 に答える 3

8

サンプルハンドラコード:

AbstractTextEditor activeEditor = 
        (AbstractTextEditor) HandlerUtil.getActiveEditor(event);

ISourceViewer sourceViewer = 
        (ISourceViewer) activeEditor.getAdapter(ITextOperationTarget.class);

Point range = sourceViewer.getSelectedRange();

// You can generate template dynamically here!
Template template = new Template("sample", 
        "sample description", 
        "no-context", 
        "private void ${name}(){\r\n" + 
        "\tSystem.out.println(\"${name}\")\r\n"
        + "}\r\n", true);

IRegion region = new Region(range.x, range.y);
TemplateContextType contextType = new TemplateContextType("test");
TemplateContext ctx =
    new DocumentTemplateContext(contextType, 
        sourceViewer.getDocument(), 
        range.x, 
        range.y);

TemplateProposal proposal 
    = new TemplateProposal(template, ctx, region, null);

proposal.apply(sourceViewer, (char) 0, 0, 0);

結果:

ここに画像の説明を入力してください

org.eclipse.jdt.ui.javaCompletionProposalComputer拡張機能を使用することをお勧めします。それはあなたがテンプレートをより合法的な方法で貢献することを可能にします。

ISourceViewer私のコードでは、合法的に取得する方法がないため、ハッキングがあります。ISourceViewerそれ自体は知ってITextTargetOperationいますが、API(Illegal Casting)ではありません。TemplateCompletionProcessorまた、テンプレートは、またはによって使用されるように設計されていますTemplateCompletionProposalComputer

于 2012-11-02T04:12:36.137 に答える
1

何が欲しいのか完全にはわかりませんが、テンプレートを使ってやりたいことができるかもしれません。

たとえば、Javaエディタを開き、メソッド内にカーソルを置き、arraya次にctlr-spaceと入力して、ポップアップメニューからarrayaddを選択します。文字列が強調表示されたテンプレートが表示され、タブを押すと次の変数にジャンプします。テンプレートソースは、で見ることができます。

環境設定->Java->エディタ->テンプレート

${array_type}[] ${result:newName(array)} = new ${array_type}[${array}.length + 1];
System.arraycopy(${array}, 0, ${result}, 0, ${array}.length);
${result}[${array}.length]= ${var};

$で始まるものはすべて入力可能な変数であり、テンプレートに入力しながら変数間をタブで移動できます。

于 2012-10-27T02:55:48.173 に答える
0

私の答えはjeeeyulの答えに基づいています。違いは、テンプレート自体だけでなく、それを自動的に解決して追加するためのインポートも必要だったことです。これは、JDTのものを使用して、次の方法で実行できます。

AbstractTextEditor activeEditor = 
            (AbstractTextEditor) HandlerUtil.getActiveEditor(event);
    if (activeEditor == null) {
        return null;
    }
    ITypeRoot element = EditorUtility.getEditorInputJavaElement(activeEditor, true);
    if (element == null) {
        return null;
    }
    ICompilationUnit unit = element.getAdapter(ICompilationUnit.class);
    if (unit == null) {
        return null;
    }
    ISourceViewer sourceViewer = (ISourceViewer) activeEditor.getAdapter(ITextOperationTarget.class);
    Point range = sourceViewer.getSelectedRange();
    // You can generate template dynamically here!
    Template template = new Template("new List", "Add new list creation", JavaContextType.ID_STATEMENTS,
            "List<${type}> ${name:newName(java.util.List)} = new ArrayList<${type}>();${:import(java.util.List, java.util.ArrayList)}",
            true);
    IRegion region = new Region(range.x, range.y);
    JavaContextType contextType = new JavaContextType();
    contextType.setId(JavaContextType.ID_STATEMENTS); //Set context type, for which we apply this template
    contextType.addResolver(new ImportsResolver("import","import")); //Add imports resolver if we want imports to be added automatically for some template
    CompilationUnitContext ctx = new JavaContext(contextType, sourceViewer.getDocument(), range.x,
            range.y, unit);
    TemplateProposal proposal = new TemplateProposal(template, ctx, region, null);
    proposal.apply(sourceViewer, (char) 0, 0, 0);
于 2018-03-09T11:31:59.987 に答える