summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Kosyakov2013-02-21 09:51:40 (EST)
committer Gerrit Code Review @ Eclipse.org2013-02-24 07:12:19 (EST)
commitf8b0a81eddd65373602f257afd1a7ea128f7942a (patch)
treeb48ef404d02084a2593441013d7e2f938402172b
parentb1d6a5671b4cdd4773420e2ae39a95650d8bf232 (diff)
downloadorg.eclipse.xtext-f8b0a81eddd65373602f257afd1a7ea128f7942a.zip
org.eclipse.xtext-f8b0a81eddd65373602f257afd1a7ea128f7942a.tar.gz
org.eclipse.xtext-f8b0a81eddd65373602f257afd1a7ea128f7942a.tar.bz2
Fixed cursor behavior when selecting the first line of a multi linerefs/changes/65/10565/2
-rwxr-xr-xplugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/XtextEditor.java64
-rw-r--r--tests/org.eclipse.xtext.ui.tests/tests/org/eclipse/xtext/ui/tests/editor/cursorHandling/CursorHandlingTest.java17
2 files changed, 61 insertions, 20 deletions
diff --git a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/XtextEditor.java b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/XtextEditor.java
index 2d2cd6c..801d529 100755
--- a/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/XtextEditor.java
+++ b/plugins/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/XtextEditor.java
@@ -89,7 +89,6 @@ import org.eclipse.xtext.ui.editor.model.CommonWordIterator;
import org.eclipse.xtext.ui.editor.model.DocumentCharacterIterator;
import org.eclipse.xtext.ui.editor.model.ITokenTypeToPartitionTypeMapperExtension;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
-import org.eclipse.xtext.ui.editor.model.TerminalsTokenTypeToPartitionMapper;
import org.eclipse.xtext.ui.editor.model.XtextDocumentProvider;
import org.eclipse.xtext.ui.editor.model.XtextDocumentUtil;
import org.eclipse.xtext.ui.editor.preferences.IPreferenceStoreAccess;
@@ -1264,29 +1263,54 @@ public class XtextEditor extends TextEditor {
}
int index = super.getLineStartPosition(document, line, length, offset);
- if (tokenTypeToPartitionTypeMapperExtension.isMultiLineComment(type) || tokenTypeToPartitionTypeMapperExtension.isSingleLineComment(type)) {
- if (index < length - 1 && line.charAt(index) == '*' && line.charAt(index + 1) != '/') {
- do {
- ++index;
- } while (index < length && Character.isWhitespace(line.charAt(index)));
- } else if (index < length - 1 && line.charAt(index) == '/'
- && (line.charAt(index + 1) == '/' || line.charAt(index + 1) == '*')) {
- index++;
- do {
- ++index;
- } while (index < length && Character.isWhitespace(line.charAt(index)));
- }
- } else if (type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
- if (index < length - 1 && line.charAt(index) == '/'
- && (line.charAt(index + 1) == '/' || line.charAt(index + 1) == '*')) {
- index++;
- do {
- ++index;
- } while (index < length && Character.isWhitespace(line.charAt(index)));
+ if (tokenTypeToPartitionTypeMapperExtension.isMultiLineComment(type)
+ || tokenTypeToPartitionTypeMapperExtension.isSingleLineComment(type)) {
+ return getCommentLineStartPosition(line, length, offset, index);
+ }
+ if (type.equals(IDocument.DEFAULT_CONTENT_TYPE)) {
+ if (isStartOfSingleLineComment(line, length, index) && !isStartOfMultiLineComment(line, length, index)) {
+ return getTextStartPosition(line, length, index + 1);
}
}
return index;
}
+
+ private int getCommentLineStartPosition(final String line, final int length, final int offset, int index) {
+ if (isMiddleOfMultiLineComment(line, length, index)) {
+ return getTextStartPosition(line, length, index);
+ }
+ if (isStartOfMultiLineComment(line, length, index)) {
+ int textStartPosition = getTextStartPosition(line, length, index + 2);
+ return offset > textStartPosition ? textStartPosition : index;
+ }
+ if (isStartOfSingleLineComment(line, length, index)) {
+ return getTextStartPosition(line, length, index + 1);
+ }
+ return index;
+ }
+
+ private boolean isStartOfSingleLineComment(final String line, final int length, int index) {
+ return index < length - 1 && line.charAt(index) == '/'
+ && (line.charAt(index + 1) == '/' || line.charAt(index + 1) == '*');
+ }
+
+ private boolean isStartOfMultiLineComment(final String line, final int length, int index) {
+ return index < length - 2 && line.charAt(index) == '/'
+ && (line.charAt(index + 1) == '*' && line.charAt(index + 2) == '*');
+ }
+
+ private boolean isMiddleOfMultiLineComment(final String line, final int length, int index) {
+ return index < length - 1 && line.charAt(index) == '*' && line.charAt(index + 1) != '/';
+ }
+
+ private int getTextStartPosition(final String line, final int length, int index) {
+ int textStartPosition = index;
+ do {
+ ++textStartPosition;
+ } while (textStartPosition < length && Character.isWhitespace(line.charAt(textStartPosition)));
+ return textStartPosition;
+ }
+
}
}
diff --git a/tests/org.eclipse.xtext.ui.tests/tests/org/eclipse/xtext/ui/tests/editor/cursorHandling/CursorHandlingTest.java b/tests/org.eclipse.xtext.ui.tests/tests/org/eclipse/xtext/ui/tests/editor/cursorHandling/CursorHandlingTest.java
index ee8e81e..94790f9 100644
--- a/tests/org.eclipse.xtext.ui.tests/tests/org/eclipse/xtext/ui/tests/editor/cursorHandling/CursorHandlingTest.java
+++ b/tests/org.eclipse.xtext.ui.tests/tests/org/eclipse/xtext/ui/tests/editor/cursorHandling/CursorHandlingTest.java
@@ -94,6 +94,23 @@ public class CursorHandlingTest extends AbstractCursorHandlingTest {
" */", editor);
}
+ @Test public void testToLineStartInMlComment_04() throws Exception {
+ XtextEditor editor = openEditor(" /** beg|in\n" +
+ "*/");
+ toLineStart(editor);
+ assertState(" /** |begin\n" +
+ "*/", editor);
+ toLineStart(editor);
+ assertState(" |/** begin\n" +
+ "*/", editor);
+ toLineStart(editor);
+ assertState("| /** begin\n" +
+ "*/", editor);
+ toLineStart(editor);
+ assertState(" |/** begin\n" +
+ "*/", editor);
+ }
+
@Test public void testNavigateLeft_01() throws Exception {
XtextEditor editor = openEditor(" CamelCaseWo|rd ");
navigateLeft(editor);