Skip to content

Commit

Permalink
CSP support with server-editing, more filtering options
Browse files Browse the repository at this point in the history
  • Loading branch information
daimor committed Jan 13, 2020
1 parent 92320d3 commit 5bfde00
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 25 deletions.
5 changes: 5 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ export class AtelierAPI {
return this.cookies;
}
const data = response.body;
/// deconde encoded content
if (data.result && data.result.enc && data.result.content) {
data.result.enc = false;
data.result.content = Buffer.from(data.result.content.join(""), "base64");
}
if (data.console) {
outputConsole(data.console);
}
Expand Down
5 changes: 2 additions & 3 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,9 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
});

workspace.onDidSaveTextDocument(file => {
if (!languages.includes(file.languageId)) {
return;
if (schemas.includes(file.uri.scheme) || languages.includes(file.languageId)) {
vscode.commands.executeCommand("vscode-objectscript.compile");
}
vscode.commands.executeCommand("vscode-objectscript.compile");
});

vscode.window.onDidChangeActiveTextEditor((textEditor: vscode.TextEditor) => {
Expand Down
52 changes: 41 additions & 11 deletions src/providers/FileSystemPovider/FileSystemProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as path from "path";
import * as vscode from "vscode";
import * as url from "url";
import { AtelierAPI } from "../../api";
import { Directory } from "./Directory";
import { File } from "./File";
Expand All @@ -26,17 +27,36 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
public async readDirectory(uri: vscode.Uri): Promise<[string, vscode.FileType][]> {
const api = new AtelierAPI(uri);
const parent = await this._lookupAsDirectory(uri);
const sql = `CALL %Library.RoutineMgr_StudioOpenDialog(?,,,,,,0)`;
const folder = uri.path === "/" ? "/" : uri.path.replace(/\//g, ".") + "/";
const spec = folder.slice(1) + "*.cls,*.inc,*.int,*.mac";
const sql = `CALL %Library.RoutineMgr_StudioOpenDialog(?,?,?,?,?,?,?)`;
const { query } = url.parse(decodeURIComponent(uri.toString()), true);
const type = String(query && query.type).toLowerCase() || "all";
const csp = query.csp === "" || query.csp === "1";
let filter = query.filter || "";
if (csp) {
filter = filter || "*";
} else if (type === "rtn") {
filter = "*.inc,*.int,*.mac";
} else if (type === "cls") {
filter = "*.cls";
} else {
filter = query.filter || "*.cls,*.inc,*.int,*.mac";
}
const folder = csp ? (uri.path.endsWith("/") ? uri.path : uri.path + "/") : uri.path.replace(/\//g, ".");
const spec = csp ? folder + filter : folder.slice(1) + filter;
const dir = "1";
const orderBy = "1";
const system = api.ns === "%SYS" ? "1" : "0";
const flat = String(query.flat) || "0";
const notStudio = "0";
const generated = String(query.generated) || "0";
return api
.actionQuery(sql, [spec])
.actionQuery(sql, [spec, dir, orderBy, system, flat, notStudio, generated])
.then(data => data.result.content || [])
.then(data =>
data.map(item => {
const name = item.Name;
const fullName = folder === "" ? name : folder + "/" + name;
if (item.IsDirectory.length) {
if (item.Type === "10" || item.Type === "9") {
parent.entries.set(name, new Directory(name, fullName));
return [name, vscode.FileType.Directory];
} else {
Expand Down Expand Up @@ -76,7 +96,9 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
overwrite: boolean;
}
): void | Thenable<void> {
const fileName = uri.path.slice(1).replace(/\//g, ".");
const { query } = url.parse(decodeURIComponent(uri.toString()), true);
const csp = query.csp === "" || query.csp === "1";
const fileName = csp ? uri.path : uri.path.slice(1).replace(/\//g, ".");
if (fileName.startsWith(".")) {
return;
}
Expand Down Expand Up @@ -143,10 +165,9 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
}

private async _lookupAsFile(uri: vscode.Uri): Promise<File> {
// if (!uri.path.match(/\.\w+$/)) {
// return Promise.resolve(new Directory(uri.path))
// }
const fileName = uri.path.slice(1).replace(/\//g, ".");
const { query } = url.parse(decodeURIComponent(uri.toString()), true);
const csp = query.csp === "" || query.csp === "1";
const fileName = csp ? uri.path : uri.path.slice(1).replace(/\//g, ".");
if (fileName.startsWith(".")) {
throw vscode.FileSystemError.FileNotFound();
}
Expand All @@ -155,7 +176,16 @@ export class FileSystemProvider implements vscode.FileSystemProvider {
return api
.getDoc(fileName)
.then(data => data.result)
.then(({ ts, content }) => new File(name, fileName, ts, content.join("\n").length, content.join("\n")))
.then(
({ ts, content }) =>
new File(
name,
fileName,
ts,
Array.isArray(content) ? content.join("\n").length : content.length,
Array.isArray(content) ? content.join("\n") : content
)
)
.then(entry =>
this._lookupParentDirectory(uri).then(parent => {
parent.entries.set(name, entry);
Expand Down
22 changes: 11 additions & 11 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs = require("fs");
import path = require("path");
import * as url from "url";
import { execSync } from "child_process";
import * as vscode from "vscode";
import { config, schemas, workspaceState } from "../extension";
Expand All @@ -22,7 +23,10 @@ export interface CurrentFile {

export function currentFile(document?: vscode.TextDocument): CurrentFile {
document = document || (vscode.window.activeTextEditor.document ? vscode.window.activeTextEditor.document : null);
if (!document || !document.fileName || !document.languageId || !document.languageId.startsWith("objectscript")) {
if (
!schemas.includes(document.uri.scheme) &&
(!document || !document.fileName || !document.languageId || !document.languageId.startsWith("objectscript"))
) {
return null;
}
const uri = document.uri;
Expand All @@ -31,20 +35,16 @@ export function currentFile(document?: vscode.TextDocument): CurrentFile {
const fileExt = fileName.match(/\.(\w+)$/)[1].toLowerCase();
let name = "";
let ext = "";
if (fileExt === "cls") {
const { query } = url.parse(decodeURIComponent(uri.toString()), true);
const csp = query.csp === "" || query.csp === "1";
if (csp) {
name = fileName;
} else if (fileExt === "cls") {
const match = content.match(/^Class (%?\w+(?:\.\w+)+)/im);
if (match) {
name = match[1];
ext = "cls";
}
} else if (fileExt === "csp") {
name =
config().conn.label.toLowerCase() +
fileName
.split(config().conn.label.toLowerCase())[1]
.replace(/\\/g, "/")
.replace(".csp", "");
ext = "csp";
} else {
const match = content.match(/^ROUTINE ([^\s]+)(?:\s+\[.*Type=([a-z]{3,}))?/i);
name = match[1];
Expand All @@ -53,7 +53,7 @@ export function currentFile(document?: vscode.TextDocument): CurrentFile {
if (!name) {
return null;
}
name += "." + ext;
name += ext ? "." + ext : "";

return {
content,
Expand Down

0 comments on commit 5bfde00

Please sign in to comment.