Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -118,4 +110,4 @@ function openFile(path: string, column: number): boolean {

// this method is called when your extension is deactivated
export function deactivate() {
}
}