Search Google - Selection

This commit is contained in:
2021-10-20 09:47:27 -07:00
parent b7453ff4f1
commit 21324f6ab6
9 changed files with 112 additions and 62 deletions

View File

@ -1,7 +1,8 @@
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as helper from './helper';
import * as replaceLinesHelper from './replaceLinesHelper';
import * as readOnlyLinesHelper from './readOnlyLinesHelper';
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
@ -25,21 +26,21 @@ export function activate(context: vscode.ExtensionContext) {
// context.subscriptions.push(disposable);
const commands = [
vscode.commands.registerCommand('helper.addCSharpComment', helper.addCSharpComment),
vscode.commands.registerCommand('helper.addVBComment', helper.addVBComment),
vscode.commands.registerCommand('helper.convertToRegularExpression', helper.convertToRegularExpression),
vscode.commands.registerCommand('helper.cutEachLine', helper.cutEachLine),
vscode.commands.registerCommand('helper.expandSql', helper.expandSql),
vscode.commands.registerCommand('helper.listToListFamily', helper.listToListFamily),
vscode.commands.registerCommand('helper.listToListWrappedComma', helper.listToListWrappedComma),
vscode.commands.registerCommand('helper.prettySql', helper.prettySql),
vscode.commands.registerCommand('helper.removeComment', helper.removeComment),
vscode.commands.registerCommand('helper.searchGoogle', helper.searchGoogle),
vscode.commands.registerCommand('helper.sortLength', helper.sortLength),
vscode.commands.registerCommand('helper.sortNormal', helper.sortNormal),
vscode.commands.registerCommand('helper.unwrapSql', helper.unwrapSql),
vscode.commands.registerCommand('helper.wrapSqlCSharp', helper.wrapSqlCSharp),
vscode.commands.registerCommand('helper.wrapSqlVB', helper.wrapSqlVB)
vscode.commands.registerCommand('readOnlyLinesHelper.searchGoogle', readOnlyLinesHelper.searchGoogle),
vscode.commands.registerCommand('replaceLinesHelper.addCSharpComment', replaceLinesHelper.addCSharpComment),
vscode.commands.registerCommand('replaceLinesHelper.addVBComment', replaceLinesHelper.addVBComment),
vscode.commands.registerCommand('replaceLinesHelper.convertToRegularExpression', replaceLinesHelper.convertToRegularExpression),
vscode.commands.registerCommand('replaceLinesHelper.cutEachLine', replaceLinesHelper.cutEachLine),
vscode.commands.registerCommand('replaceLinesHelper.expandSql', replaceLinesHelper.expandSql),
vscode.commands.registerCommand('replaceLinesHelper.listToListFamily', replaceLinesHelper.listToListFamily),
vscode.commands.registerCommand('replaceLinesHelper.listToListWrappedComma', replaceLinesHelper.listToListWrappedComma),
vscode.commands.registerCommand('replaceLinesHelper.prettySql', replaceLinesHelper.prettySql),
vscode.commands.registerCommand('replaceLinesHelper.removeComment', replaceLinesHelper.removeComment),
vscode.commands.registerCommand('replaceLinesHelper.sortLength', replaceLinesHelper.sortLength),
vscode.commands.registerCommand('replaceLinesHelper.sortNormal', replaceLinesHelper.sortNormal),
vscode.commands.registerCommand('replaceLinesHelper.unwrapSql', replaceLinesHelper.unwrapSql),
vscode.commands.registerCommand('replaceLinesHelper.wrapSqlCSharp', replaceLinesHelper.wrapSqlCSharp),
vscode.commands.registerCommand('replaceLinesHelper.wrapSqlVB', replaceLinesHelper.wrapSqlVB)
];
commands.forEach(command => context.subscriptions.push(command));

View File

@ -0,0 +1,51 @@
import * as vscode from 'vscode';
enum LinesAction {
searchGoogle
}
function searchGoogleLogic(text: string, lines: string[]): void {
if (text.length > 0) {
vscode.env.openExternal(
vscode.Uri.parse(`https://www.google.com/search?q=${text.trim()}`)
)
}
else if (lines.length > 0) {
vscode.env.openExternal(
vscode.Uri.parse(`https://www.google.com/search?q=${lines[0].trim()}`)
)
}
}
function getLines(textEditor: vscode.TextEditor, startLine: number, endLine: number) {
let results: string[] = [];
for (let i = startLine; i <= endLine; i++) {
results.push(textEditor.document.lineAt(i).text);
}
return results;
}
function linesFunction(linesAction: LinesAction): Thenable<boolean> | undefined {
const textEditor = vscode.window.activeTextEditor;
if (!textEditor) {
return undefined;
}
let text = '';
var startLine = 0;
var endLine = textEditor.document.lineCount - 1;
const selection = textEditor.selection;
if (!selection.isEmpty) {
startLine = selection.start.line;
endLine = selection.end.line;
let range = new vscode.Range(selection.start, selection.end)
text = textEditor.document.getText(range);
}
let lines: string[] = getLines(textEditor, startLine, endLine);
switch (linesAction) {
case LinesAction.searchGoogle: { searchGoogleLogic(text, lines); break; }
default: { throw new Error(); }
}
return undefined;
}
export const searchGoogle = () => linesFunction(LinesAction.searchGoogle);

View File

@ -1,5 +1,4 @@
import * as vscode from 'vscode';
import { stringify } from 'querystring';
type ArrayTransformer = (lines: string[]) => string[];
type SortingAlgorithm = (a: string, b: string) => number;
@ -15,7 +14,6 @@ enum LinesAction {
prettySql,
pdsfToFixedWidth,
removeComment,
searchGoogle,
sortLength,
sortNormal,
unwrapSql,
@ -158,14 +156,6 @@ function removeCommentLogic(lines: string[]): void {
}
}
function searchGoogleLogic(lines: string[]): void {
for (let i = 0; i < lines.length; ++i) {
vscode.env.openExternal(
vscode.Uri.parse(`https://www.google.com/search?q=${lines[i]}`)
)
}
}
function makeSorter(algorithm?: SortingAlgorithm): ArrayTransformer {
return function (lines: string[]): string[] {
return lines.sort(algorithm);
@ -276,7 +266,6 @@ function linesFunction(linesAction: LinesAction): Thenable<boolean> | undefined
case LinesAction.listToListWrappedComma: { listToListWrappedCommaLogic(lines); break; }
case LinesAction.prettySql: { prettySqlLogic(lines); break; }
case LinesAction.removeComment: { removeCommentLogic(lines); break; }
case LinesAction.searchGoogle: { searchGoogleLogic(lines); break; }
case LinesAction.sortLength: { sortLengthLogic(lines); break; }
case LinesAction.sortNormal: { sortNormalLogic(lines); break; }
case LinesAction.unwrapSql: { unwrapSqlLogic(lines); break; }
@ -297,7 +286,6 @@ export const listToListWrappedComma = () => linesFunction(LinesAction.listToList
export const prettySql = () => linesFunction(LinesAction.prettySql);
export const pdsfToFixedWidth = () => linesFunction(LinesAction.pdsfToFixedWidth);
export const removeComment = () => linesFunction(LinesAction.removeComment);
export const searchGoogle = () => linesFunction(LinesAction.searchGoogle);
export const sortLength = () => linesFunction(LinesAction.sortLength);
export const sortNormal = () => linesFunction(LinesAction.sortNormal);
export const unwrapSql = () => linesFunction(LinesAction.unwrapSql);