import * as fs from 'fs' ; import * as path from 'path' ; import * as vscode from 'vscode' ; import { DependenciesTreeNode } from '../treeDataProviders/dependenciesTree/dependenciesTreeNode' ; import { PypiUtils } from '../utils/pypiUtils' ; import { AbstractFocus } from './abstractFocus' ; export class PypiFocus extends AbstractFocus { constructor ( ) { super ( PypiUtils . PKG_TYPE ) ; } @override  public async focusOnDependency ( dependenciesTreeNode ) { if ( dependenciesTreeNode . isDependenciesTreeRoot ( ) ) { return ; } /**
         *       |-> Pypi project 1 -> dependenciesTreeNode -> child
         * Root -|-> Pypi project 2
         *       |-> Pypi project 3
         */ let child ; while ( dependenciesTreeNode . parent && dependenciesTreeNode . parent . parent && dependenciesTreeNode . parent . parent . parent ) { child = dependenciesTreeNode ; dependenciesTreeNode = dependenciesTreeNode . parent ; } let parent = dependenciesTreeNode . parent ; if ( ! parent ) { return ; } let fsPath = parent . generalInfo . path ; let requirementsFiles = await PypiUtils . getRequirementsFiles ( fsPath ) ; for ( let requirementsFile of requirementsFiles ) { let document = await vscode . workspace . openTextDocument ( requirementsFile ) ; let requirementsContent = document . getText ( ) . toLowerCase ( ) ; let pos = PypiUtils . getDependencyPos ( document , requirementsContent , dependenciesTreeNode ) ; if ( pos . length > 0 ) { this . revealRange ( document , pos ) ; return ; } if ( child ) { pos = PypiUtils . getDependencyPos ( document , requirementsContent , child ) ; if ( pos . length > 0 ) { this . revealRange ( document , pos ) ; return ; } } } this . tryOpenSetupPy ( fsPath ) ; } param textDocument param position private async revealRange ( textDocument , position ) { let textEditor = await vscode . window . showTextDocument ( textDocument ) ; textEditor . selection = new vscode . Selection ( position [ 0 ] , position [ 1 ] ) ; textEditor . revealRange ( new vscode . Range ( position [ 0 ] , position [ 1 ] ) , vscode . TextEditorRevealType . InCenter ) ; } param fsPath private async tryOpenSetupPy ( fsPath ) { let setupPyPath = path . join ( fsPath , 'setup.py' ) ; if ( ! fs . existsSync ( setupPyPath ) ) { return ; } let textDocument = await vscode . workspace . openTextDocument ( setupPyPath ) ; let pos = PypiUtils . getDependenciesPos ( textDocument ) ; if ( pos . length > 0 ) { this . revealRange ( textDocument , pos ) ; } else { this . revealRange ( textDocument , [ new vscode . Position ( 0 , 0 ) , new vscode . Position ( 0 , 0 ) ] ) ; } } }