Skip to content

Commit 5706d85

Browse files
committed
deleted old code
1 parent 743278c commit 5706d85

File tree

5 files changed

+164
-107
lines changed

5 files changed

+164
-107
lines changed

src/core/prompts/instructions/__tests__/generate-rules.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,65 @@ describe("ruleTypeDefinitions", () => {
9090
expect(rule.analysisSteps.length).toBeGreaterThan(0)
9191
})
9292
})
93+
94+
it("should have correct paths for each rule type", () => {
95+
expect(ruleTypeDefinitions.general.path).toBe(".roo/rules/coding-standards.md")
96+
expect(ruleTypeDefinitions.code.path).toBe(".roo/rules-code/implementation-rules.md")
97+
expect(ruleTypeDefinitions.architect.path).toBe(".roo/rules-architect/architecture-rules.md")
98+
expect(ruleTypeDefinitions.debug.path).toBe(".roo/rules-debug/debugging-rules.md")
99+
expect(ruleTypeDefinitions["docs-extractor"].path).toBe(".roo/rules-docs-extractor/documentation-rules.md")
100+
})
101+
102+
it("should include proper instructions for existing rule files", () => {
103+
const ruleInstructions: RuleInstruction[] = [ruleTypeDefinitions.general]
104+
const options: RulesGenerationOptions = {
105+
selectedRuleTypes: ["general"],
106+
addToGitignore: false,
107+
alwaysAllowWriteProtected: false,
108+
includeCustomRules: false,
109+
customRulesText: "",
110+
}
111+
112+
const result = generateRulesInstructions(ruleInstructions, options)
113+
114+
expect(result).toContain("Look for existing rule files")
115+
expect(result).toContain("CLAUDE.md, .cursorrules, .cursor/rules, or .github/copilot-instructions.md")
116+
expect(result).toContain("If found, incorporate and improve upon their content")
117+
})
118+
119+
it("should include instructions to open files after generation", () => {
120+
const ruleInstructions: RuleInstruction[] = [ruleTypeDefinitions.general]
121+
const options: RulesGenerationOptions = {
122+
selectedRuleTypes: ["general"],
123+
addToGitignore: false,
124+
alwaysAllowWriteProtected: false,
125+
includeCustomRules: false,
126+
customRulesText: "",
127+
}
128+
129+
const result = generateRulesInstructions(ruleInstructions, options)
130+
131+
expect(result).toContain("Open the generated files")
132+
expect(result).toContain("in the editor for review after creation")
133+
})
134+
135+
it("should include proper formatting instructions", () => {
136+
const ruleInstructions: RuleInstruction[] = [ruleTypeDefinitions.general]
137+
const options: RulesGenerationOptions = {
138+
selectedRuleTypes: ["general"],
139+
addToGitignore: false,
140+
alwaysAllowWriteProtected: false,
141+
includeCustomRules: false,
142+
customRulesText: "",
143+
}
144+
145+
const result = generateRulesInstructions(ruleInstructions, options)
146+
147+
expect(result).toContain("Make the rules actionable and specific")
148+
expect(result).toContain("Build/lint/test commands")
149+
expect(result).toContain("Code style guidelines")
150+
expect(result).toContain("Error handling patterns")
151+
expect(result).toContain("Keep rules concise")
152+
expect(result).toContain("aim for 20 lines per file")
153+
})
93154
})

src/core/webview/webviewMessageHandler.ts

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,11 +1888,7 @@ export const webviewMessageHandler = async (
18881888
try {
18891889
const workspacePath = getWorkspacePath()
18901890
if (!workspacePath) {
1891-
await provider.postMessageToWebview({
1892-
type: "rulesGenerationStatus",
1893-
success: false,
1894-
error: "No workspace folder open",
1895-
})
1891+
vscode.window.showErrorMessage("No workspace folder open. Please open a folder to generate rules.")
18961892
break
18971893
}
18981894

@@ -1907,13 +1903,13 @@ export const webviewMessageHandler = async (
19071903
const includeCustomRules = message.includeCustomRules || false
19081904
const customRulesText = message.customRulesText || ""
19091905

1910-
// Save current API config to restore later
1911-
const currentApiConfig = getGlobalState("currentApiConfigName")
1912-
1913-
// Temporarily switch to the selected API config if provided
1914-
if (apiConfigName && apiConfigName !== currentApiConfig) {
1915-
await updateGlobalState("currentApiConfigName", apiConfigName)
1916-
await provider.postStateToWebview()
1906+
// Switch to the selected API config if provided
1907+
if (apiConfigName) {
1908+
const currentApiConfig = getGlobalState("currentApiConfigName")
1909+
if (apiConfigName !== currentApiConfig) {
1910+
await updateGlobalState("currentApiConfigName", apiConfigName)
1911+
await provider.activateProviderProfile({ name: apiConfigName })
1912+
}
19171913
}
19181914

19191915
// Create a comprehensive message for the rules generation task using existing analysis logic
@@ -1929,32 +1925,16 @@ export const webviewMessageHandler = async (
19291925
// Spawn a new task in code mode to generate the rules
19301926
await provider.initClineWithTask(rulesGenerationMessage)
19311927

1932-
// Restore the original API config
1933-
if (apiConfigName && apiConfigName !== currentApiConfig) {
1934-
await updateGlobalState("currentApiConfigName", currentApiConfig)
1935-
await provider.postStateToWebview()
1936-
}
1937-
1938-
// Send success message back to webview indicating task was created
1939-
await provider.postMessageToWebview({
1940-
type: "rulesGenerationStatus",
1941-
success: true,
1942-
text: "Rules generation task created successfully. The new task will analyze your codebase and generate comprehensive rules.",
1943-
})
1944-
19451928
// Automatically navigate to the chat tab to show the new task
19461929
await provider.postMessageToWebview({
19471930
type: "action",
19481931
action: "switchTab",
19491932
tab: "chat",
19501933
})
19511934
} catch (error) {
1952-
// Send error message back to webview
1953-
await provider.postMessageToWebview({
1954-
type: "rulesGenerationStatus",
1955-
success: false,
1956-
error: error instanceof Error ? error.message : String(error),
1957-
})
1935+
// Show error message to user
1936+
const errorMessage = error instanceof Error ? error.message : String(error)
1937+
vscode.window.showErrorMessage(`Failed to generate rules: ${errorMessage}`)
19581938
}
19591939
break
19601940
case "checkExistingRuleFiles":

src/services/rules/__tests__/rulesGenerator.test.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,86 @@ describe("rulesGenerator", () => {
9696
expect(message).toContain("Identify architectural patterns")
9797
expect(message).toContain("separation of concerns")
9898
})
99+
100+
it("should include custom rules when includeCustomRules is true", async () => {
101+
const customRulesText = "Always use TypeScript interfaces instead of types"
102+
const message = await createRulesGenerationTaskMessage(
103+
mockWorkspacePath,
104+
["general"],
105+
false,
106+
false,
107+
true,
108+
customRulesText,
109+
)
110+
111+
expect(message).toContain("Additional rules from User to add to the rules file:")
112+
expect(message).toContain(customRulesText)
113+
})
114+
115+
it("should not include custom rules when includeCustomRules is false", async () => {
116+
const customRulesText = "Always use TypeScript interfaces instead of types"
117+
const message = await createRulesGenerationTaskMessage(
118+
mockWorkspacePath,
119+
["general"],
120+
false,
121+
false,
122+
false,
123+
customRulesText,
124+
)
125+
126+
expect(message).not.toContain("Additional rules from User to add to the rules file:")
127+
expect(message).not.toContain(customRulesText)
128+
})
129+
130+
it("should handle empty custom rules text", async () => {
131+
const message = await createRulesGenerationTaskMessage(
132+
mockWorkspacePath,
133+
["general"],
134+
false,
135+
false,
136+
true,
137+
"",
138+
)
139+
140+
expect(message).not.toContain("Additional rules from User to add to the rules file:")
141+
})
142+
143+
it("should handle mkdir errors gracefully", async () => {
144+
// Mock mkdir to throw an error
145+
vi.mocked(fs.mkdir).mockRejectedValueOnce(new Error("Permission denied"))
146+
147+
// Should not throw even if mkdir fails
148+
await expect(
149+
createRulesGenerationTaskMessage(mockWorkspacePath, ["general"], false, true),
150+
).resolves.toBeDefined()
151+
})
152+
153+
it("should filter out invalid rule types", async () => {
154+
const message = await createRulesGenerationTaskMessage(
155+
mockWorkspacePath,
156+
["general", "invalid-type", "code"],
157+
false,
158+
false,
159+
)
160+
161+
// Should include valid types
162+
expect(message).toContain(".roo/rules/coding-standards.md")
163+
expect(message).toContain(".roo/rules-code/implementation-rules.md")
164+
165+
// Should not include invalid type
166+
expect(message).not.toContain("invalid-type")
167+
})
168+
169+
it("should handle all rule types", async () => {
170+
const allRuleTypes = ["general", "code", "architect", "debug", "docs-extractor"]
171+
const message = await createRulesGenerationTaskMessage(mockWorkspacePath, allRuleTypes, false, false)
172+
173+
// Check all rule files are mentioned
174+
expect(message).toContain(".roo/rules/coding-standards.md")
175+
expect(message).toContain(".roo/rules-code/implementation-rules.md")
176+
expect(message).toContain(".roo/rules-architect/architecture-rules.md")
177+
expect(message).toContain(".roo/rules-debug/debugging-rules.md")
178+
expect(message).toContain(".roo/rules-docs-extractor/documentation-rules.md")
179+
})
99180
})
100181
})

webview-ui/src/components/settings/RulesSettings.tsx

Lines changed: 11 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HTMLAttributes, useState, useEffect } from "react"
22
import { useAppTranslation } from "@/i18n/TranslationContext"
3-
import { FileText, Loader2, AlertTriangle, Info, Sparkles } from "lucide-react"
3+
import { FileText, AlertTriangle, Info, Sparkles } from "lucide-react"
44
import { Button } from "@/components/ui/button"
55
import { vscode } from "@/utils/vscode"
66
import { cn } from "@/lib/utils"
@@ -25,13 +25,7 @@ interface RuleType {
2525

2626
export const RulesSettings = ({ className, hasUnsavedChanges, ...props }: RulesSettingsProps) => {
2727
const { t } = useAppTranslation()
28-
const [isGenerating, setIsGenerating] = useState(false)
29-
const [generationStatus, setGenerationStatus] = useState<{
30-
type: "success" | "error" | null
31-
message: string
32-
}>({ type: null, message: "" })
3328
const [addToGitignore, setAddToGitignore] = useState(true)
34-
const [_existingFiles, setExistingFiles] = useState<string[]>([])
3529
const [alwaysAllowWriteProtected, setAlwaysAllowWriteProtected] = useState(true)
3630
const [selectedApiConfig, setSelectedApiConfig] = useState<string>("")
3731
const [includeCustomRules, setIncludeCustomRules] = useState(false)
@@ -100,21 +94,7 @@ export const RulesSettings = ({ className, hasUnsavedChanges, ...props }: RulesS
10094
useEffect(() => {
10195
const handleMessage = (event: MessageEvent) => {
10296
const message = event.data
103-
if (message.type === "rulesGenerationStatus") {
104-
setIsGenerating(false)
105-
if (message.success) {
106-
setGenerationStatus({
107-
type: "success",
108-
message: message.text || "",
109-
})
110-
} else {
111-
setGenerationStatus({
112-
type: "error",
113-
message: message.error || "Unknown error occurred",
114-
})
115-
}
116-
} else if (message.type === "existingRuleFiles") {
117-
setExistingFiles(message.files || [])
97+
if (message.type === "existingRuleFiles") {
11898
// Update rule types with existence information
11999
setRuleTypes((prev) =>
120100
prev.map((rule) => ({
@@ -141,16 +121,9 @@ export const RulesSettings = ({ className, hasUnsavedChanges, ...props }: RulesS
141121
const handleGenerateRules = () => {
142122
const selectedRules = ruleTypes.filter((rule) => rule.checked)
143123
if (selectedRules.length === 0) {
144-
setGenerationStatus({
145-
type: "error",
146-
message: t("settings:rules.noRulesSelected"),
147-
})
148124
return
149125
}
150126

151-
setIsGenerating(true)
152-
setGenerationStatus({ type: null, message: "" })
153-
154127
// Send message to extension to generate rules
155128
vscode.postMessage({
156129
type: "generateRules",
@@ -165,6 +138,7 @@ export const RulesSettings = ({ className, hasUnsavedChanges, ...props }: RulesS
165138

166139
const existingRules = ruleTypes.filter((rule) => rule.checked && rule.exists)
167140
const hasExistingFiles = existingRules.length > 0
141+
const hasSelectedRules = ruleTypes.some((rule) => rule.checked)
168142

169143
return (
170144
<div className={cn("flex flex-col gap-2", className)} {...props}>
@@ -347,51 +321,25 @@ export const RulesSettings = ({ className, hasUnsavedChanges, ...props }: RulesS
347321
content={
348322
hasUnsavedChanges
349323
? t("settings:rules.unsavedChangesError")
350-
: t("settings:rules.generateButtonTooltip")
324+
: !hasSelectedRules
325+
? t("settings:rules.noRulesSelected")
326+
: t("settings:rules.generateButtonTooltip")
351327
}>
352328
<span className="w-full">
353329
<Button
354330
onClick={handleGenerateRules}
355-
disabled={isGenerating || !selectedApiConfig || hasUnsavedChanges}
331+
disabled={!selectedApiConfig || hasUnsavedChanges || !hasSelectedRules}
356332
variant="default"
357333
size="default"
358334
className="w-full">
359-
{isGenerating ? (
360-
<>
361-
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
362-
{t("settings:rules.generating")}
363-
</>
364-
) : (
365-
<>
366-
<FileText className="mr-2 h-4 w-4" />
367-
{t("settings:rules.generateButton")}
368-
</>
369-
)}
335+
<>
336+
<FileText className="mr-2 h-4 w-4" />
337+
{t("settings:rules.generateButton")}
338+
</>
370339
</Button>
371340
</span>
372341
</StandardTooltip>
373342
</div>
374-
375-
{isGenerating && (
376-
<p className="text-vscode-descriptionForeground text-sm">
377-
{t("settings:rules.creatingTaskDescription")}
378-
</p>
379-
)}
380-
381-
{generationStatus.type === "success" && (
382-
<div className="text-vscode-testing-iconPassed text-sm">
383-
<p>{generationStatus.message || t("settings:rules.taskCreated")}</p>
384-
</div>
385-
)}
386-
387-
{generationStatus.type === "error" && (
388-
<div className="text-vscode-testing-iconFailed text-sm">
389-
<p>{t("settings:rules.error")}</p>
390-
<p className="text-vscode-descriptionForeground">
391-
{t("settings:rules.errorDescription", { error: generationStatus.message })}
392-
</p>
393-
</div>
394-
)}
395343
</div>
396344
</div>
397345
</div>

webview-ui/src/i18n/locales/en/settings.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -611,19 +611,6 @@
611611
},
612612
"generateButton": "Generate Rules",
613613
"generateButtonTooltip": "Create a new task to analyze the codebase and generate rules automatically",
614-
"generating": "Creating task...",
615-
"generatingDescription": "Analyzing your codebase to create comprehensive rules. This may take a moment.",
616-
"creatingTaskDescription": "Creating a new task to analyze your codebase and generate comprehensive rules.",
617-
"success": "Rules generated successfully!",
618-
"successDescription": "Rules have been saved to {{path}}",
619-
"taskCreated": "Rules generation task created! You'll be automatically taken to the new task.",
620-
"error": "Failed to create rules generation task",
621-
"errorDescription": "An error occurred while creating the rules generation task: {{error}}",
622-
"viewRules": "View Generated Rules",
623-
"existingRules": "Existing rules detected",
624-
"existingRulesDescription": "Rules already exist at {{path}}. Generating new rules will create a timestamped file to preserve your existing rules.",
625-
"noWorkspace": "No workspace folder open",
626-
"noWorkspaceDescription": "Please open a workspace folder to generate rules for your project.",
627614
"selectTypes": "Select rule types to generate:",
628615
"noRulesSelected": "Please select at least one rule type to generate",
629616
"unsavedChangesError": "Please save your settings before generating rules",

0 commit comments

Comments
 (0)