use of org.eclipse.jface.text.link.ProposalPosition in project eclipse.platform.text by eclipse.
the class TemplateProposal method apply.
/**
* Inserts the template offered by this proposal into the viewer's document
* and sets up a <code>LinkedModeUI</code> on the viewer to edit any of
* the template's unresolved variables.
*
* @param viewer {@inheritDoc}
* @param trigger {@inheritDoc}
* @param stateMask {@inheritDoc}
* @param offset {@inheritDoc}
*/
@Override
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
IDocument document = viewer.getDocument();
try {
fContext.setReadOnly(false);
int start;
TemplateBuffer templateBuffer;
{
int oldReplaceOffset = getReplaceOffset();
try {
// this may already modify the document (e.g. add imports)
templateBuffer = fContext.evaluate(fTemplate);
} catch (TemplateException e1) {
fSelectedRegion = fRegion;
return;
}
start = getReplaceOffset();
int shift = start - oldReplaceOffset;
int end = Math.max(getReplaceEndOffset(), offset + shift);
// insert template string
String templateString = templateBuffer.getString();
document.replace(start, end - start, templateString);
}
// translate positions
LinkedModeModel model = new LinkedModeModel();
TemplateVariable[] variables = templateBuffer.getVariables();
boolean hasPositions = false;
for (int i = 0; i != variables.length; i++) {
TemplateVariable variable = variables[i];
if (variable.isUnambiguous())
continue;
LinkedPositionGroup group = new LinkedPositionGroup();
int[] offsets = variable.getOffsets();
int length = variable.getLength();
LinkedPosition first;
{
String[] values = variable.getValues();
ICompletionProposal[] proposals = new ICompletionProposal[values.length];
for (int j = 0; j < values.length; j++) {
ensurePositionCategoryInstalled(document, model);
Position pos = new Position(offsets[0] + start, length);
document.addPosition(getCategory(), pos);
proposals[j] = new PositionBasedCompletionProposal(values[j], pos, length);
}
if (proposals.length > 1)
first = new ProposalPosition(document, offsets[0] + start, length, proposals);
else
first = new LinkedPosition(document, offsets[0] + start, length);
}
for (int j = 0; j != offsets.length; j++) if (j == 0)
group.addPosition(first);
else
group.addPosition(new LinkedPosition(document, offsets[j] + start, length));
model.addGroup(group);
hasPositions = true;
}
if (hasPositions) {
model.forceInstall();
LinkedModeUI ui = new LinkedModeUI(model, viewer);
ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
ui.enter();
fSelectedRegion = ui.getSelectedRegion();
} else {
ensurePositionCategoryRemoved(document);
fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
}
} catch (BadLocationException e) {
openErrorDialog(viewer.getTextWidget().getShell(), e);
ensurePositionCategoryRemoved(document);
fSelectedRegion = fRegion;
} catch (BadPositionCategoryException e) {
openErrorDialog(viewer.getTextWidget().getShell(), e);
fSelectedRegion = fRegion;
}
}
Aggregations