Skip to content
Merged
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
33 changes: 12 additions & 21 deletions core/autocomplete/context/static-context/StaticContextService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as fs from "fs/promises";
import path from "path";
import { pathToFileURL } from "url";
import Parser from "web-tree-sitter";
import { IDE, Position } from "../../..";
import { FileType, IDE, Position } from "../../../";
import { localPathOrUriToPath } from "../../../util/pathToUri";
import { getFullLanguageName, getQueryForFile } from "../../../util/treeSitter";
import {
Expand Down Expand Up @@ -353,10 +352,7 @@ export class StaticContextService {
if (foundContents.has(tdLocation.filepath)) {
content = foundContents.get(tdLocation.filepath)!;
} else {
content = await fs.readFile(
localPathOrUriToPath(tdLocation.filepath),
"utf8",
);
content = await this.ide.readFile(tdLocation.filepath);
foundContents.set(tdLocation.filepath, content);
}

Expand Down Expand Up @@ -854,24 +850,19 @@ export class StaticContextService {
return skipDirs.includes(dirName) || dirName.startsWith(".");
};

async function scanRecursively(currentPath: string): Promise<void> {
const scanRecursively = async (currentPath: string): Promise<void> => {
try {
const entries = await fs.readdir(currentPath, {
withFileTypes: true,
});

for (const entry of entries) {
const fullPath = localPathOrUriToPath(
path.join(currentPath, entry.name),
);
const currentUri = pathToFileURL(currentPath).toString();
const entries = await this.ide.listDir(currentUri);
for (const [name, fileType] of entries) {
const fullPath = localPathOrUriToPath(path.join(currentPath, name));

if (entry.isDirectory()) {
// Skip common directories that typically don't contain source files
if (!shouldSkipDirectory(entry.name)) {
if (fileType === (2 as FileType.Directory)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not familiar with the ide API, we're supposed to typecast 2 as FileType.Directory?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, actually FileType enum exists in a .d.ts file which has import issues when not used as a type

we have done this in few places such as:

entry[1] === (1 as FileType.File) ||
entry[1] === (2 as FileType.Directory),

? (2 as FileType.Directory)
: dirent.isSymbolicLink()
? (64 as FileType.SymbolicLink)
: (1 as FileType.File),

if (!shouldSkipDirectory(name)) {
await scanRecursively(fullPath);
}
} else if (entry.isFile()) {
const extension = path.extname(entry.name).toLowerCase();
} else if (fileType === (1 as FileType.File)) {
const extension = path.extname(name).toLowerCase();
if (tsExtensions.includes(extension)) {
tsFiles.push(fullPath);
}
Expand All @@ -880,7 +871,7 @@ export class StaticContextService {
} catch (error) {
console.error(`Error reading directory ${currentPath}:`, error);
}
}
};

await scanRecursively(dirPath);
return tsFiles;
Expand Down
Loading