Skip to content

Commit

Permalink
Editor utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mtxr committed Feb 17, 2019
1 parent fdb9d7e commit 3e156f2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 45 deletions.
1 change: 1 addition & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"label": "watch:compile",
"type": "npm",
"script": "watch",
"problemMatcher": []
}
]
}
35 changes: 35 additions & 0 deletions packages/extension/api/editor-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { TextEditor } from 'vscode';
import { window } from 'vscode';
import { workspace } from 'vscode';
import { DismissedException } from '@sqltools/core/exception';
import { SnippetString } from 'vscode';
import Utils from './utils';

export async function getOrCreateEditor(forceCreate = false): Promise<TextEditor> {
if (forceCreate || !window.activeTextEditor) {
const doc = await workspace.openTextDocument({ language: 'sql' });
await window.showTextDocument(doc, 1, false);
}
return window.activeTextEditor;
}

export async function getSelectedText(action = 'proceed', fullText = false) {
const editor = await getOrCreateEditor();
const query = editor.document.getText(fullText ? undefined : editor.selection);
if (Utils.isEmpty(query)) {
window.showInformationMessage(`Can't ${action}. You have selected nothing.`);
throw new DismissedException();
}
return query;
}
export async function insertText(text: string, forceCreate = false) {
const editor = await getOrCreateEditor(forceCreate);
editor.edit((edit) => {
editor.selections.forEach((cursor) => edit.insert(cursor.active, text));
});
}

export async function insertSnippet(text: string, forceCreate = false) {
const editor = await getOrCreateEditor(forceCreate);
return editor.insertSnippet(new SnippetString(text));
}
4 changes: 4 additions & 0 deletions packages/extension/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ namespace Utils {

return VSCommands.executeCommand('vscode.open', Uri.parse(url));
}

export function isEmpty(v) {
return !v || v.length === 0;
}
}

export default Utils;
49 changes: 4 additions & 45 deletions packages/extension/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ import {
env as VSCodeEnv,
version as VSCodeVersion,
} from 'vscode';
import {
CloseAction,
ErrorAction,
LanguageClient,
LanguageClientOptions,
ServerOptions,
TransportKind,
} from 'vscode-languageclient';
import ConfigManager from '@sqltools/core/config-manager';

import { EXT_NAME, VERSION } from '@sqltools/core/constants';
Expand All @@ -37,7 +29,6 @@ import {
GetCachedPassword,
CloseConnectionRequest,
} from '@sqltools/core/contracts/connection-requests';
import Notification from '@sqltools/core/contracts/notifications';
import LogWriter from './log-writer';
import {
ConnectionExplorer,
Expand All @@ -54,6 +45,7 @@ import { Timer, Telemetry, query as QueryUtils, getDbId, TelemetryArgs } from '@
import { DismissedException } from '@sqltools/core/exception';
import AutoInstaller from './language-client/dep-installer';
import { SQLToolsLanguageClient } from './language-client';
import { getOrCreateEditor, insertText, getSelectedText, insertSnippet } from './api/editor-utils';

namespace SQLTools {
const cfgKey: string = EXT_NAME.toLowerCase();
Expand Down Expand Up @@ -140,7 +132,7 @@ namespace SQLTools {
}

export async function cmdAppendToCursor(node: SidebarDatabaseItem): Promise<void> {
insertText(node.value);
return insertText(node.value);
}

export async function cmdGenerateInsertQuery(node: SidebarTable): Promise<boolean> {
Expand Down Expand Up @@ -514,7 +506,7 @@ namespace SQLTools {
return cachedPass || await Win.showInputBox({
prompt: `${c.name} password`,
password: true,
validateInput: (v) => isEmpty(v) ? 'Password not provided.' : null,
validateInput: (v) => Utils.isEmpty(v) ? 'Password not provided.' : null,
});
}
async function connect(force = false): Promise<SerializedConnection> {
Expand All @@ -533,35 +525,6 @@ namespace SQLTools {
return await languageClient.start();
}

async function getOrCreateEditor(forceCreate = false): Promise<TextEditor> {
if (forceCreate || !Win.activeTextEditor) {
const doc = await Wspc.openTextDocument({ language: 'sql' });
await Win.showTextDocument(doc, 1, false);
}
return Win.activeTextEditor;
}

async function getSelectedText(action = 'proceed', fullText = false) {
const editor = await getOrCreateEditor();
const query = editor.document.getText(fullText ? undefined : editor.selection);
if (isEmpty(query)) {
Win.showInformationMessage(`Can't ${action}. You have selected nothing.`);
throw new DismissedException();
}
return query;
}
async function insertText(text: string, forceCreate = false) {
const editor = await getOrCreateEditor(forceCreate);
editor.edit((edit) => {
editor.selections.forEach((cursor) => edit.insert(cursor.active, text));
});
}

async function insertSnippet(text: string, forceCreate = false) {
const editor = await getOrCreateEditor(forceCreate);
return editor.insertSnippet(new SnippetString(text));
}

async function help() {
try {
const { current = { } } = await Utils.getlastRunInfo();
Expand Down Expand Up @@ -675,13 +638,9 @@ namespace SQLTools {

async function readInput(prompt: string, placeholder?: string) {
const data = await Win.showInputBox({ prompt, placeHolder: placeholder || prompt });
if (isEmpty(data)) throw new DismissedException();
if (Utils.isEmpty(data)) throw new DismissedException();
return data;
}

function isEmpty(v) {
return !v || v.length === 0;
}
}

export const activate = SQLTools.start;
Expand Down

0 comments on commit 3e156f2

Please sign in to comment.