Skip to content

Commit

Permalink
chore: more optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
peternhale committed Jan 3, 2024
1 parent 29ff1ea commit b3901f5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 133 deletions.
39 changes: 22 additions & 17 deletions packages/salesforcedx-vscode-apex/src/codecoverage/colorizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export class CodeCoverageHandler {

public onDidChangeActiveTextEditor(editor?: TextEditor) {
if (editor && this.statusBar.isHighlightingEnabled) {
const coverage = colorizer(editor);
const coverage = applyCoverageToSource(editor.document);
this.coveredLines = coverage.coveredLines;
this.uncoveredLines = coverage.uncoveredLines;
}
Expand All @@ -132,8 +132,8 @@ export class CodeCoverageHandler {
}
} else {
try {
if (editor) {
const coverage = colorizer(window.activeTextEditor);
if (editor?.document) {
const coverage = applyCoverageToSource(editor.document);
this.coveredLines = coverage.coveredLines;
this.uncoveredLines = coverage.uncoveredLines;
this.setCoverageDecorators(editor);
Expand All @@ -147,27 +147,27 @@ export class CodeCoverageHandler {
}

private setCoverageDecorators(editor: TextEditor) {
editor.setDecorations(coveredLinesDecorationType, []);
editor.setDecorations(uncoveredLinesDecorationType, []);
editor.setDecorations(coveredLinesDecorationType, this.coveredLines);
editor.setDecorations(uncoveredLinesDecorationType, this.uncoveredLines);
}
}

export const colorizer = (
editor?: TextEditor
const applyCoverageToSource = (
document?: TextDocument
): {
coveredLines: Range[];
uncoveredLines: Range[];
} => {
let coveredLines = Array<Range>();
let uncoveredLines = Array<Range>();
if (
editor &&
!editor.document.uri.fsPath.includes(SFDX_FOLDER) &&
isApexMetadata(editor.document.uri.fsPath) &&
!IS_TEST_REG_EXP.test(editor.document.getText())
document &&
!document.uri.fsPath.includes(SFDX_FOLDER) &&
isApexMetadata(document.uri.fsPath) &&
!IS_TEST_REG_EXP.test(document.getText())
) {
const codeCovArray = getCoverageData() as { name: string }[];
const apexMemberName = getApexMemberName(editor.document.uri.fsPath);
const apexMemberName = getApexMemberName(document.uri.fsPath);
const codeCovItem = codeCovArray.find(
covItem => covItem.name === apexMemberName
);
Expand All @@ -176,7 +176,7 @@ export const colorizer = (
channelService.appendLine(
nls.localize(
'colorizer_no_code_coverage_current_file',
editor.document.uri.fsPath
document.uri.fsPath
)
);
return { coveredLines: [], uncoveredLines: [] };
Expand All @@ -189,20 +189,25 @@ export const colorizer = (
const covItem = codeCovItem as CoverageItem;
for (const key in covItem.lines) {
if (covItem.lines[key] === 1) {
coveredLines.push(getLineRange(editor.document, Number(key)));
coveredLines.push(getLineRange(document, Number(key)));
} else {
uncoveredLines.push(getLineRange(editor.document, Number(key)));
uncoveredLines.push(getLineRange(document, Number(key)));
}
}
} else {
const covResult = codeCovItem as CodeCoverageResult;
coveredLines = covResult.coveredLines.map(cov =>
getLineRange(editor.document, Number(cov))
getLineRange(document, Number(cov))
);
uncoveredLines = covResult.uncoveredLines.map(uncov =>
getLineRange(editor.document, Number(uncov))
getLineRange(document, Number(uncov))
);
}
}
return { coveredLines, uncoveredLines };
};

// export is for testing
export const colorizer = {
applyCoverageToSource
};
34 changes: 20 additions & 14 deletions packages/salesforcedx-vscode-apex/src/commands/forceApexTestRun.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import {
workspace,
window,
CancellationToken,
QuickPickItem
QuickPickItem,
Uri
} from 'vscode';
import { channelService, OUTPUT_CHANNEL } from '../channels';
import {
Expand Down Expand Up @@ -64,12 +65,23 @@ export class TestsSelector
public async gather(): Promise<
CancelResponse | ContinueResponse<ApexTestQuickPickItem>
> {
const foundFiles = await workspace.findFiles(
`{**/*${APEX_TESTSUITE_EXT},**/*${APEX_CLASS_EXT}}`,
SFDX_FOLDER
);
const testSuites = foundFiles.filter(file =>
file.path.endsWith('.testSuite-meta.xml')
const { testSuites, apexClasses } = (
await workspace.findFiles(
`{**/*${APEX_TESTSUITE_EXT},**/*${APEX_CLASS_EXT}}`,
SFDX_FOLDER
)
)
.sort((a, b) => a.fsPath.localeCompare(b.fsPath))
.reduce(
(acc: { testSuites: Uri[]; apexClasses: Uri[] }, file) => {
if (file.path.endsWith('.cls')) {
acc.apexClasses.push(file);
} else {
acc.testSuites.push(file);
}
return acc;
},
{ testSuites: [], apexClasses: [] }
);

const fileItems = testSuites
Expand All @@ -79,8 +91,7 @@ export class TestsSelector
description: testSuite.fsPath,
type: TestType.Suite
};
})
.sort((a, b) => a.label.localeCompare(b.label));
});

fileItems.push({
label: nls.localize('force_apex_test_run_all_local_test_label'),
Expand All @@ -98,10 +109,6 @@ export class TestsSelector
type: TestType.All
});

const apexClasses = foundFiles.filter(file =>
file.path.endsWith(APEX_CLASS_EXT)
);

fileItems.push(
...apexClasses
.filter(apexClass => {
Expand All @@ -115,7 +122,6 @@ export class TestsSelector
type: TestType.Class
};
})
.sort((a, b) => a.label.localeCompare(b.label))
);

const selection = (await window.showQuickPick(
Expand Down

This file was deleted.

0 comments on commit b3901f5

Please sign in to comment.