use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class StringCharacterScanner method getColumn.
private int getColumn(int offset) {
// Bad and slow implementation
if (offset < 0 || offset > this.content.length()) {
return -1;
}
if (this.delimiters == null || this.delimiters.isEmpty()) {
return offset;
}
final StringBuilder sb = new StringBuilder("[");
for (final String delimiter : this.delimiters) {
sb.append("(?:").append(delimiter).append(")");
}
sb.append("]");
final RegExp regexp = RegExp.compile(sb.toString());
final String split = this.content;
int currentIndex = 0;
while (currentIndex < offset) {
final MatchResult matchResult = regexp.exec(split);
final int found = matchResult.getIndex();
if (found < 0) {
throw new RuntimeException("Invalid index for regexp match");
}
if (currentIndex + found > offset) {
// we're on the same line as offset
return offset - currentIndex;
}
currentIndex = currentIndex + found + 1;
}
return -1;
}
use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class CloseCStyleCommentChangeInterceptor method processChange.
@Override
public TextChange processChange(final TextChange change, ReadOnlyDocument document) {
final RegExp regex = RegExp.compile("^\n(\\s*)\\*\\s*$");
final MatchResult matchResult = regex.exec(change.getNewText());
// either must be on the first line or be just after a line break (regexp)
if (matchResult != null) {
final String line = document.getLineContent(change.getFrom().getLine());
// matches a line containing only whitespaces followed by either /** or /* and then optionally whitespaces again
if (!line.matches("^\\s*\\/\\*\\*?\\s*$")) {
return null;
}
final String whitespaces = matchResult.getGroup(1);
final String modifiedInsert = "\n" + whitespaces + "* \n" + whitespaces + "*/";
return new TextChange.Builder().from(change.getFrom()).to(change.getFrom()).insert(modifiedInsert).build();
} else {
return null;
}
}
use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class FirstLineFileTypeIdentifier method getShebang.
private String getShebang(final String content) {
final MatchResult matchResult = SHEBANG_PATTERN.exec(content);
if (matchResult == null || matchResult.getGroup(1) == null || matchResult.getGroup(1).isEmpty()) {
return null;
}
Log.debug(FirstLineFileTypeIdentifier.class, "File may be a script with a shebang.");
String loader = matchResult.getGroup(1);
// special case for /usr/bin/env
if ("/usr/bin/env".equals(loader)) {
Log.debug(FirstLineFileTypeIdentifier.class, "Shebang points to /usr/bin/env. Looking at the parameter.");
// we must use the first option as hint
if (matchResult.getGroup(2) == null || matchResult.getGroup(2).isEmpty()) {
return null;
}
loader = matchResult.getGroup(2);
Log.debug(FirstLineFileTypeIdentifier.class, "Shebang parameter kept: " + loader);
} else {
Log.debug(FirstLineFileTypeIdentifier.class, "Shebang loader kept: " + loader);
}
return loader;
}
use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class Elements method markupParagraph.
/** Creates a paragraph tag and fills it with spans and anchor tags internally. */
private static void markupParagraph(Element parent, String text, String linkCssClass) {
if (StringUtils.isNullOrWhitespace(text)) {
// don't add any dom here
return;
}
ParagraphElement myParagraph = createParagraphElement();
int index = 0;
REGEXP_MARKUP.setLastIndex(0);
SpanElement current = createSpanElement();
for (MatchResult match = REGEXP_MARKUP.exec(text); match != null; match = REGEXP_MARKUP.exec(text)) {
current.setTextContent(text.substring(index, match.getIndex()));
myParagraph.appendChild(current);
current = createSpanElement();
/*
* If our match is a \n we need to create a <br/> element to force a line break, otherwise we
* matched an http/www link so let's make an anchor tag out of it.
*/
if (match.getGroup(0).equals("\n")) {
myParagraph.appendChild(createBRElement());
} else {
AnchorElement anchor = createAnchorElement(linkCssClass);
anchor.setHref(match.getGroup(0));
anchor.setTarget("_blank");
anchor.setTextContent(match.getGroup(0));
myParagraph.appendChild(anchor);
}
index = match.getIndex() + match.getGroup(0).length();
}
current.setTextContent(text.substring(index));
myParagraph.appendChild(current);
parent.appendChild(myParagraph);
}
use of com.google.gwt.regexp.shared.MatchResult in project che by eclipse.
the class TextUtils method directionalRegexp.
/**
* Depending on the supplied direction, it will call either
* findMatchAfterIndex or findMatchBeforeIndex. Once the result is obtained it
* will return either the match index or the appropriate bound column
* (text.length() or -1).
*/
private static int directionalRegexp(boolean forward, RegExp regexp, String text, int column) {
MatchResult result = forward ? RegExpUtils.findMatchAfterIndex(regexp, text, column) : RegExpUtils.findMatchBeforeIndex(regexp, text, column);
int fallback = forward ? text.length() : -1;
return result == null ? fallback : result.getIndex();
}
Aggregations