use of org.eclipse.jface.text.IDocumentExtension 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.IDocumentExtension in project erlide_eclipse by erlang.
the class ErlangBracketInserter method left.
/*
* @see org.eclipse.jface.text.link.ILinkedModeListener#left(org.eclipse.
* jface.text.link.LinkedModeModel, int)
*/
@Override
@SuppressWarnings("synthetic-access")
public void left(final LinkedModeModel environment, final int flags) {
final BracketLevel level = fBracketLevelStack.pop();
if (flags != ILinkedModeListener.EXTERNAL_MODIFICATION) {
return;
}
// remove brackets
final IDocument document = sourceViewer.getDocument();
if (document instanceof IDocumentExtension) {
final IDocumentExtension extension = (IDocumentExtension) document;
extension.registerPostNotificationReplace(null, new IDocumentExtension.IReplace() {
@Override
public void perform(final IDocument d, final IDocumentListener owner) {
if ((level.fFirstPosition.isDeleted || level.fFirstPosition.length == 0) && !level.fSecondPosition.isDeleted && level.fSecondPosition.offset == level.fFirstPosition.offset) {
try {
document.replace(level.fSecondPosition.offset, level.fSecondPosition.length, // $NON-NLS-1$
"");
} catch (final BadLocationException e) {
ErlLogger.error(e);
}
}
if (fBracketLevelStack.isEmpty()) {
document.removePositionUpdater(fUpdater);
try {
document.removePositionCategory(CATEGORY);
} catch (final BadPositionCategoryException e) {
ErlLogger.error(e);
}
}
}
});
}
}
Aggregations