1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-31 21:05:37 +02:00

Bug 284664.

This commit is contained in:
Sergey Prigogin 2009-07-25 22:16:08 +00:00
parent 2851e70790
commit cc5f407e23
3 changed files with 66 additions and 42 deletions

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* Anton Leherbauer (Wind River Systems) - initial API and implementation * Anton Leherbauer (Wind River Systems) - initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.ui.tests.text; package org.eclipse.cdt.ui.tests.text;
@ -39,6 +40,32 @@ public class CWordFinderTest extends BaseUITestCase {
super.tearDown(); super.tearDown();
} }
public void testBasic() {
IDocument doc= new Document(" func(int a, int b);");
IRegion region = CWordFinder.findWord(doc, 1);
assertEquals(1, region.getOffset());
assertEquals(4, region.getLength());
region = CWordFinder.findWord(doc, 4);
assertEquals(1, region.getOffset());
assertEquals(4, region.getLength());
region = CWordFinder.findWord(doc, 5);
assertEquals(1, region.getOffset());
assertEquals(4, region.getLength());
region = CWordFinder.findWord(doc, 6);
assertEquals(6, region.getOffset());
assertEquals(3, region.getLength());
region = CWordFinder.findWord(doc, 12);
assertEquals(12, region.getOffset());
assertEquals(0, region.getLength());
doc= new Document("func();");
region = CWordFinder.findWord(doc, 0);
assertEquals(0, region.getOffset());
assertEquals(4, region.getLength());
region = CWordFinder.findWord(doc, 5);
assertEquals(5, region.getOffset());
assertEquals(0, region.getLength());
}
public void testFindWord() throws BadLocationException { public void testFindWord() throws BadLocationException {
IDocument doc= new Document(); IDocument doc= new Document();
StringBuffer buf= new StringBuffer(); StringBuffer buf= new StringBuffer();

View file

@ -36,7 +36,6 @@ import org.eclipse.cdt.internal.ui.editor.CElementHyperlinkDetector;
* @author Mike Kucera * @author Mike Kucera
*/ */
public class HyperlinkTest extends TestCase { public class HyperlinkTest extends TestCase {
private static final String CPP_FILE_NAME = "hyperlink_test_cpp.cpp"; private static final String CPP_FILE_NAME = "hyperlink_test_cpp.cpp";
private static final String CPP_CODE = private static final String CPP_CODE =
@ -59,14 +58,12 @@ public class HyperlinkTest extends TestCase {
" p + p;" + " p + p;" +
"} \n"; "} \n";
private static final String C_FILE_NAME_1 = "hyperlink_test_c_1.c"; private static final String C_FILE_NAME_1 = "hyperlink_test_c_1.c";
private static final String C_CODE_1 = private static final String C_CODE_1 =
"int main() { \n" + "int main() { \n" +
" int class = 99; \n" + " int class = 99; \n" +
"}"; "}";
private static final String C_FILE_NAME_2 = "hyperlink_test_c_2.c"; private static final String C_FILE_NAME_2 = "hyperlink_test_c_2.c";
private static final String C_CODE_2 = private static final String C_CODE_2 =
"#ifdef NOTDEF\n" + "#ifdef NOTDEF\n" +
@ -74,17 +71,15 @@ public class HyperlinkTest extends TestCase {
"#else\n" + "#else\n" +
" int itworks = 100; \n" + " int itworks = 100; \n" +
"#endif\n"; "#endif\n";
private ICProject project; private ICProject project;
private CEditor editor; private CEditor editor;
public static TestSuite suite() { public static TestSuite suite() {
return new TestSuite(HyperlinkTest.class); return new TestSuite(HyperlinkTest.class);
} }
private void setUpEditor(String fileName, String code) throws Exception { private void setUpEditor(String fileName, String code) throws Exception {
super.setUp(); super.setUp();
project= CProjectHelper.createCCProject(super.getName(), "unused", IPDOMManager.ID_NO_INDEXER); project= CProjectHelper.createCCProject(super.getName(), "unused", IPDOMManager.ID_NO_INDEXER);
@ -158,7 +153,7 @@ public class HyperlinkTest extends TestCase {
// no hyperlinks for punctuation // no hyperlinks for punctuation
assertNotHyperlink(CPP_CODE.indexOf("{")); assertNotHyperlink(CPP_CODE.indexOf("{"));
assertNotHyperlink(CPP_CODE.indexOf("}")); assertNotHyperlink(CPP_CODE.indexOf("}"));
assertNotHyperlink(CPP_CODE.indexOf("(")); assertNotHyperlink(CPP_CODE.indexOf("(" + 1));
assertNotHyperlink(CPP_CODE.indexOf(")")); assertNotHyperlink(CPP_CODE.indexOf(")"));
assertNotHyperlink(CPP_CODE.indexOf(":")); assertNotHyperlink(CPP_CODE.indexOf(":"));
assertNotHyperlink(CPP_CODE.indexOf(";")); assertNotHyperlink(CPP_CODE.indexOf(";"));
@ -178,8 +173,7 @@ public class HyperlinkTest extends TestCase {
// hyperlinks for overloaded operators // hyperlinks for overloaded operators
assertHyperlink(CPP_CODE.indexOf("+ p"), CPP_CODE.indexOf("+ p"), 1); assertHyperlink(CPP_CODE.indexOf("+ p"), CPP_CODE.indexOf("+ p"), 1);
} }
public void testHyperlinksCKeywords() throws Exception { public void testHyperlinksCKeywords() throws Exception {
setUpEditor(C_FILE_NAME_1, C_CODE_1); setUpEditor(C_FILE_NAME_1, C_CODE_1);
@ -189,8 +183,7 @@ public class HyperlinkTest extends TestCase {
// no hyperlinks for numeric literals // no hyperlinks for numeric literals
assertNotHyperlink(C_CODE_1.indexOf("99") + 1); assertNotHyperlink(C_CODE_1.indexOf("99") + 1);
} }
public void testHyperlinksInactiveCode() throws Exception { public void testHyperlinksInactiveCode() throws Exception {
setUpEditor(C_FILE_NAME_2, C_CODE_2); setUpEditor(C_FILE_NAME_2, C_CODE_2);
@ -202,7 +195,4 @@ public class HyperlinkTest extends TestCase {
// assertNotHyperlink(C_CODE_2.indexOf("nothere") + 1); // assertNotHyperlink(C_CODE_2.indexOf("nothere") + 1);
assertHyperlink(C_CODE_2.indexOf("itworks") + 1, C_CODE_2.indexOf("itworks"), "itworks".length()); assertHyperlink(C_CODE_2.indexOf("itworks") + 1, C_CODE_2.indexOf("itworks"), "itworks".length());
} }
} }

View file

@ -7,6 +7,7 @@
* *
* Contributors: * Contributors:
* QNX Software Systems - Initial API and implementation * QNX Software Systems - Initial API and implementation
* Sergey Prigogin (Google)
*******************************************************************************/ *******************************************************************************/
package org.eclipse.cdt.internal.ui.text; package org.eclipse.cdt.internal.ui.text;
@ -25,25 +26,30 @@ public class CWordFinder {
private static final char CBRACE_L = '{'; private static final char CBRACE_L = '{';
private static final char CBRACE_R = '}'; private static final char CBRACE_R = '}';
private static final char BRACE_R = ')'; private static final char PAREN_R = ')';
/** /**
* This method determines for a given offset into a given document what the * This method determines for a given offset into a given document what the
* region is which defines the current word. A word is defined as the set of * region is which defines the current word. A word is defined as a contiguous
* non "C" identifiers. So assuming that ! indicated the current cursor * sequence of C-identifier characters. So assuming that | indicates the current
* postion: !afunction(int a, int b) --> word = length 0 afunc!tion(int a, * cursor position:
* int b) --> word = afunction afunction!(int a, int b) --> word = afunction * <pre>
* afunction(!int a, int b) --> word = length 0 afunction(int a,! int b) --> * |afunction(int a, int b) --> word = afunction
* word = length 0 afunction(!) --> word = length 0 * afunc|tion(int a, int b) --> word = afunction
* afunction|(int a, int b) --> word = afunction
* afunction(|int a, int b) --> word = int
* afunction(int a,| int b) --> word = length 0
* afunction(|) --> word = length 0
* </pre>
* *
* @param document * @param document
* The document to be examined * The document to be examined
* @param offset * @param offset
* The offset into the document where a word should be * The offset into the document where a word should be
* idendified. * identified.
* @return The region defining the current word, which may be a region of * @return The region defining the current word, which may be a region of
* length 0 if the offset is not in a word, or null if there is an * length 0 if the offset is not in a word, or null if there is an
* error accessing the docment data. * error accessing the document data.
*/ */
public static IRegion findWord(IDocument document, int offset) { public static IRegion findWord(IDocument document, int offset) {
int start = -2; int start = -2;
@ -53,11 +59,11 @@ public class CWordFinder {
int pos = offset; int pos = offset;
char c; char c;
while (pos >= 0) { while (--pos >= 0) {
c = document.getChar(pos); c = document.getChar(pos);
if (!Character.isJavaIdentifierPart(c)) if (!Character.isJavaIdentifierPart(c)) {
break; break;
--pos; }
} }
start = pos; start = pos;
@ -73,7 +79,6 @@ public class CWordFinder {
} }
end = pos; end = pos;
} catch (BadLocationException x) { } catch (BadLocationException x) {
} }
@ -97,10 +102,10 @@ public class CWordFinder {
* The document to be examined * The document to be examined
* @param offset * @param offset
* The offset into the document where a word should be * The offset into the document where a word should be
* idendified. * identified.
* @return The region defining the current word, which may be a region of * @return The region defining the current word, which may be a region of
* length 0 if the offset is not in a function, or null if there is * length 0 if the offset is not in a function, or null if there is
* an error accessing the docment data. * an error accessing the document data.
*/ */
public static IRegion findFunction(IDocument document, int offset) { public static IRegion findFunction(IDocument document, int offset) {
int leftbracket = -1; int leftbracket = -1;
@ -144,7 +149,7 @@ public class CWordFinder {
return new Region(offset, 0); return new Region(offset, 0);
} }
//Now backtrack our way from the rightbracket to the left // Now backtrack our way from the right bracket to the left
pos = rightbracket; pos = rightbracket;
rightbracketcount = leftbracketcount = 0; rightbracketcount = leftbracketcount = 0;
while (pos >= 0) { while (pos >= 0) {
@ -173,7 +178,7 @@ public class CWordFinder {
return new Region(offset, 0); return new Region(offset, 0);
} }
//Now work our way to the function name // Now work our way to the function name
pos = leftbracket - 1; pos = leftbracket - 1;
while (pos >= 0) { while (pos >= 0) {
c = document.getChar(pos); c = document.getChar(pos);
@ -222,7 +227,7 @@ public class CWordFinder {
int bracketcount = 0; int bracketcount = 0;
char c; char c;
//Find left curled bracket from our position // Find left curled brace from our position
while (pos > 0) { while (pos > 0) {
c = document.getChar(pos--); c = document.getChar(pos--);
@ -232,18 +237,19 @@ public class CWordFinder {
if (bracketcount-- == 0) { if (bracketcount-- == 0) {
do { do {
c = document.getChar(pos--); c = document.getChar(pos--);
if (c == BRACE_R) return false; if (c == PAREN_R)
return false;
} while (Character.isWhitespace(c)); } while (Character.isWhitespace(c));
// container block seems to be not a function or statement body // Container block seems to be not a function or statement body
pos++; // step back one symbol pos++; // step back one symbol
bracketcount = 0; // let's search for upper block bracketcount = 0; // let's search for upper block
} }
} }
} }
} catch (BadLocationException x) {
} catch (BadLocationException x) { /* Ignore */ } // Ignore
// return true in case of unknown result or exception }
return true; return true; // return true in case of unknown result or exception
} }
/** /**
@ -258,14 +264,16 @@ public class CWordFinder {
int counter = 0; int counter = 0;
char lf = 0; char lf = 0;
char c; char c;
for (int i=0; i<s.length(); i++) { for (int i= 0; i < s.length(); i++) {
c = s.charAt(i); c = s.charAt(i);
if (lf == 0) { if (lf == 0) {
if (c == '\n' || c == '\r') { if (c == '\n' || c == '\r') {
lf = c; lf = c;
counter++; counter++;
} }
} else if (lf == c) counter++; } else if (lf == c) {
counter++;
}
} }
return counter; return counter;
} }
@ -283,4 +291,3 @@ public class CWordFinder {
return false; return false;
} }
} }