1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-08 18:26:01 +02:00

Fix and test for 193461: [Editor] double-click to select the first word of a file does not work

This commit is contained in:
Anton Leherbauer 2007-06-29 07:54:12 +00:00
parent 7d05c2c4ea
commit 7b01c2bb3f
3 changed files with 75 additions and 2 deletions

View file

@ -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());
}
}
}

View file

@ -54,5 +54,8 @@ public class TextTestSuite extends TestSuite {
// editor hyperlink tests // editor hyperlink tests
addTest(HyperlinkTest.suite()); addTest(HyperlinkTest.suite());
// word detection
addTest(CWordFinderTest.suite());
} }
} }

View file

@ -46,7 +46,7 @@ public class CWordFinder {
* error accessing the docment data. * error accessing the docment data.
*/ */
public static IRegion findWord(IDocument document, int offset) { public static IRegion findWord(IDocument document, int offset) {
int start = -1; int start = -2;
int end = -1; int end = -1;
try { try {
@ -77,7 +77,7 @@ public class CWordFinder {
} catch (BadLocationException x) { } catch (BadLocationException x) {
} }
if (start > -1 && end > -1) { if (start >= -1 && end > -1) {
if (start == offset && end == offset) if (start == offset && end == offset)
return new Region(offset, 0); return new Region(offset, 0);
else if (start == offset) else if (start == offset)