From 135a2f6bba76a3b2858977738970556e94c3353b Mon Sep 17 00:00:00 2001 From: Khai Hirschi Date: Thu, 16 Jan 2025 08:58:19 -0700 Subject: [PATCH] match with the biggest extension first if there are multiple possible matches Fixes a bug that skips ".test.ts" when args.extensions is [".ts", ".html", ".less", ".spec.ts", ".test.ts"] and all files exist --- src/extension.ts | 50 ++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 29 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index 75b305f..0ea327f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -65,38 +65,30 @@ function tryOpenCompanionFile(currentPath: string, args: CommandArguments, files const filesMap = {}; files.forEach(x => filesMap[x] = x); - // now lets try changing the last component, then the last 2 etc. - const minimumComponentMatches = 1; - const currentExtension = 1; - const candidates = []; - - // try all extensions - for (let e of args.extensions) { - for (let i = components.length - currentExtension; i >= minimumComponentMatches; i--) { - const nextComponents = components.slice(0, i); - const nextBase = nextComponents.join('.'); - - const nextFile = nextBase + e; - const exists = filesMap[nextFile]; - - if (exists) { - const dir = path.dirname(currentPath); - const filePath = path.join(dir, nextFile); - - if (candidates.indexOf(filePath) === -1) { - candidates.push(filePath); + // try the biggest match first (ie. match with .spec.ts before .ts) + for (let i = 1; i < components.length; i++) { + const lastComponents = components.slice(i); + const extension = '.' + lastComponents.join('.'); + + const index = args.extensions.indexOf(extension); + if (index !== -1) { + const base = components.slice(0, i).join('.'); + + // try all the other extensions, starting with the one after the match + for (let j = 1; j < args.extensions.length; j++) { + const nextExtension = args.extensions[(index + j) % args.extensions.length]; + const nextFile = base + nextExtension; + + const exists = filesMap[nextFile]; + if (exists) { + const dir = path.dirname(currentPath); + const filePath = path.join(dir, nextFile); + openFile(filePath, determineColumn(args.useOtherColumn)); + return; } } } } - - const selfIndex = candidates.indexOf(currentPath); - const nextIndex = (selfIndex + 1) % candidates.length; - - const candidate = candidates[nextIndex]; - if (candidate) { - openFile(candidate, determineColumn(args.useOtherColumn)); - } } function determineColumn(useOtherColumn: boolean): number { @@ -118,4 +110,4 @@ function openFile(path: string, column: number): boolean { // this method is called when your extension is deactivated export function deactivate() { -} \ No newline at end of file +}