diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CWordFinderTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CWordFinderTest.java new file mode 100644 index 00000000000..9b0676b71f4 --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/CWordFinderTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. 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 + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Anton Leherbauer (Wind River Systems) - initial API and implementation + *******************************************************************************/ + +package org.eclipse.cdt.ui.tests.text; + +import junit.framework.TestSuite; + +import org.eclipse.jface.text.BadLocationException; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.IDocument; +import org.eclipse.jface.text.IRegion; + +import org.eclipse.cdt.ui.tests.BaseUITestCase; + +import org.eclipse.cdt.internal.ui.text.CWordFinder; + +/** + * Tests for CWordFinder. + */ +public class CWordFinderTest extends BaseUITestCase { + + public static TestSuite suite() { + return suite(CWordFinderTest.class, "_"); + } + + protected void setUp() throws Exception { + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + public void testFindWord() throws BadLocationException { + IDocument doc= new Document(); + StringBuffer buf= new StringBuffer(); + String word= "word_0815"; + for (int i= 0; i < 10; i++) { + buf.append(' ').append(word); + } + doc.set(buf.toString()); + for (int i= 0; i < doc.getLength(); i++) { + IRegion wordRegion= CWordFinder.findWord(doc, i); + assertNotNull(wordRegion); + if (wordRegion.getLength() != 0) { + assertEquals(word.length(), wordRegion.getLength()); + assertEquals(word, doc.get(wordRegion.getOffset(), wordRegion.getLength())); + } + } + } + + public void testFindWordOnDocumentStart_Bug193461() { + IDocument doc= new Document(); + doc.set("word"); + for (int i= 0; i < doc.getLength(); i++) { + IRegion wordRegion= CWordFinder.findWord(doc, i); + assertNotNull(wordRegion); + assertEquals(doc.getLength(), wordRegion.getLength()); + assertEquals(0, wordRegion.getOffset()); + } + } +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java index 98ccb72157f..94e215d3071 100644 --- a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/TextTestSuite.java @@ -54,5 +54,8 @@ public class TextTestSuite extends TestSuite { // editor hyperlink tests addTest(HyperlinkTest.suite()); + + // word detection + addTest(CWordFinderTest.suite()); } } diff --git a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java index b06c94aa2cb..a0dace624e2 100644 --- a/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java +++ b/core/org.eclipse.cdt.ui/src/org/eclipse/cdt/internal/ui/text/CWordFinder.java @@ -46,7 +46,7 @@ public class CWordFinder { * error accessing the docment data. */ public static IRegion findWord(IDocument document, int offset) { - int start = -1; + int start = -2; int end = -1; try { @@ -77,7 +77,7 @@ public class CWordFinder { } catch (BadLocationException x) { } - if (start > -1 && end > -1) { + if (start >= -1 && end > -1) { if (start == offset && end == offset) return new Region(offset, 0); else if (start == offset)