import * as fs from 'fs' ; import * as path from 'path' ; import { Uri , workspace , WorkspaceFolder } from 'vscode' ; export function nearestParentWorkspace ( curWorkspace , filePath ) { const workspaceRoot = curWorkspace . uri . fsPath ; const rootManifest = path . join ( workspaceRoot , 'Cargo.toml' ) ; if ( fs . existsSync ( rootManifest ) ) { return curWorkspace ; } let current = filePath ; while ( true ) { const old = current ; current = path . dirname ( current ) ; if ( old === current ) { break ; } if ( workspaceRoot === current ) { break ; } const cargoPath = path . join ( current , 'Cargo.toml' ) ; if ( fs . existsSync ( cargoPath ) ) { return { ... curWorkspace , name : path . basename ( current ) , uri : Uri . file ( current ) , } ; } } return curWorkspace ; } export function getOuterMostWorkspaceFolder ( folder ) { const sortedFoldersByPrefix = ( workspace . workspaceFolders || [ ] ) . map ( folder => normalizeUriToPathPrefix ( folder . uri ) ) . sort ( ( a , b ) => a . length - b . length ) ; const uri = normalizeUriToPathPrefix ( folder . uri ) ; const outermostPath = sortedFoldersByPrefix . find ( pre => uri . startsWith ( pre ) ) ; return outermostPath ? workspace . getWorkspaceFolder ( Uri . parse ( outermostPath ) ) || folder : folder ; } /**
 * Transforms a given URI to a path prefix, namely ensures that each path
 * segment ends with a path separator `/`.
 */ function normalizeUriToPathPrefix ( uri ) { let result = uri . toString ( ) ; if ( result . charAt ( result . length - 1 ) !== '/' ) { result = result + '/' ; } return result ; }