Skip to content

Commit 6793e16

Browse files
Merge pull request #661 from LogMull/fix-593
fix #593 share logic between FileSystemProvider and FileSearchProvider
2 parents c332f6a + 3d333bb commit 6793e16

File tree

3 files changed

+91
-56
lines changed

3 files changed

+91
-56
lines changed
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as vscode from "vscode";
22
import * as url from "url";
3-
import { DocSearchResult } from "../../api/atelier";
43
import { AtelierAPI } from "../../api";
4+
import { StudioOpenDialog } from "../../queries";
5+
import { studioOpenDialogFromURI } from "../../utils/FileProviderUtil";
56

67
export class FileSearchProvider implements vscode.FileSearchProvider {
78
/**
@@ -15,58 +16,56 @@ export class FileSearchProvider implements vscode.FileSearchProvider {
1516
options: vscode.FileSearchOptions,
1617
token: vscode.CancellationToken
1718
): vscode.ProviderResult<vscode.Uri[]> {
18-
const folderQuery = url.parse(options.folder.toString(true), true).query;
19-
const type = folderQuery.type || "all";
20-
const category =
21-
folderQuery.csp === "" || folderQuery.csp === "1" ? "CSP" : type === "cls" ? "CLS" : type === "rtn" ? "RTN" : "*";
22-
const generated = folderQuery.generated === "1";
23-
const api = new AtelierAPI(options.folder);
19+
const uri = url.parse(options.folder.toString(true), true);
20+
const csp = uri.query.csp === "" || uri.query.csp === "1";
2421
let filter = query.pattern;
25-
if (category !== "CSP") {
22+
if (!csp) {
2623
if (options.folder.path !== "/") {
2724
filter = options.folder.path.slice(1) + "/%" + filter;
2825
}
2926
filter = filter.replace(/\//g, ".");
3027
}
31-
let counter = 0;
32-
if (!api.enabled) {
33-
return null;
28+
if (filter.length) {
29+
filter = "Name Like '%" + filter + "%'";
30+
} else {
31+
// When this is called without a query.pattern, every file is supposed to be returned, so do not provide a filter
32+
filter = "";
3433
}
35-
return api
36-
.getDocNames({
37-
filter,
38-
category,
39-
generated,
34+
let counter = 0;
35+
return studioOpenDialogFromURI(options.folder, { flat: true, filter: filter })
36+
.then((data) => {
37+
return data.result.content;
4038
})
41-
.then((data) => data.result.content)
42-
.then((files: DocSearchResult[]) =>
43-
files
44-
.map((file) => {
45-
if (category !== "CSP" && file.cat === "CSP") {
39+
.then((data: StudioOpenDialog[]) => {
40+
const api = new AtelierAPI(options.folder);
41+
return data
42+
.map((item: StudioOpenDialog) => {
43+
// item.Type only matters here if it is 5 (CSP)
44+
if (item.Type == "5" && !csp) {
4645
return null;
4746
}
48-
if (file.cat !== "CSP") {
49-
if (file.name.startsWith("%") && api.ns !== "%SYS") {
47+
if (item.Type !== "5") {
48+
if (item.Name.startsWith("%") && api.ns !== "%SYS") {
5049
return null;
5150
}
5251
// Convert dotted name to slashed one, treating the likes of ABC.1.int or DEF.T1.int in the same way
5352
// as the Studio dialog does.
54-
const nameParts = file.name.split(".");
53+
const nameParts = item.Name.split(".");
5554
const dotParts = nameParts
5655
.slice(-2)
5756
.join(".")
5857
.match(/^[A-Z]?\d*[.](mac|int|inc)$/)
5958
? 3
6059
: 2;
61-
file.name = nameParts.slice(0, -dotParts).join("/") + "/" + nameParts.slice(-dotParts).join(".");
60+
item.Name = nameParts.slice(0, -dotParts).join("/") + "/" + nameParts.slice(-dotParts).join(".");
6261
}
6362
if (!options.maxResults || ++counter <= options.maxResults) {
64-
return options.folder.with({ path: `/${file.name}` });
63+
return vscode.Uri.parse(`${options.folder.scheme}://${options.folder.authority}/${item.Name}`, true);
6564
} else {
6665
return null;
6766
}
6867
})
69-
.filter((el) => el !== null)
70-
);
68+
.filter((el) => el !== null);
69+
});
7170
}
7271
}

src/providers/FileSystemProvider/FileSystemProvider.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Directory } from "./Directory";
66
import { File } from "./File";
77
import { fireOtherStudioAction, OtherStudioAction } from "../../commands/studio";
88
import { StudioOpenDialog } from "../../queries";
9+
import { studioOpenDialogFromURI } from "../../utils/FileProviderUtil";
910
import { redirectDotvscodeRoot, workspaceFolderOfUri } from "../../utils/index";
1011
import { workspaceState } from "../../extension";
1112

@@ -48,40 +49,15 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
4849
if (!api.active) {
4950
return;
5051
}
51-
const sql = `CALL %Library.RoutineMgr_StudioOpenDialog(?,?,?,?,?,?,?)`;
5252
const { query } = url.parse(uri.toString(true), true);
53-
const type = query.type && query.type != "" ? query.type.toString() : "all";
5453
const csp = query.csp === "" || query.csp === "1";
55-
let filter = "";
56-
if (query.filter && query.filter.length) {
57-
filter = query.filter.toString();
58-
if (!csp) {
59-
// always exclude Studio projects, since we can't do anything with them
60-
filter += ",'*.prj";
61-
}
62-
} else if (csp) {
63-
filter = "*";
64-
} else if (type === "rtn") {
65-
filter = "*.inc,*.mac,*.int";
66-
} else if (type === "cls") {
67-
filter = "*.cls";
68-
} else {
69-
filter = "*.cls,*.inc,*.mac,*.int";
70-
}
7154
const folder = !csp
7255
? uri.path.replace(/\//g, ".")
7356
: uri.path === "/"
7457
? ""
7558
: uri.path.endsWith("/")
7659
? uri.path
7760
: uri.path + "/";
78-
const spec = csp ? folder + filter : folder.length > 1 ? folder.slice(1) + "/" + filter : filter;
79-
const dir = "1";
80-
const orderBy = "1";
81-
const system = query.system && query.system.length ? query.system.toString() : api.ns === "%SYS" ? "1" : "0";
82-
const flat = query.flat && query.flat.length ? query.flat.toString() : "0";
83-
const notStudio = "0";
84-
const generated = query.generated && query.generated.length ? query.generated.toString() : "0";
8561
// get all web apps that have a filepath (Studio dialog used below returns REST ones too)
8662
const cspApps = csp ? await api.getCSPApps().then((data) => data.result.content || []) : [];
8763
const cspSubfolderMap = new Map<string, vscode.FileType>();
@@ -95,9 +71,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
9571
}
9672
}
9773
const cspSubfolders = Array.from(cspSubfolderMap.entries());
98-
// Assemble the results
99-
return api
100-
.actionQuery(sql, [spec, dir, orderBy, system, flat, notStudio, generated])
74+
return studioOpenDialogFromURI(uri)
10175
.then((data) => data.result.content || [])
10276
.then((data) => {
10377
const results = data

src/utils/FileProviderUtil.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import * as vscode from "vscode";
2+
import * as url from "url";
3+
import { AtelierAPI } from "../api";
4+
5+
export function studioOpenDialogFromURI(
6+
uri: vscode.Uri,
7+
overrides: { flat?: boolean; filter?: string; type?: string } = { flat: false, filter: "", type: "" }
8+
): Promise<any> {
9+
const api = new AtelierAPI(uri);
10+
if (!api.active) {
11+
return;
12+
}
13+
const sql = `CALL %Library.RoutineMgr_StudioOpenDialog(?,?,?,?,?,?,?,?)`;
14+
const { query } = url.parse(uri.toString(true), true);
15+
const csp = query.csp === "" || query.csp === "1";
16+
const type =
17+
overrides.type && overrides.type != ""
18+
? overrides.type
19+
: query.type && query.type != ""
20+
? query.type.toString()
21+
: csp
22+
? "csp"
23+
: "all";
24+
25+
const folder = !csp
26+
? uri.path.replace(/\//g, ".")
27+
: uri.path === "/"
28+
? ""
29+
: uri.path.endsWith("/")
30+
? uri.path
31+
: uri.path + "/";
32+
// The query filter represents the studio spec to be used,
33+
// overrides.filter represents the SQL query that will be passed to the server
34+
35+
let specOpts = "";
36+
// If filter is specified on the URI, use it
37+
if (query.filter && query.filter.length) {
38+
specOpts = query.filter.toString();
39+
if (!csp) {
40+
// always exclude Studio projects, since we can't do anything with them
41+
specOpts += ",'*.prj";
42+
}
43+
} // otherwise, reference the type to get the desired files.
44+
else if (csp) {
45+
specOpts = "*";
46+
} else if (type === "rtn") {
47+
specOpts = "*.inc,*.mac,*.int";
48+
} else if (type === "cls") {
49+
specOpts = "*.cls";
50+
} else {
51+
specOpts = "*.cls,*.inc,*.mac,*.int";
52+
}
53+
const spec = csp ? folder + specOpts : folder.length > 1 ? folder.slice(1) + "/" + specOpts : specOpts;
54+
const notStudio = "0";
55+
const dir = "1";
56+
const orderBy = "1";
57+
const generated = query.generated && query.generated.length ? query.generated.toString() : "0";
58+
const system = query.system && query.system.length ? query.system.toString() : api.ns === "%SYS" ? "1" : "0";
59+
let flat = query.flat && query.flat.length ? query.flat.toString() : "0";
60+
if (overrides && overrides.flat) flat = "1";
61+
return api.actionQuery(sql, [spec, dir, orderBy, system, flat, notStudio, generated, overrides.filter]);
62+
}

0 commit comments

Comments
 (0)