Search in sources :

Example 31 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-xtend by eclipse.

the class CompilerTraceTest method tracesTo.

public void tracesTo(final CharSequence xtend, final CharSequence java) {
    try {
        final String xtendWithSpaces = ((" " + xtend) + " ");
        final Matcher xtendMatcher = this.p.matcher(xtendWithSpaces);
        Assert.assertTrue("xtendMatcher.matches", xtendMatcher.matches());
        final String xtendGroup1 = xtendMatcher.group(1);
        final String xtendGroup2 = xtendMatcher.group(2);
        final String xtendGroup3 = xtendMatcher.group(3);
        final String actualXtendCode = ((xtendGroup1 + xtendGroup2) + xtendGroup3);
        final XtendFile file = this.file(actualXtendCode, true);
        XtendTypeDeclaration _head = IterableExtensions.<XtendTypeDeclaration>head(file.getXtendTypes());
        final JvmGenericType inferredType = this._iXtendJvmAssociations.getInferredType(((XtendClass) _head));
        CharSequence compiledCode = this.generator.generateType(inferredType, this.generatorConfigProvider.get(inferredType));
        compiledCode = this.postProcessor.postProcess(null, compiledCode);
        final Matcher javaMatcher = this.p.matcher(java.toString());
        Assert.assertTrue("javaMatcher.matches", javaMatcher.matches());
        final String javaGroup1 = javaMatcher.group(1);
        final String javaGroup2 = javaMatcher.group(2);
        final String javaGroup3 = javaMatcher.group(3);
        final String actualJavaExpectation = ((javaGroup1 + javaGroup2) + javaGroup3);
        Assert.assertEquals(actualJavaExpectation, compiledCode.toString());
        AbstractTraceRegion _traceRegion = ((ITraceRegionProvider) compiledCode).getTraceRegion();
        URI _createURI = URI.createURI(file.eResource().getURI().path());
        SourceRelativeURI _sourceRelativeURI = new SourceRelativeURI(_createURI);
        URI _createURI_1 = URI.createURI(file.eResource().getURI().path());
        SourceRelativeURI _sourceRelativeURI_1 = new SourceRelativeURI(_createURI_1);
        AbstractTraceRegion _merge = this.merge(_traceRegion.invertFor(_sourceRelativeURI, _sourceRelativeURI_1));
        final SimpleTrace trace = new SimpleTrace(_merge);
        int _length = xtendGroup1.length();
        int _length_1 = xtendGroup2.length();
        TextRegion _textRegion = new TextRegion(_length, _length_1);
        final Iterable<? extends ILocationInResource> locations = trace.getAllAssociatedLocations(_textRegion);
        int _length_2 = javaGroup1.length();
        int _length_3 = javaGroup2.length();
        final TextRegion expectedRegion = new TextRegion(_length_2, _length_3);
        Assert.assertFalse(IterableExtensions.isEmpty(locations));
        for (final ILocationInResource location : locations) {
            ITextRegionWithLineInformation _textRegion_1 = location.getTextRegion();
            boolean _equals = Objects.equal(_textRegion_1, expectedRegion);
            if (_equals) {
                return;
            }
        }
        Assert.fail(((("locations did not match expectation: " + locations) + " / ") + expectedRegion));
    } catch (Throwable _e) {
        throw Exceptions.sneakyThrow(_e);
    }
}
Also used : XtendFile(org.eclipse.xtend.core.xtend.XtendFile) TextRegion(org.eclipse.xtext.util.TextRegion) Matcher(java.util.regex.Matcher) JvmGenericType(org.eclipse.xtext.common.types.JvmGenericType) SimpleTrace(org.eclipse.xtend.core.tests.compiler.SimpleTrace) URI(org.eclipse.emf.common.util.URI) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI) ILocationInResource(org.eclipse.xtext.generator.trace.ILocationInResource) ITextRegionWithLineInformation(org.eclipse.xtext.util.ITextRegionWithLineInformation) XtendClass(org.eclipse.xtend.core.xtend.XtendClass) XtendTypeDeclaration(org.eclipse.xtend.core.xtend.XtendTypeDeclaration) AbstractTraceRegion(org.eclipse.xtext.generator.trace.AbstractTraceRegion) ITraceRegionProvider(org.eclipse.xtext.generator.trace.ITraceRegionProvider) SourceRelativeURI(org.eclipse.xtext.generator.trace.SourceRelativeURI)

Example 32 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-xtend by eclipse.

the class PartialParserBenchmark method doTime.

public int doTime(IParser parser, int windowSize) {
    int result = 0;
    for (int j = 0; j < contentToParse.length() - windowSize; j++) {
        ReplaceRegion replaceRegion = new ReplaceRegion(new TextRegion(j, windowSize), contentToParse.substring(j, j + windowSize));
        IParseResult newParseResult = parser.reparse(parseResult, replaceRegion);
        if (newParseResult.getRootASTElement() != null) {
            result++;
        }
    }
    return result;
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) IParseResult(org.eclipse.xtext.parser.IParseResult)

Example 33 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.

the class IdeContentProposalProvider method _createProposals.

protected void _createProposals(final Assignment assignment, final ContentAssistContext context, final IIdeContentProposalAcceptor acceptor) {
    final AbstractElement terminal = assignment.getTerminal();
    if ((terminal instanceof CrossReference)) {
        this.createProposals(terminal, context, acceptor);
    } else {
        if ((terminal instanceof RuleCall)) {
            final AbstractRule rule = ((RuleCall) terminal).getRule();
            if (((rule instanceof TerminalRule) && context.getPrefix().isEmpty())) {
                String _xifexpression = null;
                String _name = rule.getName();
                boolean _equals = Objects.equal(_name, "STRING");
                if (_equals) {
                    String _feature = assignment.getFeature();
                    String _plus = ("\"" + _feature);
                    _xifexpression = (_plus + "\"");
                } else {
                    _xifexpression = assignment.getFeature();
                }
                final String proposal = _xifexpression;
                final Procedure1<ContentAssistEntry> _function = (ContentAssistEntry it) -> {
                    String _name_1 = rule.getName();
                    boolean _equals_1 = Objects.equal(_name_1, "STRING");
                    if (_equals_1) {
                        ArrayList<TextRegion> _editPositions = it.getEditPositions();
                        int _offset = context.getOffset();
                        int _plus_1 = (_offset + 1);
                        int _length = proposal.length();
                        int _minus = (_length - 2);
                        TextRegion _textRegion = new TextRegion(_plus_1, _minus);
                        _editPositions.add(_textRegion);
                        it.setKind(ContentAssistEntry.KIND_TEXT);
                    } else {
                        ArrayList<TextRegion> _editPositions_1 = it.getEditPositions();
                        int _offset_1 = context.getOffset();
                        int _length_1 = proposal.length();
                        TextRegion _textRegion_1 = new TextRegion(_offset_1, _length_1);
                        _editPositions_1.add(_textRegion_1);
                        it.setKind(ContentAssistEntry.KIND_VALUE);
                    }
                    it.setDescription(rule.getName());
                };
                final ContentAssistEntry entry = this.proposalCreator.createProposal(proposal, context, _function);
                acceptor.accept(entry, this.proposalPriorities.getDefaultPriority(entry));
            }
        }
    }
}
Also used : ContentAssistEntry(org.eclipse.xtext.ide.editor.contentassist.ContentAssistEntry) AbstractElement(org.eclipse.xtext.AbstractElement) TextRegion(org.eclipse.xtext.util.TextRegion) ArrayList(java.util.ArrayList) CrossReference(org.eclipse.xtext.CrossReference) TerminalRule(org.eclipse.xtext.TerminalRule) AbstractRule(org.eclipse.xtext.AbstractRule) RuleCall(org.eclipse.xtext.RuleCall)

Example 34 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.

the class FormattingService method format.

/**
 * @since 2.14
 */
public List<TextEdit> format(final XtextResource resource, final Document document, final int offset, final int length, final FormattingOptions options) {
    String indent = this.indentationInformation.getIndentString();
    if ((options != null)) {
        boolean _isInsertSpaces = options.isInsertSpaces();
        if (_isInsertSpaces) {
            indent = Strings.padEnd("", options.getTabSize(), ' ');
        }
    }
    if ((this.formatter2Provider != null)) {
        final MapBasedPreferenceValues preferences = new MapBasedPreferenceValues();
        preferences.put("indentation", indent);
        TextRegion _textRegion = new TextRegion(offset, length);
        final List<ITextReplacement> replacements = this.format2(resource, _textRegion, preferences);
        final Function1<ITextReplacement, TextEdit> _function = (ITextReplacement r) -> {
            return this.toTextEdit(document, r.getReplacementText(), r.getOffset(), r.getLength());
        };
        return IterableExtensions.<TextEdit>toList(ListExtensions.<ITextReplacement, TextEdit>map(replacements, _function));
    } else {
        return CollectionLiterals.<TextEdit>newArrayList();
    }
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion) ITextReplacement(org.eclipse.xtext.formatting2.regionaccess.ITextReplacement) TextEdit(org.eclipse.lsp4j.TextEdit) MapBasedPreferenceValues(org.eclipse.xtext.preferences.MapBasedPreferenceValues)

Example 35 with TextRegion

use of org.eclipse.xtext.util.TextRegion in project xtext-core by eclipse.

the class ContentAssistContextTestHelper method firstSetGrammarElementsToString.

public String firstSetGrammarElementsToString(final ContentAssistContextFactory factory) {
    final int offset = this.document.indexOf(this.cursor);
    Preconditions.checkArgument((offset >= 0), "you forgot to provide a cursor");
    final String doc = this.document.replace(this.cursor, "");
    final XtextResource res = this.parse(doc);
    factory.setPool(Executors.newCachedThreadPool());
    TextRegion _textRegion = new TextRegion(0, 0);
    final ContentAssistContext[] ctxs = factory.create(doc, _textRegion, offset, res);
    final GrammarElementTitleSwitch f = new GrammarElementTitleSwitch().showAssignments().showQualified().showRule();
    StringConcatenation _builder = new StringConcatenation();
    {
        Iterable<Pair<Integer, ContentAssistContext>> _indexed = IterableExtensions.<ContentAssistContext>indexed(((Iterable<? extends ContentAssistContext>) Conversions.doWrapArray(ctxs)));
        for (final Pair<Integer, ContentAssistContext> ctx : _indexed) {
            _builder.append("context");
            Integer _key = ctx.getKey();
            _builder.append(_key);
            _builder.append(" {");
            _builder.newLineIfNotEmpty();
            {
                ImmutableList<AbstractElement> _firstSetGrammarElements = ctx.getValue().getFirstSetGrammarElements();
                for (final AbstractElement ele : _firstSetGrammarElements) {
                    _builder.append("\t");
                    String _name = ele.eClass().getName();
                    _builder.append(_name, "\t");
                    _builder.append(": ");
                    String _apply = f.apply(ele);
                    _builder.append(_apply, "\t");
                    _builder.newLineIfNotEmpty();
                }
            }
            _builder.append("}");
            _builder.newLine();
        }
    }
    return _builder.toString();
}
Also used : TextRegion(org.eclipse.xtext.util.TextRegion) AbstractElement(org.eclipse.xtext.AbstractElement) XtextResource(org.eclipse.xtext.resource.XtextResource) GrammarElementTitleSwitch(org.eclipse.xtext.grammaranalysis.impl.GrammarElementTitleSwitch) ContentAssistContext(org.eclipse.xtext.ide.editor.contentassist.ContentAssistContext) StringConcatenation(org.eclipse.xtend2.lib.StringConcatenation) Pair(org.eclipse.xtext.xbase.lib.Pair)

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