blob: 7b8e37ef584e07edc6b56a6c75f531abee7a1405 [file] [log] [blame]
/*=============================================================================#
# Copyright (c) 2011, 2021 Stephan Wahlbrink and others.
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License 2.0 which is available at
# https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
# which is available at https://www.apache.org/licenses/LICENSE-2.0.
#
# SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
#
# Contributors:
# Stephan Wahlbrink <sw@wahlbrink.eu> - initial API and implementation
#=============================================================================*/
package org.eclipse.statet.ltk.ui.sourceediting.assist;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.bindings.keys.KeySequence;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension5;
import org.eclipse.jface.text.contentassist.ICompletionProposalExtension6;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.jface.viewers.StyledString;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.statet.jcommons.lang.NonNullByDefault;
import org.eclipse.statet.jcommons.lang.Nullable;
import org.eclipse.statet.ecommons.text.ui.DefaultBrowserInformationInput;
import org.eclipse.statet.ecommons.ui.workbench.WorkbenchUIUtils;
import org.eclipse.statet.ltk.ui.sourceediting.CommandAccess;
@NonNullByDefault
public abstract class CommandAssistProposal implements AssistProposal, CommandAccess,
ICompletionProposalExtension5, ICompletionProposalExtension6 {
protected static StyledString addAcceleratorStyled(final String message,
final @Nullable KeySequence binding) {
final StyledString styledString= new StyledString(message);
if (binding != null) {
styledString.append(" (", StyledString.QUALIFIER_STYLER); //$NON-NLS-1$
styledString.append(binding.format(), StyledString.QUALIFIER_STYLER);
styledString.append(')', StyledString.QUALIFIER_STYLER);
}
return styledString;
}
private final AssistInvocationContext context;
private final String commandId;
private String label;
private String description;
private int relevance;
protected CommandAssistProposal(final AssistInvocationContext invocationContext,
final String commandId) {
this.context= invocationContext;
this.commandId= commandId;
}
public CommandAssistProposal(final AssistInvocationContext invocationContext,
final String commandId,
final String label, final String description) {
this(invocationContext, commandId);
this.label= label;
this.description= description;
}
@Override
public final String getCommandId() {
return this.commandId;
}
protected AssistInvocationContext getInvocationContext() {
return this.context;
}
protected void setLabel(final String label) {
this.label= label;
}
protected void setDescription(final String description) {
this.description= description;
}
protected void setRelevance(final int relevance) {
this.relevance= relevance;
}
@Override
public boolean validate(final IDocument document, final int offset,
final @Nullable DocumentEvent event) {
return false;
}
@Override
public void apply(final IDocument document) {
throw new UnsupportedOperationException();
}
@Override
public @Nullable Point getSelection(final IDocument document) {
return null;
}
@Override
public int getRelevance() {
return this.relevance;
}
@Override
public String getSortingString() {
return this.label;
}
@Override
public String getDisplayString() {
return this.label;
}
@Override
public StyledString getStyledDisplayString() {
return addAcceleratorStyled(getDisplayString(), WorkbenchUIUtils.getBestKeyBinding(this.commandId));
}
@Override
public @Nullable Image getImage() {
return null;
}
@Override
public String getAdditionalProposalInfo() {
return this.description;
}
@Override
public Object getAdditionalProposalInfo(final IProgressMonitor monitor) {
return new DefaultBrowserInformationInput(null, getDisplayString(), this.description,
DefaultBrowserInformationInput.FORMAT_TEXT_INPUT);
}
@Override
public @Nullable IContextInformation getContextInformation() {
return null;
}
}