Skip to content

Commit

Permalink
Improvements to include handling
Browse files Browse the repository at this point in the history
Signed-off-by: Liam Barry Allan <[email protected]>
  • Loading branch information
worksofliam committed Aug 3, 2022
1 parent d0bfa2d commit a8dd969
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
35 changes: 31 additions & 4 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,18 @@ module.exports = class Parser {
/**
* @param {vscode.Uri} workingUri Path being worked with
* @param {string} getPath IFS or member path to fetch
* @returns {Promise<string[]>}
* @returns {Promise<{lines: string[], found: boolean, uri: vscode.Uri, path: string, type: "member"|"streamfile"|"file"}>}
* @deprecated Use `Parser.getContent` instead.
*/
async getContent(workingUri, getPath) {
//const hrstart = process.hrtime();

let content;
let lines = undefined;

let uri;
let found = true;
let attemptedPath;

let {type, memberPath, finishedPath} = Generic.getPathInfo(workingUri, getPath);

Expand All @@ -136,6 +141,7 @@ module.exports = class Parser {
let eol;
switch (type) {
case `member`:
attemptedPath = finishedPath;

// We have to be agnostic for the type. First we check if we opened a copybook before.
const openDoc = vscode.workspace.textDocuments.find(doc =>
Expand All @@ -149,22 +155,26 @@ module.exports = class Parser {
// Otherwise, we go and fetch the correct content
doc = await vscode.workspace.openTextDocument(vscode.Uri.from({
scheme: type,
path: finishedPath + `.MBR`
path: finishedPath + `.RPGLE`
}));
}

eol = doc.eol === vscode.EndOfLine.CRLF ? `\r\n` : `\n`;

uri = doc.uri;
lines = doc.getText().split(eol);
break;

case `streamfile`:
attemptedPath = finishedPath;

doc = await vscode.workspace.openTextDocument(vscode.Uri.from({
scheme: type,
path: finishedPath
}));
eol = doc.eol === vscode.EndOfLine.CRLF ? `\r\n` : `\n`;

uri = doc.uri;
lines = doc.getText().split(eol);
break;

Expand All @@ -184,6 +194,8 @@ module.exports = class Parser {
getPath = memberParts.join(path.sep) + `*`
}

attemptedPath = getPath;

/** @type {vscode.Uri} */
let possibleFile;

Expand All @@ -200,20 +212,29 @@ module.exports = class Parser {

if (possibleFile) {
content = (await vscode.workspace.fs.readFile(possibleFile)).toString();
uri = possibleFile;
lines = content.replace(new RegExp(`\\\r`, `g`), ``).split(`\n`);
} else {
lines = [];
found = false;
}
break;
}
} catch (e) {
lines = [];
found = false;
}

//const hrend = process.hrtime(hrstart);
//console.info(`getContent() took ${hrend[0]}s and ${hrend[1] / 1000000}ms: ${getPath} (${lines.length})`);

return lines;
return {
lines,
path: attemptedPath,
uri,
found,
type
};
}

/**
Expand Down Expand Up @@ -362,9 +383,15 @@ module.exports = class Parser {

pieces = line.split(` `).filter(piece => piece !== ``);

this.brokenPaths = [];

if ([`/COPY`, `/INCLUDE`].includes(pieces[0].toUpperCase())) {
files[pieces[1]] = (await this.getContent(workingUri, pieces[1]));
const include = (await this.getContent(workingUri, pieces[1]));
files[pieces[1]] = include.lines;
fileList.push(pieces[1]);

if (include.lines.length === 0) {
}
}
}
}
Expand Down
38 changes: 13 additions & 25 deletions src/vscode/LanguageWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = class LanguageWorker {
*/
constructor(context) {
context.subscriptions.push(
vscode.commands.registerCommand(`vscode-rpgle.rpgleOpenInclude`, () => {
vscode.commands.registerCommand(`vscode-rpgle.rpgleOpenInclude`, async () => {
const editor = vscode.window.activeTextEditor;

if (editor) {
Expand All @@ -25,22 +25,12 @@ module.exports = class LanguageWorker {
if (document.languageId === `rpgle`) {
const linePieces = document.lineAt(position.line).text.trim().split(` `);
if ([`/COPY`, `/INCLUDE`].includes(linePieces[0].toUpperCase())) {
const {finishedPath, type} = Generic.getPathInfo(document.uri, linePieces[1]);
const {uri} = await Parser.getContent(document.uri, linePieces[1]);

switch (type) {
case `member`:
vscode.commands.executeCommand(`code-for-ibmi.openEditable`, `${finishedPath.substring(1)}.rpgle`);
break;

case `streamfile`:
vscode.commands.executeCommand(`code-for-ibmi.openEditable`, finishedPath);
break;

case `file`:
vscode.workspace.openTextDocument(vscode.Uri.file(finishedPath)).then(doc => {
if (uri) {
vscode.workspace.openTextDocument(uri).then(doc => {
vscode.window.showTextDocument(doc);
});
break;
}
}
}
Expand Down Expand Up @@ -179,11 +169,11 @@ module.exports = class LanguageWorker {

} else {
if ([`/COPY`, `/INCLUDE`].includes(linePieces[0].toUpperCase())) {
const {type, memberPath, finishedPath} = Generic.getPathInfo(document.uri, linePieces[1]);
const include = await Parser.getContent(document.uri, linePieces[1]);

return new vscode.Hover(
new vscode.MarkdownString(
`\`'${finishedPath}'\` (${type})`
`\`'${include.path}'\` (${include.type}${include.found ? `` : `, not found`})`
)
)
}
Expand Down Expand Up @@ -360,22 +350,20 @@ module.exports = class LanguageWorker {
}

if (def) {
let {finishedPath, type} = Generic.getPathInfo(document.uri, def.position.path);
if (document.uri.path.startsWith(finishedPath)) {
let {path, type, uri} = await Parser.getContent(document.uri, def.position.path);
if (document.uri.path.startsWith(path)) {
return new vscode.Location(
document.uri,
new vscode.Range(def.position.line, 0, def.position.line, 0)
);

} else {
if (type === `member`) {
finishedPath = `${finishedPath}.rpgle`;
if (uri) {
return new vscode.Location(
uri,
new vscode.Range(def.position.line, 0, def.position.line, 0)
);
}

return new vscode.Location(
vscode.Uri.parse(finishedPath).with({scheme: type, path: finishedPath}),
new vscode.Range(def.position.line, 0, def.position.line, 0)
);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/vscode/LinterWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ module.exports = class LinterWorker {

const possibleJson = await this.getLinterFile(workingUri);
if (possibleJson) {
const jsonString = possibleJson.join(``).trim();
const jsonString = possibleJson.lines.join(``).trim();
if (jsonString) {
try {
options = JSON.parse(jsonString);
Expand Down

0 comments on commit a8dd969

Please sign in to comment.