Skip to content

Commit

Permalink
organized code
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmel committed May 2, 2024
1 parent 65b3d0c commit 8f8b8ab
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 91 deletions.
155 changes: 91 additions & 64 deletions src/commands/scratchpadCommand.ts → src/commands/workspaceCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
import {
CodeLens,
CodeLensProvider,
Command,
ProviderResult,
Range,
StatusBarAlignment,
TextDocument,
TextEditor,
ThemeColor,
Uri,
window,
workspace,
Expand All @@ -30,15 +33,43 @@ import { ExecutionTypes } from "../models/execution";

const connectionService = new ConnectionManagementService();

function setRunScratchpadItemText(text: string) {
ext.runScratchpadItem.text = `$(run) ${text}`;
}

export function workspaceFoldersChanged() {
function workspaceFoldersChanged() {
ext.dataSourceTreeProvider.reload();
ext.scratchpadTreeProvider.reload();
}

function setRealActiveTextEditor(editor?: TextEditor | undefined) {
if (editor) {
const scheme = editor.document.uri.scheme;
if (scheme === "file") {
ext.activeTextEditor = editor;
}
} else {
ext.activeTextEditor = undefined;
}
}

function activeEditorChanged(editor?: TextEditor | undefined) {
setRealActiveTextEditor(editor);
const item = ext.runScratchpadItem;
if (ext.activeTextEditor) {
const uri = ext.activeTextEditor.document.uri;
if (isScratchpad(uri)) {
const server = getServerForUri(uri);
setRunScratchpadItemText(server || "Run");
item.show();
} else {
item.hide();
}
} else {
item.hide();
}
}

function setRunScratchpadItemText(text: string) {
ext.runScratchpadItem.text = `$(run) ${text}`;
}

function getServers() {
const conf = workspace.getConfiguration("kdb");
const servers = conf.get<{ [key: string]: { serverAlias: string } }>(
Expand All @@ -56,39 +87,6 @@ function getServers() {
];
}

export function getServerForUri(uri: Uri) {
const conf = workspace.getConfiguration("kdb", uri);
const scratchpads = conf.get<{ [key: string]: string | undefined }>(
"connectionMap",
{},
);
return scratchpads[workspace.asRelativePath(uri)];
}

async function setServerForUri(uri: Uri, server: string | undefined) {
const conf = workspace.getConfiguration("kdb", uri);
const map = conf.get<{ [key: string]: string | undefined }>(
"connectionMap",
{},
);
map[workspace.asRelativePath(uri)] = server;
await conf.update("connectionMap", map);
}

export function getConnectionForUri(uri: Uri) {
const server = getServerForUri(uri);
if (server) {
return ext.connectionsList.find((item) => {
if (item instanceof InsightsNode) {
return item.details.alias === server;
} else if (item instanceof KdbNode) {
return item.details.serverAlias === server;
}
return false;
}) as KdbNode | InsightsNode;
}
}

async function getConnectionForServer(server: string) {
if (server) {
const servers = await ext.serverProvider.getChildren();
Expand Down Expand Up @@ -122,31 +120,36 @@ async function waitForConnection(name: string) {
});
}

function setRealActiveTextEditor(editor?: TextEditor | undefined) {
if (editor) {
const scheme = editor.document.uri.scheme;
if (scheme === "file") {
ext.activeTextEditor = editor;
}
} else {
ext.activeTextEditor = undefined;
}
async function setServerForUri(uri: Uri, server: string | undefined) {
const conf = workspace.getConfiguration("kdb", uri);
const map = conf.get<{ [key: string]: string | undefined }>(
"connectionMap",
{},
);
map[workspace.asRelativePath(uri)] = server;
await conf.update("connectionMap", map);
}

export function activeEditorChanged(editor?: TextEditor | undefined) {
setRealActiveTextEditor(editor);
const item = ext.runScratchpadItem;
if (ext.activeTextEditor) {
const uri = ext.activeTextEditor.document.uri;
if (isScratchpad(uri)) {
const server = getServerForUri(uri);
setRunScratchpadItemText(server || "Run");
item.show();
} else {
item.hide();
}
} else {
item.hide();
export function getServerForUri(uri: Uri) {
const conf = workspace.getConfiguration("kdb", uri);
const scratchpads = conf.get<{ [key: string]: string | undefined }>(
"connectionMap",
{},
);
return scratchpads[workspace.asRelativePath(uri)];
}

export function getConnectionForUri(uri: Uri) {
const server = getServerForUri(uri);
if (server) {
return ext.connectionsList.find((item) => {
if (item instanceof InsightsNode) {
return item.details.alias === server;
} else if (item instanceof KdbNode) {
return item.details.serverAlias === server;
}
return false;
}) as KdbNode | InsightsNode;
}
}

Expand Down Expand Up @@ -209,9 +212,11 @@ export async function runActiveEditor(type?: ExecutionTypes) {
}

runQuery(
type || isPython(uri)
? ExecutionTypes.PythonQueryFile
: ExecutionTypes.QueryFile,
type === undefined
? isPython(uri)
? ExecutionTypes.PythonQueryFile
: ExecutionTypes.QueryFile
: type,
);
}
}
Expand All @@ -233,3 +238,25 @@ export class ConnectionLensProvider implements CodeLensProvider {
return [runScratchpad, pickConnection];
}
}

export function connectWorkspaceCommsnds() {
ext.runScratchpadItem = window.createStatusBarItem(
StatusBarAlignment.Right,
10000,
);
ext.runScratchpadItem.backgroundColor = new ThemeColor(
"statusBarItem.warningBackground",
);
ext.runScratchpadItem.command = <Command>{
title: "",
command: "kdb.runScratchpad",
arguments: [],
};

const watcher = workspace.createFileSystemWatcher("**/*.kdb.{json,q,py}");
watcher.onDidCreate(workspaceFoldersChanged);
watcher.onDidDelete(workspaceFoldersChanged);
workspace.onDidChangeWorkspaceFolders(workspaceFoldersChanged);
window.onDidChangeActiveTextEditor(activeEditorChanged);
activeEditorChanged(window.activeTextEditor);
}
36 changes: 10 additions & 26 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@
import { env } from "node:process";
import path from "path";
import {
Command,
commands,
ConfigurationTarget,
EventEmitter,
ExtensionContext,
extensions,
languages,
Range,
StatusBarAlignment,
TextDocumentContentProvider,
ThemeColor,
Uri,
window,
workspace,
Expand Down Expand Up @@ -100,12 +97,11 @@ import {
WorkspaceTreeProvider,
} from "./services/workspaceTreeProvider";
import {
activeEditorChanged,
ConnectionLensProvider,
connectWorkspaceCommsnds,
pickConnection,
runActiveEditor,
workspaceFoldersChanged,
} from "./commands/scratchpadCommand";
} from "./commands/workspaceCommand";
import { createDefaultDataSourceFile } from "./models/dataSource";
import { connectBuildTools, lintCommand } from "./commands/buildToolsCommand";
import { CompletionProvider } from "./services/completionProvider";
Expand Down Expand Up @@ -423,6 +419,12 @@ export async function activate(context: ExtensionContext) {
new QuickFixProvider(),
),
ext.diagnosticCollection,
workspace.onDidChangeConfiguration((event) => {
if (event.affectsConfiguration("kdb.connectionMap")) {
ext.dataSourceTreeProvider.reload();
ext.scratchpadTreeProvider.reload();
}
}),
);

const lastResult: QueryResult | undefined = undefined;
Expand All @@ -449,25 +451,8 @@ export async function activate(context: ExtensionContext) {
),
);

ext.runScratchpadItem = window.createStatusBarItem(
StatusBarAlignment.Right,
10000,
);
ext.runScratchpadItem.backgroundColor = new ThemeColor(
"statusBarItem.warningBackground",
);
ext.runScratchpadItem.command = <Command>{
title: "",
command: "kdb.runScratchpad",
arguments: [],
};

const watcher = workspace.createFileSystemWatcher("**/*.kdb.{json,q,py}");
watcher.onDidCreate(workspaceFoldersChanged);
watcher.onDidDelete(workspaceFoldersChanged);
workspace.onDidChangeWorkspaceFolders(workspaceFoldersChanged);
window.onDidChangeActiveTextEditor(activeEditorChanged);
activeEditorChanged(window.activeTextEditor);
connectWorkspaceCommsnds();
await connectBuildTools();

//q language server
const serverModule = path.join(context.extensionPath, "out", "server.js");
Expand Down Expand Up @@ -495,7 +480,6 @@ export async function activate(context: ExtensionContext) {
);

await client.start();
await connectBuildTools();

Telemetry.sendEvent("Extension.Activated");
const yamlExtension = extensions.getExtension("redhat.vscode-yaml");
Expand Down
2 changes: 1 addition & 1 deletion src/services/workspaceTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
} from "vscode";
import Path from "path";
import { getServerIconState } from "../utils/core";
import { getConnectionForUri } from "../commands/scratchpadCommand";
import { getConnectionForUri } from "../commands/workspaceCommand";
import { ext } from "../extensionVariables";

export class WorkspaceTreeProvider implements TreeDataProvider<FileTreeItem> {
Expand Down

0 comments on commit 8f8b8ab

Please sign in to comment.