/*******************************************************************************
* Copyright (c) 2005, 2006 committers of openArchitectureWare and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* committers of openArchitectureWare - initial API and implementation
*******************************************************************************/
package org.eclipse.xtend.shared.ui.expression.editor.codeassist;
import org.eclipse.core.runtime.Assert;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.jface.text.contentassist.IContextInformation;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
public class TextSelectingProposal implements ICompletionProposal {
/** The string to be displayed in the completion proposal popup. */
private String fDisplayString;
/** The replacement string. */
private String fReplacementString;
/** The replacement offset. */
private int fReplacementOffset;
/** The replacement length. */
private int fReplacementLength;
/** The cursor position after this proposal has been applied. */
private int fCursorPosition;
/** The image to be displayed in the completion proposal popup. */
private Image fImage;
/** The context information of this proposal. */
private IContextInformation fContextInformation;
/** The additional info of this proposal. */
private String fAdditionalProposalInfo;
private int fSelectionLength;
/**
* Creates a new completion proposal based on the provided information. The
* replacement string is considered being the display string too. All
* remaining fields are set to <code>null</code>.
*
* @param replacementString
* the actual string to be inserted into the document
* @param replacementOffset
* the offset of the text to be replaced
* @param replacementLength
* the length of the text to be replaced
* @param cursorPosition
* the position of the cursor following the insert relative to
* replacementOffset
*/
public TextSelectingProposal(final String replacementString, final int replacementOffset, final int replacementLength, final int cursorPosition,
final int selectionLength) {
this(replacementString, replacementOffset, replacementLength, cursorPosition, selectionLength, null, null, null, null);
}
/**
* Creates a new completion proposal. All fields are initialized based on
* the provided information.
*
* @param replacementString
* the actual string to be inserted into the document
* @param replacementOffset
* the offset of the text to be replaced
* @param replacementLength
* the length of the text to be replaced
* @param cursorPosition
* the position of the cursor following the insert relative to
* replacementOffset
* @param image
* the image to display for this proposal
* @param displayString
* the string to be displayed for the proposal
* @param contextInformation
* the context information associated with this proposal
* @param additionalProposalInfo
* the additional information associated with this proposal
*/
public TextSelectingProposal(final String replacementString, final int replacementOffset, final int replacementLength, final int cursorPosition,
final int selectionLength, final Image image, final String displayString, final IContextInformation contextInformation,
final String additionalProposalInfo) {
Assert.isNotNull(replacementString);
Assert.isTrue(replacementOffset >= 0);
Assert.isTrue(replacementLength >= 0);
Assert.isTrue(cursorPosition >= 0);
fReplacementString = replacementString;
fReplacementOffset = replacementOffset;
fReplacementLength = replacementLength;
fCursorPosition = cursorPosition;
fImage = image;
fDisplayString = displayString;
fContextInformation = contextInformation;
fAdditionalProposalInfo = additionalProposalInfo;
fSelectionLength = selectionLength;
}
// private final static Pattern pattern = Pattern.compile("\\A\\s*" +
// XpandTokens.RT);
/*
* @see ICompletionProposal#apply(IDocument)
*/
public void apply(final IDocument document) {
try {
// String s = document.get().substring(fReplacementOffset +
// fReplacementLength);
// Matcher m = pattern.matcher(s);
// if (m.find()) {
// document.replace(m.start(), m.end(), "");
// }
document.replace(fReplacementOffset, fReplacementLength, fReplacementString);
} catch (final BadLocationException x) {
// ignore
}
}
/*
* @see ICompletionProposal#getSelection(IDocument)
*/
public Point getSelection(final IDocument document) {
return new Point(fReplacementOffset + fCursorPosition, fSelectionLength);
}
/*
* @see ICompletionProposal#getContextInformation()
*/
public IContextInformation getContextInformation() {
return fContextInformation;
}
/*
* @see ICompletionProposal#getImage()
*/
public Image getImage() {
return fImage;
}
/*
* @see ICompletionProposal#getDisplayString()
*/
public String getDisplayString() {
if (fDisplayString != null)
return fDisplayString;
return fReplacementString;
}
/*
* @see ICompletionProposal#getAdditionalProposalInfo()
*/
public String getAdditionalProposalInfo() {
return fAdditionalProposalInfo;
}
}