Skip to content

Commit 25ef372

Browse files
committed
Update documentIndex.ts
1 parent 4f5f982 commit 25ef372

File tree

1 file changed

+26
-30
lines changed

1 file changed

+26
-30
lines changed

src/utils/documentIndex.ts

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -443,39 +443,35 @@ export function inferDocName(uri: vscode.Uri): string | undefined {
443443
const wsFolder = vscode.workspace.getWorkspaceFolder(uri);
444444
if (!wsFolder) return;
445445
const index = wsFolderIndex.get(wsFolder.uri.toString());
446-
if (!index) return;
447-
// Convert the URI into an array of path segments
448-
const uriParts = uri.path.split("/");
449-
uriParts.pop();
450-
// Stop looping once we reach the workspace folder root
451-
const loopEnd = wsFolder.uri.path.split("/").length - (wsFolder.uri.path.endsWith("/") ? 1 : 0);
452-
// Look for known documents in the same directory tree as the target URI.
453-
// Once we find a match, look at the relationship between the URI and name
454-
// and apply that same relationship to the target URI. Start at the containing
455-
// directory of the target and then work up the tree until we have a match.
456-
let result: string;
457-
for (let i = uriParts.length; i >= loopEnd; i--) {
458-
const uriDir = `${uriParts.slice(0, i).join("/")}/`;
459-
for (const [docUriStr, docName] of index.uris) {
446+
if (!index || !index.uris.size) return;
447+
// Get a list of all unique paths containing classes or routines that
448+
// do not contribute to the name of the documents contained within
449+
const containingPaths: Set<string> = new Set();
450+
index.uris.forEach((docName, docUriStr) => {
451+
const docNameExt = docName.slice(-4);
452+
if (exts.includes(docNameExt)) {
460453
const docUri = vscode.Uri.parse(docUriStr);
461-
if (exts.includes(docName.slice(-4)) && docUri.path.startsWith(uriDir)) {
462-
// This class or routine is in the same directory tree as the target
463-
// so attempt to determine how its name relates to its URI
464-
const docNamePath = `/${docName.slice(0, -4).replaceAll(".", "/")}${docName.slice(-4)}`;
465-
// Make sure the file extension is lowercased in the path before matching
466-
const startOfDocName = (docUri.path.slice(0, -3) + docUri.path.slice(-3).toLowerCase()).lastIndexOf(
467-
docNamePath
468-
);
469-
if (startOfDocName > -1) {
470-
// We've identified the leading path segments that don't contribute to the document name,
471-
// so remove them from the target URI before generating the document name. Need the + 1 to
472-
// remove the leading slash which was part of the match string.
473-
result = `${uri.path.slice(startOfDocName + 1, -4).replaceAll("/", ".")}${fileExt}`;
474-
break;
475-
}
454+
// This entry is for a class or routine so see if its name and file system path match
455+
const docNamePath = `/${docName.slice(0, -4).replaceAll(".", "/")}${docNameExt}`;
456+
// Make sure the file extension is lowercased in the path before matching
457+
const startOfDocName = (docUri.path.slice(0, -3) + docUri.path.slice(-3).toLowerCase()).lastIndexOf(docNamePath);
458+
if (startOfDocName > -1) {
459+
// The document name is the trailing substring of the file system path with different delimiters
460+
containingPaths.add(docUri.path.slice(0, startOfDocName + 1));
476461
}
477462
}
478-
if (result) break;
463+
});
464+
if (!containingPaths.size) return; // We couldn't learn anyhting from the documents in the index
465+
// Sort the values in the Set by number of segments descending so we check the longest paths first
466+
const containingPathsSorted = Array.from(containingPaths).sort((a, b) => b.split("/").length - a.split("/").length);
467+
let result: string;
468+
for (const prefix of containingPathsSorted) {
469+
if (uri.path.startsWith(prefix)) {
470+
// We've identified the leading path segments that don't contribute to the document
471+
// name, so remove them from the target URI before generating the document name
472+
result = `${uri.path.slice(prefix.length, -4).replaceAll("/", ".")}${fileExt}`;
473+
break;
474+
}
479475
}
480476
return result;
481477
}

0 commit comments

Comments
 (0)