Skip to content

Commit a33f0fb

Browse files
committed
feat: Evaluate variable in the path settings
1 parent 33cf502 commit a33f0fb

File tree

9 files changed

+130
-29
lines changed

9 files changed

+130
-29
lines changed

clients/cobol-lsp-vscode-extension/src/__tests__/SettingsTest.spec.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
import * as fs from "fs-extra";
1515
import * as path from "path";
1616
import * as vscode from "vscode";
17-
import {C4Z_FOLDER, GITIGNORE_FILE} from "../constants";
18-
import {createFileWithGivenPath} from "../services/Settings";
17+
import {C4Z_FOLDER, GITIGNORE_FILE, PATHS_LOCAL_KEY, SETTINGS_CPY_SECTION} from "../constants";
18+
import {createFileWithGivenPath, SettingsService} from "../services/Settings";
1919
import {SettingsUtils} from "../services/util/SettingsUtils";
2020

2121
const fsPath = "tmp-ws";
@@ -74,3 +74,13 @@ describe("Validate URI generation for a given workspace folder", () => {
7474
expect(SettingsUtils.getWorkspacesURI()[0]).toBe("file:///ws-vscode");
7575
});
7676
});
77+
78+
describe("SettingsService evaluate variables", () => {
79+
test("Evaluate program_name", () => {
80+
vscode.workspace.getConfiguration = jest.fn().mockReturnValue({
81+
get: jest.fn().mockReturnValue(["copybook/$program_name"]),
82+
});
83+
const paths = SettingsService.getCopybookLocalPath("PROGRAM");
84+
expect(paths[0]).toEqual("copybook/PROGRAM")
85+
});
86+
});

clients/cobol-lsp-vscode-extension/src/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
export const SETTINGS_CPY_SECTION: string = "cobol-lsp.cpy-manager";
1515

16+
export const SERVER_PORT = "cobol-lsp.server.port"
1617
export const PATHS_LOCAL_KEY = "paths-local";
1718
export const PATHS_ZOWE = "paths-dsn";
1819
export const PATHS_USS = "paths-uss";

clients/cobol-lsp-vscode-extension/src/services/LanguageClientService.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import * as fs from "fs";
1616
import * as net from "net";
1717
import * as vscode from "vscode";
18-
import { Location } from "vscode";
1918
import {
2019
ConfigurationParams,
2120
ConfigurationRequest,
@@ -28,6 +27,7 @@ import {LANGUAGE_ID} from "../constants";
2827
import {JavaCheck} from "./JavaCheck";
2928
import {Middleware} from "./Middleware";
3029
import {GenericNotificationHandler, GenericRequestHandler} from "vscode-languageserver-protocol";
30+
import { SettingsService } from "./Settings";
3131

3232
export class LanguageClientService {
3333
private readonly jarPath: string;
@@ -41,7 +41,7 @@ export class LanguageClientService {
4141

4242
public async checkPrerequisites(): Promise<void> {
4343
await new JavaCheck().isJavaInstalled();
44-
if (!this.getLspPort() && !fs.existsSync(this.jarPath)) {
44+
if (!SettingsService.getLspPort() && !fs.existsSync(this.jarPath)) {
4545
throw new Error("LSP server for " + LANGUAGE_ID + " not found");
4646
}
4747
}
@@ -88,10 +88,6 @@ export class LanguageClientService {
8888
return this.languageClient;
8989
}
9090

91-
private getLspPort(): number | undefined {
92-
return +vscode.workspace.getConfiguration().get("cobol-lsp.server.port");
93-
}
94-
9591
private createClientOptions(): LanguageClientOptions {
9692
const signatureFunc: ConfigurationRequest.MiddlewareSignature = async (
9793
params: ConfigurationParams,
@@ -110,7 +106,7 @@ export class LanguageClientService {
110106
}
111107

112108
private createServerOptions(jarPath: string) {
113-
const port = this.getLspPort();
109+
const port = SettingsService.getLspPort();
114110
if (port) {
115111
// Connect to language server via socket
116112
const connectionInfo = {

clients/cobol-lsp-vscode-extension/src/services/PathsService.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,22 @@ import {
1818
DSN_MUSTBE_NOT_EMPTY,
1919
DSN_NOMORE_8CHARS,
2020
DSN_START_PROHIBITED_CHAR,
21-
PATHS_ZOWE,
2221
SEGMENT_PLACEHOLDER,
23-
SETTINGS_CPY_SECTION
2422
} from "../constants";
23+
import { SettingsService } from "./Settings";
2524

2625
export class PathsService {
2726

2827
async listPathDatasets(): Promise<string[]> {
29-
if (!vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).has(PATHS_ZOWE)) {
28+
if (!SettingsService.hasDsnPath()) {
3029
await vscode.window.showErrorMessage("Please, specify DATASET paths for copybooks in settings.");
3130
return [];
3231
}
33-
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_ZOWE);
32+
return SettingsService.getDsnPath();
3433
}
3534

3635
setPathDatasets(paths: string[]) {
37-
vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).update(PATHS_ZOWE, paths);
36+
SettingsService.setDsnPath(paths);
3837
}
3938
}
4039

clients/cobol-lsp-vscode-extension/src/services/Settings.ts

+95-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import * as fs from "fs";
1616
import * as path from "path";
1717
import * as vscode from "vscode";
18-
import { SETTINGS_CPY_SECTION } from "../constants";
18+
import { PATHS_LOCAL_KEY, PATHS_USS, PATHS_ZOWE, SERVER_PORT, SETTINGS_CPY_SECTION, SETTINGS_SUBROUTINE_LOCAL_KEY } from "../constants";
1919

2020
/**
2121
* New file (e.g .gitignore) will be created or edited if exits, under project folder
@@ -49,3 +49,97 @@ export function createFileWithGivenPath(folderPath: string, fileName: string, pa
4949
}
5050

5151
}
52+
53+
/**
54+
* SettingsService provides read/write configurstion settings functionality
55+
*/
56+
export class SettingsService {
57+
/**
58+
* Get list of local subroutine path
59+
* @returns a list of local subroutine path
60+
*/
61+
public static getSubroutineLocalPath(): string[] {
62+
return vscode.workspace.getConfiguration().get(SETTINGS_SUBROUTINE_LOCAL_KEY);
63+
}
64+
65+
/**
66+
* Get copybook local path based on program file name
67+
* @param cobolProgramName is a program file name
68+
* @returns a list of local path
69+
*/
70+
public static getCopybookLocalPath(cobolProgramName: string): string[] {
71+
const pathList: string[] = vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_LOCAL_KEY);
72+
return SettingsService.evaluateVariable(pathList, "program_name", cobolProgramName);
73+
}
74+
75+
/**
76+
* Get Lsp Port from configuration
77+
* @returns lsp port number
78+
*/
79+
public static getLspPort(): number | undefined {
80+
return +vscode.workspace.getConfiguration().get(SERVER_PORT);
81+
}
82+
83+
/**
84+
* Determine if dsn path exists in the configurstion
85+
* @returns true if path exists and false otherwise
86+
*/
87+
public static hasDsnPath(): boolean {
88+
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).has(PATHS_ZOWE)
89+
}
90+
91+
/**
92+
* Get list of dsn path
93+
* @returns a list of dsn path
94+
*/
95+
public static getDsnPath(): string[] {
96+
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_ZOWE);
97+
}
98+
99+
/**
100+
* Set list of dsn path to the configuration
101+
* @param paths is a list of new values
102+
*/
103+
public static setDsnPath(paths: string[]) {
104+
vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).update(PATHS_ZOWE, paths);
105+
}
106+
107+
/**
108+
* Determine if uss path exists in the configurstion
109+
* @returns true if path exists and false otherwise
110+
*/
111+
public static hasUssPath(): boolean {
112+
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).has(PATHS_USS)
113+
}
114+
115+
/**
116+
* Get list of uss path
117+
* @returns a list of uss path
118+
*/
119+
public static getUssPath(): string[] {
120+
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_USS)
121+
}
122+
123+
/**
124+
* Get profile name
125+
* @returns a profile name
126+
*/
127+
public static getProfileName(): string {
128+
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get("profiles")
129+
}
130+
131+
/**
132+
* Determine if confuguration is invalid
133+
* @returns true if configurstion is invalid and false otherwise
134+
*/
135+
public static isConfigurationInvalid() {
136+
return !SettingsService.hasDsnPath() &&
137+
!SettingsService.hasUssPath();
138+
}
139+
140+
private static evaluateVariable(dataList: string[], variable: string, value: string): string[] {
141+
const result: string[] = [];
142+
dataList.forEach(d => result.push(d.replace("$" + variable, value)))
143+
return result;
144+
}
145+
}

clients/cobol-lsp-vscode-extension/src/services/copybook/CopybookURI.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
* Contributors:
1212
* Broadcom, Inc. - initial API and implementation
1313
*/
14-
import * as vscode from "vscode";
15-
import { C4Z_FOLDER, COPYBOOK_EXT_ARRAY, COPYBOOKS_FOLDER, PATHS_LOCAL_KEY, PATHS_USS, PATHS_ZOWE, SETTINGS_CPY_SECTION } from "../../constants";
14+
import { C4Z_FOLDER, COPYBOOK_EXT_ARRAY, COPYBOOKS_FOLDER } from "../../constants";
15+
import { SettingsService } from "../Settings";
1616
import { searchInWorkspace } from "../util/FSUtils";
1717
import { ProfileUtils } from "../util/ProfileUtils";
1818

@@ -34,7 +34,7 @@ export class CopybookURI {
3434
// check on local paths provided by the user
3535
let result: string;
3636
result = searchInWorkspace(copybookName,
37-
vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_LOCAL_KEY),
37+
SettingsService.getCopybookLocalPath(cobolProgramName),
3838
COPYBOOK_EXT_ARRAY);
3939
// check in subfolders under .copybooks (copybook downloaded from MF)
4040
if (!result) {
@@ -54,14 +54,14 @@ export class CopybookURI {
5454
*/
5555
private createPathForCopybookDownloaded(profile: string): string[] {
5656
let result: string[] = [];
57-
const datasets: string[] = vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_ZOWE);
57+
const datasets: string[] = SettingsService.getDsnPath();
5858
if (profile && datasets) {
5959
result = Object.assign([], datasets);
6060
result.forEach((value, index) => result[index] = C4Z_FOLDER + "/" + COPYBOOKS_FOLDER + "/" +
6161
profile + "/" + value);
6262
}
6363

64-
const ussPaths: string[] = vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_USS);
64+
const ussPaths: string[] = SettingsService.getUssPath();
6565
const baseIndex = result.length;
6666
if (profile && ussPaths) {
6767
Object.assign([], ussPaths).forEach((value, index) => result[index + baseIndex] = C4Z_FOLDER + "/" + COPYBOOKS_FOLDER + "/" +

clients/cobol-lsp-vscode-extension/src/services/copybook/CopybooksPathGenerator.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@
1414

1515
import * as path from "path";
1616
import * as vscode from "vscode";
17-
import { C4Z_FOLDER, COPYBOOKS_FOLDER, PATHS_USS, PATHS_ZOWE, SETTINGS_CPY_SECTION } from "../../constants";
17+
import { C4Z_FOLDER, COPYBOOKS_FOLDER } from "../../constants";
18+
import { SettingsService } from "../Settings";
1819

1920
export class CopybooksPathGenerator {
2021

2122
public async listUris(): Promise<vscode.Uri[]> {
2223
const result: vscode.Uri[] = [];
23-
const profile: string = vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get("profiles");
24+
const profile: string = SettingsService.getProfileName();
2425
if (profile) {
2526
for (const dataset of await this.listDatasets()) {
2627
const uri: vscode.Uri = vscode.Uri.file(createDatasetPath(profile, dataset));
@@ -31,11 +32,11 @@ export class CopybooksPathGenerator {
3132
}
3233

3334
public async listDatasets(): Promise<string[]> {
34-
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_ZOWE);
35+
return SettingsService.getDsnPath();
3536
}
3637

3738
public async listUSSPaths(): Promise<string[]> {
38-
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get(PATHS_USS);
39+
return SettingsService.getUssPath();
3940
}
4041
}
4142

clients/cobol-lsp-vscode-extension/src/services/util/ProfileUtils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ZoweVsCodeExtension } from "@zowe/zowe-explorer-api/lib/vscode";
22
import * as path from "path";
33
import * as vscode from "vscode";
4-
import { SETTINGS_CPY_SECTION } from "../../constants";
4+
import { SettingsService } from "../Settings";
55

66
export class ProfileUtils {
77
public static getProfileNameForCopybook(cobolFileName: string): (string | undefined) {
@@ -13,7 +13,7 @@ export class ProfileUtils {
1313
private static getValidProfileForCopybookDownload(programName: string, availableProfiles: string[]): string {
1414
const profileFromDoc = ProfileUtils.getProfileFromDocument(programName, availableProfiles);
1515
if (!profileFromDoc) {
16-
return vscode.workspace.getConfiguration(SETTINGS_CPY_SECTION).get("profiles");
16+
return SettingsService.getProfileName();
1717
}
1818
return profileFromDoc;
1919
}

clients/cobol-lsp-vscode-extension/src/services/util/SubroutineUtils.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
* Broadcom, Inc. - initial API and implementation
1313
*/
1414

15-
import * as vscode from "vscode";
1615
import {searchInWorkspace} from "./FSUtils";
17-
import {COBOL_EXT_ARRAY, SETTINGS_SUBROUTINE_LOCAL_KEY} from "../../constants";
16+
import {COBOL_EXT_ARRAY} from "../../constants";
17+
import { SettingsService } from "../Settings";
1818

1919
/**
2020
* This function try to resolve a given subroutine by searching COBOL source file with the same name
@@ -23,6 +23,6 @@ import {COBOL_EXT_ARRAY, SETTINGS_SUBROUTINE_LOCAL_KEY} from "../../constants";
2323
* @return subroutine file URI if it was found or undefined otherwise
2424
*/
2525
export function resolveSubroutineURI(name: string): string {
26-
const folders: string[] = vscode.workspace.getConfiguration().get(SETTINGS_SUBROUTINE_LOCAL_KEY);
26+
const folders: string[] = SettingsService.getSubroutineLocalPath();
2727
return searchInWorkspace(name, folders, COBOL_EXT_ARRAY);
2828
}

0 commit comments

Comments
 (0)