initial import
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / editors / PHPDoubleClickStrategy.java
1 package net.sourceforge.phpeclipse.editors;
2
3 import org.eclipse.jface.text.*;
4
5 public class PHPDoubleClickStrategy implements ITextDoubleClickStrategy {
6         protected ITextViewer fText;
7
8         public PHPDoubleClickStrategy() {
9                 super();
10         }
11         public void doubleClicked(ITextViewer part) {
12                 int pos = part.getSelectedRange().x;
13
14                 if (pos < 0)
15                         return;
16
17                 fText = part;
18
19                 if (!selectComment(pos)) {
20                         selectWord(pos);
21                 }
22         }
23         protected boolean selectComment(int caretPos) {
24                 IDocument doc = fText.getDocument();
25                 int startPos, endPos;
26
27                 try {
28                         int pos = caretPos;
29                         char c = ' ';
30
31                         while (pos >= 0) {
32                                 c = doc.getChar(pos);
33                                 if (c == '\\') {
34                                         pos -= 2;
35                                         continue;
36                                 }
37                                 if (c == Character.LINE_SEPARATOR || c == '\"')
38                                         break;
39                                 --pos;
40                         }
41
42                         if (c != '\"')
43                                 return false;
44
45                         startPos = pos;
46
47                         pos = caretPos;
48                         int length = doc.getLength();
49                         c = ' ';
50
51                         while (pos < length) {
52                                 c = doc.getChar(pos);
53                                 if (c == Character.LINE_SEPARATOR || c == '\"')
54                                         break;
55                                 ++pos;
56                         }
57                         if (c != '\"')
58                                 return false;
59
60                         endPos = pos;
61
62                         int offset = startPos + 1;
63                         int len = endPos - offset;
64                         fText.setSelectedRange(offset, len);
65                         return true;
66                 } catch (BadLocationException x) {
67                 }
68
69                 return false;
70         }
71         protected boolean selectWord(int caretPos) {
72
73                 IDocument doc = fText.getDocument();
74                 int startPos, endPos;
75
76                 try {
77
78                         int pos = caretPos;
79                         char c;
80
81                         while (pos >= 0) {
82                                 c = doc.getChar(pos);
83                                 if (!Character.isJavaIdentifierPart(c))
84                                         break;
85                                 --pos;
86                         }
87
88                         startPos = pos;
89
90                         pos = caretPos;
91                         int length = doc.getLength();
92
93                         while (pos < length) {
94                                 c = doc.getChar(pos);
95                                 if (!Character.isJavaIdentifierPart(c))
96                                         break;
97                                 ++pos;
98                         }
99
100                         endPos = pos;
101                         selectRange(startPos, endPos);
102                         return true;
103
104                 } catch (BadLocationException x) {
105                 }
106
107                 return false;
108         }
109
110         private void selectRange(int startPos, int stopPos) {
111                 int offset = startPos + 1;
112                 int length = stopPos - offset;
113                 fText.setSelectedRange(offset, length);
114         }
115 }