use of org.eclipse.jface.text.ITextSelection in project eclipse.platform.text by eclipse.
the class ProjectionViewer method handleVerifyEvent.
@Override
protected void handleVerifyEvent(VerifyEvent e) {
if (getTextWidget().getBlockSelection()) {
ITextSelection selection = (ITextSelection) getSelection();
if (exposeModelRange(new Region(selection.getOffset(), selection.getLength())))
setSelection(selection);
super.handleVerifyEvent(e);
return;
}
Point selection = getSelectedRange();
IRegion modelRange = event2ModelRange(e);
if (exposeModelRange(modelRange)) {
e.doit = false;
try {
if (selection.y == 0 && e.text.length() <= 1 && modelRange.getLength() == 1) {
selection.y = 1;
if (selection.x - 1 == modelRange.getOffset())
selection.x--;
}
getDocument().replace(selection.x, selection.y, e.text);
setSelectedRange(selection.x + e.text.length(), 0);
} catch (BadLocationException e1) {
// ignore as nothing bad happens (no log at this level)
}
} else
super.handleVerifyEvent(e);
}
use of org.eclipse.jface.text.ITextSelection in project xtext-xtend by eclipse.
the class DerivedSourceView method mapTextRegion.
private TextRegion mapTextRegion(IWorkbenchPartSelection workbenchPartSelection) {
ITextSelection textSelection = (ITextSelection) workbenchPartSelection.getSelection();
TextRegion localRegion = new TextRegion(textSelection.getOffset(), textSelection.getLength());
return localRegion;
}
use of org.eclipse.jface.text.ITextSelection in project xtext-xtend by eclipse.
the class XtendOutlineWithEditorLinker method selectInTreeView.
@Override
public void selectInTreeView(final ISelection selection) {
if (((selection instanceof ITextSelection) && (!this.treeViewer.getTree().isDisposed()))) {
final ITextSelection textSelection = ((ITextSelection) selection);
int _offset = textSelection.getOffset();
int _length = textSelection.getLength();
final TextRegion selectedTextRegion = new TextRegion(_offset, _length);
final Object input = this.treeViewer.getInput();
if ((input instanceof IOutlineNode)) {
final List<IOutlineNode> matchingNodes = CollectionLiterals.<IOutlineNode>newArrayList();
this.findNodesInRange(((IOutlineNode) input), selectedTextRegion, matchingNodes);
boolean _isEmpty = matchingNodes.isEmpty();
boolean _not = (!_isEmpty);
if (_not) {
final Function1<IOutlineNode, Integer> _function = (IOutlineNode it) -> {
return Integer.valueOf(it.getFullTextRegion().getLength());
};
final Integer smallestMatch = IterableExtensions.<Integer>min(ListExtensions.<IOutlineNode, Integer>map(matchingNodes, _function));
final Function1<IOutlineNode, Boolean> _function_1 = (IOutlineNode it) -> {
int _length_1 = it.getFullTextRegion().getLength();
return Boolean.valueOf((_length_1 == (smallestMatch).intValue()));
};
final Iterable<IOutlineNode> nodesToBeSelected = IterableExtensions.<IOutlineNode>filter(matchingNodes, _function_1);
boolean _isEmpty_1 = IterableExtensions.isEmpty(nodesToBeSelected);
boolean _not_1 = (!_isEmpty_1);
if (_not_1) {
Object[] _array = IterableExtensions.<IOutlineNode>toList(nodesToBeSelected).toArray();
StructuredSelection _structuredSelection = new StructuredSelection(_array);
this.treeViewer.setSelection(_structuredSelection);
}
}
}
}
}
use of org.eclipse.jface.text.ITextSelection in project xtext-xtend by eclipse.
the class RichStringAwareSourceViewer method shift.
@SuppressWarnings("deprecation")
@Override
protected void shift(boolean useDefaultPrefixes, boolean right, boolean ignoreWhitespace) {
if (useDefaultPrefixes && ignoreWhitespace) {
// let's assume we toggled comments
if (fUndoManager != null)
fUndoManager.beginCompoundChange();
IDocument d = getDocument();
@SuppressWarnings("rawtypes") Map partitioners = null;
DocumentRewriteSession rewriteSession = null;
try {
ITextSelection selection = (ITextSelection) getSelection();
IRegion block = copiedGetTextBlockFromSelection(selection);
ITypedRegion[] regions = TextUtilities.computePartitioning(d, getDocumentPartitioning(), block.getOffset(), block.getLength(), false);
regions = merger.merge(regions);
int lineCount = 0;
// [start line, end line, start line, end line, ...]
int[] lines = new int[regions.length * 2];
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
// start line of region
lines[j] = copiedGetFirstCompleteLineOfRegion(regions[i]);
// end line of region
int length = regions[i].getLength();
int offset = regions[i].getOffset() + length;
if (length > 0)
offset--;
lines[j + 1] = (lines[j] == -1 ? -1 : d.getLineOfOffset(offset));
lineCount += lines[j + 1] - lines[j] + 1;
}
if (d instanceof IDocumentExtension4) {
IDocumentExtension4 extension = (IDocumentExtension4) d;
rewriteSession = extension.startRewriteSession(DocumentRewriteSessionType.SEQUENTIAL);
} else {
setRedraw(false);
startSequentialRewriteMode(true);
}
if (lineCount >= 20)
partitioners = TextUtilities.removeDocumentPartitioners(d);
// Perform the shift operation.
@SuppressWarnings("rawtypes") Map map = fDefaultPrefixChars;
for (int i = 0, j = 0; i < regions.length; i++, j += 2) {
String[] prefixes = (String[]) copiedSelectContentTypePlugin(regions[i].getType(), map);
if (prefixes != null && prefixes.length > 0 && lines[j] >= 0 && lines[j + 1] >= 0) {
if (right)
copiedShiftRight(lines[j], lines[j + 1], prefixes[0]);
else
copiedShiftLeft(lines[j], lines[j + 1], prefixes, ignoreWhitespace);
}
}
} catch (BadLocationException x) {
log.debug(x.getMessage(), x);
} finally {
if (partitioners != null)
TextUtilities.addDocumentPartitioners(d, partitioners);
if (d instanceof IDocumentExtension4) {
IDocumentExtension4 extension = (IDocumentExtension4) d;
extension.stopRewriteSession(rewriteSession);
} else {
stopSequentialRewriteMode();
setRedraw(true);
}
if (fUndoManager != null)
fUndoManager.endCompoundChange();
}
} else {
super.shift(useDefaultPrefixes, right, ignoreWhitespace);
}
}
use of org.eclipse.jface.text.ITextSelection in project linuxtools by eclipse.
the class SpecfileCompletionProcessor method computeTemplateProposals.
/**
* Compute the templates proposals, these proposals are contextual on
* sections. Return an array of template proposals for the given viewer,
* region, specfile, prefix.
*
* @param viewer
* the viewer for which the context is created
* @param region
* the region into <code>document</code> for which the context is
* created
* @param specfile
* the specfile element
* @param prefix
* the prefix string
* @return a ICompletionProposal[]
*/
private List<? extends ICompletionProposal> computeTemplateProposals(ITextViewer viewer, IRegion region, Specfile specfile, String prefix) {
TemplateContext context = createContext(viewer, region, specfile);
List<TemplateProposal> matches = new ArrayList<>();
if (context == null) {
return matches;
}
ITextSelection selection = (ITextSelection) viewer.getSelectionProvider().getSelection();
// $NON-NLS-1$
context.setVariable("selection", selection.getText());
String id = context.getContextType().getId();
Template[] templates = Activator.getDefault().getTemplateStore().getTemplates(id);
for (Template template : templates) {
try {
context.getContextType().validate(template.getPattern());
} catch (TemplateException e) {
continue;
}
int relevance = getRelevance(template, prefix);
if (relevance > 0) {
matches.add(new TemplateProposal(template, context, region, Activator.getDefault().getImage(TEMPLATE_ICON), relevance));
}
}
Collections.sort(matches, (t1, t2) -> (t2.getRelevance() - t1.getRelevance()));
return matches;
}
Aggregations