1 19 20 package ca.mcgill.sable.soot.editors; 21 22 import org.eclipse.jface.text.*; 23 24 25 26 public class JimpleDoubleClickStrategy implements ITextDoubleClickStrategy { 27 protected ITextViewer fText; 28 29 public JimpleDoubleClickStrategy() { 30 super(); 31 } 32 public void doubleClicked(ITextViewer part) { 33 int pos = part.getSelectedRange().x; 34 35 if (pos < 0) 36 return; 37 38 fText = part; 39 40 if (!selectComment(pos)) { 41 selectWord(pos); 42 } 43 } 44 protected boolean selectComment(int caretPos) { 45 IDocument doc = fText.getDocument(); 46 int startPos, endPos; 47 48 try { 49 int pos = caretPos; 50 char c = ' '; 51 52 while (pos >= 0) { 53 c = doc.getChar(pos); 54 if (c == '\\') { 55 pos -= 2; 56 continue; 57 } 58 if (c == Character.LINE_SEPARATOR || c == '\"') 59 break; 60 --pos; 61 } 62 63 if (c != '\"') 64 return false; 65 66 startPos = pos; 67 68 pos = caretPos; 69 int length = doc.getLength(); 70 c = ' '; 71 72 while (pos < length) { 73 c = doc.getChar(pos); 74 if (c == Character.LINE_SEPARATOR || c == '\"') 75 break; 76 ++pos; 77 } 78 if (c != '\"') 79 return false; 80 81 endPos = pos; 82 83 int offset = startPos +1; 84 int len = endPos - offset; 85 fText.setSelectedRange(offset, len); 86 return true; 87 } catch (BadLocationException x) { 88 } 89 90 return false; 91 } 92 protected boolean selectWord(int caretPos) { 93 94 IDocument doc = fText.getDocument(); 95 int startPos, endPos; 96 97 try { 98 99 int pos = caretPos; 100 char c; 101 102 while (pos >= 0) { 103 c = doc.getChar(pos); 104 if (!Character.isJavaIdentifierPart(c)) 105 break; 106 --pos; 107 } 108 109 startPos = pos; 110 111 pos = caretPos; 112 int length = doc.getLength(); 113 114 while (pos < length) { 115 c = doc.getChar(pos); 116 if (!Character.isJavaIdentifierPart(c)) 117 break; 118 ++pos; 119 } 120 121 endPos = pos; 122 selectRange(startPos, endPos); 123 return true; 124 125 } catch (BadLocationException x) { 126 } 127 128 return false; 129 } 130 131 private void selectRange(int startPos, int stopPos) { 132 int offset = startPos+1; 133 int length = stopPos - offset; 134 fText.setSelectedRange(offset, length); 135 } 136 } 137 | Popular Tags |