diff options
author | Aparna Argade | 2018-07-05 15:28:21 +0000 |
---|---|---|
committer | Aparna Argade | 2018-08-06 10:17:08 +0000 |
commit | 80c71e85cba3bde597e7bd32d934c9bc3e500549 (patch) | |
tree | 4f0d5b5fd67a3b0e6a5abd17405008dc285e6120 | |
parent | 29c0445dbc322e4b527aaee741aa3d151918bbbd (diff) | |
download | org.eclipse.swtbot-80c71e85cba3bde597e7bd32d934c9bc3e500549.tar.gz org.eclipse.swtbot-80c71e85cba3bde597e7bd32d934c9bc3e500549.tar.xz org.eclipse.swtbot-80c71e85cba3bde597e7bd32d934c9bc3e500549.zip |
BugĀ 536131: SWTBotStyledText/Editor API to consider tabwidth for column
It provides additional functions for navigation, getting cursor
position, styles and also for type/insert text that consider tab width
preference. The user can specify the column number as shown by the
Eclipse text editor.
Change-Id: I79d8d78b576fc04eeaed6b4f2e296797a0539bcf
Signed-off-by: Aparna Argade <aprsac@yahoo.com>
3 files changed, 429 insertions, 27 deletions
diff --git a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseEditor.java b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseEditor.java index 9e0978e3..4208e35b 100644 --- a/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseEditor.java +++ b/org.eclipse.swtbot.eclipse.finder/src/org/eclipse/swtbot/eclipse/finder/widgets/SWTBotEclipseEditor.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2017 Ketan Padegaonkar and others. + * Copyright (c) 2008, 2018 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 @@ -11,7 +11,8 @@ * Ralf Ebert www.ralfebert.de - (bug 271630) SWTBot Improved RCP / Workbench support * Ingo Mohr - Bug 416859 * Lorenzo Bettini - https://bugs.eclipse.org/bugs/show_bug.cgi?id=435390 - * Patrick Tasse - Improve SWTBot menu API and implementation (Bug 479091) + * Patrick Tasse - Improve SWTBot menu API and implementation (Bug 479091) + * Aparna Argade - API to consider Tab width for column (Bug 536131) *******************************************************************************/ package org.eclipse.swtbot.eclipse.finder.widgets; @@ -173,7 +174,7 @@ public class SWTBotEclipseEditor extends SWTBotEditor { } /** - * Attempst to applys the quick fix. + * Attempts to apply the quick fix. * <p> * FIXME: this needs a lot of optimization. * </p> @@ -281,12 +282,26 @@ public class SWTBotEclipseEditor extends SWTBotEditor { * * @return the position of the cursor. * @see SWTBotStyledText#cursorPosition() + * @see SWTBotEclipseEditor#cursorPosition(boolean) */ public Position cursorPosition() { return styledText.cursorPosition(); } /** + * 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. + * @return the position of the cursor. + * @see SWTBotStyledText#cursorPosition(boolean) + * @since 2.8 + */ + public Position cursorPosition(boolean withTabWidth) { + return styledText.cursorPosition(withTabWidth); + } + + /** * Gets if the object's widget is enabled. * * @return <code>true</code> if the widget is enabled. @@ -309,8 +324,9 @@ public class SWTBotEclipseEditor extends SWTBotEditor { * Gets the style text. * * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @return the {@link StyleRange} at the specified location + * @see SWTBotEclipseEditor#getStyle(int, int, boolean) * @see SWTBotStyledText#getStyle(int, int) */ public StyleRange getStyle(int line, int column) { @@ -318,6 +334,21 @@ public class SWTBotEclipseEditor extends SWTBotEditor { } /** + * Gets the style text. + * + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @return the {@link StyleRange} at the specified location + * @see SWTBotStyledText#getStyle(int, int, boolean) + * @since 2.8 + */ + public StyleRange getStyle(int line, int column, boolean withTabWidth) { + return styledText.getStyle(line, column, withTabWidth); + } + + /** * Gets the text of this object's widget. * * @return the text on the styledtext. @@ -329,8 +360,9 @@ public class SWTBotEclipseEditor extends SWTBotEditor { /** * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @param text the text to be typed at the specified location + * @see SWTBotEclipseEditor#typeText(int, int, String, boolean) * @see SWTBotStyledText#typeText(int, int, java.lang.String) * @since 1.0 */ @@ -339,6 +371,19 @@ public class SWTBotEclipseEditor extends SWTBotEditor { } /** + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param text the text to be typed at the specified location + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @see SWTBotStyledText#typeText(int, int, String, boolean) + * @since 2.8 + */ + public void typeText(int line, int column, String text, boolean withTabWidth) { + styledText.typeText(line, column, text, withTabWidth); + } + + /** * @param text the text to be typed at the location of the caret. * * @see SWTBotStyledText#typeText(java.lang.String) * @since 1.0 @@ -349,8 +394,9 @@ public class SWTBotEclipseEditor extends SWTBotEditor { /** * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @param text the text to be inserted at the specified location + * @see SWTBotEclipseEditor#insertText(int, int, String, boolean) * @see SWTBotStyledText#insertText(int, int, java.lang.String) */ public void insertText(int line, int column, String text) { @@ -358,6 +404,19 @@ public class SWTBotEclipseEditor extends SWTBotEditor { } /** + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param text the text to be inserted at the specified location. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @see SWTBotStyledText#insertText(int, int, String, boolean) + * @since 2.8 + */ + public void insertText(int line, int column, String text, boolean withTabWidth) { + styledText.insertText(line, column, text, withTabWidth); + } + + /** * @param text the text to be inserted at the location of the caret. * @see SWTBotStyledText#insertText(java.lang.String) */ @@ -386,7 +445,8 @@ public class SWTBotEclipseEditor extends SWTBotEditor { /** * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. + * @see SWTBotEclipseEditor#navigateTo(int, int, boolean) * @see SWTBotStyledText#navigateTo(int, int) */ public void navigateTo(int line, int column) { @@ -394,6 +454,18 @@ public class SWTBotEclipseEditor extends SWTBotEditor { } /** + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @see SWTBotStyledText#navigateTo(int, int, boolean) + * @since 2.8 + */ + public void navigateTo(int line, int column, boolean withTabWidth) { + styledText.navigateTo(line, column, withTabWidth); + } + + /** * Notifies of the keyboard event. * <p> * FIXME need some work for CTRL|SHIFT + 1 the 1 is to be sent as '!' in this case. @@ -463,8 +535,9 @@ public class SWTBotEclipseEditor extends SWTBotEditor { /** * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @param length the length of the selection. + * @see SWTBotEclipseEditor#selectRange(int, int, int, boolean) * @see SWTBotStyledText#selectRange(int, int, int) */ public void selectRange(int line, int column, int length) { @@ -472,6 +545,19 @@ public class SWTBotEclipseEditor extends SWTBotEditor { } /** + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param length the length of the selection. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @see SWTBotStyledText#selectRange(int, int, int, boolean) + * @since 2.8 + */ + public void selectRange(int line, int column, int length, boolean withTabWidth) { + styledText.selectRange(line, column, length, withTabWidth); + } + + /** * @param line the line number to select, 0 based. * @see SWTBotStyledText#selectLine(int) * @since 1.1 @@ -525,9 +611,10 @@ public class SWTBotEclipseEditor extends SWTBotEditor { /** * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @param length the length. * @return the styles in the specified range. + * @see SWTBotEclipseEditor#getStyles(int, int, int, boolean) * @see SWTBotStyledText#getStyles(int, int, int) */ public StyleRange[] getStyles(int line, int column, int length) { @@ -535,6 +622,20 @@ public class SWTBotEclipseEditor extends SWTBotEditor { } /** + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param length the length. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @return the styles in the specified range. + * @see SWTBotStyledText#getStyles(int, int, int, boolean) + * @since 2.8 + */ + public StyleRange[] getStyles(int line, int column, int length, boolean withTabWidth) { + return styledText.getStyles(line, column, length, withTabWidth); + } + + /** * @return the text on the current line, without the line delimiters. * @see SWTBotStyledText#getTextOnCurrentLine() */ 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 dbcef9da..f39f2df4 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,2010 Ketan Padegaonkar and others. + * Copyright (c) 2008, 2018 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 @@ -7,6 +7,7 @@ * * Contributors: * Ketan Padegaonkar - initial API and implementation + * Aparna Argade - API to consider Tab width for column (Bug 536131) *******************************************************************************/ package org.eclipse.swtbot.swt.finder.widgets; @@ -44,6 +45,15 @@ public class SWTBotStyledTextTest extends AbstractCustomControlExampleTest { } @Test + public void selectsRangeWithTabWidth() throws Exception { + styledText.setText("hello world\n" + "it is a \tvery good day today\n" + "good bye world\n" + "it was nice to meet you"); + styledText.selectRange(1, 0, 31, true); //display column considering tab width + assertEquals("it is a \tvery good day today", styledText.getSelection()); + styledText.selectRange(1, 0, 28, false); //character count + assertEquals("it is a \tvery good day today", styledText.getSelection()); + } + + @Test public void findsTextStyle() throws Exception { setStyles(); StyleRange range = styledText.getStyle(1, 2); @@ -75,6 +85,48 @@ public class SWTBotStyledTextTest extends AbstractCustomControlExampleTest { } @Test + public void findsTextStyleWithTabWidth() throws Exception { + setStylesWithTabWidth(); + StyleRange range = styledText.getStyle(1, 12, true); + assertEquals(SWT.BOLD, range.fontStyle); + assertTrue(range.underline); + range = styledText.getStyle(1, 9, false); + assertEquals(SWT.BOLD, range.fontStyle); + assertTrue(range.underline); + } + + @Test + public void findsStylesRangeInARangeWithTabWidth() throws Exception { + setStylesWithTabWidth(); + StyleRange[] styles = styledText.getStyles(1, 0, 50, true); + assertEquals(3, styles.length); + assertTrue(styles[0].underline); + assertEquals(SWT.BOLD, styles[0].fontStyle); + assertTrue(styles[0].underline); + assertEquals(SWT.ITALIC, styles[1].fontStyle); + assertTrue(styles[2].strikeout); + styles = styledText.getStyles(1, 0, 44, false); + assertEquals(3, styles.length); + assertTrue(styles[0].underline); + assertEquals(SWT.BOLD, styles[0].fontStyle); + assertTrue(styles[0].underline); + assertEquals(SWT.ITALIC, styles[1].fontStyle); + assertTrue(styles[2].strikeout); + } + + private void setStylesWithTabWidth() throws WidgetNotFoundException { + styledText.setText("hello world\n" + "it is a \tvery good \tday today\n" + "good\tbye\tworld\n" + "it was nice to meet you"); + styledText.selectRange(1, 12, 9, true); //selects "very good" and make it bold, underline + bot.buttonWithLabel("Bold").click(); + styledText.selectRange(1, 12, 9, true); + bot.buttonWithLabel("Underline").click(); + styledText.selectRange(1, 24, 9, true); //selects "day today" and make it Italic + bot.buttonWithLabel("Italic").click(); + styledText.selectRange(2, 12, 5, true); //selects "world" and strike it out + bot.buttonWithLabel("Strikeout").click(); + } + + @Test public void setsText() throws Exception { styledText.setText("hello world"); assertTextContains("hello world", styledText.widget); @@ -88,12 +140,31 @@ public class SWTBotStyledTextTest extends AbstractCustomControlExampleTest { } @Test + public void navigatesToAParticularLocationWithTAB() throws Exception { + styledText.setText("\thello\tworld\n"); + styledText.navigateTo(0, 13, true); + assertEquals(new Position(0, 13), styledText.cursorPosition(true)); + styledText.selectRange(0, 13, 1, true); + assertEquals("o", styledText.getSelection()); + } + + @Test public void typesTextAtALocation() throws Exception { styledText.typeText(1, 0, "---typed Text---\n"); assertTextContains("---typed Text---", styledText.widget); } @Test + public void typesTextAtALocationWithTabWidth() throws Exception { + styledText.setText("hello world\n" + "it is a \tvery good day today\n"); + styledText.typeText(1, 26, "---typed Text---", true); + assertTextContains("it is a \tvery good day ---typed Text---today\n", styledText.widget); + styledText.setText("hello world\n" + "it is a \tvery good day today\n"); + styledText.typeText(1, 23, "---typed Text---", false); + assertTextContains("it is a \tvery good day ---typed Text---today\n", styledText.widget); + } + + @Test public void typesSpecialCharactersAtALocation() throws Exception { styledText.typeText(1, 0, "---123 #@! :; {} [] ---\n"); assertTextContains("---123 #@! :; {} [] ---", styledText.widget); @@ -105,10 +176,20 @@ public class SWTBotStyledTextTest extends AbstractCustomControlExampleTest { assertTextContains("---inserted text---", styledText.widget); } - // FIXME on windows - public void _testTypesTextAtALocation() throws Exception { - styledText.typeText(1, 0, "---\n\typed text\n---\n"); - assertTextContains("---\n\typed text\n---\n", styledText.widget); + @Test + public void insertsTextAtALocationWithTabWidth() throws Exception { + styledText.setText("hello world\n" + "it is a \tvery good day today\n"); + styledText.insertText(1, 26, "---inserted text---\n", true); + assertTextContains("it is a \tvery good day ---inserted text---\ntoday\n", styledText.widget); + styledText.setText("hello world\n" + "it is a \tvery good day today\n"); + styledText.insertText(1, 23, "---inserted text---\n", false); + assertTextContains("it is a \tvery good day ---inserted text---\ntoday\n", styledText.widget); + } + + @Test + public void testTypesTextAtALocation() throws Exception { + styledText.typeText(1, 0, "---\n\ttyped text\n---\n"); + assertTextContains("---\n\ttyped text\n---\n", styledText.widget); } @Test 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 148780f0..f42307ea 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, 2017 Ketan Padegaonkar and others. + * Copyright (c) 2008, 2018 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 @@ -7,6 +7,7 @@ * * Contributors: * Ketan Padegaonkar - initial API and implementation + * Aparna Argade - API to consider Tab width for column (Bug 536131) *******************************************************************************/ package org.eclipse.swtbot.swt.finder.widgets; @@ -84,7 +85,7 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { * <p> * FIXME need some work for CTRL|SHIFT + 1 the 1 is to be sent as '!' in this case. * </p> - * + * * @param modificationKeys the modification keys. * @param c the character. * @see Event#character @@ -116,13 +117,14 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { * Sets the caret at the specified location. * * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. + * @see SWTBotStyledText#navigateTo(int, int, boolean) */ public void navigateTo(final int line, final int column) { log.debug(MessageFormat.format("Enquing navigation to location {0}, {1} in {2}", line, column, this)); //$NON-NLS-1$ waitForEnabled(); setFocus(); - asyncExec(new VoidResult() { + syncExec(new VoidResult() { @Override public void run() { log.debug(MessageFormat.format("Navigating to location {0}, {1} in {2}", line, column, widget)); //$NON-NLS-1$ @@ -134,6 +136,28 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { /** * Sets the caret at the specified location. * + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as a 1. + * @since 2.8 + */ + public void navigateTo(final int line, final int column, final boolean withTabWidth) { + log.debug(MessageFormat.format("Enquing navigation to location {0}, {1} in {2}", line, column, this)); //$NON-NLS-1$ + waitForEnabled(); + setFocus(); + syncExec(new VoidResult() { + @Override + public void run() { + log.debug(MessageFormat.format("Navigating to location {0}, {1} in {2}", line, column, widget)); //$NON-NLS-1$ + widget.setSelection(offset(line, column, withTabWidth)); + } + }); + } + + /** + * Sets the caret at the specified location. + * * @param position the position of the caret. */ public void navigateTo(Position position) { @@ -141,9 +165,22 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { } /** + * Sets the caret at the specified location. + * + * @param position the position of the caret. + * @param withTabWidth <code>true</code> if column of the position is specified considering tab width preference; + * <code>false</code> if column of the position is specified counting tab as 1. + * @since 2.8 + */ + public void navigateTo(Position position, final boolean withTabWidth) { + navigateTo(position.line, position.column, withTabWidth); + } + + /** * Gets the current position of the cursor. The returned position will contain a 0-based line and column. * * @return the position of the cursor in the styled text. + * @see SWTBotStyledText#cursorPosition(boolean) */ public Position cursorPosition() { return syncExec(new Result<Position>() { @@ -160,11 +197,69 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { } /** + * 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. + * @return the position of the cursor in the styled text. + * @since 2.8 + */ + public Position cursorPosition(final boolean withTabWidth) { + 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; + if (!withTabWidth) { + return new Position(line, column); + } else { + int tabwidth = widget.getTabs(); + int displayColumn = 0; + // Get display column corresponding to character index + for (int i = 0; i < column; i++) { + displayColumn = getDisplayColumnForNextChar(offsetAtLine, i, displayColumn, tabwidth); + } + return new Position(line, displayColumn); + } + } + }); + } + + /** + * Returns display column index for next character. + * + * If current character is tab, it calculates the spaces consumed by tab by + * considering the column index at which tab occurs. If current character is not + * tab, it increments display column by 1. + * + * @param offset offset of start of the line + * @param column column of current character, 0 based + * @param displayColumn display column for the current character, 0 based + * @param tabwidth tab width measured in characters + * @return display column for the next character, 0 based + */ + private int getDisplayColumnForNextChar(int offset, int column, int displayColumn, int tabwidth) { + char ch = widget.getContent().getTextRange(offset + column, 1).charAt(0); + if (ch == SWT.TAB) { + // Increment displayColumn by spaces required to align to tab-width + int mod = displayColumn % tabwidth; + displayColumn += (tabwidth - mod); + } else { + displayColumn++; + } + return displayColumn; + } + + /** * Types the text at the given location. * * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @param text the text to be typed at the specified location + * @see SWTBotStyledText#typeText(int, int, String, boolean) * @since 1.0 */ public void typeText(int line, int column, String text) { @@ -173,11 +268,27 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { } /** - * Inserts text at the given location. + * Types text at the given location. * * @param line the line number, 0 based. * @param column the column number, 0 based. + * @param text the text to be typed at the specified location + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @since 2.8 + */ + public void typeText(int line, int column, String text, boolean withTabWidth) { + navigateTo(line, column, withTabWidth); + typeText(text); + } + + /** + * Inserts text at the given location. + * + * @param line the line number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @param text the text to be inserted at the specified location + * @see SWTBotStyledText#insertText(int, int, String, boolean) */ public void insertText(int line, int column, String text) { navigateTo(line, column); @@ -185,6 +296,21 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { } /** + * Inserts text at the given location. + * + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param text the text to be inserted at the specified location. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @since 2.8 + */ + public void insertText(int line, int column, String text, boolean withTabWidth) { + navigateTo(line, column, withTabWidth); + insertText(text); + } + + /** * Inserts text at the end. * <p> * FIXME handle line endings @@ -235,8 +361,9 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { * Gets the style for the given line. * * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @return the {@link StyleRange} at the specified location + * @see SWTBotStyledText#getStyle(int, int, boolean) */ public StyleRange getStyle(final int line, final int column) { return syncExec(new Result<StyleRange>() { @@ -248,23 +375,70 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { } /** - * Gets the offset. + * Gets the style for the given line. * * @param line the line number, 0 based. * @param column the column number, 0 based. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @return the {@link StyleRange} at the specified location + * @since 2.8 + */ + public StyleRange getStyle(final int line, final int column, final boolean withTabWidth) { + return syncExec(new Result<StyleRange>() { + @Override + public StyleRange run() { + return widget.getStyleRangeAtOffset(offset(line, column, withTabWidth)); + } + }); + } + + /** + * Gets the offset. + * + * @param line the line number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @return the character offset at the specified location in the styledtext. * @see StyledTextContent#getOffsetAtLine(int) + * @see SWTBotStyledText#offset(int, int, boolean) */ protected int offset(final int line, final int column) { return widget.getContent().getOffsetAtLine(line) + column; } /** - * Selects the range. + * Gets the offset. * * @param line the line number, 0 based. * @param column the column number, 0 based. + * @param withTabWidth <code>true</code> if column is specified considering tab width preference; + * <code>false</code> if column is specified counting tab as 1. + * @return the character offset at the specified location in the styledtext. + * @see StyledTextContent#getOffsetAtLine(int) + * @since 2.8 + */ + protected int offset(final int line, final int column, final boolean withTabWidth) { + int offset = widget.getContent().getOffsetAtLine(line); + if (!withTabWidth) { + return offset + column; + } else { + int tabwidth = widget.getTabs(); + int charIndex = 0; + // Get character index considering display column that may change due to tabs. + for (int displayColumn = 0; column > charIndex && column > displayColumn; charIndex++) { + displayColumn = getDisplayColumnForNextChar(offset, charIndex, displayColumn, tabwidth); + } + return offset + charIndex; + } + } + + /** + * Selects the range. + * + * @param line the line number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @param length the length of the selection. + * @see SWTBotStyledText#selectRange(int, int, int, boolean) */ public void selectRange(final int line, final int column, final int length) { waitForEnabled(); @@ -279,6 +453,29 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { } /** + * Selects the range. + * + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param length the length of the selection. + * @param withTabWidth <code>true</code> if column and length are specified considering tab width preference; + * <code>false</code> if column and length are specified considering tab as 1. + * @since 2.8 + */ + public void selectRange(final int line, final int column, final int length, final boolean withTabWidth) { + waitForEnabled(); + asyncExec(new VoidResult() { + @Override + public void run() { + int offset = offset(line, column, withTabWidth); + int endOffset = offset(line, column + length, withTabWidth); + widget.setSelection(offset, endOffset); + } + }); + notify(SWT.Selection); + } + + /** * Gets the current selection text. * * @return the selection in the styled text @@ -296,9 +493,10 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { * Gets the style information. * * @param line the line number, 0 based. - * @param column the column number, 0 based. + * @param column the column number, 0 based. Here Tab needs to be counted as 1. * @param length the length. * @return the styles in the specified range. + * @see SWTBotStyledText#getStyles(int, int, int, boolean) * @see StyledText#getStyleRanges(int, int) */ public StyleRange[] getStyles(final int line, final int column, final int length) { @@ -307,7 +505,29 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { public StyleRange[] run() { return widget.getStyleRanges(offset(line, column), length); } + }); + } + /** + * Gets the style information. + * + * @param line the line number, 0 based. + * @param column the column number, 0 based. + * @param length the length. + * @param withTabWidth <code>true</code> if column and length are specified considering tab width preference; + * <code>false</code> if column and length are specified counting tab as 1. + * @return the styles in the specified range. + * @see StyledText#getStyleRanges(int, int) + * @since 2.8 + */ + public StyleRange[] getStyles(final int line, final int column, final int length, final boolean withTabWidth) { + return syncExec(new ArrayResult<StyleRange>() { + @Override + public StyleRange[] run() { + int offset = offset(line, column, withTabWidth); + int endOffset = offset(line, column + length, withTabWidth); + return widget.getStyleRanges(offset, endOffset - offset); + } }); } @@ -425,7 +645,7 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { /** * Gets the number of lines in the {@link StyledText}. - * + * * @return the number of lines in the {@link StyledText}. */ public int getLineCount(){ @@ -439,7 +659,7 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { /** * Gets all the lines in the editor. - * + * * @return the lines in the editor. */ public List<String> getLines() { @@ -458,7 +678,7 @@ public class SWTBotStyledText extends AbstractSWTBotControl<StyledText> { /** * Gets the tab width of the {@link StyledText} measured in characters. - * + * * @return the tab width of the {@link StyledText} measured in characters. */ public int getTabs() { |