📄 pyselectiontest.java
字号:
/*
* Created on Apr 12, 2005
*
* @author Fabio Zadrozny
*/
package org.python.pydev.editor.actions;
import java.util.Arrays;
import java.util.List;
import junit.framework.TestCase;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.Document;
import org.eclipse.jface.text.TextSelection;
import org.python.pydev.core.Tuple;
import org.python.pydev.core.Tuple3;
import org.python.pydev.core.docutils.PyDocIterator;
import org.python.pydev.core.docutils.PySelection;
import org.python.pydev.core.docutils.StringUtils;
import org.python.pydev.parser.jython.ast.commentType;
/**
* @author Fabio Zadrozny
*/
public class PySelectionTest extends TestCase {
private PySelection ps;
private Document doc;
private String docContents;
public static void main(String[] args) {
try {
PySelectionTest test = new PySelectionTest();
test.setUp();
test.testIsInClassOrFunctionLine();
test.tearDown();
junit.textui.TestRunner.run(PySelectionTest.class);
} catch (Throwable e) {
e.printStackTrace();
}
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
super.setUp();
docContents = "" +
"TestLine1\n"+
"TestLine2#comm2\n"+
"TestLine3#comm3\n"+
"TestLine4#comm4\n";
doc = new Document(docContents);
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
public void testAddLine() {
ps = new PySelection(new Document("line1\nline2\n"), new TextSelection(doc, 0,0));
ps.addLine("foo", 0);
assertEquals("line1\nfoo\nline2\n", ps.getDoc().get());
ps = new PySelection(new Document("line1\n"), new TextSelection(doc, 0,0));
ps.addLine("foo", 0);
assertEquals("line1\nfoo\n", ps.getDoc().get());
ps = new PySelection(new Document("line1"), new TextSelection(doc, 0,0));
ps.addLine("foo", 0);
assertEquals("line1\nfoo\n", ps.getDoc().get().replace("\r\n", "\n"));
}
/**
* @throws BadLocationException
*
*/
public void testGeneral() throws BadLocationException {
ps = new PySelection(doc, new TextSelection(doc, 0,0));
assertEquals("TestLine1",ps.getCursorLineContents());
assertEquals("",ps.getLineContentsToCursor());
ps.selectCompleteLine();
assertEquals("TestLine1",ps.getCursorLineContents());
assertEquals("TestLine1",ps.getLine(0));
assertEquals("TestLine2#comm2",ps.getLine(1));
ps.deleteLine(0);
assertEquals("TestLine2#comm2",ps.getLine(0));
ps.addLine("TestLine1", 0);
}
public void testImportLine() {
String strDoc = "" +
"#coding \n"+
"''' this should be ignored\n"+
"from xxx import yyy \n"+
"import www''' \n"+
"#we want the import to appear after this line\n"+
"Class C: \n"+
" pass \n"+
"import kkk \n"+
"\n"+
"\n";
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(5, selection.getLineAvailableForImport());
}
public void testImportLine2() {
String strDoc = "" +
"#coding \n"+
"#we want the import to appear after this line\n"+
"Class C: \n"+
" pass \n"+
"import kkk \n"+
"\n"+
"\n";
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(2, selection.getLineAvailableForImport());
}
public void testImportLine3() {
String strDoc = "" +
"#coding \n"+
"#we want the import to appear after this line\n"+
"Class C: \n"+
" pass \n"+
"import kkk \n"+
" \n"+
"''' this should be ignored\n"+
"from xxx import yyy \n"+
"import www''' \n"+
"\n"+
"\n";
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(2, selection.getLineAvailableForImport());
}
public void testImportLine4() {
String strDoc = "" +
"class SomeClass( object ):\n"+
" '''This is the data that should be set...\n"+
" '''\n"+
"\n"+
"\n";
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(0, selection.getLineAvailableForImport());
}
public void testImportLine5() {
String strDoc = "" +
"'''This is the data that should be set...\n"+
"'''\n"+
"\n"+
"\n";
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(2, selection.getLineAvailableForImport());
}
public void testImportLine6() {
String strDoc = "" +
"\n"+
"\n" +
"from __future__ import xxx\n"+
"from a import xxx\n"+
"from __future__ import xxx\n" +
"#we want it to appear in this line\n";
//must be after the last from __future__ import statement
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(5, selection.getLineAvailableForImport());
}
public void testImportLine7() {
String strDoc = "" +
"'''comment block\n"+
"from false_import import *\n" +
"finish comment'''\n" +
"\n" +
"from __future__ import xxx\n"+
"from a import xxx\n"+
"from __future__ import xxx\n" +
"#we want it to appear in this line\n";
//must be after the last from __future__ import statement
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(7, selection.getLineAvailableForImport());
}
public void testImportLine8() {
String strDoc = "" +
"from a import ( #foo\n"+
"a,\n"+
"b, #bar\n"+
"c)\n"+
"#we want it to appear in this line\n";
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(4, selection.getLineAvailableForImport());
}
public void testImportLine9() {
String strDoc = "" +
"from a import \\\n"+
"a,\\\n"+
"b,\\\n"+
"c\n"+
"#we want it to appear in this line\n";
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(4, selection.getLineAvailableForImport());
}
public void testImportLine10() {
String strDoc = "" +
"from coilib40 import unittest\n"+
"from plugins10.plugins.editorsstack import (\n"+
" EditorsStackDock )\n"+
"#we want it to appear in this line\n"+
"def m1():\n"+
" testca\n"+
"\n";
Document document = new Document(strDoc);
PySelection selection = new PySelection(document);
assertEquals(3, selection.getLineAvailableForImport());
}
public void testSelectAll() {
ps = new PySelection(doc, new TextSelection(doc, 0,0));
ps.selectAll(true);
assertEquals(docContents, ps.getCursorLineContents()+"\n");
assertEquals(docContents, ps.getSelectedText());
ps = new PySelection(doc, new TextSelection(doc, 0,9)); //first line selected
ps.selectAll(true); //changes
assertEquals(docContents, ps.getCursorLineContents()+"\n");
assertEquals(docContents, ps.getSelectedText());
ps = new PySelection(doc, new TextSelection(doc, 0,9)); //first line selected
ps.selectAll(false); //nothing changes
assertEquals(ps.getLine(0), ps.getCursorLineContents());
assertEquals(ps.getLine(0), ps.getSelectedText());
}
public void testFullRep() throws Exception {
String s = "v=aa.bb.cc()";
doc = new Document(s);
ps = new PySelection(doc, new TextSelection(doc, 2,2));
assertEquals("aa.bb.cc", ps.getFullRepAfterSelection());
s = "v=aa.bb.cc";
doc = new Document(s);
ps = new PySelection(doc, new TextSelection(doc, 2,2));
assertEquals("aa.bb.cc", ps.getFullRepAfterSelection());
}
public void testReplaceToSelection() throws Exception {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -