From 8f8b8abc1ba49d8a5621ea540b79beec30eddb77 Mon Sep 17 00:00:00 2001 From: ecmel Date: Thu, 2 May 2024 07:39:41 +0300 Subject: [PATCH] organized code --- ...ratchpadCommand.ts => workspaceCommand.ts} | 155 ++++++++++-------- src/extension.ts | 36 ++-- src/services/workspaceTreeProvider.ts | 2 +- 3 files changed, 102 insertions(+), 91 deletions(-) rename src/commands/{scratchpadCommand.ts => workspaceCommand.ts} (86%) diff --git a/src/commands/scratchpadCommand.ts b/src/commands/workspaceCommand.ts similarity index 86% rename from src/commands/scratchpadCommand.ts rename to src/commands/workspaceCommand.ts index 38736175..1cf4c503 100644 --- a/src/commands/scratchpadCommand.ts +++ b/src/commands/workspaceCommand.ts @@ -14,10 +14,13 @@ import { CodeLens, CodeLensProvider, + Command, ProviderResult, Range, + StatusBarAlignment, TextDocument, TextEditor, + ThemeColor, Uri, window, workspace, @@ -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 } }>( @@ -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(); @@ -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; } } @@ -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, ); } } @@ -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 = { + 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); +} diff --git a/src/extension.ts b/src/extension.ts index 0f0ed8ab..dc1254f4 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -14,7 +14,6 @@ import { env } from "node:process"; import path from "path"; import { - Command, commands, ConfigurationTarget, EventEmitter, @@ -22,9 +21,7 @@ import { extensions, languages, Range, - StatusBarAlignment, TextDocumentContentProvider, - ThemeColor, Uri, window, workspace, @@ -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"; @@ -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; @@ -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 = { - 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"); @@ -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"); diff --git a/src/services/workspaceTreeProvider.ts b/src/services/workspaceTreeProvider.ts index 766c3c20..f6449df3 100644 --- a/src/services/workspaceTreeProvider.ts +++ b/src/services/workspaceTreeProvider.ts @@ -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 {