From 5cb3946bd59fc121412165c6ebb7f20b9be6eb7f Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Wed, 14 Aug 2024 09:53:47 -0700 Subject: [PATCH 1/8] code back up: fixer on apex guru --- .vscode/settings.json | 6 ++++- src/apexguru/apex-guru-service.ts | 6 +++-- src/extension.ts | 10 ++++++- src/lib/constants.ts | 2 +- src/lib/diagnostics.ts | 19 ++++++++++++- src/lib/fixer.ts | 45 +++++++++++++++++++++++++++++-- src/types.ts | 1 + 7 files changed, 81 insertions(+), 8 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 30bf8c2..8d3f7e4 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,5 +7,9 @@ "out": true // set this to false to include "out" folder in search results }, // Turn off tsc task auto detection since we have the necessary tasks as npm scripts - "typescript.tsc.autoDetect": "off" + "typescript.tsc.autoDetect": "off", + "workbench.colorCustomizations": { + "diffEditor.insertedTextBackground": "#520dd144", // Green with some transparency + "diffEditor.removedTextBackground": "#ff000044" // Red with some transparency + } } \ No newline at end of file diff --git a/src/apexguru/apex-guru-service.ts b/src/apexguru/apex-guru-service.ts index 78b57ec..1cd6fb2 100644 --- a/src/apexguru/apex-guru-service.ts +++ b/src/apexguru/apex-guru-service.ts @@ -128,7 +128,8 @@ export function transformStringToRuleResult(fileName: string, jsonString: string }; reports.forEach(parsed => { - const encodedClassAfter = 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 violation: ApexGuruViolation = { ruleName: parsed.type, @@ -137,7 +138,8 @@ export function transformStringToRuleResult(fileName: string, jsonString: string category: parsed.type, // Replace with actual category if available line: parseInt(parsed.properties.find((prop: ApexGuruProperty) => prop.name === 'line_number')?.value), column: 1, - suggestedCode: Buffer.from(encodedClassAfter, 'base64').toString('utf8') + currentCode: Buffer.from(encodedCodeBefore, 'base64').toString('utf8'), + suggestedCode: Buffer.from(encodedCodeAfter, 'base64').toString('utf8'), }; ruleResult.violations.push(violation); diff --git a/src/extension.ts b/src/extension.ts index 0c89c9d..fb078f2 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -149,7 +149,15 @@ export async function activate(context: vscode.ExtensionContext): Promise { + await vscode.workspace.openTextDocument(documentUri); + const edit = new vscode.WorkspaceEdit(); + edit.replace(documentUri, range, suggestedCode); + await vscode.workspace.applyEdit(edit); + }); + + context.subscriptions.push(runApexGuruOnSelectedFile, apexGuruReplaceCode); } TelemetryService.sendExtensionActivationEvent(extensionHrStart); diff --git a/src/lib/constants.ts b/src/lib/constants.ts index cc05793..40f01fe 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -37,6 +37,6 @@ export const APEX_GURU_AUTH_ENDPOINT = '/services/data/v62.0/apexguru/validate' export const APEX_GURU_REQUEST = '/services/data/v62.0/apexguru/request' // feature gates -export const APEX_GURU_FEATURE_FLAG_ENABLED = false; +export const APEX_GURU_FEATURE_FLAG_ENABLED = true; export const APEX_GURU_MAX_TIMEOUT_SECONDS = 60; export const APEX_GURU_RETRY_INTERVAL_MILLIS = 1000; diff --git a/src/lib/diagnostics.ts b/src/lib/diagnostics.ts index 12dfffc..61f0ff4 100644 --- a/src/lib/diagnostics.ts +++ b/src/lib/diagnostics.ts @@ -4,7 +4,7 @@ * SPDX-License-Identifier: BSD-3-Clause * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ -import { PathlessRuleViolation, RuleResult, RuleViolation } from '../types'; +import { ApexGuruViolation, PathlessRuleViolation, RuleResult, RuleViolation } from '../types'; import {messages} from './messages'; import * as vscode from 'vscode'; @@ -89,6 +89,23 @@ export class DiagnosticManager { target: vscode.Uri.parse(violation.url), value: violation.ruleName } : violation.ruleName; + if (engine === 'apexguru') { + const apexGuruViolation = violation as ApexGuruViolation; + + if (apexGuruViolation.suggestedCode?.trim()) { + diagnostic.relatedInformation = [ + new vscode.DiagnosticRelatedInformation( + new vscode.Location(vscode.Uri.parse('Code Before'), range), + `Code before: ${apexGuruViolation.currentCode}` + ), + new vscode.DiagnosticRelatedInformation( + new vscode.Location(vscode.Uri.parse('Code After'), range), + `Code after: ${apexGuruViolation.suggestedCode}` + ) + ]; + } + + } return diagnostic; } diff --git a/src/lib/fixer.ts b/src/lib/fixer.ts index 9dc7dbb..6e51b98 100644 --- a/src/lib/fixer.ts +++ b/src/lib/fixer.ts @@ -5,8 +5,11 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import * as vscode from 'vscode'; +import * as path from 'path'; import {messages} from './messages'; import * as Constants from './constants'; +import * as fs from 'fs'; +import * as os from 'os'; /** * Class for creating and adding {@link vscode.CodeAction}s allowing violations to be fixed or suppressed. @@ -26,7 +29,7 @@ export class Fixer implements vscode.CodeActionProvider { // Throw out diagnostics that aren't ours, or are for the wrong line. .filter(diagnostic => messages.diagnostics.source && messages.diagnostics.source.isSource(diagnostic.source) && diagnostic.range.isEqual(range)) // Get and use the appropriate fix generator. - .map(diagnostic => this.getFixGenerator(document, diagnostic).generateFixes(processedLines)) + .map(diagnostic => this.getFixGenerator(document, diagnostic).generateFixes(processedLines, document, diagnostic)) // Combine all the fixes into one array. .reduce((acc, next) => [...acc, ...next], []); } @@ -45,6 +48,8 @@ export class Fixer implements vscode.CodeActionProvider { case 'pmd': case 'pmd-custom': return new _PmdFixGenerator(document, diagnostic); + case 'apexguru': + return new _ApexGuruFixGenerator(document, diagnostic); default: return new _NoOpFixGenerator(document, diagnostic); } @@ -73,7 +78,7 @@ abstract class FixGenerator { * Abstract template method for generating fixes. * @abstract */ - public abstract generateFixes(processedLines: Set): vscode.CodeAction[]; + public abstract generateFixes(processedLines: Set, document?: vscode.TextDocument, diagnostic?: vscode.Diagnostic): vscode.CodeAction[]; } /** @@ -87,6 +92,42 @@ export class _NoOpFixGenerator extends FixGenerator { } } +export class _ApexGuruFixGenerator extends FixGenerator { + /** + * Generate an array of fixes, if possible. + * @returns + */ + // eslint-disable-next-line @typescript-eslint/no-unused-vars + public generateFixes(processedLines: Set, document: vscode.TextDocument, diagnostic: vscode.Diagnostic): vscode.CodeAction[] { + console.log(diagnostic); + const fixes: vscode.CodeAction[] = []; + const lineNumber = this.diagnostic.range.start.line; + if (!processedLines.has(lineNumber)) { + fixes.push(this.generateApexGuruSuppresssion(document)) + processedLines.add(lineNumber); + } + return fixes; + } + + public generateApexGuruSuppresssion(document: vscode.TextDocument): vscode.CodeAction { + const existingCode = this.diagnostic.relatedInformation[0].message; + const suggestedCode = this.diagnostic.relatedInformation[1].message; + + const action = new vscode.CodeAction(messages.fixer.fixWithApexGuruSuggestions, vscode.CodeActionKind.QuickFix); + action.diagnostics = [this.diagnostic]; + + const edit = new vscode.WorkspaceEdit(); + const range = this.diagnostic.range; // Assuming the range is the location of the existing code in the document + edit.replace(document.uri, range, suggestedCode); + + // Assign the edit to the action + action.edit = edit; + + return action; + } + +} + /** * FixGenerator to be used for PMD and Custom PMD. * @private Must be exported for testing purposes, but shouldn't be used publicly, hence the leading underscore. diff --git a/src/types.ts b/src/types.ts index 589fb23..50ab2c0 100644 --- a/src/types.ts +++ b/src/types.ts @@ -35,6 +35,7 @@ export type DfaRuleViolation = BaseViolation & { export type ApexGuruViolation = BaseViolation & { line: number; column: number; + currentCode: string; suggestedCode: string; } From 0e564747cb10ecd2c60b303cec3a56c526f233db Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Wed, 14 Aug 2024 11:04:12 -0700 Subject: [PATCH 2/8] NEW (Extension) @W-16442046@ Apex guru fixer logic --- src/lib/fixer.ts | 3 +-- src/lib/messages.ts | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib/fixer.ts b/src/lib/fixer.ts index 6e51b98..1a2024f 100644 --- a/src/lib/fixer.ts +++ b/src/lib/fixer.ts @@ -110,7 +110,6 @@ export class _ApexGuruFixGenerator extends FixGenerator { } public generateApexGuruSuppresssion(document: vscode.TextDocument): vscode.CodeAction { - const existingCode = this.diagnostic.relatedInformation[0].message; const suggestedCode = this.diagnostic.relatedInformation[1].message; const action = new vscode.CodeAction(messages.fixer.fixWithApexGuruSuggestions, vscode.CodeActionKind.QuickFix); @@ -118,7 +117,7 @@ export class _ApexGuruFixGenerator extends FixGenerator { const edit = new vscode.WorkspaceEdit(); const range = this.diagnostic.range; // Assuming the range is the location of the existing code in the document - edit.replace(document.uri, range, suggestedCode); + edit.insert(document.uri, range.start, suggestedCode + '\n'); // Assign the edit to the action action.edit = edit; diff --git a/src/lib/messages.ts b/src/lib/messages.ts index b9968c5..36f9964 100644 --- a/src/lib/messages.ts +++ b/src/lib/messages.ts @@ -39,7 +39,7 @@ export const messages = { fixer: { supressOnLine: "Suppress violations on this line.", supressOnClass: "Suppress violations on this class.", - fixWithApexGuruSuggestions: "***Fix violations with suggestions from Apex Guru***" + fixWithApexGuruSuggestions: "***Insert code suggestions from Apex Guru***" }, diagnostics: { messageGenerator: (severity: number, message: string) => `Sev${severity}: ${message}`, From 97362ca4b563b822c500d4d15ec567462265ecd7 Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Wed, 14 Aug 2024 15:09:40 -0700 Subject: [PATCH 3/8] NEW (Extension) @W-16442046@ Apex guru fixer logic - part 3 --- src/apexguru/apex-guru-service.ts | 7 +- src/lib/diagnostics.ts | 8 +- src/lib/fixer.ts | 3 +- .../suite/apexguru/apex-guru-service.test.ts | 1 + src/test/suite/fixer.test.ts | 126 +++++++++++++++++- 5 files changed, 134 insertions(+), 11 deletions(-) diff --git a/src/apexguru/apex-guru-service.ts b/src/apexguru/apex-guru-service.ts index 1cd6fb2..5eb4b1b 100644 --- a/src/apexguru/apex-guru-service.ts +++ b/src/apexguru/apex-guru-service.ts @@ -128,15 +128,16 @@ 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 = { ruleName: parsed.type, message: parsed.value, severity: 1, category: parsed.type, // Replace with actual category if available - line: parseInt(parsed.properties.find((prop: ApexGuruProperty) => prop.name === 'line_number')?.value), + line: lineNumber, column: 1, currentCode: Buffer.from(encodedCodeBefore, 'base64').toString('utf8'), suggestedCode: Buffer.from(encodedCodeAfter, 'base64').toString('utf8'), diff --git a/src/lib/diagnostics.ts b/src/lib/diagnostics.ts index 61f0ff4..461c665 100644 --- a/src/lib/diagnostics.ts +++ b/src/lib/diagnostics.ts @@ -95,12 +95,12 @@ export class DiagnosticManager { if (apexGuruViolation.suggestedCode?.trim()) { diagnostic.relatedInformation = [ new vscode.DiagnosticRelatedInformation( - new vscode.Location(vscode.Uri.parse('Code Before'), range), - `Code before: ${apexGuruViolation.currentCode}` + new vscode.Location(vscode.Uri.parse('***Current Code***'), range), + `${apexGuruViolation.currentCode}` ), new vscode.DiagnosticRelatedInformation( - new vscode.Location(vscode.Uri.parse('Code After'), range), - `Code after: ${apexGuruViolation.suggestedCode}` + new vscode.Location(vscode.Uri.parse('***Apex Guru Suggestions***'), range), + `${apexGuruViolation.suggestedCode}` ) ]; } diff --git a/src/lib/fixer.ts b/src/lib/fixer.ts index 1a2024f..00ed8b4 100644 --- a/src/lib/fixer.ts +++ b/src/lib/fixer.ts @@ -117,7 +117,8 @@ export class _ApexGuruFixGenerator extends FixGenerator { const edit = new vscode.WorkspaceEdit(); const range = this.diagnostic.range; // Assuming the range is the location of the existing code in the document - edit.insert(document.uri, range.start, suggestedCode + '\n'); + const oneLineAbove = new vscode.Position(range.start.line > 1 ? range.start.line - 1 : 0, range.start.character); + edit.insert(document.uri, oneLineAbove, suggestedCode + '\n'); // Assign the edit to the action action.edit = edit; diff --git a/src/test/suite/apexguru/apex-guru-service.test.ts b/src/test/suite/apexguru/apex-guru-service.test.ts index 446131d..770274a 100644 --- a/src/test/suite/apexguru/apex-guru-service.test.ts +++ b/src/test/suite/apexguru/apex-guru-service.test.ts @@ -176,6 +176,7 @@ suite('Apex Guru Test Suite', () => { category: 'BestPractices', line: 10, column: 1, + currentCode: '', suggestedCode: 'System.out.println("Hello World");' }] }); diff --git a/src/test/suite/fixer.test.ts b/src/test/suite/fixer.test.ts index 0e22e42..7f72999 100644 --- a/src/test/suite/fixer.test.ts +++ b/src/test/suite/fixer.test.ts @@ -7,9 +7,7 @@ import * as vscode from 'vscode'; import {expect} from 'chai'; import path = require('path'); -import Sinon = require('sinon'); -import {messages} from '../../lib/messages'; -import {_NoOpFixGenerator, _PmdFixGenerator} from '../../lib/fixer'; +import {_NoOpFixGenerator, _PmdFixGenerator, _ApexGuruFixGenerator} from '../../lib/fixer'; suite('fixer.ts', () => { // Note: __dirname is used here because it's consistent across file systems. @@ -489,4 +487,126 @@ suite('fixer.ts', () => { }); }); }); + suite('_ApexGuruFixGenerator', () => { + const fileUri = vscode.Uri.file(path.join(codeFixturesPath, 'MyClass1.cls')); + suite('#generateFixes()', () => { + const processedLines = new Set(); + const fileUri = vscode.Uri.file(path.join(codeFixturesPath, 'MyClass1.cls')); + + let doc: vscode.TextDocument; + // Load the document and store its starting contents. + setup(async () => { + doc = await vscode.workspace.openTextDocument(fileUri); + await vscode.window.showTextDocument(doc); + }); + + test('Should generate a suppression fix if line is not processed', async () => { + // Create a fake diagnostic. + const diag = new vscode.Diagnostic( + new vscode.Range( + new vscode.Position(7, 4), + new vscode.Position(7, 10) + ), + 'some message', + vscode.DiagnosticSeverity.Warning + ); + diag.relatedInformation = [ + new vscode.DiagnosticRelatedInformation( + new vscode.Location(fileUri, new vscode.Position(0, 0)), + 'current code' + ), + new vscode.DiagnosticRelatedInformation( + new vscode.Location(fileUri, new vscode.Position(0, 0)), + 'apex guru suggested code' + ) + ]; + + // Instantiate the fixer. + const fixGenerator: _ApexGuruFixGenerator = new _ApexGuruFixGenerator(doc, diag); + + // Generate fixes. + const fixes: vscode.CodeAction[] = fixGenerator.generateFixes(processedLines, doc, diag); + + // Validate results. + expect(fixes).to.have.lengthOf(1, 'One fix should be offered'); + const fix = fixes[0].edit.get(fileUri)[0]; + expect(fix.newText).to.equal('apex guru suggested code\n', 'The suppression code should match the suggestion'); + expect(fix.range.start.line).to.equal(6, 'The suppression should be added one line above the diagnostic line'); + }); + + test('Should not generate a suppression fix if line is already processed', async () => { + processedLines.add(7); + + // Create a fake diagnostic. + const diag = new vscode.Diagnostic( + new vscode.Range( + new vscode.Position(7, 4), + new vscode.Position(7, 10) + ), + 'This message is unimportant', + vscode.DiagnosticSeverity.Warning + ); + diag.relatedInformation = [ + new vscode.DiagnosticRelatedInformation( + new vscode.Location(fileUri, new vscode.Position(0, 0)), + 'current code' + ), + new vscode.DiagnosticRelatedInformation( + new vscode.Location(fileUri, new vscode.Position(0, 0)), + 'apex guru suggested code' + ) + ]; + + // Instantiate the fixer. + const fixGenerator: _ApexGuruFixGenerator = new _ApexGuruFixGenerator(doc, diag); + + // Generate fixes. + const fixes: vscode.CodeAction[] = fixGenerator.generateFixes(processedLines, doc, diag); + + // Validate results. + expect(fixes).to.have.lengthOf(0, 'No fix should be offered if the line is already processed'); + }); + }); + + suite('#generateApexGuruSuppresssion()', () => { + let doc: vscode.TextDocument; + // Load the document and store its starting contents. + setup(async () => { + doc = await vscode.workspace.openTextDocument(fileUri); + await vscode.window.showTextDocument(doc); + }); + + test('Should generate the correct ApexGuru suppression code action', async () => { + // Create a fake diagnostic. + const diag = new vscode.Diagnostic( + new vscode.Range( + new vscode.Position(7, 4), + new vscode.Position(7, 10) + ), + 'Some message', + vscode.DiagnosticSeverity.Warning + ); + diag.relatedInformation = [ + new vscode.DiagnosticRelatedInformation( + new vscode.Location(fileUri, new vscode.Position(0, 0)), + 'Some other information' + ), + new vscode.DiagnosticRelatedInformation( + new vscode.Location(fileUri, new vscode.Position(0, 0)), + 'apex guru suggested code' + ) + ]; + + // Instantiate the fixer. + const fixGenerator: _ApexGuruFixGenerator = new _ApexGuruFixGenerator(doc, diag); + + // Generate the suppression code action. + const fix = fixGenerator.generateApexGuruSuppresssion(doc); + + // Validate results. + expect(fix.edit.get(fileUri)[0].newText).to.equal('apex guru suggested code\n', 'The suppression code should match the suggestion'); + expect(fix.edit.get(fileUri)[0].range.start.line).to.equal(6, 'The suppression should be added one line above the diagnostic line'); + }); + }); + }); }); From fa4a36c12fb29285ca0d4f00c96588e0954cd49b Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Wed, 14 Aug 2024 15:11:31 -0700 Subject: [PATCH 4/8] NEW (Extension) @W-16442046@ Remove accidentally committed file --- .vscode/settings.json | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8d3f7e4..30bf8c2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -7,9 +7,5 @@ "out": true // set this to false to include "out" folder in search results }, // Turn off tsc task auto detection since we have the necessary tasks as npm scripts - "typescript.tsc.autoDetect": "off", - "workbench.colorCustomizations": { - "diffEditor.insertedTextBackground": "#520dd144", // Green with some transparency - "diffEditor.removedTextBackground": "#ff000044" // Red with some transparency - } + "typescript.tsc.autoDetect": "off" } \ No newline at end of file From 622cb8e8204d5337427fd96a50d1022d03916f11 Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Wed, 14 Aug 2024 15:22:21 -0700 Subject: [PATCH 5/8] NEW (Extension) @W-16442046@ Remove unused command and imports --- src/extension.ts | 9 +-------- src/lib/constants.ts | 2 +- src/lib/fixer.ts | 3 --- 3 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/extension.ts b/src/extension.ts index fb078f2..9fd6174 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -150,14 +150,7 @@ export async function activate(context: vscode.ExtensionContext): Promise { - await vscode.workspace.openTextDocument(documentUri); - const edit = new vscode.WorkspaceEdit(); - edit.replace(documentUri, range, suggestedCode); - await vscode.workspace.applyEdit(edit); - }); - - context.subscriptions.push(runApexGuruOnSelectedFile, apexGuruReplaceCode); + context.subscriptions.push(runApexGuruOnSelectedFile); } TelemetryService.sendExtensionActivationEvent(extensionHrStart); diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 40f01fe..cc05793 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -37,6 +37,6 @@ export const APEX_GURU_AUTH_ENDPOINT = '/services/data/v62.0/apexguru/validate' export const APEX_GURU_REQUEST = '/services/data/v62.0/apexguru/request' // feature gates -export const APEX_GURU_FEATURE_FLAG_ENABLED = true; +export const APEX_GURU_FEATURE_FLAG_ENABLED = false; export const APEX_GURU_MAX_TIMEOUT_SECONDS = 60; export const APEX_GURU_RETRY_INTERVAL_MILLIS = 1000; diff --git a/src/lib/fixer.ts b/src/lib/fixer.ts index 00ed8b4..08cd5bd 100644 --- a/src/lib/fixer.ts +++ b/src/lib/fixer.ts @@ -5,11 +5,8 @@ * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ import * as vscode from 'vscode'; -import * as path from 'path'; import {messages} from './messages'; import * as Constants from './constants'; -import * as fs from 'fs'; -import * as os from 'os'; /** * Class for creating and adding {@link vscode.CodeAction}s allowing violations to be fixed or suppressed. From 70890fc10ef3e43dd121d15ce364890244801da1 Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Thu, 15 Aug 2024 10:34:06 -0700 Subject: [PATCH 6/8] NEW (Extension) @W-16442046@ Include doc reviews command name + messages --- package.json | 2 +- src/apexguru/apex-guru-service.ts | 5 ++--- src/lib/diagnostics.ts | 4 ++-- src/lib/messages.ts | 4 ++-- 4 files changed, 7 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 0a3ddd1..9e26887 100644 --- a/package.json +++ b/package.json @@ -105,7 +105,7 @@ }, { "command": "sfca.runApexGuruAnalysisOnSelectedFile", - "title": "***SFDX: Scan for Performance Issues with ApexGuru***" + "title": "SFDX: Scan for Performance Issues with ApexGuru on Selected File" } ], "configuration": { diff --git a/src/apexguru/apex-guru-service.ts b/src/apexguru/apex-guru-service.ts index 5eb4b1b..de717da 100644 --- a/src/apexguru/apex-guru-service.ts +++ b/src/apexguru/apex-guru-service.ts @@ -47,12 +47,11 @@ export async function runApexGuruOnFile(selection: vscode.Uri, runInfo: RunInfo) progress.report(messages.apexGuru.progress); const connection = await CoreExtensionService.getConnection(); const requestId = await initiateApexGuruRequest(selection, outputChannel, connection); - outputChannel.appendLine('***Apex Guru request Id:***' + requestId); + outputChannel.appendLine('Code Analyzer with ApexGuru request Id:' + requestId); const queryResponse: ApexGuruQueryResponse = await pollAndGetApexGuruResponse(connection, requestId, Constants.APEX_GURU_MAX_TIMEOUT_SECONDS, Constants.APEX_GURU_RETRY_INTERVAL_MILLIS); const decodedReport = Buffer.from(queryResponse.report, 'base64').toString('utf8'); - outputChannel.appendLine('***Retrieved analysis report from ApexGuru***:' + decodedReport); const ruleResult = transformStringToRuleResult(selection.fsPath, decodedReport); new DiagnosticManager().displayDiagnostics([selection.fsPath], [ruleResult], diagnosticCollection); @@ -63,7 +62,7 @@ export async function runApexGuruOnFile(selection: vscode.Uri, runInfo: RunInfo) }); } catch (e) { const errMsg = e instanceof Error ? e.message : e as string; - outputChannel.appendLine('***Apex Guru initiate request failed***'); + outputChannel.error('Initial Code Analyzer with ApexGuru request failed.'); outputChannel.appendLine(errMsg); } } diff --git a/src/lib/diagnostics.ts b/src/lib/diagnostics.ts index 461c665..f0f0535 100644 --- a/src/lib/diagnostics.ts +++ b/src/lib/diagnostics.ts @@ -95,11 +95,11 @@ export class DiagnosticManager { if (apexGuruViolation.suggestedCode?.trim()) { diagnostic.relatedInformation = [ new vscode.DiagnosticRelatedInformation( - new vscode.Location(vscode.Uri.parse('***Current Code***'), range), + new vscode.Location(vscode.Uri.parse('Current Code'), range), `${apexGuruViolation.currentCode}` ), new vscode.DiagnosticRelatedInformation( - new vscode.Location(vscode.Uri.parse('***Apex Guru Suggestions***'), range), + new vscode.Location(vscode.Uri.parse('ApexGuru Suggestions'), range), `${apexGuruViolation.suggestedCode}` ) ]; diff --git a/src/lib/messages.ts b/src/lib/messages.ts index 36f9964..413e492 100644 --- a/src/lib/messages.ts +++ b/src/lib/messages.ts @@ -21,7 +21,7 @@ export const messages = { }, apexGuru: { progress: { - message: "Code Analyzer running ApexGuru analysis." + message: "Code Analyzer is running ApexGuru analysis." } }, info: { @@ -39,7 +39,7 @@ export const messages = { fixer: { supressOnLine: "Suppress violations on this line.", supressOnClass: "Suppress violations on this class.", - fixWithApexGuruSuggestions: "***Insert code suggestions from Apex Guru***" + fixWithApexGuruSuggestions: "Insert ApexGuru suggestions." }, diagnostics: { messageGenerator: (severity: number, message: string) => `Sev${severity}: ${message}`, From 3f1c22ad193bcc528c45655e5b704d3a17d438ae Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Thu, 15 Aug 2024 16:27:06 -0700 Subject: [PATCH 7/8] NEW (Extension) @W-16442046@ Review comments --- src/apexguru/apex-guru-service.ts | 6 +++--- src/lib/diagnostics.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/apexguru/apex-guru-service.ts b/src/apexguru/apex-guru-service.ts index f5aaffd..b8bea13 100644 --- a/src/apexguru/apex-guru-service.ts +++ b/src/apexguru/apex-guru-service.ts @@ -27,7 +27,7 @@ export async function isApexGuruEnabledInOrg(outputChannel: vscode.LogOutputChan // This could throw an error for a variety of reasons. The API endpoint has not been deployed to the instance, org has no perms, timeouts etc,. // In all of these scenarios, we return false. const errMsg = e instanceof Error ? e.message : e as string; - outputChannel.error('***ApexGuru perm check failed with error:***' + errMsg); + outputChannel.error('Apex Guru perm check failed with error:' + errMsg); outputChannel.show(); return false; } @@ -106,8 +106,8 @@ export async function initiateApexGuruRequest(selection: vscode.Uri, outputChann }); if (response.status != 'new' && response.status != 'success') { - outputChannel.warn('***Apex Guru returned unexpected response:***' + response.status); - throw Error('***Apex Guru returned unexpected response:***' + response.status); + outputChannel.warn('Code Analyzer with Apex Guru returned unexpected response:' + response.status); + throw Error('Code Analyzer with Apex Guru returned unexpected response:' + response.status); } const requestId = response.requestId; diff --git a/src/lib/diagnostics.ts b/src/lib/diagnostics.ts index f0f0535..0af8611 100644 --- a/src/lib/diagnostics.ts +++ b/src/lib/diagnostics.ts @@ -92,7 +92,7 @@ export class DiagnosticManager { if (engine === 'apexguru') { const apexGuruViolation = violation as ApexGuruViolation; - if (apexGuruViolation.suggestedCode?.trim()) { + if (apexGuruViolation.suggestedCode) { diagnostic.relatedInformation = [ new vscode.DiagnosticRelatedInformation( new vscode.Location(vscode.Uri.parse('Current Code'), range), From d5bf863d0e081282b250c43c925773bc5f7c89c6 Mon Sep 17 00:00:00 2001 From: Jag Jayaprakash Date: Fri, 16 Aug 2024 14:00:23 -0700 Subject: [PATCH 8/8] NEW (Extension) @W-16442046@ Address review comments --- src/apexguru/apex-guru-service.ts | 8 ++++---- src/lib/diagnostics.ts | 2 +- src/test/suite/apexguru/apex-guru-service.test.ts | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/apexguru/apex-guru-service.ts b/src/apexguru/apex-guru-service.ts index b8bea13..e4fb791 100644 --- a/src/apexguru/apex-guru-service.ts +++ b/src/apexguru/apex-guru-service.ts @@ -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 = { @@ -139,8 +139,8 @@ export function transformStringToRuleResult(fileName: string, jsonString: string category: parsed.type, // Replace with actual category if available line: lineNumber, column: 1, - currentCode: Buffer.from(encodedCodeBefore, 'base64').toString('utf8'), - suggestedCode: Buffer.from(encodedCodeAfter, 'base64').toString('utf8'), + currentCode: encodedCodeBefore ? Buffer.from(encodedCodeBefore, 'base64').toString('utf8') : encodedCodeBefore, + suggestedCode: encodedCodeAfter ? Buffer.from(encodedCodeAfter, 'base64').toString('utf8') : encodedCodeAfter, }; ruleResult.violations.push(violation); diff --git a/src/lib/diagnostics.ts b/src/lib/diagnostics.ts index 0af8611..ab033ff 100644 --- a/src/lib/diagnostics.ts +++ b/src/lib/diagnostics.ts @@ -92,7 +92,7 @@ export class DiagnosticManager { if (engine === 'apexguru') { const apexGuruViolation = violation as ApexGuruViolation; - if (apexGuruViolation.suggestedCode) { + if (apexGuruViolation.suggestedCode !== undefined) { diagnostic.relatedInformation = [ new vscode.DiagnosticRelatedInformation( new vscode.Location(vscode.Uri.parse('Current Code'), range), diff --git a/src/test/suite/apexguru/apex-guru-service.test.ts b/src/test/suite/apexguru/apex-guru-service.test.ts index 770274a..c83471b 100644 --- a/src/test/suite/apexguru/apex-guru-service.test.ts +++ b/src/test/suite/apexguru/apex-guru-service.test.ts @@ -176,7 +176,7 @@ suite('Apex Guru Test Suite', () => { category: 'BestPractices', line: 10, column: 1, - currentCode: '', + currentCode: undefined, suggestedCode: 'System.out.println("Hello World");' }] });