51 lines
1.6 KiB
TypeScript
51 lines
1.6 KiB
TypeScript
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); |