Skip to content

Commit 781d1fd

Browse files
committed
refactor: helper function getQuery() to remove duplicate code in queryUtils.ts
1 parent 979cce9 commit 781d1fd

1 file changed

Lines changed: 12 additions & 15 deletions

File tree

packages/salesforcedx-vscode-soql/src/commands/queryUtils.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ const pickApiForQuery = async (
3434
return selection ? { type: 'CONTINUE', data: { query, api: selection.api } } : { type: 'CANCEL' };
3535
};
3636

37+
const getQuery = (useSelection: boolean): string | undefined => {
38+
const editor = vscode.window.activeTextEditor;
39+
if (!editor || (useSelection && editor.selection.isEmpty)) {
40+
return undefined;
41+
}
42+
const text = useSelection ? editor.document.getText(editor.selection) : editor.document.getText();
43+
return normalizeQuery(text) || undefined;
44+
};
45+
3746
export class GetQueryAndApiInputs implements ParametersGatherer<QueryAndApiInputs> {
3847
public async gather(): Promise<CancelResponse | ContinueResponse<QueryAndApiInputs>> {
3948
const editor = vscode.window.activeTextEditor;
@@ -50,33 +59,21 @@ export class GetQueryAndApiInputs implements ParametersGatherer<QueryAndApiInput
5059

5160
export class GetDocumentQueryAndApiInputs implements ParametersGatherer<QueryAndApiInputs> {
5261
public async gather(): Promise<CancelResponse | ContinueResponse<QueryAndApiInputs>> {
53-
const editor = vscode.window.activeTextEditor;
54-
if (!editor) {
55-
return { type: 'CANCEL' };
56-
}
57-
const query = normalizeQuery(editor.document.getText());
62+
const query = getQuery(false);
5863
return query ? pickApiForQuery(query) : { type: 'CANCEL' };
5964
}
6065
}
6166

6267
export class GetQueryInputsForPlan implements ParametersGatherer<QueryInputs> {
6368
public async gather(): Promise<CancelResponse | ContinueResponse<QueryInputs>> {
64-
const editor = vscode.window.activeTextEditor;
65-
if (!editor || editor.selection.isEmpty) {
66-
return { type: 'CANCEL' };
67-
}
68-
const query = normalizeQuery(editor.document.getText(editor.selection));
69+
const query = getQuery(true);
6970
return query ? { type: 'CONTINUE', data: { query } } : { type: 'CANCEL' };
7071
}
7172
}
7273

7374
export class GetDocumentQueryInputsForPlan implements ParametersGatherer<QueryInputs> {
7475
public async gather(): Promise<CancelResponse | ContinueResponse<QueryInputs>> {
75-
const editor = vscode.window.activeTextEditor;
76-
if (!editor) {
77-
return { type: 'CANCEL' };
78-
}
79-
const query = normalizeQuery(editor.document.getText());
76+
const query = getQuery(false);
8077
return query ? { type: 'CONTINUE', data: { query } } : { type: 'CANCEL' };
8178
}
8279
}

0 commit comments

Comments
 (0)