Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 785decabb3bc0da42d56721be3477ae1384e341d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*******************************************************************************
 * Copyright (c) 2007, 2015 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.texteditor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.core.commands.Command;
import org.eclipse.core.commands.IHandler;

import org.eclipse.core.runtime.Assert;

import org.eclipse.jface.text.contentassist.ContentAssistEvent;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.ICompletionListener;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.quickassist.IQuickAssistAssistant;
import org.eclipse.jface.text.quickassist.IQuickAssistAssistantExtension;
import org.eclipse.jface.text.source.ContentAssistantFacade;
import org.eclipse.jface.text.source.ISourceViewerExtension4;

import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.commands.ICommandService;


/**
 * Helper class to make navigation key bindings work for the content assistant
 * and the quick assist assistant while the editor has focus.
 * <p>
 * Clients normally don't need to use that class as the setup is done by the
 * framework.
 * </p>
 *
 * @since 3.4
 */
public final class KeyBindingSupportForAssistant implements ICompletionListener {


	private static final class ReplacedCommand {

		private IHandler handler;

		private Command command;

		ReplacedCommand(String commandId, ICommandService commandService) {
			this.command= commandService.getCommand(commandId);
			replaceWith(null);
		}

		ReplacedCommand(String commandId, IHandler handler, ICommandService commandService) {
			this.command= commandService.getCommand(commandId);
			replaceWith(handler);
		}

		void activate() {
			if (handler != null) {
				/*
				 *  Next check ensures that we don't overwrite newly activated editor contributions.
				 *  For details see https://bugs.eclipse.org/bugs/show_bug.cgi?id=297834.
				 */
				if (!handler.getClass().isInstance(command.getHandler()))
					command.setHandler(handler);
			}
		}

		private void replaceWith(IHandler newHandler) {
			if (command.isHandled()) {
				handler= command.getHandler();
				command.setHandler(newHandler);
			}
		}
	}


	private List<ReplacedCommand> fReplacedCommands;
	private ContentAssistantFacade fContentAssistantFacade;
	private IQuickAssistAssistant fQuickAssistAssistant;

	/**
	 * Creates the support for a content assistant facade.
	 *
	 * @param contentAssistFacade the content assist facade
	 * @deprecated As of 3.5, this is a NOP since the framework installs this now
	 */
	@Deprecated
	public KeyBindingSupportForAssistant(ContentAssistantFacade contentAssistFacade) {
	}

	/**
	 * Creates the support for a content assistant facade.
	 *
	 * @param sourceViewerExtension the source viewer extension
	 * @since 3.5
	 */
	public KeyBindingSupportForAssistant(ISourceViewerExtension4 sourceViewerExtension) {
		Assert.isLegal(sourceViewerExtension != null);
		fContentAssistantFacade= sourceViewerExtension.getContentAssistantFacade();
		if (fContentAssistantFacade != null)
			fContentAssistantFacade.addCompletionListener(this);
	}

	/**
	 * Creates the support for a content assistant facade.
	 *
	 * @param contentAssistant the content assist facade
	 * @deprecated As of 3.5, this is a NOP since the framework installs this now
	 */
	@Deprecated
	public KeyBindingSupportForAssistant(ContentAssistant contentAssistant) {
	}

	/**
	 * Creates the support for a quick assist assistant.
	 *
	 * @param quickAssistAssistant the quick assist assistant.
	 */
	public KeyBindingSupportForAssistant(IQuickAssistAssistant quickAssistAssistant) {
		Assert.isLegal(quickAssistAssistant != null);
		fQuickAssistAssistant= quickAssistAssistant;
		fQuickAssistAssistant.addCompletionListener(this);
	}

	@Override
	public void assistSessionStarted(ContentAssistEvent event) {
		ICommandService commandService= PlatformUI.getWorkbench().getService(ICommandService.class);
		IHandler handler= getHandler(ContentAssistant.SELECT_NEXT_PROPOSAL_COMMAND_ID);
		fReplacedCommands= new ArrayList<>(10);
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.LINE_DOWN, handler, commandService));
		handler= getHandler(ContentAssistant.SELECT_PREVIOUS_PROPOSAL_COMMAND_ID);
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.LINE_UP, handler, commandService));
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.LINE_START, commandService));
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.LINE_END, commandService));
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.PAGE_UP, commandService));
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.PAGE_DOWN, commandService));
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.TEXT_START, commandService));
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.TEXT_END, commandService));
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.SCROLL_LINE_UP, commandService));
		fReplacedCommands.add(new ReplacedCommand(ITextEditorActionDefinitionIds.SCROLL_LINE_DOWN, commandService));
	}

	/**
	 * Returns the handler for the given command identifier.
	 * <p>
	 * The same handler instance will be returned when called a more than once
	 * with the same command identifier.
	 * </p>
	 *
	 * @param commandId the command identifier
	 * @return the handler for the given command identifier
	 */
	private IHandler getHandler(String commandId) {
		if (fContentAssistantFacade != null)
			return fContentAssistantFacade.getHandler(commandId);
		if (fQuickAssistAssistant instanceof IQuickAssistAssistantExtension)
			return ((IQuickAssistAssistantExtension)fQuickAssistAssistant).getHandler(commandId);
		return null;
	}

	@Override
	public void assistSessionEnded(ContentAssistEvent event) {
		if (fReplacedCommands == null)
			return;

		Iterator<ReplacedCommand> iter= fReplacedCommands.iterator();
		while (iter.hasNext())
			iter.next().activate();
		fReplacedCommands= null;
	}

	@Override
	public void selectionChanged(ICompletionProposal proposal, boolean smartToggle) {
	}

	public void dispose() {
		if (fContentAssistantFacade != null) {
			fContentAssistantFacade.removeCompletionListener(this);
			fContentAssistantFacade= null;
		}

		if (fQuickAssistAssistant != null) {
			fQuickAssistAssistant.removeCompletionListener(this);
			fQuickAssistAssistant= null;
		}
	}
}

Back to the top