Search in sources :

Example 51 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class FindReplaceDocumentAdapterTest method testReplace.

@Test
public void testReplace() {
    FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument);
    try {
        // $NON-NLS-1$
        findReplaceDocumentAdapter.find(0, "public", true, true, false, false);
        // $NON-NLS-1$
        IRegion r = findReplaceDocumentAdapter.replace("private", false);
        assertNotNull(r);
        // $NON-NLS-1$
        findReplaceDocumentAdapter.find(0, "public", true, true, false, false);
        // $NON-NLS-1$
        r = findReplaceDocumentAdapter.replace("private", false);
        assertNotNull(r);
        // $NON-NLS-1$
        findReplaceDocumentAdapter.find(0, "public", true, true, false, false);
        // $NON-NLS-1$
        r = findReplaceDocumentAdapter.replace("private", false);
        assertNotNull(r);
        // Search again: there will be no match
        // $NON-NLS-1$
        findReplaceDocumentAdapter.find(0, "public", true, true, false, false);
        try {
            // $NON-NLS-1$
            findReplaceDocumentAdapter.replace("private", false);
        } catch (IllegalStateException e) {
            assertTrue(true);
        }
        String text = // $NON-NLS-1$
        "package TestPackage;\n" + // $NON-NLS-1$
        "/*\n" + // $NON-NLS-1$
        "* comment\n" + // $NON-NLS-1$
        "*/\n" + // $NON-NLS-1$
        "	private class Class {\n" + // $NON-NLS-1$
        "		// comment1\n" + // $NON-NLS-1$
        "		private void method1() {\n" + // $NON-NLS-1$
        "		}\n" + // $NON-NLS-1$
        "		// comment2\n" + // $NON-NLS-1$
        "		private void method2() {\n" + // $NON-NLS-1$
        "		}\n" + // $NON-NLS-1$
        "	}\n" + // $NON-NLS-1$S
        "// Gel\u00f6st";
        assertEquals(text, fDocument.get());
    } catch (BadLocationException e) {
        Assert.assertTrue(false);
    }
}
Also used : FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Example 52 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class FindReplaceDocumentAdapterTest method testRegexFindLinebreakIllegal.

@Test
public void testRegexFindLinebreakIllegal() throws Exception {
    FindReplaceDocumentAdapter adapter = new FindReplaceDocumentAdapter(fDocument);
    fDocument.set("\n");
    IRegion region = null;
    try {
        region = adapter.find(0, "[\\R]", true, false, false, true);
    } catch (PatternSyntaxException e) {
    // expected
    }
    assertNull(region);
    try {
        region = adapter.find(0, "[\\s&&[^\\R]]", true, false, false, true);
    } catch (PatternSyntaxException e) {
    // expected
    }
    assertNull(region);
    try {
        region = adapter.find(0, "\\p{\\R}", true, false, false, true);
    } catch (PatternSyntaxException e) {
    // expected
    }
    assertNull(region);
}
Also used : FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) PatternSyntaxException(java.util.regex.PatternSyntaxException) Test(org.junit.Test)

Example 53 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class FindReplaceDocumentAdapterTest method testUTF8Pattern.

@Test
public void testUTF8Pattern() {
    FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument);
    try {
        IRegion result = new Region(153, 6);
        // Find case-sensitive
        // $NON-NLS-1$
        IRegion r = findReplaceDocumentAdapter.find(0, "Gel\u00F6st", true, true, false, false);
        assertEquals(result, r);
        // $NON-NLS-1$
        r = findReplaceDocumentAdapter.find(0, "Gel\u00F6st", true, true, false, false);
        assertEquals(result, r);
        // Find non-case-sensitive
        // $NON-NLS-1$
        r = findReplaceDocumentAdapter.find(0, "Gel\u00D6st", true, false, false, false);
        assertEquals(result, r);
        // $NON-NLS-1$
        r = findReplaceDocumentAdapter.find(0, "Gel\u00D6st", true, false, false, false);
        assertEquals(result, r);
    } catch (BadLocationException e) {
        Assert.assertTrue(false);
    }
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Example 54 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class FindReplaceDocumentAdapterTest method testBug74993.

/**
 * Test case for: https://bugs.eclipse.org/bugs/show_bug.cgi?id=74993
 */
@Test
public void testBug74993() {
    FindReplaceDocumentAdapter findReplaceDocumentAdapter = new FindReplaceDocumentAdapter(fDocument);
    try {
        // $NON-NLS-1$
        IRegion r = findReplaceDocumentAdapter.find(12, "\\w+", false, false, false, true);
        assertEquals(new Region(6, 1), r);
    } catch (BadLocationException e) {
        Assert.assertTrue(false);
    }
}
Also used : Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Example 55 with IRegion

use of org.eclipse.jface.text.IRegion in project eclipse.platform.text by eclipse.

the class LineTrackerTest3 method testFunnyLastLineCompatibility.

@Test
public void testFunnyLastLineCompatibility() throws Exception {
    /* empty last line */
    set("x\n");
    int[] offsets = { 0, 2 };
    int[] lengths = { 1, 0 };
    assertEquals("invalid number of lines, ", lengths.length, fTracker.getNumberOfLines());
    assertEquals("invalid number of lines, ", lengths.length, fTracker.getNumberOfLines(0, fText.getLength()));
    for (int i = 0; i < lengths.length; i++) {
        IRegion line = fTracker.getLineInformation(i);
        assertEquals("line: " + i, lengths[i], line.getLength());
        assertEquals("line: " + i, offsets[i], line.getOffset());
    }
    try {
        fTracker.getLineInformation(lengths.length);
        fail();
    } catch (Exception e) {
    }
    try {
        fTracker.getLineInformationOfOffset(offsets[offsets.length] + 1);
        fail();
    } catch (Exception e) {
    }
    /* phantom last line when the last line is not empty */
    set("x\nx");
    offsets = new int[] { 0, 2, 3 };
    lengths = new int[] { 1, 1, 0 };
    assertEquals("invalid number of lines, ", lengths.length - 1, /* !!!! */
    fTracker.getNumberOfLines());
    assertEquals("invalid number of lines, ", lengths.length - 1, /* !!!! */
    fTracker.getNumberOfLines(0, fText.getLength()));
    for (int i = 0; i < lengths.length; i++) {
        IRegion line = fTracker.getLineInformation(i);
        int len = lengths[i];
        int offset = offsets[i];
        assertEquals("length of line: " + i, len, line.getLength());
        assertEquals("offset of line: " + i, offset, line.getOffset());
        line = fTracker.getLineInformationOfOffset(offset);
        if (i == lengths.length - 1) {
            // phantom line cannot be queried by offset
            len = lengths[i - 1];
            offset = offsets[i - 1];
        }
        assertEquals("length of line: " + i, len, line.getLength());
        assertEquals("offset of line: " + i, offset, line.getOffset());
    }
    try {
        fTracker.getLineInformation(lengths.length);
        fail();
    } catch (Exception e) {
    }
    try {
        fTracker.getLineInformationOfOffset(offsets[offsets.length] + 1);
        fail();
    } catch (Exception e) {
    }
}
Also used : IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) Test(org.junit.Test)

Aggregations

IRegion (org.eclipse.jface.text.IRegion)341 BadLocationException (org.eclipse.jface.text.BadLocationException)158 Region (org.eclipse.jface.text.Region)151 Test (org.junit.Test)94 IDocument (org.eclipse.jface.text.IDocument)68 Point (org.eclipse.swt.graphics.Point)49 Position (org.eclipse.jface.text.Position)38 ITextViewerExtension5 (org.eclipse.jface.text.ITextViewerExtension5)20 ITypedRegion (org.eclipse.jface.text.ITypedRegion)20 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)18 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)17 IEditorPart (org.eclipse.ui.IEditorPart)13 ArrayList (java.util.ArrayList)11 Document (org.eclipse.jface.text.Document)11 StyledText (org.eclipse.swt.custom.StyledText)10 ITextSelection (org.eclipse.jface.text.ITextSelection)9 IFile (org.eclipse.core.resources.IFile)8 Annotation (org.eclipse.jface.text.source.Annotation)7 HashMap (java.util.HashMap)6 CoreException (org.eclipse.core.runtime.CoreException)6