Skip to content

Commit c0f66e8

Browse files
authored
Merge pull request #457 from codefori/worksofliam/issue442
Fix multiline statement range handling
2 parents 8940814 + 3c6a312 commit c0f66e8

File tree

2 files changed

+42
-11
lines changed

2 files changed

+42
-11
lines changed

language/parser.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ export default class Parser {
155155
if (withBlocks) {
156156
tokens = createBlocks(tokens);
157157
}
158-
158+
159+
// Remove newlines
160+
if (tokens.some(t => t.type === `newline`)) {
161+
tokens = tokens.filter(t => t.type !== `newline`);
162+
}
163+
159164
return tokens;
160165
}
161166

@@ -388,7 +393,6 @@ export default class Parser {
388393
//Now the real work
389394
const parseContent = async (fileUri: string, allContent: string) => {
390395
const EOL = allContent.includes(`\r\n`) ? `\r\n` : `\n`;
391-
const LINEEND = ``.padEnd(EOL.length);
392396
let lines = allContent.split(EOL);
393397

394398
let postProcessingStatements: {[procedure: string]: Token[][]} = {'GLOBAL': []};
@@ -664,7 +668,7 @@ export default class Parser {
664668
if ([spec, comment].includes(`*`)) {
665669
if (currentStmtStart && currentStmtStart.content) {
666670
// Since we're in an extended statement (usually fixed exec), we still need to collect the lengths for the tokeniser
667-
currentStmtStart.content += ``.padEnd(baseLine.length) + LINEEND;
671+
currentStmtStart.content += ``.padEnd(baseLine.length) + EOL;
668672
}
669673

670674
continue;
@@ -677,7 +681,7 @@ export default class Parser {
677681
} else if (comment === `+` && fixedExec && currentStmtStart.content) {
678682
// Fixed format EXEC SQL
679683
baseLine = ``.padEnd(7) + baseLine.substring(7);
680-
currentStmtStart.content += baseLine + LINEEND;
684+
currentStmtStart.content += baseLine + EOL;
681685
continue;
682686
} else {
683687
if (spec === ` `) {
@@ -714,7 +718,7 @@ export default class Parser {
714718
// if part of a continued statement to ensure positions are correct.
715719
// See issue_358_no_reference_2
716720
if (currentStmtStart && currentStmtStart.content) {
717-
currentStmtStart.content += ``.padEnd(baseLine.length) + LINEEND;
721+
currentStmtStart.content += ``.padEnd(baseLine.length) + EOL;
718722
}
719723
continue;
720724
};
@@ -736,7 +740,7 @@ export default class Parser {
736740
currentStmtStart = {
737741
line: lineNumber,
738742
index: lineIndex,
739-
content: baseLine + LINEEND
743+
content: baseLine + EOL
740744
}
741745
continue;
742746
case '/END':
@@ -747,15 +751,15 @@ export default class Parser {
747751
case '/':
748752
// Comments in SQL, usually free-format
749753
if (parts[1] === `*` && currentStmtStart) {
750-
currentStmtStart.content += ``.padEnd(baseLine.length) + LINEEND;
754+
currentStmtStart.content += ``.padEnd(baseLine.length) + EOL;
751755
continue;
752756
}
753757
break;
754758
default:
755759
// Maybe we're in a fixed exec statement, but a directive is being used.
756760
// See test case references_21_fixed_exec1
757761
if (fixedExec && currentStmtStart && currentStmtStart.content) {
758-
currentStmtStart.content += ``.padEnd(baseLine.length) + LINEEND;
762+
currentStmtStart.content += ``.padEnd(baseLine.length) + EOL;
759763
continue;
760764
}
761765
break;
@@ -853,7 +857,7 @@ export default class Parser {
853857
// This happens when we put a comment on a line which is part of one long statement.
854858
// See references_24_comment_in_statement
855859
if (currentStmtStart.content) {
856-
currentStmtStart.content += ``.padEnd(baseLine.length) + LINEEND;
860+
currentStmtStart.content += ``.padEnd(baseLine.length) + EOL;
857861
}
858862
} else {
859863
if (stripComment(line).endsWith(`;`)) {
@@ -875,7 +879,7 @@ export default class Parser {
875879
if (currentStmtStart.content.endsWith(`-`))
876880
currentStmtStart.content = currentStmtStart.content.substring(0, currentStmtStart.content.length - 1) + ` `;
877881

878-
currentStmtStart.content += LINEEND;
882+
currentStmtStart.content += EOL;
879883

880884
continue;
881885
}
@@ -1025,7 +1029,7 @@ export default class Parser {
10251029
lastItem.subItems.push(currentItem);
10261030
} else {
10271031
// Otherwise, we push as a new item
1028-
currentItem.range.end = currentStmtStart.line;
1032+
currentItem.range.end = tokens[tokens.length-1].range.line;
10291033
scope.addSymbol(currentItem);
10301034
}
10311035
} else {

tests/suite/basics.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1980,4 +1980,31 @@ test('**free after first line (#451)', async () => {
19801980
expect(cache).toBeDefined();
19811981

19821982
expect(cache.find(`txtMsg`)).toBeDefined();
1983+
});
1984+
1985+
test('multi-line definition (#442)', async () => {
1986+
const lines = [
1987+
`**free`,
1988+
``,
1989+
`dcl-s myprogramText char(20);`,
1990+
``,
1991+
`dcl-ds name qualified dim;`,
1992+
`end-ds;`,
1993+
``,
1994+
`dcl-ds person qualified dim`,
1995+
`end-ds;`,
1996+
].join(`\n`);
1997+
1998+
const cache = await parser.getDocs(uri, lines, { ignoreCache: true, withIncludes: false, collectReferences: true });
1999+
2000+
expect(cache).toBeDefined();
2001+
2002+
const name = cache.find(`name`);
2003+
expect(name).toBeDefined();
2004+
expect(name.range).toMatchObject({ start: 4, end: 5 });
2005+
2006+
const person = cache.find(`person`);
2007+
expect(person).toBeDefined();
2008+
console.log(person);
2009+
expect(person.range).toMatchObject({ start: 7, end: 8 });
19832010
});

0 commit comments

Comments
 (0)