1

ファイルを右クリックすると表示される[ソース]メニューの下に作成したポップアップメニュー拡張機能を使用して、ファイルの内容を正常に更新することができました。

ファイルが変更されており、保存する必要があることを示したいと思います。現在、ファイルの内容は自動的に変更されて保存されます。IFile.touchメソッドを使用すると、ファイルを保存する必要がある状態になると思いましたが、それが発生することはありません。

これが私のコードです...

public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
    IEditorInput input = editorPart.getEditorInput();
    InputStream is = null;
    if (input instanceof FileEditorInput) {
        IFile file = ((FileEditorInput) input).getFile();
        try {
            is = file.getContents();
            String originalContents = convertStreamToString(is);
            String newContents = originalContents + "testing changing the contents...";
            InputStream newInput = new ByteArrayInputStream(newContents.getBytes());
            file.setContents(newInput, false, true, null);
            file.touch(null);
        } catch (CoreException e) {
            MessageDialog.openError(
                window.getShell(),
                "Generate Builder Error",
                "An Exception has been thrown when interacting with file " + file.getName() +
                ": " + e.getMessage());
        }
    }
    return null;
}
4

2 に答える 2

3

ファイルの内容を保存する必要があるとマークしたい場合は、保存する必要があるとしてプロンプトを表示するメモリ内表現(入力を取得するエディター)を操作する必要があります。

于 2013-02-20T17:17:04.523 に答える
0

nitindとこの投稿の助けに基づいて-Eclipseエディターからプラグインコマンドを介して選択したコードを置き換えます-テキストエディターを更新することができ、ファイルは変更されたものとして表示されます。重要なのは、FileEditorの代わりにITextEditorとIDocumentProviderを使用することでした。

これが更新されたコードです...

public Object execute(ExecutionEvent event) throws ExecutionException {
    IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
    IEditorPart editorPart = HandlerUtil.getActiveEditor(event);
    if (editorPart instanceof ITextEditor ) {
        ITextEditor editor = (ITextEditor)editorPart;
        IDocumentProvider prov = editor.getDocumentProvider();
        IDocument doc = prov.getDocument( editor.getEditorInput() );
        String className = getClassName(doc.get());
        ISelection sel = editor.getSelectionProvider().getSelection();
        if (sel instanceof TextSelection ) {
            TextSelection textSel = (TextSelection)sel;
            String newText = generateBuilderText(className, textSel.getText());
            try {
                doc.replace(textSel.getOffset(), textSel.getLength(), newText);
            } catch (Exception e) {
                MessageDialog.openError(
                    window.getShell(),
                    "Generate Builder Error",
                    "An Exception has been thrown when attempting to replace " 
                    + "text in editor " + editor.getTitle() +
                    ": " + e.getMessage());
            }
        }
    }
    return null;
}
于 2013-02-21T15:46:22.937 に答える