use of org.eclipse.xtext.util.TextRegion 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.xtext.util.TextRegion in project xtext-core by eclipse.
the class ContentAssistService method createCompletionList.
public CompletionList createCompletionList(final Document document, final XtextResource resource, final TextDocumentPositionParams params, final CancelIndicator cancelIndicator) {
try {
final CompletionList result = new CompletionList();
result.setIsIncomplete(true);
final IdeContentProposalAcceptor acceptor = this.proposalAcceptorProvider.get();
final int caretOffset = document.getOffSet(params.getPosition());
final Position caretPosition = params.getPosition();
final TextRegion position = new TextRegion(caretOffset, 0);
try {
this.createProposals(document.getContents(), position, caretOffset, resource, acceptor);
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
final Throwable t = (Throwable) _t;
boolean _isOperationCanceledException = this.operationCanceledManager.isOperationCanceledException(t);
boolean _not = (!_isOperationCanceledException);
if (_not) {
throw t;
}
} else {
throw Exceptions.sneakyThrow(_t);
}
}
final Procedure2<ContentAssistEntry, Integer> _function = (ContentAssistEntry it, Integer idx) -> {
final CompletionItem item = this.toCompletionItem(it, caretOffset, caretPosition, document);
item.setSortText(Strings.padStart(Integer.toString((idx).intValue()), 5, '0'));
List<CompletionItem> _items = result.getItems();
_items.add(item);
};
IterableExtensions.<ContentAssistEntry>forEach(acceptor.getEntries(), _function);
return result;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.
the class ContentAssistContextFactory method doCreateContext.
public ContentAssistContext.Builder doCreateContext(INode lastCompleteNode, EObject currentModel, EObject previousModel, INode currentNode, String prefix) {
ContentAssistContext.Builder context = contentAssistContextProvider.get();
context.setRootNode(rootNode);
context.setLastCompleteNode(lastCompleteNode);
context.setCurrentNode(currentNode);
context.setRootModel(parseResult.getRootASTElement());
context.setCurrentModel(currentModel);
context.setPreviousModel(previousModel);
context.setOffset(completionOffset);
context.setPrefix(prefix);
int regionLength = prefix.length();
if (selection.getLength() > 0) {
regionLength = regionLength + selection.getLength();
}
ITextRegion region = new TextRegion(completionOffset - prefix.length(), regionLength);
if (selection.getOffset() >= 0 && selection.getLength() >= 0) {
context.setSelectedText(document.substring(selection.getOffset(), selection.getOffset() + selection.getLength()));
}
context.setReplaceRegion(region);
context.setResource(resource);
return context;
}
use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.
the class TokenRegionProviderTest method testTokenRegionContainsRegion.
@Test
public void testTokenRegionContainsRegion() throws Exception {
String modelAsString = "a1 / /* comment */ b2";
List<CommonToken> tokens = getTokens(modelAsString);
for (int length = 0; length < modelAsString.length(); ++length) {
for (int offset = 0; offset + length < modelAsString.length(); ++offset) {
ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(modelAsString, new TextRegion(offset, length));
// System.out.println(offset + ":" + length + " -> " + tokenRegion);
CommonToken firstToken = findTokenStartingAt(tokenRegion.getOffset(), tokens);
assertTrue(firstToken.getStartIndex() <= offset);
if (tokenRegion.getLength() != 0) {
CommonToken lastToken = findTokenStopingAt(tokenRegion.getOffset() + tokenRegion.getLength() - 1, tokens);
assertTrue(lastToken.getStopIndex() >= offset + length - 1);
}
}
}
}
use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.
the class TokenRegionProviderTest method testTokenMerge.
@Test
public void testTokenMerge() throws Exception {
String model = " ";
ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(model, new TextRegion(1, 0));
assertEquals(0, tokenRegion.getOffset());
assertEquals(2, tokenRegion.getLength());
}
Aggregations