Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions src/commands/_shared/file-resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import path from 'node:path';
import type { IndexDatabase } from '../../db/database.js';

/**
* Resolve a file path to a database file ID.
*
* Tries three lookup strategies in order:
* 1. Relative path (db.toRelativePath of the resolved absolute path)
* 2. Absolute resolved path
* 3. The original path as-is (handles suffix/partial matches stored in the db)
*
* Returns `null` if no match is found.
*/
export function resolveFileId(db: IndexDatabase, filePath: string): number | null {
const resolvedPath = path.resolve(filePath);
const relativePath = db.toRelativePath(resolvedPath);
return db.files.getIdByPath(relativePath) ?? db.files.getIdByPath(resolvedPath) ?? db.files.getIdByPath(filePath);
}
1 change: 1 addition & 0 deletions src/commands/_shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { openDatabase, withDatabase, resolveDbPath } from './db-helper.js';
export { resolveFileId } from './file-resolver.js';
export { SymbolResolver, type ResolvedSymbol, type ResolvedSymbolWithDetails } from './symbol-resolver.js';
export { SharedFlags, LlmFlags } from './flags.js';
export { outputJsonOrPlain, truncate, tableSeparator, formatLineNumber } from './output.js';
Expand Down
4 changes: 2 additions & 2 deletions src/commands/files/imported-by.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import { Args, Command } from '@oclif/core';
import chalk from 'chalk';
import { SharedFlags, withDatabase } from '../_shared/index.js';
import { SharedFlags, resolveFileId, withDatabase } from '../_shared/index.js';

export default class ImportedBy extends Command {
static override description = 'List files that import a specific file';
Expand All @@ -27,7 +27,7 @@ export default class ImportedBy extends Command {
const filePath = path.resolve(args.file);

await withDatabase(flags.database, this, async (db) => {
const fileId = db.files.getIdByPath(db.toRelativePath(filePath)) ?? db.files.getIdByPath(filePath);
const fileId = resolveFileId(db, filePath);
if (fileId === null) {
this.error(chalk.red(`File "${filePath}" not found in the index.`));
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/files/imports.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import { Args, Command, Flags } from '@oclif/core';
import chalk from 'chalk';
import { SharedFlags, withDatabase } from '../_shared/index.js';
import { SharedFlags, resolveFileId, withDatabase } from '../_shared/index.js';

export default class Imports extends Command {
static override description = 'List files imported by a specific file';
Expand Down Expand Up @@ -32,7 +32,7 @@ export default class Imports extends Command {
const filePath = path.resolve(args.file);

await withDatabase(flags.database, this, async (db) => {
const fileId = db.files.getIdByPath(db.toRelativePath(filePath)) ?? db.files.getIdByPath(filePath);
const fileId = resolveFileId(db, filePath);
if (fileId === null) {
this.error(chalk.red(`File "${filePath}" not found in the index.`));
}
Expand Down
4 changes: 2 additions & 2 deletions src/commands/files/show.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'node:path';
import { Args, Command } from '@oclif/core';
import chalk from 'chalk';
import { SharedFlags, formatModuleRef, outputJsonOrPlain, withDatabase } from '../_shared/index.js';
import { SharedFlags, formatModuleRef, outputJsonOrPlain, resolveFileId, withDatabase } from '../_shared/index.js';

export default class FilesShow extends Command {
static override description = 'Show file details including definitions and imports';
Expand All @@ -25,7 +25,7 @@ export default class FilesShow extends Command {
const filePath = path.resolve(args.path);

await withDatabase(flags.database, this, async (db) => {
const fileId = db.files.getIdByPath(db.toRelativePath(filePath)) ?? db.files.getIdByPath(filePath);
const fileId = resolveFileId(db, filePath);
if (fileId === null) {
this.error(chalk.red(`File "${filePath}" not found in the index.`));
}
Expand Down
16 changes: 1 addition & 15 deletions src/commands/llm/_shared/pure-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { SyntaxNode } from 'tree-sitter';
import Parser from 'tree-sitter';
import Ruby from 'tree-sitter-ruby';
import TypeScript from 'tree-sitter-typescript';
import { countArguments } from '../../../parser/_shared/ast-utils.js';

let tsParser: Parser | null = null;
function getParser(): Parser {
Expand Down Expand Up @@ -626,21 +627,6 @@ function resolveRootIdentifier(node: SyntaxNode): string | null {
return null;
}

/**
* Count non-punctuation arguments in an arguments node.
*/
function countArguments(argsNode: SyntaxNode): number {
let count = 0;
for (let i = 0; i < argsNode.childCount; i++) {
const child = argsNode.child(i);
if (child) {
const t = child.type;
if (t !== '(' && t !== ')' && t !== ',') count++;
}
}
return count;
}

/**
* Check if a node has any function/arrow_function/method ancestor.
* Used to determine if a throw_statement is inside a function body
Expand Down
Loading
Loading