Search in sources :

Example 16 with ReplaceRegion

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

the class TokenRegionProvider method getTokenReplaceRegion.

public ReplaceRegion getTokenReplaceRegion(String changedText, ReplaceRegion replaceRegion) {
    int lengthDelta = replaceRegion.getText().length() - replaceRegion.getLength();
    ITextRegion tokenRegion = getTokenRegion(changedText, new TextRegion(replaceRegion.getOffset(), replaceRegion.getText().length()));
    if (tokenRegion.getOffset() == replaceRegion.getOffset() && tokenRegion.getLength() == replaceRegion.getText().length())
        return replaceRegion;
    else
        return new ReplaceRegion(tokenRegion.getOffset(), tokenRegion.getLength() - lengthDelta, changedText.substring(tokenRegion.getOffset(), tokenRegion.getOffset() + tokenRegion.getLength()));
}
Also used : ITextRegion(org.eclipse.xtext.util.ITextRegion) TextRegion(org.eclipse.xtext.util.TextRegion) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ITextRegion(org.eclipse.xtext.util.ITextRegion)

Example 17 with ReplaceRegion

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

the class DeltaScenarioProcessor method applyDelta.

protected String applyDelta(String data, int offset, int len, String change) {
    StringBuilder builder = new StringBuilder(data);
    new ReplaceRegion(offset, len, change).applyTo(builder);
    String result = builder.toString();
    return result;
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion)

Example 18 with ReplaceRegion

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

the class ContentAssistService method toCompletionItem.

protected CompletionItem toCompletionItem(ContentAssistEntry entry, int caretOffset, Position caretPosition, Document document) {
    CompletionItem completionItem = new CompletionItem();
    String label = entry.getLabel();
    if (label == null) {
        label = entry.getProposal();
    }
    completionItem.setLabel(label);
    completionItem.setDetail(entry.getDescription());
    completionItem.setDocumentation(entry.getDocumentation());
    String prefix = entry.getPrefix();
    if (prefix == null) {
        prefix = "";
    }
    int prefixOffset = caretOffset - prefix.length();
    Position prefixPosition = document.getPosition(prefixOffset);
    completionItem.setTextEdit(Either.forLeft(new TextEdit(new Range(prefixPosition, caretPosition), entry.getProposal())));
    completionItem.setKind(translateKind(entry));
    if (!entry.getTextReplacements().isEmpty()) {
        if (completionItem.getAdditionalTextEdits() == null) {
            completionItem.setAdditionalTextEdits(new ArrayList<>(entry.getTextReplacements().size()));
        }
        entry.getTextReplacements().forEach((ReplaceRegion it) -> completionItem.getAdditionalTextEdits().add(toTextEdit(it, document)));
    }
    if (ContentAssistEntry.KIND_SNIPPET.equals(entry.getKind())) {
        completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
    }
    return completionItem;
}
Also used : CompletionItem(org.eclipse.lsp4j.CompletionItem) Position(org.eclipse.lsp4j.Position) ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) TextEdit(org.eclipse.lsp4j.TextEdit) Range(org.eclipse.lsp4j.Range)

Example 19 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion 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 20 with ReplaceRegion

use of org.eclipse.xtext.util.ReplaceRegion in project xtext-eclipse by eclipse.

the class PartialParsingProcessor method processFile.

@Override
public String processFile(String completeData, String data, int offset, int len, String change) throws Exception {
    IParseResult initialParseResult = parser.parse(new StringReader(data));
    String newData = applyDelta(data, offset, len, change);
    ReplaceRegion replaceRegion = new ReplaceRegion(offset, len, change);
    try {
        IParseResult reparsed = parser.reparse(initialParseResult, replaceRegion);
        IParseResult parsedFromScratch = parser.parse(new StringReader(newData));
        assertEqual(data, newData, parsedFromScratch, reparsed);
        return newData;
    } catch (Throwable e) {
        ComparisonFailure throwMe = new ComparisonFailure(e.getMessage(), newData, replaceRegion + DELIM + data);
        throwMe.initCause(e);
        throw throwMe;
    }
}
Also used : ReplaceRegion(org.eclipse.xtext.util.ReplaceRegion) ComparisonFailure(org.junit.ComparisonFailure) StringReader(java.io.StringReader) IParseResult(org.eclipse.xtext.parser.IParseResult)

Aggregations

ReplaceRegion (org.eclipse.xtext.util.ReplaceRegion)52 ICompositeNode (org.eclipse.xtext.nodemodel.ICompositeNode)20 Test (org.junit.Test)19 XtextResource (org.eclipse.xtext.resource.XtextResource)18 Resource (org.eclipse.emf.ecore.resource.Resource)13 INode (org.eclipse.xtext.nodemodel.INode)11 IParseResult (org.eclipse.xtext.parser.IParseResult)10 StringConcatenation (org.eclipse.xtend2.lib.StringConcatenation)9 EObject (org.eclipse.emf.ecore.EObject)7 LazyLinkingResource (org.eclipse.xtext.linking.lazy.LazyLinkingResource)6 StringReader (java.io.StringReader)5 SyntheticCompositeNode (org.eclipse.xtext.nodemodel.impl.SyntheticCompositeNode)4 TextRegion (org.eclipse.xtext.util.TextRegion)4 List (java.util.List)3 BadLocationException (org.eclipse.jface.text.BadLocationException)3 TextEdit (org.eclipse.text.edits.TextEdit)3 XtendFile (org.eclipse.xtend.core.xtend.XtendFile)3 PartialParsingPointers (org.eclipse.xtext.parser.impl.PartialParsingPointers)3 ITextRegion (org.eclipse.xtext.util.ITextRegion)3 ArrayList (java.util.ArrayList)2