Skip to content

Commit

Permalink
FIX (Extension) @W-16442046@ Show suggested code only when available …
Browse files Browse the repository at this point in the history
…from ApexGuru response (#120)
  • Loading branch information
jag-j committed Aug 20, 2024
1 parent b8c157f commit 6d6502a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
9 changes: 5 additions & 4 deletions src/apexguru/apex-guru-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ export function transformStringToRuleResult(fileName: string, jsonString: string
};

reports.forEach(parsed => {
const encodedCodeBefore = parsed.properties.find((prop: ApexGuruProperty) => prop.name === 'code_before')?.value;
const encodedCodeAfter = parsed.properties.find((prop: ApexGuruProperty) => prop.name === 'code_after')?.value;
const encodedCodeBefore = parsed.properties.find((prop: ApexGuruProperty) => prop.name === 'code_before')?.value ?? '';
const encodedCodeAfter = parsed.properties.find((prop: ApexGuruProperty) => prop.name === 'code_after')?.value ?? '';
const lineNumber = parseInt(parsed.properties.find((prop: ApexGuruProperty) => prop.name === 'line_number')?.value);

const violation: ApexGuruViolation = {
Expand All @@ -139,8 +139,9 @@ export function transformStringToRuleResult(fileName: string, jsonString: string
category: parsed.type, // Replace with actual category if available
line: lineNumber,
column: 1,
currentCode: encodedCodeBefore ? Buffer.from(encodedCodeBefore, 'base64').toString('utf8') : encodedCodeBefore,
suggestedCode: encodedCodeAfter ? Buffer.from(encodedCodeAfter, 'base64').toString('utf8') : encodedCodeAfter,
currentCode: Buffer.from(encodedCodeBefore, 'base64').toString('utf8'),
suggestedCode: Buffer.from(encodedCodeAfter, 'base64').toString('utf8'),
url: fileName
};

ruleResult.violations.push(violation);
Expand Down
10 changes: 5 additions & 5 deletions src/lib/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,15 @@ export class DiagnosticManager {
if (engine === 'apexguru') {
const apexGuruViolation = violation as ApexGuruViolation;

if (apexGuruViolation.suggestedCode !== undefined) {
if (apexGuruViolation.suggestedCode) {
diagnostic.relatedInformation = [
new vscode.DiagnosticRelatedInformation(
new vscode.Location(vscode.Uri.parse('Current Code'), range),
`${apexGuruViolation.currentCode}`
new vscode.Location(vscode.Uri.parse(violation.url), range),
`Current Code: ${apexGuruViolation.currentCode}`
),
new vscode.DiagnosticRelatedInformation(
new vscode.Location(vscode.Uri.parse('ApexGuru Suggestions'), range),
`${apexGuruViolation.suggestedCode}`
new vscode.Location(vscode.Uri.parse(violation.url), range),
`ApexGuru Suggestions: ${apexGuruViolation.suggestedCode}`
)
];
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/suite/apexguru/apex-guru-service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,9 @@ suite('Apex Guru Test Suite', () => {
category: 'BestPractices',
line: 10,
column: 1,
currentCode: undefined,
suggestedCode: 'System.out.println("Hello World");'
currentCode: '',
suggestedCode: 'System.out.println("Hello World");',
url: "TestFile.cls"
}]
});
});
Expand Down

0 comments on commit 6d6502a

Please sign in to comment.