📄 pyselection.java
字号:
c = ParsingUtils.charAt(doc, calltipOffset);
}
return calltipOffset;
}
return -1;
}
/**
* This function gets the activation token from the document given the current cursor position.
*
* @param document this is the document we want info on
* @param offset this is the cursor position
* @param getFullQualifier if true we get the full qualifier (even if it passes the current cursor location)
* @return a tuple with the activation token and the cursor offset (may change if we need to get the full qualifier,
* otherwise, it is the same offset passed as a parameter).
*/
public static Tuple<String, Integer> extractActivationToken(IDocument document, int offset, boolean getFullQualifier) {
try {
if(getFullQualifier){
//if we have to get the full qualifier, we'll have to walk the offset (cursor) forward
while(offset < document.getLength()){
char ch= document.getChar(offset);
if (Character.isJavaIdentifierPart(ch)){
offset++;
}else{
break;
}
}
}
int i= offset;
if (i > document.getLength())
return new Tuple<String, Integer>("", document.getLength()); //$NON-NLS-1$
while (i > 0) {
char ch= document.getChar(i - 1);
if (!Character.isJavaIdentifierPart(ch))
break;
i--;
}
return new Tuple<String, Integer>(document.get(i, offset - i), offset);
} catch (BadLocationException e) {
return new Tuple<String, Integer>("", offset); //$NON-NLS-1$
}
}
/**
* @param c
* @param string
*/
public static boolean containsOnly(char c, String string) {
for (int i = 0; i < string.length(); i++) {
if(string.charAt(i) != c){
return false;
}
}
return true;
}
/**
* @param string the string we care about
* @return true if the string passed is only composed of whitespaces (or characters that
* are regarded as whitespaces by Character.isWhitespace)
*/
public static boolean containsOnlyWhitespaces(String string) {
for (int i = 0; i < string.length(); i++) {
if(Character.isWhitespace(string.charAt(i)) == false){
return false;
}
}
return true;
}
/**
* @param selection the text from where we want to get the indentation
* @return a string representing the whitespaces and tabs befor the first char in the passed line.
*/
public static String getIndentationFromLine(String selection) {
int firstCharPosition = getFirstCharPosition(selection);
return selection.substring(0, firstCharPosition);
}
public String getIndentationFromLine() {
return getIndentationFromLine(getCursorLineContents());
}
/**
* @param src
* @return
*/
public static int getFirstCharPosition(String src) {
int i = 0;
boolean breaked = false;
while (i < src.length()) {
if (Character.isWhitespace(src.charAt(i)) == false && src.charAt(i) != '\t') {
i++;
breaked = true;
break;
}
i++;
}
if (!breaked){
i++;
}
return (i - 1);
}
/**
* @param doc
* @param region
* @return
* @throws BadLocationException
*/
public static int getFirstCharRelativePosition(IDocument doc, IRegion region) throws BadLocationException {
int offset = region.getOffset();
String src = doc.get(offset, region.getLength());
return getFirstCharPosition(src);
}
/**
* @param doc
* @param cursorOffset
* @return
* @throws BadLocationException
*/
public static int getFirstCharRelativeLinePosition(IDocument doc, int line) throws BadLocationException {
IRegion region;
region = doc.getLineInformation(line);
return getFirstCharRelativePosition(doc, region);
}
/**
* @param doc
* @param cursorOffset
* @return
* @throws BadLocationException
*/
public static int getFirstCharRelativePosition(IDocument doc, int cursorOffset) throws BadLocationException {
IRegion region;
region = doc.getLineInformationOfOffset(cursorOffset);
return getFirstCharRelativePosition(doc, region);
}
/**
* Returns the position of the first non whitespace char in the current line.
* @param doc
* @param cursorOffset
* @return position of the first character of the line (returned as an absolute
* offset)
* @throws BadLocationException
*/
public static int getFirstCharPosition(IDocument doc, int cursorOffset)
throws BadLocationException {
IRegion region;
region = doc.getLineInformationOfOffset(cursorOffset);
int offset = region.getOffset();
return offset + getFirstCharRelativePosition(doc, cursorOffset);
}
/**
* @return true if this line starts with a dedent token (the passed string should be already trimmed)
*/
public static boolean startsWithDedentToken(String trimmedLine) {
for (String dedent : PySelection.DEDENT_TOKENS) {
if(trimmedLine.startsWith(dedent)){
return isCompleteToken(trimmedLine, dedent);
}
}
return false;
}
private static boolean isCompleteToken(String trimmedLine, String dedent) {
if(dedent.length() < trimmedLine.length()){
char afterToken = trimmedLine.charAt(dedent.length());
if(afterToken == ' ' || afterToken == ':' || afterToken == ';' || afterToken == '('){
return true;
}
return false;
}else{
return true;
}
}
/**
* Class to help iterating through the document
*/
public static class DocIterator implements Iterator<String>{
private int startingLine;
private boolean forward;
private boolean isFirst = true;
private int numberOfLines;
private PySelection ps;
public DocIterator(boolean forward, PySelection ps){
this(forward, ps, ps.getCursorLine(), true);
}
public DocIterator(boolean forward, PySelection ps, int startingLine, boolean considerFirst){
this.startingLine = startingLine;
this.forward = forward;
numberOfLines = ps.getDoc().getNumberOfLines();
this.ps = ps;
if(!considerFirst){
isFirst = false;
}
}
public int getCurrentLine(){
return startingLine;
}
public boolean hasNext() {
if(forward){
return startingLine < numberOfLines;
}else{
return startingLine >= 0;
}
}
/**
* Note that the first thing it returns is the lineContents to cursor (and only after that
* does it return from the full line -- if it is iterating backwards).
*/
public String next() {
try {
String line;
if (forward) {
line = ps.getLine(startingLine);
startingLine++;
} else {
if (isFirst) {
line = ps.getLineContentsToCursor();
isFirst = false;
}else{
line = ps.getLine(startingLine);
}
startingLine--;
}
return line;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void remove() {
throw new RuntimeException("Remove not implemented.");
}
}
/**
* @return if the offset is inside the region
*/
public static boolean isInside(int offset, IRegion region) {
if(offset >= region.getOffset() && offset <= (region.getOffset() + region.getLength())){
return true;
}
return false;
}
/**
* @return if the col is inside the initial col/len
*/
public static boolean isInside(int col, int initialCol, int len) {
if(col >= initialCol && col <= (initialCol + len)){
return true;
}
return false;
}
/**
* @return if the region passed is composed of a single line
*/
public static boolean endsInSameLine(IDocument document, IRegion region) {
try {
int startLine = document.getLineOfOffset(region.getOffset());
int end = region.getOffset() + region.getLength();
int endLine = document.getLineOfOffset(end);
return startLine == endLine;
} catch (BadLocationException e) {
return false;
}
}
/**
* @param offset the offset we want info on
* @return a tuple with the line, col of the passed offset in the document
*/
public Tuple<Integer, Integer> getLineAndCol(int offset) {
try {
IRegion region = doc.getLineInformationOfOffset(offset);
int line = doc.getLineOfOffset(offset);
int col = offset - region.getOffset();
return new Tuple<Integer, Integer>(line, col);
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
/**
* @return the contents from the document starting at the cursor line until a colon is reached.
*/
public String getToColon() {
StringBuffer buffer = new StringBuffer();
for(int i = getLineOffset(); i < doc.getLength();i++){
try {
char c = doc.getChar(i);
buffer.append(c);
if(c == ':'){
return buffer.toString();
}
} catch (BadLocationException e) {
throw new RuntimeException(e);
}
}
return ""; //unable to find a colon
}
public boolean isInFunctionLine() {
return FunctionPattern.matcher(getToColon().trim()).matches();
}
public static boolean isIdentifier(String str) {
return IdentifierPattern.matcher(str).matches();
}
public boolean isInClassLine() {
return ClassPattern.matcher(getToColon().trim()).matches();
}
//spaces* 'def' space+ identifier space* ( (space|char|.|,|=|*|(|)|'|")* ):
private static final Pattern FunctionPattern = Pattern.compile("\\s*def\\s+\\w*.*");
//spaces* 'class' space+ identifier space* (? (.|char|space |,)* )?
private static final Pattern ClassPattern = Pattern.compile("\\s*class\\s+\\w*\\s*\\(?(\\s|\\w|\\.|\\,)*\\)?\\s*:");
private static final Pattern IdentifierPattern = Pattern.compile("\\w*");
public static boolean isCommentLine(String line) {
for(int j=0;j<line.length();j++){
char c = line.charAt(j);
if(c != ' '){
if(c=='#'){
//ok, it starts with # (so, it is a comment)
return true;
}
}
}
return false;
}
public static int DECLARATION_NONE = 0;
public static int DECLARATION_CLASS = 1;
public static int DECLARATION_METHOD = 2;
/**
* @return whether the current selection is on the ClassName or Function name context
* (just after the 'class' or 'def' tokens)
*/
public int isInDeclarationLine() {
try {
String contents = getLineContentsToCursor();
StringTokenizer strTok = new StringTokenizer(contents);
if(strTok.hasMoreTokens()){
String tok = strTok.nextToken();
int decl = DECLARATION_NONE;
if(tok.equals("class")){
decl = DECLARATION_CLASS;
} else if(tok.equals("def")){
decl = DECLARATION_METHOD;
}
if(decl != DECLARATION_NONE){
//ok, we're in a class or def line... so, if we find a '(' or ':', we're not in the declaration...
//(otherwise, we're in it)
while(strTok.hasMoreTokens()){
tok = strTok.nextToken();
if(tok.indexOf('(') != -1 || tok.indexOf(':') != -1){
return 0;
}
}
return decl;
}
}
} catch (BadLocationException e) {
}
return 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -