import * as vscode from 'vscode'; enum LinesAction { searchGoogle } function searchGoogleLogic(text: string, lines: string[]): void { let searchText: string = ''; if (text.length > 0) { searchText = text.trim(); } else if (lines.length > 0) { searchText = lines[0].trim(); } else return undefined; if (searchText != '') { 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 | 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);