use of org.eclipse.jface.text.Position in project che by eclipse.
the class LazyGenericTypeProposal method installLinkedMode.
private void installLinkedMode(final IDocument document, int[] offsets, int[] lengths, TypeArgumentProposal[] typeArgumentProposals, boolean withParentheses, final boolean onlyAppendArguments) {
int replacementOffset = getReplacementOffset();
String replacementString = getReplacementString();
try {
LinkedModeModelImpl model = new LinkedModeModelImpl();
for (int i = 0; i != offsets.length; i++) {
if (typeArgumentProposals[i].isAmbiguous()) {
LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
RegionImpl region = new RegionImpl();
region.setOffset(replacementOffset + offsets[i]);
region.setLength(lengths[i]);
group.addPositions(region);
model.addGroups(group);
}
}
if (withParentheses) {
LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
RegionImpl region = new RegionImpl();
region.setOffset(replacementOffset + getCursorPosition());
region.setLength(0);
group.addPositions(region);
model.addGroups(group);
}
model.setEscapePosition(replacementOffset + replacementString.length());
this.linkedModel = model;
if (!onlyAppendArguments && (document instanceof IDocumentExtension)) {
// see bug 301990
FormatterPrefs prefs = getFormatterPrefs();
final Position firstBracketPosition;
final Position secondBracketPosition;
int firstBracketOffset = replacementOffset + offsets[0] - 1;
if (prefs.afterOpeningBracket) {
firstBracketOffset--;
}
firstBracketPosition = new Position(firstBracketOffset, 1);
document.addPosition(firstBracketPosition);
int secondBracketOffset = replacementOffset + offsets[offsets.length - 1] + lengths[offsets.length - 1] + 1;
if (prefs.beforeClosingBracket) {
secondBracketOffset++;
}
secondBracketPosition = new Position(secondBracketOffset, 0);
document.addPosition(secondBracketPosition);
// model.addLinkingListener(new ILinkedModeListener() {
// public void left(LinkedModeModel environment, int flags) {
// try {
// if (getTextViewer().getSelectedRange().y > 1 || flags != ILinkedModeListener.EXTERNAL_MODIFICATION)
// return;
// ((IDocumentExtension) document).registerPostNotificationReplace(null, new IDocumentExtension.IReplace() {
// public void perform(IDocument d, IDocumentListener owner) {
// try {
// if ((firstBracketPosition.length == 0 || firstBracketPosition.isDeleted) && !secondBracketPosition.isDeleted) {
// d.replace(firstBracketPosition.offset, secondBracketPosition.offset - firstBracketPosition.offset, ""); //$NON-NLS-1$
// }
// } catch (BadLocationException e) {
// JavaPlugin.log(e);
// }
// }
// });
// } finally {
// document.removePosition(firstBracketPosition);
// document.removePosition(secondBracketPosition);
// }
// }
//
// public void suspend(LinkedModeModel environment) {
// }
//
// public void resume(LinkedModeModel environment, int flags) {
// }
// });
}
// LinkedModeUI ui= new EditorLinkedModeUI(model, getTextViewer());
// ui.setExitPolicy(new ExitPolicy(withParentheses ? ')' : '>', document));
// ui.setExitPosition(getTextViewer(), replacementOffset + replacementString.length(), 0, Integer.MAX_VALUE);
// ui.setDoContextInfo(true);
// ui.enter();
//ui.getSelectedRegion();
fSelectedRegion = new Region(replacementOffset + replacementString.length(), 0);
} catch (BadLocationException e) {
JavaPlugin.log(e);
openErrorDialog(e);
}
}
use of org.eclipse.jface.text.Position in project che by eclipse.
the class TemplateProposal method apply.
/*
* @see org.eclipse.jface.text.contentassist.ICompletionProposalExtension2#apply(org.eclipse.jface.text.ITextViewer, char, int, int)
*/
public void apply(ITextViewer viewer, char trigger, int stateMask, int offset) {
IDocument document = viewer.getDocument();
try {
fContext.setReadOnly(false);
int start;
TemplateBuffer templateBuffer;
try {
beginCompoundChange(viewer);
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
if (end > document.getLength())
end = offset;
String templateString = templateBuffer.getString();
document.replace(start, end - start, templateString);
} finally {
endCompoundChange(viewer);
}
// translate positions
LinkedModeModelImpl model = new LinkedModeModelImpl();
TemplateVariable[] variables = templateBuffer.getVariables();
MultiVariableGuess guess = fContext instanceof CompilationUnitContext ? ((CompilationUnitContext) fContext).getMultiVariableGuess() : null;
boolean hasPositions = false;
for (int i = 0; i != variables.length; i++) {
TemplateVariable variable = variables[i];
if (variable.isUnambiguous())
continue;
LinkedPositionGroupImpl group = new LinkedPositionGroupImpl();
int[] offsets = variable.getOffsets();
int length = variable.getLength();
LinkedPosition first;
if (guess != null && variable instanceof MultiVariable) {
first = new VariablePosition(document, offsets[0] + start, length, guess, (MultiVariable) variable);
guess.addSlave((VariablePosition) first);
} else {
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) {
if (first instanceof ProposalPosition) {
RegionImpl region = new RegionImpl();
region.setLength(first.getLength());
region.setOffset(first.getOffset());
LinkedDataImpl data = new LinkedDataImpl();
ICompletionProposal[] choices = ((ProposalPosition) first).getChoices();
if (choices != null) {
for (ICompletionProposal choice : choices) {
data.addValues(choice.getDisplayString());
}
group.setData(data);
}
group.addPositions(region);
} else {
RegionImpl region = new RegionImpl();
region.setLength(first.getLength());
region.setOffset(first.getOffset());
group.addPositions(region);
}
} else {
RegionImpl region = new RegionImpl();
region.setLength(length);
region.setOffset(offsets[j] + start);
group.addPositions(region);
}
model.addGroups(group);
hasPositions = true;
}
if (hasPositions) {
model.setEscapePosition(getCaretOffset(templateBuffer) + start);
this.linkedModeModel = model;
// model.forceInstall();
// JavaEditor editor= getJavaEditor();
// if (editor != null) {
// model.addLinkingListener(new EditorHighlightingSynchronizer(editor));
// }
//
// LinkedModeUI ui= new EditorLinkedModeUI(model, viewer);
// ui.setExitPosition(viewer, getCaretOffset(templateBuffer) + start, 0, Integer.MAX_VALUE);
// ui.enter();
//ui.getSelectedRegion();
fSelectedRegion = fRegion;
} else {
fSelectedRegion = new Region(getCaretOffset(templateBuffer) + start, 0);
}
} catch (BadLocationException e) {
JavaPlugin.log(e);
// openErrorDialog(viewer.getTextWidget().getShell(), e);
fSelectedRegion = fRegion;
}
}
use of org.eclipse.jface.text.Position in project tdi-studio-se by Talend.
the class ReconcilerViewer method getAllSnippetsAnnotations.
private Map<ProjectionAnnotation, Position> getAllSnippetsAnnotations() {
Map<ProjectionAnnotation, Position> annotations = new HashMap<ProjectionAnnotation, Position>();
IDocument document = getDocument();
int curOffset = 0;
FindReplaceDocumentAdapter frda = new FindReplaceDocumentAdapter(document);
try {
//$NON-NLS-1$
IRegion startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false);
while (startRegion != null && startRegion.getOffset() >= curOffset) {
int startLine = document.getLineOfOffset(startRegion.getOffset());
int startOffset = document.getLineOffset(startLine);
curOffset = startOffset + document.getLineLength(startLine);
//$NON-NLS-1$
IRegion endRegion = frda.find(startRegion.getOffset(), "SNIPPET_END", true, false, false, false);
if (endRegion != null) {
int endLine = document.getLineOfOffset(endRegion.getOffset());
int endOffset = document.getLineOffset(endLine);
endOffset += document.getLineLength(endLine);
curOffset = endOffset;
String text = document.get(startOffset, endOffset - startOffset);
ProjectionAnnotation annotation = new ProjectionAnnotation(true);
annotation.setText(text);
annotation.setRangeIndication(true);
annotations.put(annotation, new Position(startOffset, endOffset - startOffset));
}
if (curOffset < document.getLength()) {
//$NON-NLS-1$
startRegion = frda.find(curOffset, "SNIPPET_START", true, false, false, false);
}
}
} catch (BadLocationException e) {
ExceptionHandler.process(e);
}
return annotations;
}
use of org.eclipse.jface.text.Position in project KaiZen-OpenAPI-Editor by RepreZen.
the class JsonEditor method show.
@Override
public boolean show(ShowInContext context) {
ISelection selection = context.getSelection();
if (selection instanceof IStructuredSelection) {
Object selected = ((IStructuredSelection) selection).getFirstElement();
if (selected instanceof AbstractNode) {
Position position = ((AbstractNode) selected).getPosition(getSourceViewer().getDocument());
selectAndReveal(position.getOffset(), position.getLength());
return true;
}
}
return false;
}
use of org.eclipse.jface.text.Position in project dbeaver by serge-rider.
the class SQLReconcilingStrategy method calculatePositions.
protected void calculatePositions() {
ProjectionAnnotationModel annotationModel = editor.getAnnotationModel();
if (annotationModel == null) {
return;
}
Set<SQLScriptPosition> removedPositions = editor.getRuleManager().getRemovedPositions(true);
Set<SQLScriptPosition> addedPositions = editor.getRuleManager().getAddedPositions(true);
Annotation[] removedAnnotations = null;
if (!removedPositions.isEmpty()) {
removedAnnotations = new Annotation[removedPositions.size()];
int index = 0;
for (SQLScriptPosition pos : removedPositions) {
removedAnnotations[index++] = pos.getFoldingAnnotation();
}
}
Map<Annotation, Position> addedAnnotations = null;
if (!addedPositions.isEmpty()) {
addedAnnotations = new HashMap<>();
for (SQLScriptPosition pos : addedPositions) {
addedAnnotations.put(pos.getFoldingAnnotation(), pos);
}
}
if (removedAnnotations != null || addedAnnotations != null) {
annotationModel.modifyAnnotations(removedAnnotations, addedAnnotations, null);
}
/*
final List<Position> positions;
try {
positions = parseRegion();
} catch (BadLocationException e) {
e.printStackTrace();
return;
}
*/
/*
Display.getDefault().asyncExec(new Runnable()
{
public void run()
{
editor.updateFoldingStructure(regionOffset, regionLength, positions);
}
});
*/
}
Aggregations