Bug 547624- StyledText.cursorPos should use getCaretOffset

After some text is selected, the caret actually points at the end of
selection. But cursorPos API was returning start column of selection,
because it was using x of selection range.
The API now uses StyledText's getCaretOffset.

Change-Id: I9ce73db82f6ab01e6ce2fa65a44486bf0f99431f
Signed-off-by: Aparna Argade <aprsac@yahoo.com>
Signed-off-by: Patrick Tasse <patrick.tasse@gmail.com>
diff --git a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotStyledTextTest.java b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotStyledTextTest.java
index b451ab0..12ff67c 100644
--- a/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotStyledTextTest.java
+++ b/org.eclipse.swtbot.swt.finder.test/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotStyledTextTest.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008, 2018 Ketan Padegaonkar and others.
+ * Copyright (c) 2008, 2019 Ketan Padegaonkar and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -293,7 +293,7 @@
 		styledText.setText("This is sample text");
 		bot.button("Clear").click();
 		styledText.click(line, column, SWT.MOD2);
-		assertEquals(new Position(0, 0), styledText.cursorPosition(true));
+		assertEquals(new Position(line, column), styledText.cursorPosition(true));
 		assertEquals("This is sample te", styledText.getSelection());
 		verifyNotifyClick(SWT.MOD2);
 	}
@@ -318,8 +318,8 @@
 		bot.button("Clear").click();
 		styledText.doubleClick(line, column);
 		// Default behavior: word is selected upon doubleClick, so cursor position needs
-		// to be compared with start column of the word
-		assertEquals(new Position(line, column - 1), styledText.cursorPosition(true));
+		// to be compared with end of the word
+		assertEquals(new Position(line, column + 1), styledText.cursorPosition(true));
 		assertEquals(styledText.getSelection(), "is");
 
 		verifyNotifyClick(0);
diff --git a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotStyledText.java b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotStyledText.java
index 3f72fc8..dbc0492 100644
--- a/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotStyledText.java
+++ b/org.eclipse.swtbot.swt.finder/src/org/eclipse/swtbot/swt/finder/widgets/SWTBotStyledText.java
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2008, 2018 Ketan Padegaonkar and others.
+ * Copyright (c) 2008, 2019 Ketan Padegaonkar and others.
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License v1.0
  * which accompanies this distribution, and is available at
@@ -179,30 +179,23 @@
 	}
 
 	/**
-	 * Gets the current position of the cursor. The returned position will contain a 0-based line and column.
+	 * Gets the current position of the cursor. The returned position will contain a
+	 * 0-based line and column, with tabs counting as 1 column.
 	 *
 	 * @return the position of the cursor in the styled text.
 	 * @see SWTBotStyledText#cursorPosition(boolean)
 	 */
 	public Position cursorPosition() {
-		return syncExec(new Result<Position>() {
-			@Override
-			public Position run() {
-				widget.setFocus();
-				int offset = widget.getSelectionRange().x;
-				int line = widget.getContent().getLineAtOffset(offset);
-				int offsetAtLine = widget.getContent().getOffsetAtLine(line);
-				int column = offset - offsetAtLine;
-				return new Position(line, column);
-			}
-		});
+		return cursorPosition(false);
 	}
 
 	/**
-	 * Gets the current position of the cursor. The returned position will contain a 0-based line and column.
+	 * Gets the current position of the cursor. The returned position will contain a
+	 * 0-based line and column.
 	 *
-	 * @param withTabWidth <code>true</code> if column in the returned position should consider tab width preference;
-	 *                     <code>false</code> if column in the returned position should count tab as 1.
+	 * @param withTabWidth <code>true</code> if column in the returned position
+	 *                     should consider tab width preference; <code>false</code>
+	 *                     if column in the returned position should count tab as 1.
 	 * @return the position of the cursor in the styled text.
 	 * @since 2.8
 	 */
@@ -211,7 +204,7 @@
 			@Override
 			public Position run() {
 				widget.setFocus();
-				int offset = widget.getSelectionRange().x;
+				int offset = widget.getCaretOffset();
 				int line = widget.getContent().getLineAtOffset(offset);
 				int offsetAtLine = widget.getContent().getOffsetAtLine(line);
 				int column = offset - offsetAtLine;