Skip to content
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

Connection Session Files #188

Merged
merged 11 commits into from May 18, 2019
@@ -2,6 +2,7 @@
"sqltools.telemetry": true,
"sqltools.useNodeRuntime": true,
"sqltools.sortColumns": "ordinalnumber",
"sqltools.autoOpenSessionFiles": true,
"sqltools.results": {
"limit": 100,
"location": "beside",
@@ -163,4 +163,13 @@ export interface Settings {
* @memberof Settings
*/
flattenGroupsIfOne?: boolean;

/**
* Auto open session file when connect
* @default true
* @type {boolean}
* @memberof Settings
*/
autoOpenSessionFiles?: boolean;

}
@@ -18,7 +18,6 @@ import DependencyManagerPlugin from '@sqltools/plugins/dependency-manager/extens
import HistoryManagerPlugin from '@sqltools/plugins/history-manager/extension';
import BookmarksManagerPlugin from '@sqltools/plugins/bookmarks-manager/extension';
import FormatterPlugin from '@sqltools/plugins/formatter/extension';
import CodeLensPlugin from '@sqltools/plugins/codelens/extension';

export class SQLToolsExtension implements SQLTools.ExtensionInterface {
private telemetry: Telemetry;
@@ -254,14 +253,14 @@ export class SQLToolsExtension implements SQLTools.ExtensionInterface {
let instance: SQLToolsExtension;
export function activate(context: ExtensionContext) {
if (instance) return;
instance = new SQLToolsExtension(context)
instance = new SQLToolsExtension(context);
instance
.registerPlugin(FormatterPlugin)
.registerPlugin(AutoRestartPlugin)
.registerPlugin(new ConnectionManagerPlugin)
.registerPlugin(new ConnectionManagerPlugin(instance))
.registerPlugin(new DependencyManagerPlugin)
.registerPlugin(new HistoryManagerPlugin)
.registerPlugin(new BookmarksManagerPlugin)
.registerPlugin(new CodeLensPlugin);
.registerPlugin(new BookmarksManagerPlugin);

return instance.activate();
}
@@ -1,7 +1,7 @@
import path from 'path';
import fs from 'fs';
import ConfigManager from '@sqltools/core/config-manager';
import { DISPLAY_NAME } from '@sqltools/core/constants';
import { DISPLAY_NAME, EXT_NAME } from '@sqltools/core/constants';
import SQLTools from '@sqltools/core/plugin-api';
import { commandExists, Telemetry } from '@sqltools/core/utils';
import { env as VSCodeEnv, version as VSCodeVersion, workspace as Wspc, ExtensionContext, window, commands } from 'vscode';
@@ -107,11 +107,12 @@ export class SQLToolsLanguageClient implements SQLTools.LanguageClientInterface
if (typeof language === 'string') {
agg.push({ language, scheme: 'untitled' });
agg.push({ language, scheme: 'file' });
agg.push({ language, scheme: 'sqltools' });
} else {
agg.push(language);
}
return agg;
}, []);
}, [{ scheme: EXT_NAME.toLowerCase(), language: undefined }]);

return {
documentSelector: selector,
@@ -120,7 +121,7 @@ export class SQLToolsLanguageClient implements SQLTools.LanguageClientInterface
extensionPath: this.context.extensionPath,
},
synchronize: {
configurationSection: 'sqltools',
configurationSection: EXT_NAME.toLowerCase(),
fileEvents: Wspc.createFileSystemWatcher('**/.sqltoolsrc'),
},
initializationFailedHandler: error => {
@@ -43,7 +43,9 @@
"sql",
"sqlite",
"sqlplus",
"sqltools"
"sqltools",
"sap",
"saphana"
],
"galleryBanner": {
"theme": "dark",
@@ -115,12 +117,12 @@
}
},
{
"title": "Create from selection",
"title": "Create From Selection",
"command": "SQLTools.bookmarkSelection",
"category": "SQLTools Bookmarks"
},
{
"title": "Delete all",
"title": "Delete All",
"command": "SQLTools.clearBookmarks",
"category": "SQLTools Bookmarks"
},
@@ -145,47 +147,47 @@
"category": "SQLTools History"
},
{
"title": "Edit from history",
"title": "Edit From History",
"command": "SQLTools.editHistory",
"category": "SQLTools History"
},
{
"title": "Describe table",
"title": "Describe Table",
"command": "SQLTools.describeTable",
"category": "SQLTools Connection"
},
{
"title": "Format SQL (for any doc)",
"title": "Format Sql (For Any Document)",
"command": "SQLTools.formatSql",
"category": "SQLTools"
},
{
"title": "Run selected query",
"title": "Run Selected Query",
"command": "SQLTools.executeQuery",
"category": "SQLTools Connection"
},
{
"title": "Run this file",
"title": "Run This File",
"command": "SQLTools.executeQueryFromFile",
"category": "SQLTools Connection"
},
{
"title": "Run query",
"title": "Run Query",
"command": "SQLTools.executeFromInput",
"category": "SQLTools Connection"
},
{
"title": "Show output channel (Logs)",
"title": "Show Output Channel (Logs)",
"command": "SQLTools.showOutputChannel",
"category": "SQLTools Management"
},
{
"title": "Focus on connection explorer",
"title": "Focus On Connection Explorer",
"command": "SQLTools.focusOnExplorer",
"category": "SQLTools Management"
},
{
"title": "Show table records",
"title": "Show Table Records",
"command": "SQLTools.showRecords",
"category": "SQLTools Connection",
"icon": {
@@ -194,12 +196,12 @@
}
},
{
"title": "About SQLTools",
"title": "About",
"command": "SQLTools.aboutVersion",
"category": "SQLTools Management"
},
{
"title": "Add new connection",
"title": "Add New Connection",
"command": "SQLTools.openAddConnectionScreen",
"category": "SQLTools Management",
"icon": {
@@ -208,17 +210,17 @@
}
},
{
"title": "Edit connection",
"title": "Edit Connection",
"command": "SQLTools.openEditConnectionScreen",
"category": "SQLTools Management"
},
{
"title": "Delete connection",
"title": "Delete Connection",
"command": "SQLTools.deleteConnection",
"category": "SQLTools Management"
},
{
"title": "Add name to cursor",
"title": "Add Name To Cursor",
"command": "SQLTools.insertText",
"category": "SQLTools Sidebar"
},
@@ -241,7 +243,7 @@
}
},
{
"title": "New SQL File",
"title": "New Sql File",
"command": "SQLTools.newSqlFile",
"category": "SQLTools",
"icon": {
@@ -250,13 +252,23 @@
}
},
{
"title": "Save results",
"title": "Save Results",
"command": "SQLTools.saveResults",
"category": "SQLTools Results",
"icon": {
"light": "icons/save-light.svg",
"dark": "icons/save-dark.svg"
}
},
{
"title": "SQLTools: Attach Connection To This File",
"command": "SQLTools.attachFileToConnection",
"category": "SQLTools Internal"
},
{
"title": "SQLTools: Detach Connection From File",
"command": "SQLTools.detachConnectionFromFile",
"category": "SQLTools Internal"
}
],
"keybindings": [
@@ -674,6 +686,11 @@
"type": "boolean",
"default": false,
"description": "Flatten groups with has only one child"
},
"sqltools.autoOpenSessionFiles": {
"type": "boolean",
"default": true,
"description": "Auto open session file when connect"
}
}
},
@@ -712,6 +729,14 @@
"command": "SQLTools.generateInsertQuery",
"when": "false"
},
{
"command": "SQLTools.attachFileToConnection",
"when": "false"
},
{
"command": "SQLTools.detachConnectionFromFile",
"when": "false"
},
{
"command": "SQLTools.saveResults",
"when": "sqltools.Results.active"
@@ -854,6 +879,18 @@
"group": "navigation",
"when": "sqltools.Results.active"
}
],
"editor/context": [
{
"command": "SQLTools.attachFileToConnection",
"group": "sqltools@0",
"when": "!sqltools.file.connectionAttached && editorLangId == sql"
},
{
"command": "SQLTools.detachConnectionFromFile",
"group": "sqltools@0",
"when": "sqltools.file.connectionAttached && editorLangId == sql"
}
]
}
},
@@ -7,13 +7,14 @@ import * as Utils from '@sqltools/core/utils';
export default class CodeLensPlugin implements SQLTools.ExtensionPlugin {
private codelensDisposable: Disposable;
private registeredLanguages: string[] = [];
private provider: SQLToolsCodeLensProvider;
async dispose() {
if (!this.codelensDisposable) return;
await this.codelensDisposable.dispose();
this.codelensDisposable = null;
}

async createCodelens() {
async createCodelens(context: SQLTools.ExtensionInterface['context']) {
const oldLang = this.registeredLanguages.sort(Utils.sortText);
const newLang = ConfigManager.codelensLanguages.sort(Utils.sortText);
const shouldRegister = newLang.length > 0 && (oldLang.join() !== newLang.join());
@@ -23,18 +24,23 @@ export default class CodeLensPlugin implements SQLTools.ExtensionPlugin {
if(this.registeredLanguages) {
await this.dispose();
}
this.provider = new SQLToolsCodeLensProvider(context);
this.codelensDisposable = languages.registerCodeLensProvider(
ConfigManager.codelensLanguages.map(language => ({ language })),
new SQLToolsCodeLensProvider(),
this.provider,
);
this.registeredLanguages = ConfigManager.codelensLanguages;
}

register(extension: SQLTools.ExtensionInterface) {
extension.context.subscriptions.push(this);
this.createCodelens();
this.createCodelens(extension.context);
ConfigManager.addOnUpdateHook(() => {
this.createCodelens();
this.createCodelens(extension.context);
});
}

reset() {
return this.provider.reset();
}
}
@@ -1,20 +1,41 @@
import { CodeLensProvider, TextDocument, CodeLens, Range, Command } from 'vscode';
import { CodeLensProvider, TextDocument, CodeLens, Range, Command, Event, EventEmitter } from 'vscode';
import * as Constants from '@sqltools/core/constants';
import Selector from '@sqltools/core/utils/vscode/selector';
import { getNameFromId } from '@sqltools/core/utils';
import SQLTools from '@sqltools/core/plugin-api';

export default class SQLToolsCodeLensProvider implements CodeLensProvider {
private _onDidChangeCodeLenses = new EventEmitter<void>();
get onDidChangeCodeLenses(): Event<void> {
return this._onDidChangeCodeLenses.event;
}

reset() {
this._onDidChangeCodeLenses.fire();
}
async provideCodeLenses(document: TextDocument): Promise<CodeLens[]> {
const lines: string[] = document.getText().split(Constants.LineSplitterRegex);
const requestRanges: [number, number][] = Selector.getQueryRanges(lines);
const defaultConn = (document.getText(new Range(0, 0, 1, 0)).match(/@conn\s*([\w_]+)/) || [])[1];
const defaultConn = (document.getText(new Range(0, 0, 1, 0)).match(/@conn\s*(.+)$/) || [])[1];
const lenses: CodeLens[] = [];
const attachedId = this.context.workspaceState.get('attachedFilesMap', {})[document.uri.toString()];
if (attachedId) {
// attached to a connection
const connName = getNameFromId(attachedId);
const runCmd: Command = {
arguments: [document.uri],
title: `Detach file from ${connName.trim()}`,
command: `${Constants.EXT_NAME}.detachConnectionFromFile`,
};
lenses.push(new CodeLens(new Range(0, 0, 0, 0), runCmd))
}
requestRanges.forEach(([blockStart, blockEnd]) => {
const range = new Range(blockStart, 0, blockEnd, 0);
const queries = document.getText(range);
const connName = (queries.match(/@conn\s*([\w_]+)/) || [])[1];
const connName = (queries.match(/@conn\s*(.+)$/) || [])[1];
const runCmd: Command = {
arguments: [queries, connName || defaultConn],
title: `Run query block on ${connName || defaultConn || 'active connection'}`.trim(),
arguments: [queries, (connName || defaultConn || '').trim() || undefined].filter(Boolean),
title: `Run query block on ${(connName || defaultConn || 'active connection').trim()}`,
command: `${Constants.EXT_NAME}.executeQuery`,
};
lenses.push(new CodeLens(range, runCmd));
@@ -29,4 +50,8 @@ export default class SQLToolsCodeLensProvider implements CodeLensProvider {

return lenses;
}

constructor(private context: SQLTools.ExtensionInterface['context']) {
context.subscriptions.push(this._onDidChangeCodeLenses);
}
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.