Skip to content

Commit 3bbc226

Browse files
Extend parser to support starting line of a package procedure
Works in package specifications and package bodies. But only for procedures. If a unit is not found 1 is returned.
1 parent 77a84ba commit 3bbc226

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

sqldev/src/main/java/org/utplsql/sqldev/model/parser/Unit.xtend

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ import org.utplsql.sqldev.model.AbstractModel
2222
class Unit extends AbstractModel {
2323
String name
2424
Integer position
25+
Integer positionOfName
2526
}

sqldev/src/main/java/org/utplsql/sqldev/parser/UtplsqlParser.xtend

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class UtplsqlParser {
110110
val u = new Unit
111111
u.name = m.group(4)
112112
u.position = m.start
113+
u.positionOfName = m.start(4)
113114
units.add(u)
114115
}
115116
}
@@ -221,5 +222,32 @@ class UtplsqlParser {
221222
}
222223
return ""
223224
}
224-
225+
226+
private def getStartLine(int position) {
227+
var int line = 1
228+
for (var i = 0; i < plsql.length; i++) {
229+
val c = plsql.substring(i, i+1)
230+
if (i > position) {
231+
return line
232+
} else if (c == '\n') {
233+
line = line + 1
234+
}
235+
}
236+
return line
237+
}
238+
239+
/**
240+
* get the line of a PL/SQL package unit
241+
*
242+
* @param unitName name of the unit. Only procedures are supported
243+
* @return the line where the procedure is defined
244+
*/
245+
def getLineOf(String unitName) {
246+
for (u : units) {
247+
if (u.name.equalsIgnoreCase(unitName)) {
248+
return u.positionOfName.startLine
249+
}
250+
}
251+
return 1
252+
}
225253
}

sqldev/src/test/java/org/utplsql/sqldev/test/parser/UtplsqlParserTest.xtend

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,90 @@ class UtplsqlParserTest extends AbstractJdbcTest {
208208
val parser = new UtplsqlParser(plsql)
209209
Assert.assertEquals(null, parser.getObjectAt(0))
210210
}
211+
212+
@Test
213+
def void StartLineSpec() {
214+
val plsql = '''
215+
CREATE OR REPLACE PACKAGE junit_utplsql_test1_pkg is
216+
--%suite(JUnit testing)
217+
--%suitepath(a)
218+
219+
--%context(test context)
220+
221+
--%test(test 1 - OK)
222+
PRoCeDURE test_1_ok;
223+
224+
--%test(test 2 - NOK)
225+
PROCEDURE test_2_nok;
226+
227+
--%test(test 3 - disabled)
228+
--%disabled
229+
PROCEDURE test_3_disabled;
230+
231+
--%test(test 4 - errored)
232+
PROCEDURE test_4_errored;
233+
234+
--%test(test 5 - warnings)
235+
PROCEDURE test_5_warnings;
236+
--%endcontext
237+
238+
function my_Func (p IN number) RETURN BOOLEAN;
239+
END;
240+
'''
241+
val parser = new UtplsqlParser(plsql)
242+
val first = parser.getLineOf('test_1_ok')
243+
Assert.assertEquals(8, first)
244+
val last = parser.getLineOf('test_5_warnings')
245+
Assert.assertEquals(21, last)
246+
}
247+
248+
@Test
249+
def void StartLineBody() {
250+
val plsql = '''
251+
CREATE OR REPLACE PACKAGE BODY junit_utplsql_test1_pkg IS
252+
PROCEDURE test_1_ok IS
253+
BEGIN
254+
dbms_output.put_line('start test 1');
255+
dbms_session.sleep(1);
256+
ut.expect(1).to_equal(1);
257+
dbms_output.put_line('end test 1');
258+
END;
259+
260+
PROCEDURE test_2_nok IS
261+
BEGIN
262+
dbms_output.put_line('start test 2');
263+
dbms_session.sleep(2);
264+
ut.expect(1, 'first assert.').to_equal(2);
265+
ut.expect(1, 'second assert.').to_equal(2);
266+
dbms_output.put_line('end test 2');
267+
END;
268+
269+
PROCEDURE test_3_disabled IS
270+
BEGIN
271+
NULL;
272+
END;
273+
274+
PROCEDURE test_4_errored IS
275+
BEGIN
276+
EXECUTE IMMEDIATE 'bla bla';
277+
END;
278+
279+
PROCEDURE test_5_warnings IS
280+
BEGIN
281+
COMMIT; -- will raise a warning
282+
ut.expect(1).to_equal(1);
283+
END;
284+
285+
FUNCTION my_Func (p IN number) RETURN BOOLEAN IS
286+
RETURN TRUE;
287+
END;
288+
END;
289+
'''
290+
val parser = new UtplsqlParser(plsql)
291+
val first = parser.getLineOf('test_1_ok')
292+
Assert.assertEquals(2, first)
293+
val last = parser.getLineOf('test_5_warnings')
294+
Assert.assertEquals(29, last)
295+
}
296+
211297
}

0 commit comments

Comments
 (0)