Aktivierung eines contentassistent von einem Texteditor

darksmilie

Erfahrenes Mitglied
Hi,

ich habe mir ein eigenen XML Editor geschrieben. Nun möchte ich, das mein Editor auch eine Autovervollständigung besitzt, aber ich habe das Problem, das mein contentassistent nicht aktiv ist, bzw. nix macht, wenn ich die Tastenkombination drücke.

hier ist mein code für mein content assistent:
Editor.properties:
Code:
## Actions ##

ContentAssistProposal.label=Content Assist@Ctrl+SPACE
ContentAssistProposal.tooltip=Content Assist
ContentAssistProposal.image=
ContentAssistProposal.description=Content Assist

XMLCompletionProcessor:
Code:
public class XMLCompletionProcessor implements IContentAssistProcessor {

  private final static String[] proposalDisplays = new String[] { "test" };

  private final static String[] proposalReplacements = new String[] { "TEST" };

  public ICompletionProposal[] computeCompletionProposals(
      final ITextViewer viewer, final int documentOffset) {
    // we return just the list of proposals
    int length = proposalDisplays.length;
    ICompletionProposal[] result = new ICompletionProposal[length];
    for (int i = 0; i < length; i++) {
      String toDisplay = proposalDisplays[i];
      IContextInformation info = new ContextInformation(toDisplay, toDisplay);
      result[i] = new CompletionProposal(proposalReplacements[i],
          documentOffset, 0, proposalReplacements[i].length(), null,
          proposalDisplays[i], info, "Test2");
    }
    return result;
  }

  public IContextInformation[] computeContextInformation(
      final ITextViewer viewer, final int documentOffset) {
    // no context infopops
    return null;
  }

  public char[] getCompletionProposalAutoActivationCharacters() {
    // no auto activation characters, we trigger only on user request
    return new char[0];
  }

  public char[] getContextInformationAutoActivationCharacters() {
    // no context infopops
    return new char[0];
  }

  public String getErrorMessage() {
    return null;
  }

  public IContextInformationValidator getContextInformationValidator() {
    // no context infopops
    return null;
  }
}

XMLSourceViewerConfiguration:
Code:
  @Override
  public IContentAssistant getContentAssistant(final ISourceViewer sv) {
    ContentAssistant result = new ContentAssistant();
    result.setContentAssistProcessor(new XMLCompletionProcessor(),
        IDocument.DEFAULT_CONTENT_TYPE);
    result.setProposalPopupOrientation(IContentAssistant.PROPOSAL_OVERLAY);
    return result;
  }

XMLEditor:
Code:
  @Override
  protected void createActions() {
    super.createActions();
    IAction action = createAction();
    String actionId = ITextEditorActionDefinitionIds.CONTENT_ASSIST_PROPOSALS;
    action.setActionDefinitionId(actionId);
    setAction("ContentAssistProposal", action);
  }

  private IAction createAction() {
    ResourceBundle bundle = Plugin.getDefault().getResourceBundle();
    return new TextOperationAction(bundle, "ContentAssistProposal.", this,
        ISourceViewer.CONTENTASSIST_PROPOSALS);
  }
 
Hi,

ich hab den Fehler gefunden :). Es war der fehlende true Wert beim erstellen der TextOperationAction. Dieser true Wert macht das einzeigen des content assisent erst möglich

XMLEditor:
Code:
  private IAction createAction() {
    ResourceBundle bundle = Plugin.getDefault().getResourceBundle();
    return new TextOperationAction(bundle, "ContentAssistProposal.", this,
        ISourceViewer.CONTENTASSIST_PROPOSALS, true);
  }

TextOperationAction:
Code:
	/**
	 * Creates and initializes the action for the given text editor and operation
	 * code. The action configures its visual representation from the given resource
	 * bundle. The action works by asking the text editor at the time for its
	 * text operation target adapter (using
	 * <code>getAdapter(ITextOperationTarget.class)</code>. The action runs that
	 * operation with the given opcode.
	 *
	 * @param bundle the resource bundle
	 * @param prefix a prefix to be prepended to the various resource keys
	 *   (described in <code>ResourceAction</code> constructor), or
	 *   <code>null</code> if none
	 * @param editor the text editor
	 * @param operationCode the operation code
	 * @param runsOnReadOnly <code>true</code> if action can be executed on read-only files
	 *
	 * @see TextEditorAction#TextEditorAction(ResourceBundle, String, ITextEditor)
	 * @since 2.0
	 */
	public TextOperationAction(ResourceBundle bundle, String prefix, ITextEditor editor, int operationCode, boolean runsOnReadOnly) {
		super(bundle, prefix, editor);
		fOperationCode= operationCode;
		fRunsOnReadOnly= runsOnReadOnly;
		fAllowUpdate= true;
		update();
	}

	/**
	 * The <code>TextOperationAction</code> implementation of this
	 * <code>IAction</code> method runs the operation with the current
	 * operation code.
	 */
	public void run() {
		if (fOperationCode == -1 || fOperationTarget == null)
			return;

		ITextEditor editor= getTextEditor();
		if (editor == null)
			return;

		if (!fRunsOnReadOnly && !validateEditorInputState())
			return;

		Display display= null;

		IWorkbenchPartSite site= editor.getSite();
		Shell shell= site.getShell();
		if (shell != null && !shell.isDisposed())
			display= shell.getDisplay();

		BusyIndicator.showWhile(display, new Runnable() {
			public void run() {
				fOperationTarget.doOperation(fOperationCode);
			}
		});
	}
 

Neue Beiträge

Zurück