summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorakosyakov2013-07-12 05:21:37 (EDT)
committer akosyakov2013-07-22 03:11:41 (EDT)
commit95f69b57aef4c363deab94081e7f56f797a9c91b (patch)
treeda74393495e671a66ff4eeca8bf3ae9c967c7572
parente8e0472348c02066e267317b4e00223cccf81533 (diff)
downloadorg.eclipse.xtext-95f69b57aef4c363deab94081e7f56f797a9c91b.zip
org.eclipse.xtext-95f69b57aef4c363deab94081e7f56f797a9c91b.tar.gz
org.eclipse.xtext-95f69b57aef4c363deab94081e7f56f797a9c91b.tar.bz2
[410481]: replacing the invocation of IDocument#get(offset, length)refs/changes/98/14498/6
method by invocations of String#indexOf and String#lastIndexOf methods Change-Id: Ic9fcd8346640e8ef3462d6dde9c71f472f7639a5 Signed-off-by: akosyakov <anton.kosyakov@itemis.de>
-rw-r--r--plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/autoedit/AbstractTerminalsEditStrategy.java30
-rw-r--r--plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/model/DocumentUtil.java78
2 files changed, 70 insertions, 38 deletions
diff --git a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/autoedit/AbstractTerminalsEditStrategy.java b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/autoedit/AbstractTerminalsEditStrategy.java
index d37b10b..bb8ffbf 100644
--- a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/autoedit/AbstractTerminalsEditStrategy.java
+++ b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/autoedit/AbstractTerminalsEditStrategy.java
@@ -55,36 +55,40 @@ public abstract class AbstractTerminalsEditStrategy extends AbstractEditStrategy
}
/**
- * finds the first start terminal which is not closed before the cursor position.
+ * finds the first stop terminal which has not been started after the cursor position.
*/
protected IRegion findStopTerminal(IDocument document, int offset) throws BadLocationException {
+ String documentText = document.get();
int stopOffset = offset;
int startOffset = offset;
while (true) {
- IRegion stop = util.searchInSamePartition(getRightTerminal(), document, stopOffset);
- if (stop==null)
+ IRegion stop = util.searchInSamePartition(getRightTerminal(), documentText, document, stopOffset);
+ if (stop == null) {
return null;
- IRegion start = util.searchInSamePartition(getLeftTerminal(), document, startOffset);
- if (start==null || start.getOffset()>stop.getOffset()) {
+ }
+ IRegion start = util.searchInSamePartition(getLeftTerminal(), documentText, document, startOffset);
+ if (start == null || start.getOffset() > stop.getOffset()) {
return stop;
}
- stopOffset = util.findNextOffSetInPartition(document, stopOffset, stop.getOffset()+stop.getLength());
- startOffset = util.findNextOffSetInPartition(document, startOffset, start.getOffset()+start.getLength());
+ stopOffset = util.findNextOffSetInPartition(document, stopOffset, stop.getOffset() + stop.getLength());
+ startOffset = util.findNextOffSetInPartition(document, startOffset, start.getOffset() + start.getLength());
}
}
/**
- * finds the first stop terminal which has not been started after the cursor position.
+ * finds the first start terminal which is not closed before the cursor position.
*/
protected IRegion findStartTerminal(IDocument document, int offset) throws BadLocationException {
+ String documentText = document.get();
int stopOffset = offset;
int startOffset = offset;
- while(true) {
- IRegion start = util.searchBackwardsInSamePartition(getLeftTerminal(), document, startOffset);
- if (start==null)
+ while (true) {
+ IRegion start = util.searchBackwardsInSamePartition(getLeftTerminal(), documentText, document, startOffset);
+ if (start == null) {
return null;
- IRegion stop = util.searchBackwardsInSamePartition(getRightTerminal(), document, stopOffset);
- if (stop == null || stop.getOffset()<start.getOffset())
+ }
+ IRegion stop = util.searchBackwardsInSamePartition(getRightTerminal(), documentText, document, stopOffset);
+ if (stop == null || stop.getOffset() < start.getOffset())
return start;
stopOffset = stop.getOffset();
startOffset = start.getOffset();
diff --git a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/model/DocumentUtil.java b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/model/DocumentUtil.java
index b93fd2f..5054169 100644
--- a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/model/DocumentUtil.java
+++ b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/model/DocumentUtil.java
@@ -23,20 +23,32 @@ public class DocumentUtil {
* @return the region of the match or <code>null</code> if no match were found
*/
public IRegion searchBackwardsInSamePartition(String toFind, IDocument document, int endOffset) throws BadLocationException {
- if (endOffset<0)
+ return searchBackwardsInSamePartition(toFind, document.get(), document, endOffset);
+ }
+
+ /**
+ * searches backwards for the given string within the same partition type
+ * @return the region of the match or <code>null</code> if no match were found
+ * @since 2.4
+ */
+ public IRegion searchBackwardsInSamePartition(String toFind, String documentText, IDocument document, int endOffset) throws BadLocationException {
+ if (endOffset < 0) {
return null;
+ }
+ int length = toFind.length();
+ String text = preProcessSearchString(documentText);
ITypedRegion partition = document.getPartition(endOffset);
- int indexOf = preProcessSearchString(document.get(0, endOffset)).lastIndexOf(toFind);
- while (indexOf>=0) {
+ int indexOf = text.lastIndexOf(toFind, endOffset - length);
+ while (indexOf >= 0) {
ITypedRegion partition2 = document.getPartition(indexOf);
if (partition2.getType().equals(partition.getType())) {
- return new Region(indexOf,toFind.length());
- }
- indexOf=preProcessSearchString(document.get(0, partition2.getOffset())).lastIndexOf(toFind);
+ return new Region(indexOf, length);
+ }
+ indexOf = text.lastIndexOf(toFind, partition2.getOffset() - length);
}
String trimmed = toFind.trim();
- if (trimmed.length() > 0 && trimmed.length() != toFind.length()) {
- return searchBackwardsInSamePartition(trimmed, document, endOffset);
+ if (trimmed.length() > 0 && trimmed.length() != length) {
+ return searchBackwardsInSamePartition(trimmed, documentText, document, endOffset);
}
return null;
}
@@ -50,35 +62,51 @@ public class DocumentUtil {
* @return the region of the match or <code>null</code> if no match were found
*/
public IRegion searchInSamePartition(String toFind, IDocument document, int startOffset) throws BadLocationException {
- if (startOffset>=document.getLength())
+ return searchInSamePartition(toFind, document.get(), document, startOffset);
+ }
+
+ /**
+ * searches for the given string within the same partition type
+ *
+ * @return the region of the match or <code>null</code> if no match were found
+ * @since 2.4
+ */
+ public IRegion searchInSamePartition(String toFind, String documentText, IDocument document, int startOffset)
+ throws BadLocationException {
+ if (startOffset >= document.getLength()) {
return null;
+ }
+ String text = preProcessSearchString(documentText);
ITypedRegion partition = document.getPartition(startOffset);
- int ignoredPrefix = startOffset;
- int subtractMe = 0;
- while(toFind.length() > subtractMe && Character.isWhitespace(toFind.charAt(subtractMe)))
- subtractMe++;
- ignoredPrefix = Math.max(ignoredPrefix - subtractMe, 0);
- String text = document.get();
- int indexOf = preProcessSearchString(text.substring(ignoredPrefix)).indexOf(toFind);
- if (indexOf!=-1)
- indexOf+=ignoredPrefix;
- while (indexOf>=0 && indexOf<document.getLength()) {
+
+ int indexOf = text.indexOf(toFind, getOffset(toFind, startOffset));
+ while (indexOf >= 0 && indexOf < document.getLength()) {
ITypedRegion partition2 = document.getPartition(indexOf);
if (partition2.getType().equals(partition.getType())) {
- return new Region(indexOf,toFind.length());
+ return new Region(indexOf, toFind.length());
}
- ignoredPrefix = partition2.getOffset()+partition2.getLength();
- indexOf=preProcessSearchString(text.substring(ignoredPrefix)).indexOf(toFind);
- if (indexOf!=-1)
- indexOf+=ignoredPrefix;
+ indexOf = text.indexOf(toFind, partition2.getOffset() + partition2.getLength());
}
String trimmed = toFind.trim();
if (trimmed.length() > 0 && trimmed.length() != toFind.length()) {
- return searchInSamePartition(trimmed, document, startOffset);
+ return searchInSamePartition(trimmed, documentText, document, startOffset);
}
return null;
}
+ private int getOffset(String toFind, int startOffset) {
+ int whitespacesCount = getWhitespacesCount(toFind);
+ return Math.max(startOffset - whitespacesCount, 0);
+ }
+
+ private int getWhitespacesCount(String toFind) {
+ int whitespacesCount = 0;
+ while (toFind.length() > whitespacesCount && Character.isWhitespace(toFind.charAt(whitespacesCount))) {
+ whitespacesCount++;
+ }
+ return whitespacesCount;
+ }
+
public boolean isSameLine(IDocument doc, int offset, int offset2) throws BadLocationException {
return doc.getLineOfOffset(offset)==doc.getLineOfOffset(offset2);
}