From d65c1d526c4b54f5acf1e864fae159c7c1d00489 Mon Sep 17 00:00:00 2001 From: Wroud Date: Fri, 15 Apr 2022 16:08:13 +0300 Subject: [PATCH] feat(plugin-sql-editor): CB-1825 mode context --- .../src/ExecutorHandlersCollection.ts | 10 ++++++++ .../src/SqlEditor/ISQLEditorData.ts | 10 ++++++++ .../src/SqlEditor/SQLEditorModeContext.ts | 25 +++++++++++++++++++ .../src/SqlEditor/SqlEditor.tsx | 25 +++++++++++-------- .../src/SqlEditor/useSqlEditor.ts | 25 +++++++++++++------ .../packages/plugin-sql-editor/src/index.ts | 1 + 6 files changed, 78 insertions(+), 18 deletions(-) create mode 100644 webapp/packages/plugin-sql-editor/src/SqlEditor/SQLEditorModeContext.ts diff --git a/webapp/packages/core-executor/src/ExecutorHandlersCollection.ts b/webapp/packages/core-executor/src/ExecutorHandlersCollection.ts index 9b18d601a3..b97d9a580c 100644 --- a/webapp/packages/core-executor/src/ExecutorHandlersCollection.ts +++ b/webapp/packages/core-executor/src/ExecutorHandlersCollection.ts @@ -6,6 +6,8 @@ * you may not use this file except in compliance with the License. */ +import { makeObservable, observable } from 'mobx'; + import type { IExecutorHandler } from './IExecutorHandler'; import type { ExecutorDataMap, IChainLink, IExecutorHandlersCollection } from './IExecutorHandlersCollection'; @@ -22,6 +24,14 @@ implements IExecutorHandlersCollection { this.links = new Map(); this.collections = []; this.initialDataGetter = null; + + makeObservable(this, { + handlers: observable.shallow, + postHandlers: observable.shallow, + chain: observable.shallow, + collections: observable.shallow, + links: observable.shallow, + }); } setInitialDataGetter(getter: (() => T) | null): this { diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditor/ISQLEditorData.ts b/webapp/packages/plugin-sql-editor/src/SqlEditor/ISQLEditorData.ts index 1f967b34d2..bf83d35ef7 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlEditor/ISQLEditorData.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlEditor/ISQLEditorData.ts @@ -20,13 +20,21 @@ import type { SqlDialectInfo } from '@cloudbeaver/core-sdk'; import type { SQLProposal } from '../SqlEditorService'; import type { SQLParser, ISQLScriptSegment } from '../SQLParser'; +import type { ISQLEditorMode } from './SQLEditorModeContext'; export interface ISegmentExecutionData { segment: ISQLScriptSegment; type: 'start' | 'end' | 'error'; } +export interface ICursor { + begin: number; + end: number; +} + export interface ISQLEditorData { + readonly cursor: ICursor; + readonly activeSegmentMode: ISQLEditorMode; readonly parser: SQLParser; readonly dialect: SqlDialectInfo | undefined; readonly activeSegment: ISQLScriptSegment | undefined; @@ -39,6 +47,8 @@ export interface ISQLEditorData { readonly onExecute: ISyncExecutor; readonly onSegmentExecute: ISyncExecutor; readonly onUpdate: ISyncExecutor; + readonly onMode: ISyncExecutor; + updateParserScriptsThrottle(): Promise; setQuery(query: string): void; init(): void; diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditor/SQLEditorModeContext.ts b/webapp/packages/plugin-sql-editor/src/SqlEditor/SQLEditorModeContext.ts new file mode 100644 index 0000000000..e07286f09a --- /dev/null +++ b/webapp/packages/plugin-sql-editor/src/SqlEditor/SQLEditorModeContext.ts @@ -0,0 +1,25 @@ +/* + * CloudBeaver - Cloud Database Manager + * Copyright (C) 2020-2022 DBeaver Corp and others + * + * Licensed under the Apache License, Version 2.0. + * you may not use this file except in compliance with the License. + */ + +import type { ISyncContextLoader } from '@cloudbeaver/core-executor'; + +import type { ISQLScriptSegment } from '../SQLParser'; +import type { ISQLEditorData } from './ISQLEditorData'; + + +export interface ISQLEditorMode { + activeSegment: ISQLScriptSegment | undefined; + activeSegmentMode: boolean; +} + +export const SQLEditorModeContext: ISyncContextLoader = function SQLEditorModeContext(context, data) { + return { + activeSegment: data.parser.getSegment(data.cursor.begin, data.cursor.end), + activeSegmentMode: false, + }; +}; \ No newline at end of file diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditor/SqlEditor.tsx b/webapp/packages/plugin-sql-editor/src/SqlEditor/SqlEditor.tsx index bcfa713788..022a5cf098 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlEditor/SqlEditor.tsx +++ b/webapp/packages/plugin-sql-editor/src/SqlEditor/SqlEditor.tsx @@ -167,6 +167,7 @@ export const SqlEditor = observer(function SqlEditor({ state, c - - - - - + {!data.activeSegmentMode.activeSegmentMode && ( + + + + + + )} diff --git a/webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts b/webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts index ab8a603104..bede396d02 100644 --- a/webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts +++ b/webapp/packages/plugin-sql-editor/src/SqlEditor/useSqlEditor.ts @@ -24,12 +24,8 @@ import { ISQLScriptSegment, SQLParser } from '../SQLParser'; import { SqlExecutionPlanService } from '../SqlResultTabs/ExecutionPlan/SqlExecutionPlanService'; import { SqlQueryService } from '../SqlResultTabs/SqlQueryService'; import { SqlResultTabsService } from '../SqlResultTabs/SqlResultTabsService'; -import type { ISQLEditorData } from './ISQLEditorData'; - -interface ICursor { - begin: number; - end: number; -} +import type { ICursor, ISQLEditorData } from './ISQLEditorData'; +import { ISQLEditorMode, SQLEditorModeContext } from './SQLEditorModeContext'; interface ISQLEditorDataPrivate extends ISQLEditorData { readonly sqlDialectInfoService: SqlDialectInfoService; @@ -69,8 +65,15 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { return this.sqlDialectInfoService.getDialectInfo(this.state.executionContext.connectionId); }, + get activeSegmentMode(): ISQLEditorMode { + const contexts = this.onMode.execute(this); + const mode = contexts.getContext(SQLEditorModeContext); + + return mode; + }, + get activeSegment(): ISQLScriptSegment | undefined { - return this.parser.getSegment(this.cursor.begin, this.cursor.end); + return this.activeSegmentMode.activeSegment; }, get cursorSegment(): ISQLScriptSegment | undefined { @@ -103,6 +106,7 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { return this.state.query; }, + onMode: new SyncExecutor(), onExecute: new SyncExecutor(), onSegmentExecute: new SyncExecutor(), onUpdate: new SyncExecutor(), @@ -353,7 +357,7 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { return this.parser.getScriptSegment(); } - return this.parser.getSegment(this.cursor.begin, this.cursor.end); + return this.activeSegment; }, async getResolvedSegment(): Promise { @@ -363,6 +367,10 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { return this.getSubQuery(); } + if (this.activeSegmentMode.activeSegmentMode) { + return this.activeSegment; + } + const result = await this.sqlEditorService.parseSQLQuery( connectionId, @@ -401,6 +409,7 @@ export function useSqlEditor(state: ISqlEditorTabState): ISQLEditorData { executeQueryNewTab: action.bound, showExecutionPlan: action.bound, executeScript: action.bound, + activeSegmentMode: computed, dialect: computed, activeSegment: computed, isLineScriptEmpty: computed, diff --git a/webapp/packages/plugin-sql-editor/src/index.ts b/webapp/packages/plugin-sql-editor/src/index.ts index ac3f784077..a4572ad29e 100644 --- a/webapp/packages/plugin-sql-editor/src/index.ts +++ b/webapp/packages/plugin-sql-editor/src/index.ts @@ -2,6 +2,7 @@ import { sqlEditorPluginManifest } from './manifest'; export * from './SqlEditor/SQLCodeEditor/SQLCodeEditorLoader'; export * from './SqlEditor/ISQLEditorData'; export * from './SqlEditor/DATA_CONTEXT_SQL_EDITOR_DATA'; +export * from './SqlEditor/SQLEditorModeContext'; export * from './SqlResultTabs/DATA_CONTEXT_SQL_EDITOR_RESULT_ID'; export * from './SqlResultTabs/SqlResultTabsService'; export * from './DATA_CONTEXT_SQL_EDITOR_STATE';