Search in sources :

Example 11 with TextRegion

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);
                }
            }
        }
    }
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) StructuredSelection(org.eclipse.jface.viewers.StructuredSelection) ITextSelection(org.eclipse.jface.text.ITextSelection) IOutlineNode(org.eclipse.xtext.ui.editor.outline.IOutlineNode)

Example 12 with TextRegion

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);
    }
}
Also used : CompletionList(org.eclipse.lsp4j.CompletionList) ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) TextRegion(org.eclipse.xtext.util.TextRegion) Position(org.eclipse.lsp4j.Position) CompletionItem(org.eclipse.lsp4j.CompletionItem) CompletionList(org.eclipse.lsp4j.CompletionList) List(java.util.List) IdeContentProposalAcceptor(org.eclipse.xtext.ide.editor.contentassist.IdeContentProposalAcceptor) IIdeContentProposalAcceptor(org.eclipse.xtext.ide.editor.contentassist.IIdeContentProposalAcceptor)

Example 13 with TextRegion

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;
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) Builder(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext.Builder)

Example 14 with TextRegion

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);
            }
        }
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) CommonToken(org.antlr.runtime.CommonToken) Test(org.junit.Test)

Example 15 with TextRegion

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());
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) Test(org.junit.Test)

Aggregations

TextRegion (org.eclipse.xtext.util.TextRegion)41 ITextRegion (org.eclipse.xtext.util.ITextRegion)28 Test (org.junit.Test)19 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)6 Collection (java.util.Collection)5 StringReader (java.io.StringReader)3 CommonToken (org.antlr.runtime.CommonToken)3 IFile (org.eclipse.core.resources.IFile)3 IFormattableDocument (org.eclipse.xtext.formatting2.IFormattableDocument)3 GenericFormatter (org.eclipse.xtext.formatting2.internal.GenericFormatter)3 GenericFormatterTestRequest (org.eclipse.xtext.formatting2.internal.GenericFormatterTestRequest)3 ITextRegionExtensions (org.eclipse.xtext.formatting2.regionaccess.ITextRegionExtensions)3 SourceRelativeURI (org.eclipse.xtext.generator.trace.SourceRelativeURI)3 IEclipseTrace (org.eclipse.xtext.ui.generator.trace.IEclipseTrace)3 EObject (org.eclipse.emf.ecore.EObject)2 ITextSelection (org.eclipse.jface.text.ITextSelection)2 AbstractElement (org.eclipse.xtext.AbstractElement)2 ILocationData (org.eclipse.xtext.generator.trace.ILocationData)2 ILocationInResource (org.eclipse.xtext.generator.trace.ILocationInResource)2 TraceRegionToStringTester (org.eclipse.xtext.generator.trace.TraceRegionToStringTester)2