@@ -16,34 +16,65 @@ package org.utplsql.sqldev.parser
16
16
17
17
import java.util.ArrayList
18
18
import java.util.regex.Pattern
19
- import org.utplsql.sqldev.model.parser.Unit
19
+ import javax.swing.text.JTextComponent
20
20
import org.utplsql.sqldev.model.parser.PlsqlObject
21
+ import org.utplsql.sqldev.model.parser.Unit
21
22
22
23
class UtplsqlParser {
23
24
private String plsql
25
+ private String plsqlWithoutComments
24
26
private ArrayList<PlsqlObject > objects = new ArrayList<PlsqlObject >
25
27
private ArrayList<Unit > units = new ArrayList<Unit >
26
28
27
29
new (String plsql) {
28
30
this . plsql = plsql
31
+ setPlsqlWithoutComments
29
32
populateObjects
30
33
populateUnits
31
34
}
32
35
33
- private def populateObjects () {
34
- val p = Pattern . compile(" (?i)(\\ s*)(create(\\ s+or\\ s+replace)?\\ s+(package|type)\\ s+(body\\ s+)?)(.*?)(\\ s+)" , Pattern . DOTALL )
36
+ /**
37
+ * replace multi-line and single-line PL/SQL comments with space
38
+ * to simplify and improve performance of subsequent regex expressions
39
+ */
40
+ private def setPlsqlWithoutComments () {
41
+ val sb = new StringBuffer
42
+ val p = Pattern . compile(" (/\\ *(.|[\\ r\\ n])*?\\ */)|(--.*\\ r?\\ n)" )
35
43
val m = p. matcher(plsql)
44
+ var pos = 0
45
+ while (m. find) {
46
+ if (pos < m. start) {
47
+ sb. append(plsql. substring(pos, m. start))
48
+ }
49
+ for (var i= m. start; i< m. end; i++ ) {
50
+ val c = plsql. substring(i, i+ 1 )
51
+ if (c == " \n " || c == " \r " ) {
52
+ sb. append(c)
53
+ } else {
54
+ sb. append(" " )
55
+ }
56
+ }
57
+ pos = m. end
58
+ }
59
+ if (plsql. length > pos) {
60
+ sb. append(plsql. substring(pos, plsql. length))
61
+ }
62
+ plsqlWithoutComments= sb. toString
63
+ }
64
+
65
+ private def populateObjects () {
66
+ val p = Pattern . compile(" (?i)(\\ s*)(create(\\ s+or\\ s+replace)?\\ s+(package|type)\\ s+(body\\ s+)?)(.+?)(\\ s+)" )
67
+ val m = p. matcher(plsqlWithoutComments)
36
68
while (m. find) {
37
69
val o = new PlsqlObject
38
70
o. name = m. group(6 )
39
71
o. position = m. start
40
72
objects. add(o)
41
73
}
42
74
}
43
-
44
75
private def populateUnits () {
45
- val p = Pattern . compile(" (?i)(\\ s*)(function|procedure)(\\ s+)(.* ?)(\\ s+)" , Pattern . DOTALL )
46
- val m = p. matcher(plsql )
76
+ val p = Pattern . compile(" (?i)(\\ s*)(function|procedure)(\\ s+)(.+ ?)(\\ s+)" )
77
+ val m = p. matcher(plsqlWithoutComments )
47
78
while (m. find) {
48
79
val u = new Unit
49
80
u. name = m. group(4 )
@@ -55,7 +86,7 @@ class UtplsqlParser {
55
86
private def getObjectNameAt (int position ) {
56
87
var name = " "
57
88
for (o : objects) {
58
- if (o. position < position) {
89
+ if (o. position <= position) {
59
90
name = o. name
60
91
}
61
92
}
@@ -84,17 +115,43 @@ class UtplsqlParser {
84
115
return units
85
116
}
86
117
87
- def getUtPlsqlCall (int position ) {
118
+ /**
119
+ * gets the utPLSQL path based on the current editor position
120
+ *
121
+ * @param position the absolute position as used in {@link JTextComponent#getCaretPosition()}
122
+ * @return the utPLSQL path
123
+ */
124
+ def getPathAt (int position ) {
88
125
var objectName = getObjectNameAt(position)
89
126
if (! objectName. empty) {
90
127
var unitName = getUnitNameAt(position)
91
128
if (unitName. empty) {
92
- return ' ' ' ut.run( ' « objectName. removeQuotes» ' ); ' ' '
129
+ return objectName. removeQuotes
93
130
} else {
94
- return ' ' ' ut.run( ' «objectName. removeQuotes». «unitName. removeQuotes»' ); ' ' '
131
+ return ' ' ' «objectName.removeQuotes».«unitName.removeQuotes»' ' '
95
132
}
96
133
}
97
134
return " "
98
135
}
136
+
F438
td>
137
+ /**
138
+ * gets the utPLSQL path based on the current editor position
139
+ *
140
+ * @param line the line as used in SQL Developer, starting with 1
141
+ * @param column the column as used in SQL Developer, starting with 1
142
+ * @return the utPLSQL path
143
+ */
144
+ def getPathAt (int line , int column ) {
145
+ var lines= 0
146
+ for (var i= 0 ; i< plsql. length; i++ ) {
147
+ if (plsql. substring(i,i+ 1 ) == " \n " ) {
148
+ lines++
149
+ if (lines == line - 1 ) {
150
+ return getPathAt(i + column)
151
+ }
152
+ }
153
+ }
154
+ throw new RuntimeException (' ' ' Line «line» not found.' ' ' )
155
+ }
99
156
100
157
}
0 commit comments