-
Notifications
You must be signed in to change notification settings - Fork 57
Infer doc URI based on doc name for the New File command #1741
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -478,3 +478,56 @@ export function inferDocName(uri: vscode.Uri): string | undefined { | |
| } | ||
| return result; | ||
| } | ||
|
|
||
| /** | ||
| * Use the known mappings between files and document names to infer | ||
| * a URI for a new document with name `docName` in `wsFolder`. | ||
| * For example, if the index shows that `User.Existing.cls` maps to | ||
| * `/wsFolder/src/User/Existing.cls`, then calling this with | ||
| * `User.NewClass.cls` will return a URI for `/wsFolder/src/User/NewClass.cls`. | ||
| * Returns `undefined` if an inference couldn't be made. | ||
| * | ||
| * Finds the indexed document that shares the longest package prefix | ||
| * with `docName` and uses its containing path. | ||
| */ | ||
| export function inferDocUri(docName: string, wsFolder: vscode.WorkspaceFolder): vscode.Uri | undefined { | ||
| const exts = [".cls", ".mac", ".int", ".inc"]; | ||
| const docExt = docName.slice(-4).toLowerCase(); | ||
| if (!exts.includes(docExt)) return; | ||
| const index = wsFolderIndex.get(wsFolder.uri.toString()); | ||
| if (!index || !index.uris.size) return; | ||
| const docNameNoExt = docName.slice(0, -4); // remove extension | ||
| const docPkgSegments = docNameNoExt.split(".").slice(0, -1); // remove class/routine name | ||
| // For each indexed document, compute its containing path and measure how | ||
| // closely its package matches the target document's package. Pick the one | ||
| // with the most shared leading package segments | ||
| let bestPathPrefix = ""; | ||
| let bestMatchLen = -1; | ||
| index.uris.forEach((indexDocName, indexDocUriStr) => { | ||
| const indexDocExt = indexDocName.slice(-4).toLowerCase(); | ||
| if (!exts.includes(indexDocExt)) return; | ||
|
isc-klu marked this conversation as resolved.
|
||
| const indexDocNamePath = `/${indexDocName.slice(0, -4).replaceAll(".", "/")}${indexDocExt}`; | ||
| let indexDocFullPath = vscode.Uri.parse(indexDocUriStr).path; | ||
| indexDocFullPath = indexDocFullPath.slice(0, -3) + indexDocFullPath.slice(-3).toLowerCase(); | ||
|
|
||
| if (!indexDocFullPath.endsWith(indexDocNamePath)) return; | ||
| const indexPathPrefix = indexDocFullPath.slice(0, -indexDocNamePath.length + 1); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The purpose of lines 507–514 appears to be computing
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to leave it as is given that it's only a few lines, is kind of an abstract operation, has cases where the closure needs to exit, and I think currently would be only be reusable in the inferDocName method |
||
|
|
||
| // Count how many leading package segments the indexed doc shares with the target | ||
| const indexPkgSegments = indexDocName.slice(0, -4).split(".").slice(0, -1); | ||
| let matchLen = 0; | ||
| while ( | ||
| matchLen < Math.min(docPkgSegments.length, indexPkgSegments.length) && | ||
| docPkgSegments[matchLen] === indexPkgSegments[matchLen] | ||
| ) { | ||
| matchLen++; | ||
| } | ||
| if (matchLen > bestMatchLen) { | ||
| bestMatchLen = matchLen; | ||
| bestPathPrefix = indexPathPrefix; | ||
| } | ||
| }); | ||
| if (!bestPathPrefix) return; | ||
| // Convert the document name to a file path and prepend the prefix | ||
| return wsFolder.uri.with({ path: `${bestPathPrefix}${docNameNoExt.replaceAll(".", "/")}${docExt}` }); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.