Search in sources :

Example 1 with CodeAssistProcessor

use of org.eclipse.che.ide.api.editor.codeassist.CodeAssistProcessor in project che by eclipse.

the class OrionEditorInit method configureCodeAssist.

/**
     * Configure the editor's code assistant.
     * @param documentHandle the handle on the document
     */
private void configureCodeAssist(final DocumentHandle documentHandle) {
    if (this.codeAssistantFactory == null) {
        return;
    }
    final Map<String, CodeAssistProcessor> processors = configuration.getContentAssistantProcessors();
    if (processors != null && !processors.isEmpty()) {
        LOG.info("Creating code assistant.");
        final CodeAssistant codeAssistant = this.codeAssistantFactory.create(this.textEditor, this.configuration.getPartitioner());
        for (String key : processors.keySet()) {
            codeAssistant.setCodeAssistantProcessor(key, processors.get(key));
        }
        final KeyBindingAction action = new KeyBindingAction() {

            @Override
            public boolean action() {
                showCompletion(codeAssistant, true);
                return true;
            }
        };
        final HasKeyBindings hasKeyBindings = this.textEditor.getHasKeybindings();
        hasKeyBindings.addKeyBinding(new KeyBinding(true, false, false, false, KeyCode.SPACE, action), CONTENT_ASSIST);
        // handle CompletionRequest events that come from text operations instead of simple key binding
        documentHandle.getDocEventBus().addHandler(CompletionRequestEvent.TYPE, new CompletionRequestHandler() {

            @Override
            public void onCompletionRequest(final CompletionRequestEvent event) {
                showCompletion(codeAssistant, false);
            }
        });
    } else {
        final KeyBindingAction action = new KeyBindingAction() {

            @Override
            public boolean action() {
                showCompletion();
                return true;
            }
        };
        final HasKeyBindings hasKeyBindings = this.textEditor.getHasKeybindings();
        if (UserAgent.isMac()) {
            hasKeyBindings.addKeyBinding(new KeyBinding(false, false, false, true, KeyCode.SPACE, action), CONTENT_ASSIST);
            hasKeyBindings.addKeyBinding(new KeyBinding(false, false, true, true, KeyCode.SPACE, action), CONTENT_ASSIST);
        } else {
            hasKeyBindings.addKeyBinding(new KeyBinding(true, false, false, false, KeyCode.SPACE, action), CONTENT_ASSIST);
        }
        // handle CompletionRequest events that come from text operations instead of simple key binding
        documentHandle.getDocEventBus().addHandler(CompletionRequestEvent.TYPE, new CompletionRequestHandler() {

            @Override
            public void onCompletionRequest(final CompletionRequestEvent event) {
                showCompletion();
            }
        });
    }
}
Also used : KeyBinding(org.eclipse.che.ide.api.editor.keymap.KeyBinding) CompletionRequestHandler(org.eclipse.che.ide.api.editor.events.CompletionRequestHandler) CompletionRequestEvent(org.eclipse.che.ide.api.editor.events.CompletionRequestEvent) CodeAssistProcessor(org.eclipse.che.ide.api.editor.codeassist.CodeAssistProcessor) HasKeyBindings(org.eclipse.che.ide.api.editor.texteditor.HasKeyBindings) CodeAssistant(org.eclipse.che.ide.api.editor.codeassist.CodeAssistant) KeyBindingAction(org.eclipse.che.ide.api.editor.keymap.KeyBindingAction)

Example 2 with CodeAssistProcessor

use of org.eclipse.che.ide.api.editor.codeassist.CodeAssistProcessor in project che by eclipse.

the class OrionEditorInit method showCompletion.

/**
     * Show the available completions.
     *
     * @param codeAssistant the code assistant
     * @param triggered if triggered by the content assist key binding
     */
private void showCompletion(final CodeAssistant codeAssistant, final boolean triggered) {
    final int cursor = textEditor.getCursorOffset();
    if (cursor < 0) {
        return;
    }
    final CodeAssistProcessor processor = codeAssistant.getProcessor(cursor);
    if (processor != null) {
        this.textEditor.showCompletionProposals(new CompletionsSource() {

            @Override
            public void computeCompletions(final CompletionReadyCallback callback) {
                // cursor must be computed here again so it's original value is not baked in
                // the SMI instance closure - important for completion update when typing
                final int cursor = textEditor.getCursorOffset();
                codeAssistant.computeCompletionProposals(cursor, triggered, new CodeAssistCallback() {

                    @Override
                    public void proposalComputed(final List<CompletionProposal> proposals) {
                        callback.onCompletionReady(proposals);
                    }
                });
            }
        });
    } else {
        showCompletion();
    }
}
Also used : CodeAssistCallback(org.eclipse.che.ide.api.editor.codeassist.CodeAssistCallback) CompletionsSource(org.eclipse.che.ide.api.editor.codeassist.CompletionsSource) CodeAssistProcessor(org.eclipse.che.ide.api.editor.codeassist.CodeAssistProcessor) CompletionReadyCallback(org.eclipse.che.ide.api.editor.codeassist.CompletionReadyCallback) List(java.util.List)

Aggregations

CodeAssistProcessor (org.eclipse.che.ide.api.editor.codeassist.CodeAssistProcessor)2 List (java.util.List)1 CodeAssistCallback (org.eclipse.che.ide.api.editor.codeassist.CodeAssistCallback)1 CodeAssistant (org.eclipse.che.ide.api.editor.codeassist.CodeAssistant)1 CompletionReadyCallback (org.eclipse.che.ide.api.editor.codeassist.CompletionReadyCallback)1 CompletionsSource (org.eclipse.che.ide.api.editor.codeassist.CompletionsSource)1 CompletionRequestEvent (org.eclipse.che.ide.api.editor.events.CompletionRequestEvent)1 CompletionRequestHandler (org.eclipse.che.ide.api.editor.events.CompletionRequestHandler)1 KeyBinding (org.eclipse.che.ide.api.editor.keymap.KeyBinding)1 KeyBindingAction (org.eclipse.che.ide.api.editor.keymap.KeyBindingAction)1 HasKeyBindings (org.eclipse.che.ide.api.editor.texteditor.HasKeyBindings)1