Skip to content

fix #593 share logic between FileSystemProvider and FileSearchProvider #661

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions src/providers/FileSystemProvider/FileSearchProvider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as vscode from "vscode";
import * as url from "url";
import { DocSearchResult } from "../../api/atelier";
import { AtelierAPI } from "../../api";
import { StudioOpenDialog } from "../../queries";
import { studioOpenDialogFromURI } from "../../utils/FileProviderUtil";

export class FileSearchProvider implements vscode.FileSearchProvider {
/**
Expand All @@ -15,58 +16,56 @@ export class FileSearchProvider implements vscode.FileSearchProvider {
options: vscode.FileSearchOptions,
token: vscode.CancellationToken
): vscode.ProviderResult<vscode.Uri[]> {
const folderQuery = url.parse(options.folder.toString(true), true).query;
const type = folderQuery.type || "all";
const category =
folderQuery.csp === "" || folderQuery.csp === "1" ? "CSP" : type === "cls" ? "CLS" : type === "rtn" ? "RTN" : "*";
const generated = folderQuery.generated === "1";
const api = new AtelierAPI(options.folder);
const uri = url.parse(options.folder.toString(true), true);
const csp = uri.query.csp === "" || uri.query.csp === "1";
let filter = query.pattern;
if (category !== "CSP") {
if (!csp) {
if (options.folder.path !== "/") {
filter = options.folder.path.slice(1) + "/%" + filter;
}
filter = filter.replace(/\//g, ".");
}
let counter = 0;
if (!api.enabled) {
return null;
if (filter.length) {
filter = "Name Like '%" + filter + "%'";
} else {
// When this is called without a query.pattern, every file is supposed to be returned, so do not provide a filter
filter = "";
}
return api
.getDocNames({
filter,
category,
generated,
let counter = 0;
return studioOpenDialogFromURI(options.folder, { flat: true, filter: filter })
.then((data) => {
return data.result.content;
})
.then((data) => data.result.content)
.then((files: DocSearchResult[]) =>
files
.map((file) => {
if (category !== "CSP" && file.cat === "CSP") {
.then((data: StudioOpenDialog[]) => {
const api = new AtelierAPI(options.folder);
return data
.map((item: StudioOpenDialog) => {
// item.Type only matters here if it is 5 (CSP)
if (item.Type == "5" && !csp) {
return null;
}
if (file.cat !== "CSP") {
if (file.name.startsWith("%") && api.ns !== "%SYS") {
if (item.Type !== "5") {
if (item.Name.startsWith("%") && api.ns !== "%SYS") {
return null;
}
// Convert dotted name to slashed one, treating the likes of ABC.1.int or DEF.T1.int in the same way
// as the Studio dialog does.
const nameParts = file.name.split(".");
const nameParts = item.Name.split(".");
const dotParts = nameParts
.slice(-2)
.join(".")
.match(/^[A-Z]?\d*[.](mac|int|inc)$/)
? 3
: 2;
file.name = nameParts.slice(0, -dotParts).join("/") + "/" + nameParts.slice(-dotParts).join(".");
item.Name = nameParts.slice(0, -dotParts).join("/") + "/" + nameParts.slice(-dotParts).join(".");
}
if (!options.maxResults || ++counter <= options.maxResults) {
return options.folder.with({ path: `/${file.name}` });
return vscode.Uri.parse(`${options.folder.scheme}://${options.folder.authority}/${item.Name}`, true);
} else {
return null;
}
})
.filter((el) => el !== null)
);
.filter((el) => el !== null);
});
}
}
30 changes: 2 additions & 28 deletions src/providers/FileSystemProvider/FileSystemProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Directory } from "./Directory";
import { File } from "./File";
import { fireOtherStudioAction, OtherStudioAction } from "../../commands/studio";
import { StudioOpenDialog } from "../../queries";
import { studioOpenDialogFromURI } from "../../utils/FileProviderUtil";
import { redirectDotvscodeRoot, workspaceFolderOfUri } from "../../utils/index";
import { workspaceState } from "../../extension";

Expand Down Expand Up @@ -48,40 +49,15 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
if (!api.active) {
return;
}
const sql = `CALL %Library.RoutineMgr_StudioOpenDialog(?,?,?,?,?,?,?)`;
const { query } = url.parse(uri.toString(true), true);
const type = query.type && query.type != "" ? query.type.toString() : "all";
const csp = query.csp === "" || query.csp === "1";
let filter = "";
if (query.filter && query.filter.length) {
filter = query.filter.toString();
if (!csp) {
// always exclude Studio projects, since we can't do anything with them
filter += ",'*.prj";
}
} else if (csp) {
filter = "*";
} else if (type === "rtn") {
filter = "*.inc,*.mac,*.int";
} else if (type === "cls") {
filter = "*.cls";
} else {
filter = "*.cls,*.inc,*.mac,*.int";
}
const folder = !csp
? uri.path.replace(/\//g, ".")
: uri.path === "/"
? ""
: uri.path.endsWith("/")
? uri.path
: uri.path + "/";
const spec = csp ? folder + filter : folder.length > 1 ? folder.slice(1) + "/" + filter : filter;
const dir = "1";
const orderBy = "1";
const system = query.system && query.system.length ? query.system.toString() : api.ns === "%SYS" ? "1" : "0";
const flat = query.flat && query.flat.length ? query.flat.toString() : "0";
const notStudio = "0";
const generated = query.generated && query.generated.length ? query.generated.toString() : "0";
// get all web apps that have a filepath (Studio dialog used below returns REST ones too)
const cspApps = csp ? await api.getCSPApps().then((data) => data.result.content || []) : [];
const cspSubfolderMap = new Map<string, vscode.FileType>();
Expand All @@ -95,9 +71,7 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
}
}
const cspSubfolders = Array.from(cspSubfolderMap.entries());
// Assemble the results
return api
.actionQuery(sql, [spec, dir, orderBy, system, flat, notStudio, generated])
return studioOpenDialogFromURI(uri)
.then((data) => data.result.content || [])
.then((data) => {
const results = data
Expand Down
62 changes: 62 additions & 0 deletions src/utils/FileProviderUtil.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as vscode from "vscode";
import * as url from "url";
import { AtelierAPI } from "../api";

export function studioOpenDialogFromURI(
uri: vscode.Uri,
overrides: { flat?: boolean; filter?: string; type?: string } = { flat: false, filter: "", type: "" }
): Promise<any> {
const api = new AtelierAPI(uri);
if (!api.active) {
return;
}
const sql = `CALL %Library.RoutineMgr_StudioOpenDialog(?,?,?,?,?,?,?,?)`;
const { query } = url.parse(uri.toString(true), true);
const csp = query.csp === "" || query.csp === "1";
const type =
overrides.type && overrides.type != ""
? overrides.type
: query.type && query.type != ""
? query.type.toString()
: csp
? "csp"
: "all";

const folder = !csp
? uri.path.replace(/\//g, ".")
: uri.path === "/"
? ""
: uri.path.endsWith("/")
? uri.path
: uri.path + "/";
// The query filter represents the studio spec to be used,
// overrides.filter represents the SQL query that will be passed to the server

let specOpts = "";
// If filter is specified on the URI, use it
if (query.filter && query.filter.length) {
specOpts = query.filter.toString();
if (!csp) {
// always exclude Studio projects, since we can't do anything with them
specOpts += ",'*.prj";
}
} // otherwise, reference the type to get the desired files.
else if (csp) {
specOpts = "*";
} else if (type === "rtn") {
specOpts = "*.inc,*.mac,*.int";
} else if (type === "cls") {
specOpts = "*.cls";
} else {
specOpts = "*.cls,*.inc,*.mac,*.int";
}
const spec = csp ? folder + specOpts : folder.length > 1 ? folder.slice(1) + "/" + specOpts : specOpts;
const notStudio = "0";
const dir = "1";
const orderBy = "1";
const generated = query.generated && query.generated.length ? query.generated.toString() : "0";
const system = query.system && query.system.length ? query.system.toString() : api.ns === "%SYS" ? "1" : "0";
let flat = query.flat && query.flat.length ? query.flat.toString() : "0";
if (overrides && overrides.flat) flat = "1";
return api.actionQuery(sql, [spec, dir, orderBy, system, flat, notStudio, generated, overrides.filter]);
}