Skip to content

Commit 6a78892

Browse files
authored
Merge pull request #10 from Yandrik/main
Commands "Add File to Cody", "Add Selection to Cody" and shallow folder
2 parents 9ba12f5 + c86fbf1 commit 6a78892

File tree

6 files changed

+1308
-1001
lines changed

6 files changed

+1308
-1001
lines changed

package.json

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,24 @@
2121
"main": "./dist/extension.js",
2222
"contributes": {
2323
"commands": [
24+
{
25+
"command": "cody-plus-plus.addFile",
26+
"title": "Add File to Cody",
27+
"category": "Cody++"
28+
},
29+
{
30+
"command": "cody-plus-plus.addSelection",
31+
"title": "Add Selected Files to Cody",
32+
"category": "Cody++"
33+
},
2434
{
2535
"command": "cody-plus-plus.addFolder",
26-
"title": "Add Folder to Cody Chat",
36+
"title": "Add Folder (Recursive) to Cody",
37+
"category": "Cody++"
38+
},
39+
{
40+
"command": "cody-plus-plus.addShallowFolder",
41+
"title": "Add Folder to Cody",
2742
"category": "Cody++"
2843
},
2944
{
@@ -48,9 +63,24 @@
4863
"menus": {
4964
"explorer/context": [
5065
{
51-
"command": "cody-plus-plus.addFolder",
52-
"when": "explorerResourceIsFolder && cody.activated",
66+
"command": "cody-plus-plus.addFile",
67+
"when": "!explorerResourceIsFolder && !listMultiSelection && explorerViewletFocus && resourceLangId && cody.activated",
5368
"group": "0_cody"
69+
},
70+
{
71+
"command": "cody-plus-plus.addSelection",
72+
"when": "listMultiSelection && explorerViewletFocus && listHasSelectionOrFocus && cody.activated",
73+
"group": "0_cody"
74+
},
75+
{
76+
"command": "cody-plus-plus.addFolder",
77+
"when": "explorerResourceIsFolder && explorerViewletFocus && cody.activated",
78+
"group": "1_cody"
79+
},
80+
{
81+
"command": "cody-plus-plus.addShallowFolder",
82+
"when": "explorerResourceIsFolder && explorerViewletFocus && cody.activated",
83+
"group": "1_cody"
5484
}
5585
],
5686
"view/title": [
@@ -174,4 +204,4 @@
174204
"zod": "^3.23.8"
175205
},
176206
"packageManager": "[email protected]"
177-
}
207+
}

src/commands/addFolder.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@ import * as vscode from 'vscode'
33
import { CODY_COMMAND } from '../constants/cody'
44
import { countFilesInDirectory, walkDirectory } from '../utils/file'
55

6-
// Command function to add all files in a folder to Cody
6+
// Command function to add all files in a folder to Cody (recursive)
77
export async function addFolderCommand(uri: vscode.Uri) {
8+
await addFolderInternal(uri, true)
9+
}
10+
11+
// Command function to add only files in the current folder to Cody (non-recursive)
12+
export async function addShallowFolderCommand(uri: vscode.Uri) {
13+
await addFolderInternal(uri, false)
14+
}
15+
16+
// Internal helper function to handle both recursive and non-recursive folder scanning
17+
async function addFolderInternal(uri: vscode.Uri, recursive: boolean) {
818
// Retrieve extension configurations
919
const config = vscode.workspace.getConfiguration('codyPlusPlus')
1020
// Maximum number of files before prompting the user
@@ -17,8 +27,13 @@ export async function addFolderCommand(uri: vscode.Uri) {
1727
console.log('CODY++', 'EXCLUDED FOLDERS', { excludedFolders })
1828

1929
try {
20-
// Pass excludedFolders to countFilesInDirectory
21-
const fileCount = await countFilesInDirectory(uri, excludedFileTypes, excludedFolders)
30+
// Count files
31+
const fileCount = await countFilesInDirectory(
32+
uri,
33+
excludedFileTypes,
34+
excludedFolders,
35+
recursive
36+
)
2237

2338
// Prompt the user if file count exceeds threshold
2439
if (fileCount > fileThreshold) {
@@ -33,10 +48,14 @@ export async function addFolderCommand(uri: vscode.Uri) {
3348
}
3449
}
3550

36-
// Traverse the directory
37-
await walkDirectory(uri, excludedFileTypes, excludedFolders, async fileUri => {
38-
await executeMentionFileCommand(fileUri)
39-
})
51+
// Process files using walkDirectory with shallow flag based on recursive parameter
52+
await walkDirectory(
53+
uri,
54+
excludedFileTypes,
55+
excludedFolders,
56+
executeMentionFileCommand,
57+
!recursive
58+
)
4059
} catch (error: any) {
4160
vscode.window.showErrorMessage(`Failed to add folder to Cody: ${error.message}`)
4261
}

src/commands/addToCody.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import * as vscode from 'vscode'
2+
import { CODY_COMMAND } from '../constants/cody'
3+
import { countFilesInDirectory, walkDirectory } from '../utils/file'
4+
5+
export async function addFile(uri: vscode.Uri) {
6+
try {
7+
await executeMentionFileCommand(uri)
8+
} catch (error: any) {
9+
vscode.window.showErrorMessage(`Failed to add file to Cody: ${error.message}`)
10+
}
11+
}
12+
13+
export async function addSelection(uris: vscode.Uri[]) {
14+
if (!uris || uris.length === 0) {
15+
vscode.window.showWarningMessage('No files selected to add to Cody.')
16+
return
17+
}
18+
19+
const config = vscode.workspace.getConfiguration('codyPlusPlus')
20+
const fileThreshold: number = config.get<number>('fileThreshold', 15)
21+
const excludedFileTypes: string[] = config.get<string[]>('excludedFileTypes', [])
22+
const excludedFolders: string[] = config.get<string[]>('excludedFolders', [])
23+
24+
try {
25+
let totalFileCount = 0
26+
for (const uri of uris) {
27+
const stat = await vscode.workspace.fs.stat(uri)
28+
if (stat.type === vscode.FileType.Directory) {
29+
totalFileCount += await countFilesInDirectory(uri, excludedFileTypes, excludedFolders)
30+
} else {
31+
totalFileCount++
32+
}
33+
}
34+
35+
if (totalFileCount > fileThreshold) {
36+
const userResponse = await vscode.window.showWarningMessage(
37+
`The selection contains ${totalFileCount} files. Do you want to proceed?`,
38+
{ modal: true },
39+
'Yes',
40+
'No'
41+
)
42+
if (userResponse !== 'Yes') {
43+
return
44+
}
45+
}
46+
47+
for (const uri of uris) {
48+
const stat = await vscode.workspace.fs.stat(uri)
49+
if (stat.type === vscode.FileType.File) {
50+
await executeMentionFileCommand(uri)
51+
} else if (stat.type === vscode.FileType.Directory) {
52+
await walkDirectory(uri, excludedFileTypes, excludedFolders, async fileUri => {
53+
await executeMentionFileCommand(fileUri)
54+
})
55+
}
56+
}
57+
} catch (error: any) {
58+
vscode.window.showErrorMessage(`Failed to add selection to Cody: ${error.message}`)
59+
}
60+
}
61+
62+
async function executeMentionFileCommand(uri: vscode.Uri) {
63+
try {
64+
await vscode.commands.executeCommand(CODY_COMMAND.MENTION.FILE, uri)
65+
} catch (error: any) {
66+
vscode.window.showErrorMessage(`Failed to trigger Cody to mention file: ${error.message}`)
67+
}
68+
}

src/extension.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
import * as vscode from 'vscode'
33
// Import custom command handlers
44
import { addCustomCommand, editCustomCommand } from './commands/addCustomCommand'
5-
import { addFolderCommand } from './commands/addFolder'
5+
import { addFolderCommand, addShallowFolderCommand } from './commands/addFolder'
6+
import { addFile, addSelection } from './commands/addToCody'
67
// Import services and views
78
import { CustomCommandService } from './services/customCommand.service'
89
import { CustomCommandsTreeView } from './views/CustomCommandsTreeView'
@@ -20,6 +21,12 @@ export function activate(context: vscode.ExtensionContext) {
2021
addFolderCommand
2122
)
2223

24+
// Register the "Add Shallow Folder" command, which adds only files in the current folder to Cody
25+
const addShallowFolderDisposable = vscode.commands.registerCommand(
26+
'cody-plus-plus.addShallowFolder',
27+
addShallowFolderCommand
28+
)
29+
2330
// Register the "Add Custom Command" command, which opens a UI to create a custom command
2431
const addCustomCommandDisposable = vscode.commands.registerCommand(
2532
'cody-plus-plus.addCustomCommand',
@@ -53,16 +60,29 @@ export function activate(context: vscode.ExtensionContext) {
5360
}
5461
)
5562

63+
const addFileToCodyDisposable = vscode.commands.registerCommand('cody-plus-plus.addFile', addFile)
64+
65+
const addSelectionToCodyDisposable = vscode.commands.registerCommand(
66+
'cody-plus-plus.addSelection',
67+
async (contextSelection: vscode.Uri, allSelections: vscode.Uri[]) => {
68+
const urisToAdd = allSelections || [contextSelection]
69+
await addSelection(urisToAdd)
70+
}
71+
)
72+
5673
// Create and register the tree view for displaying custom commands in the sidebar
5774
const customCommandsTreeView = new CustomCommandsTreeView()
5875
vscode.window.registerTreeDataProvider('customCommands', customCommandsTreeView)
5976

6077
// Add all disposables to the extension context for proper cleanup on deactivation
6178
context.subscriptions.push(
6279
addFolderDisposable,
80+
addShallowFolderDisposable,
6381
addCustomCommandDisposable,
6482
editCommandDisposable,
65-
deleteCommandDisposable
83+
deleteCommandDisposable,
84+
addFileToCodyDisposable,
85+
addSelectionToCodyDisposable
6686
)
6787
}
6888

src/utils/file.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import * as vscode from 'vscode'
55
export async function countFilesInDirectory(
66
uri: vscode.Uri,
77
excludedFileTypes: string[] = [],
8-
excludedFolders: string[] = []
8+
excludedFolders: string[] = [],
9+
shallow: boolean = false
910
): Promise<number> {
1011
let fileCount = 0
1112
// Read the contents of the directory
@@ -28,20 +29,20 @@ export async function countFilesInDirectory(
2829
if (fileType === vscode.FileType.File && !isFileExcluded) {
2930
// Increment count for non-excluded files
3031
fileCount++
31-
} else if (fileType === vscode.FileType.Directory) {
32+
} else if (fileType === vscode.FileType.Directory && !shallow) {
3233
// Recursively count files in subdirectories
33-
fileCount += await countFilesInDirectory(fileUri, excludedFileTypes, excludedFolders)
34+
fileCount += await countFilesInDirectory(fileUri, excludedFileTypes, excludedFolders, shallow)
3435
}
3536
}
3637
return fileCount
3738
}
38-
3939
// Walk through a directory and execute a callback for each file, excluding specified folders
4040
export async function walkDirectory(
4141
uri: vscode.Uri,
4242
excludedFileTypes: string[] = [],
4343
excludedFolders: string[] = [],
44-
callback: (fileUri: vscode.Uri) => Promise<void>
44+
callback: (fileUri: vscode.Uri) => Promise<void>,
45+
shallow: boolean = false
4546
) {
4647
// Read the contents of the directory
4748
const files = await vscode.workspace.fs.readDirectory(uri)
@@ -54,14 +55,14 @@ export async function walkDirectory(
5455
console.log('CODY++', `File ${fileName} is being processed`)
5556
// Execute callback for non-excluded files
5657
await callback(fileUri)
57-
} else if (fileType === vscode.FileType.Directory) {
58+
} else if (fileType === vscode.FileType.Directory && !shallow) {
5859
const isFolderExcluded = isFolderNameExcluded(fileName, excludedFolders)
5960
if (isFolderExcluded) {
6061
console.log('CODY++', `Folder ${fileName} is excluded`)
6162
continue
6263
}
63-
// Recursively walk through subdirectories
64-
await walkDirectory(fileUri, excludedFileTypes, excludedFolders, callback)
64+
// Recursively walk through subdirectories unless shallow mode is enabled
65+
await walkDirectory(fileUri, excludedFileTypes, excludedFolders, callback, shallow)
6566
}
6667
}
6768
}

0 commit comments

Comments
 (0)