@@ -16,34 +16,65 @@ package org.utplsql.sqldev.parser
1616
1717import java.util.ArrayList
1818import java.util.regex.Pattern
19- import org.utplsql.sqldev.model.parser.Unit
19+ import javax.swing.text.JTextComponent
2020import org.utplsql.sqldev.model.parser.PlsqlObject
21+ import org.utplsql.sqldev.model.parser.Unit
2122
2223class UtplsqlParser {
2324 private String plsql
25+ private String plsqlWithoutComments
2426 private ArrayList<PlsqlObject > objects = new ArrayList<PlsqlObject >
2527 private ArrayList<Unit > units = new ArrayList<Unit >
2628
2729 new (String plsql) {
2830 this . plsql = plsql
31+ setPlsqlWithoutComments
2932 populateObjects
3033 populateUnits
3134 }
3235
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)" )
3543 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)
3668 while (m. find) {
3769 val o = new PlsqlObject
3870 o. name = m. group(6 )
3971 o. position = m. start
4072 objects. add(o)
4173 }
4274 }
43-
4475 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 )
4778 while (m. find) {
4879 val u = new Unit
4980 u. name = m. group(4 )
@@ -55,7 +86,7 @@ class UtplsqlParser {
5586 private def getObjectNameAt (int position ) {
5687 var name = " "
5788 for (o : objects) {
58- if (o. position < position) {
89+ if (o. position <= position) {
5990 name = o. name
6091 }
6192 }
@@ -84,17 +115,43 @@ class UtplsqlParser {
84115 return units
85116 }
86117
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 ) {
88125 var objectName = getObjectNameAt(position)
89126 if (! objectName. empty) {
90127 var unitName = getUnitNameAt(position)
91128 if (unitName. empty) {
92- return ' ' ' ut.run( ' « objectName. removeQuotes» ' ); ' ' '
129+ return objectName. removeQuotes
93130 } else {
94- return ' ' ' ut.run( ' «objectName. removeQuotes». «unitName. removeQuotes»' ); ' ' '
131+ return ' ' ' «objectName.removeQuotes».«unitName.removeQuotes»' ' '
95132 }
96133 }
97134 return " "
98135 }
136+
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+ }
99156
100157}
0 commit comments