Skip to content

Commit 75a00ca

Browse files
committed
Add fix to ensure procedure range is correct
Signed-off-by: worksofliam <[email protected]>
1 parent 9fd20ff commit 75a00ca

File tree

2 files changed

+39
-22
lines changed

2 files changed

+39
-22
lines changed

language/parser.ts

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,12 @@ export default class Parser {
203203
return objectName;
204204
};
205205

206+
// ========
207+
// Potential name logic is used for fixed-format only.
208+
// In fixed format, symbol names can be spread over multiple lines
209+
// and we use tokens to collect each part of the name.
210+
// ========
206211
let potentialName: Token[]|undefined;
207-
const getPotentialName = () => {
208-
if (potentialName && potentialName.length > 0) {
209-
return potentialName.map(p => p.value).join(``);
210-
} else {
211-
return undefined;
212-
}
213-
}
214212
const pushPotentialNameToken = (token?: Token) => {
215213
if (!potentialName) {
216214
potentialName = [];
@@ -221,7 +219,15 @@ export default class Parser {
221219
}
222220
const getPotentialNameToken = (): Token|undefined => {
223221
if (potentialName && potentialName.length > 0) {
224-
return potentialName[potentialName.length - 1];
222+
return {
223+
type: `block`,
224+
range: {
225+
line: potentialName[0].range.line,
226+
start: potentialName[0].range.start,
227+
end: potentialName[potentialName.length - 1].range.end
228+
},
229+
value: potentialName.map(p => p.value).join(``),
230+
};
225231
} else {
226232
return undefined;
227233
}
@@ -1558,7 +1564,7 @@ export default class Parser {
15581564
if (currentNameToken) {
15591565
currentItem = new Declaration(`procedure`);
15601566

1561-
currentProcName = getPotentialName();
1567+
currentProcName = currentNameToken.value;
15621568
currentItem.name = currentProcName;
15631569
currentItem.keyword = pSpec.keywords;
15641570

@@ -1609,10 +1615,12 @@ export default class Parser {
16091615

16101616
tokens = [dSpec.field, ...dSpec.keywordsRaw, dSpec.name];
16111617

1618+
const currentNameToken = getPotentialNameToken();
1619+
16121620
switch (dSpec.field && dSpec.field.value) {
16131621
case `C`:
16141622
currentItem = new Declaration(`constant`);
1615-
currentItem.name = getPotentialName() || NO_NAME;
1623+
currentItem.name = currentNameToken?.value || NO_NAME;
16161624
currentItem.keyword = dSpec.keywords || {};
16171625

16181626
// TODO: line number might be different with ...?
@@ -1626,7 +1634,7 @@ export default class Parser {
16261634
break;
16271635
case `S`:
16281636
currentItem = new Declaration(`variable`);
1629-
currentItem.name = getPotentialName() || NO_NAME;
1637+
currentItem.name = currentNameToken?.value || NO_NAME;
16301638
currentItem.keyword = {
16311639
...dSpec.keywords,
16321640
...prettyTypeFromToken(dSpec),
@@ -1635,7 +1643,7 @@ export default class Parser {
16351643
// TODO: line number might be different with ...?
16361644
currentItem.position = {
16371645
path: fileUri,
1638-
range: getPotentialNameToken().range
1646+
range: currentNameToken.range
16391647
};
16401648

16411649
scope.addSymbol(currentItem);
@@ -1644,20 +1652,20 @@ export default class Parser {
16441652

16451653
case `DS`:
16461654
currentItem = new Declaration(`struct`);
1647-
currentItem.name = getPotentialName() || NO_NAME;
1655+
currentItem.name = currentNameToken?.value || NO_NAME;
16481656
currentItem.keyword = dSpec.keywords;
16491657

16501658
currentItem.position = {
16511659
path: fileUri,
1652-
range: getPotentialNameToken()?.range || dSpec.field.range
1660+
range: currentNameToken?.range || dSpec.field.range
16531661
};
16541662

16551663
currentItem.range = {
16561664
start: currentItem.position.range.line,
16571665
end: currentItem.position.range.line
16581666
};
16591667

1660-
expandDs(fileUri, getPotentialNameToken(), currentItem);
1668+
expandDs(fileUri, currentNameToken, currentItem);
16611669

16621670
currentGroup = `structs`;
16631671
scope.addSymbol(currentItem);
@@ -1666,7 +1674,7 @@ export default class Parser {
16661674

16671675
case `PR`:
16681676
currentItem = new Declaration(`procedure`);
1669-
currentItem.name = getPotentialName() || NO_NAME;
1677+
currentItem.name = currentNameToken?.value || NO_NAME;
16701678
currentItem.keyword = {
16711679
...prettyTypeFromToken(dSpec),
16721680
...dSpec.keywords
@@ -1735,21 +1743,23 @@ export default class Parser {
17351743
});
17361744
}
17371745

1746+
const currentNameToken = getPotentialNameToken();
1747+
17381748
if (potentialName) {
17391749
currentSub = new Declaration(`subitem`);
1740-
currentSub.name = getPotentialName();
1750+
currentSub.name = currentNameToken?.value || NO_NAME;
17411751
currentSub.keyword = {
17421752
...prettyTypeFromToken(dSpec),
17431753
...dSpec.keywords
17441754
}
17451755

17461756
currentSub.position = {
17471757
path: fileUri,
1748-
range: getPotentialNameToken().range
1758+
range: currentNameToken?.range
17491759
};
17501760

17511761
// If the parameter has likeds, add the subitems to make it a struct.
1752-
await expandDs(fileUri, getPotentialNameToken(), currentSub);
1762+
await expandDs(fileUri, currentNameToken, currentSub);
17531763

17541764
currentItem.subItems.push(currentSub);
17551765
currentSub = undefined;

tests/suite/fixed.test.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1357,12 +1357,19 @@ test('multiline procedure names', async () => {
13571357
` PTestA B Export`,
13581358
` Ddnasdhhfhbd PI`,
13591359
` P E`,
1360-
].join(`\n`);
1360+
];
13611361

1362-
const cache = await parser.getDocs(uri, lines, { ignoreCache: true, withIncludes: true });
1362+
const cache = await parser.getDocs(uri, lines.join(`\n`), { ignoreCache: true, withIncludes: true });
13631363
expect(cache.procedures.length).to.equal(4);
13641364
expect(cache.procedures[0].name).to.equal(`abcTest`);
1365-
expect(cache.procedures[1].name).to.equal(`abcxyzTest`);
13661365
expect(cache.procedures[2].name).to.equal(`Test`);
13671366
expect(cache.procedures[3].name).to.equal(`TestA`);
1367+
1368+
const abcxyzTest = cache.find(`abcxyzTest`);
1369+
expect(abcxyzTest).toBeDefined();
1370+
expect(abcxyzTest.name).to.equal(`abcxyzTest`);
1371+
1372+
const procRange = abcxyzTest.range;
1373+
expect(lines[procRange.start]).to.equal(` Pabc...`);
1374+
expect(lines[procRange.end]).to.equal(` P E`);
13681375
});

0 commit comments

Comments
 (0)