use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.
the class FlexTokenRegionProviderTest method testTokenSplit.
@Test
public void testTokenSplit() throws Exception {
String model = " a b ";
ITextRegion tokenRegion = tokenRegionProvider.getTokenRegion(model, new TextRegion(2, 1));
assertEquals(2, tokenRegion.getOffset());
assertEquals(1, tokenRegion.getLength());
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.
the class FlexTokenRegionProvider method getTokenRegion.
@Override
public ITextRegion getTokenRegion(String text, ITextRegion region) {
try {
InternalFlexer flexer = flexerFactory.createFlexer(new StringReader(text));
int token = flexer.advance();
int prevStart = 0;
int nextStart = flexer.getTokenLength();
final int regionStartOffset = region.getOffset();
final int regionEnd = regionStartOffset + region.getLength();
while (token != Token.EOF && nextStart <= regionStartOffset) {
prevStart = nextStart;
token = flexer.advance();
nextStart += flexer.getTokenLength();
}
while (token != Token.EOF && nextStart < regionEnd) {
token = flexer.advance();
nextStart += flexer.getTokenLength();
}
return new TextRegion(prevStart, nextStart - prevStart);
} catch (IOException e) {
// cannot happen since StringReader doesn't throw IOException
throw new RuntimeException(e);
}
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.
the class XtendProposalProvider method completeInRichString.
public void completeInRichString(EObject model, RuleCall ruleCall, ContentAssistContext context, ICompletionProposalAcceptor acceptor) {
INode node = context.getCurrentNode();
ITextRegion textRegion = node.getTextRegion();
int offset = textRegion.getOffset();
int length = textRegion.getLength();
String currentNodeText = node.getText();
if (currentNodeText.startsWith("\u00BB") && offset + 1 <= context.getOffset() || currentNodeText.startsWith("'''") && offset + 3 <= context.getOffset()) {
if (context.getOffset() > offset && context.getOffset() < offset + length)
addGuillemotsProposal(context, acceptor);
} else if (currentNodeText.startsWith("\u00AB\u00AB")) {
try {
IDocument document = context.getViewer().getDocument();
int nodeLine = document.getLineOfOffset(offset);
int completionLine = document.getLineOfOffset(context.getOffset());
if (completionLine > nodeLine) {
addGuillemotsProposal(context, acceptor);
}
} catch (BadLocationException e) {
// ignore
}
}
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-xtend by eclipse.
the class XtendJavaDocProposalFactory method getReplacementContextLength.
@Override
protected int getReplacementContextLength(ContentAssistContext context) {
int replacementOffset = context.getReplaceRegion().getOffset();
INode currentNode = context.getCurrentNode();
ITextRegion currentRegion = currentNode.getTextRegion();
String text = currentNode.getText();
int index = text.indexOf('}', replacementOffset - currentRegion.getOffset());
if (index == -1) {
index = text.indexOf('\n', replacementOffset - currentRegion.getOffset());
}
if (index != -1) {
int indexFromStart = index + currentNode.getOffset();
return indexFromStart - replacementOffset;
} else {
return context.getReplaceContextLength();
}
}
use of org.eclipse.xtext.util.ITextRegion in project xtext-core by eclipse.
the class ContentAssistContext method getReplaceContextLength.
/**
* The length of the region left to the completion offset that is part of the
* replace region.
*/
public int getReplaceContextLength() {
if (replaceContextLength == null) {
int replacementOffset = getReplaceRegion().getOffset();
ITextRegion currentRegion = getCurrentNode().getTextRegion();
int replaceContextLength = currentRegion.getLength() - (replacementOffset - currentRegion.getOffset());
this.replaceContextLength = replaceContextLength;
return replaceContextLength;
}
return replaceContextLength.intValue();
}
Aggregations