Java Code Examples for org.eclipse.xtext.util.Strings#countLineBreaks()

The following examples show how to use org.eclipse.xtext.util.Strings#countLineBreaks() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AppendableBasedTraceRegion.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void leftCompressTrace(String completeContent) {
	List<AbstractTraceRegion> allNested = getWritableNestedRegions();
	int i = 0;
	while (i < allNested.size()) {
		AppendableBasedTraceRegion nested = (AppendableBasedTraceRegion) allNested.get(i);
		int offset = nested.getMyOffset();
		int diff = 0;
		while(completeContent.charAt(offset + diff) <= ' ' && diff < nested.length) {
			diff++;
		}
		if (diff == nested.length) {
			allNested.remove(i);
		} else {
			nested.lineNumber += Strings.countLineBreaks(completeContent, offset, offset + diff);
			nested.offset += diff;
			nested.length -= diff;
			nested.leftCompressTrace(completeContent);
			i++;
		}
	}
}
 
Example 2
Source File: AppendableBasedTraceRegion.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
protected void rightCompressTrace(String completeContent) {
	List<AbstractTraceRegion> allNested = getWritableNestedRegions();
	int i = 0;
	while (i < allNested.size()) {
		AppendableBasedTraceRegion nested = (AppendableBasedTraceRegion) allNested.get(i);
		int endOffset = nested.getMyOffset() + nested.getMyLength() - 1;
		int diff = 0;
		while(completeContent.charAt(endOffset - diff) <= ' ' && diff < nested.length) {
			diff++;
		}
		if (diff != 0) {
			nested.endLineNumber -= Strings.countLineBreaks(completeContent, endOffset - diff + 1, endOffset + 1);
			nested.length -= diff;
			nested.rightCompressTrace(completeContent);
		}
		i++;
	}
}
 
Example 3
Source File: NodeModelTokenSource.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Skips the given leaf as it's hidden. If it was the last token to be returned, a hidden token may be syntesized if
 * would affect the semicolon insertion.
 */
private Token processHiddenToken(ILeafNode leaf) {
	Token result = nextToken();
	if (result == Token.EOF_TOKEN && Strings.countLineBreaks(leaf.getText()) > 0) {
		next = result;
		CommonToken hidden = new CommonToken(tokenTypeMapper.getInternalTokenType(leaf), leaf.getText());
		hidden.setChannel(Token.HIDDEN_CHANNEL);
		return hidden;
	}
	return result;
}
 
Example 4
Source File: DefaultTaskParser.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public List<Task> parseTasks(String source, TaskTags taskTags) {
	if (isEmpty(taskTags)) {
		return emptyList();
	}
	Map<String, TaskTag> taskTagsByName = getTaskTagsByName(taskTags);
	Matcher matcher = toPattern(taskTags).matcher(source);

	List<Task> tasks = null;
	int prevLine = 1;
	int prevOffset = 0;
	while (matcher.find()) {
		if (tasks == null) {
			tasks = new ArrayList<>();
		}
		Task task = new Task();
		String matchedTag = matcher.group(2);
		TaskTag tag = taskTagsByName.get(taskTags.isCaseSensitive() ? matchedTag : matchedTag.toLowerCase());
		task.setTag(tag);
		task.setDescription(matcher.group(3));
		task.setOffset(matcher.start(2));
		int countLineBreaks = Strings.countLineBreaks(source, prevOffset, task.getOffset());
		task.setLineNumber(countLineBreaks + prevLine);
		prevLine = task.getLineNumber();
		prevOffset = task.getOffset();
		tasks.add(task);
	}

	return tasks != null ? tasks : emptyList();
}
 
Example 5
Source File: XtextSyntaxDiagnosticWithRange.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public int getLine() {
	int startLine = getNode().getTotalStartLine();
	String text = getNode().getText();
	int result = Strings.countLineBreaks(text, 0, offset) + startLine;
	return result;
}
 
Example 6
Source File: AppendableBasedTraceRegion.java    From xtext-extras with Eclipse Public License 2.0 4 votes vote down vote up
public AppendableBasedTraceRegion(/* @Nullable */ AbstractTraceRegion parent, TreeAppendable delegate, int offset, int lineNumber) {
	super(parent);
	this.offset = offset;
	this.lineNumber = lineNumber;
	this.useForDebugging = delegate.isUseForDebugging();
	boolean useLocationsFromDelegate = true;
	if (parent != null) {
		SourceRelativeURI parentPath = parent.getAssociatedSrcRelativePath();
		if (parentPath != null) {
			useLocationsFromDelegate = !allLocationsMatch(delegate, parentPath);
		}
	}
	this.locations = copyLocationData(delegate, useLocationsFromDelegate);
	int length = 0;
	int line = lineNumber;
	for (Object child : delegate.getChildren()) {
		if (child instanceof TreeAppendable) {
			TreeAppendable castedChild = (TreeAppendable) child;
			if (hasVisibleChildren(castedChild)) {
				AppendableBasedTraceRegion childRegion = new AppendableBasedTraceRegion(this, castedChild, offset + length, line);
				length += childRegion.getMyLength();
				line = childRegion.getMyEndLineNumber();
			}
		} else {
			String s = child.toString();
			length += s.length();
			line += Strings.countLineBreaks(s);
		}
	}
	this.length = length;
	this.endLineNumber = line;
	if (parent == null) {
		this.generatedText = delegate.getContent();
		compressTrace(this.generatedText);
		IParseResult result = ((XtextResource) delegate.getState().getResource()).getParseResult();
		if (result != null)
			this.sourceText = result.getRootNode().getText();
		else
			this.sourceText = null;
	} else {
		this.generatedText = null;
		this.sourceText = null;
	}
}
 
Example 7
Source File: CharSequenceTraceWrapper.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public AbstractTraceRegion getTraceRegion() {
	ILocationData locationData = new LocationData(originOffset, originLength, originLineNumber, originEndLineNumber, originURI);
	AbstractTraceRegion result = new TraceRegion(0, delegate.length(), 0, Strings.countLineBreaks(delegate), true, locationData, null);
	return result;
}
 
Example 8
Source File: AbstractTextSegment.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public int getLineCount() {
	return Strings.countLineBreaks(getText()) + 1;
}