@@ -599,12 +599,33 @@ async function run(
599599 // Get the project url
600600 const projectFolder = addedFolder [ 0 ] ;
601601
602- // Set the root folder to the file parent if no project folder is set
602+ // FIXED: Better root folder determination for Termux URIs
603603 let rootFolder = pathName ;
604- if ( projectFolder !== undefined && pathName . includes ( projectFolder ) ) {
604+
605+ // Special handling for Termux URIs - extract the actual root from the URI structure
606+ if (
607+ activeFile &&
608+ activeFile . uri &&
609+ activeFile . uri . includes ( "com.termux.documents" ) &&
610+ activeFile . uri . includes ( "tree/" )
611+ ) {
612+ // Extract the tree part and decode it to get the actual root path
613+ const treeMatch = activeFile . uri . match ( / t r e e \/ ( [ ^ : ] + ) / ) ;
614+ if ( treeMatch ) {
615+ try {
616+ const decodedRoot = decodeURIComponent ( treeMatch [ 1 ] ) ;
617+ rootFolder = decodedRoot ;
618+ console . log ( `DEBUG - Termux root folder set to: ${ rootFolder } ` ) ;
619+ } catch ( e ) {
620+ console . error ( "Error decoding Termux root:" , e ) ;
621+ }
622+ }
623+ } else if (
624+ projectFolder !== undefined &&
625+ pathName &&
626+ pathName . includes ( projectFolder . url )
627+ ) {
605628 rootFolder = projectFolder . url ;
606- } else {
607- rootFolder = pathName ;
608629 }
609630
610631 //make the uri absolute if necessary
@@ -634,30 +655,25 @@ async function run(
634655 try {
635656 const [ , realPath ] = temp . split ( "::" ) ;
636657
637- // Determine root folder inside :: path
638- let rootPath = rootFolder ;
639- if ( rootFolder . includes ( "::" ) ) {
640- rootPath = rootFolder . split ( "::" ) [ 1 ] ;
641- }
658+ console . log ( `DEBUG - realPath: ${ realPath } ` ) ;
659+ console . log ( `DEBUG - rootFolder: ${ rootFolder } ` ) ;
642660
643- // Normalize both paths to arrays
644- const realParts = realPath . split ( "/" ) . filter ( Boolean ) ;
645- const rootParts = rootPath . split ( "/" ) . filter ( Boolean ) ;
661+ // Ensure rootFolder doesn't have trailing slash for comparison
662+ const normalizedRoot = rootFolder . replace ( / \/ + $ / , "" ) ;
646663
647- // Find where the paths start to differ
648- let diffIndex = 0 ;
649- while (
650- diffIndex < realParts . length &&
651- diffIndex < rootParts . length &&
652- realParts [ diffIndex ] === rootParts [ diffIndex ]
653- ) {
654- diffIndex ++ ;
655- }
664+ // Check if realPath starts with rootFolder
665+ if ( realPath . startsWith ( normalizedRoot ) ) {
666+ // Remove the rootFolder from the beginning of realPath
667+ let relativePath = realPath . substring ( normalizedRoot . length ) ;
656668
657- // Return everything after the common root
658- const relativeParts = realParts . slice ( diffIndex ) ;
659- if ( relativeParts . length > 0 ) {
660- return relativeParts . join ( "/" ) ;
669+ // Remove leading slash if present
670+ relativePath = relativePath . replace ( / ^ \/ + / , "" ) ;
671+
672+ console . log ( `DEBUG - relativePath: ${ relativePath } ` ) ;
673+
674+ if ( relativePath ) {
675+ return relativePath ;
676+ }
661677 }
662678 } catch ( e ) {
663679 console . error ( "Error handling Termux URI:" , e ) ;
@@ -697,26 +713,17 @@ async function run(
697713 }
698714 }
699715
700- // Now find this rootFolderPath in the afterDoubleColon string
701- if ( afterDoubleColon . includes ( rootFolderPath ) ) {
702- // Find where to start the relative path
703- const parts = afterDoubleColon . split ( "/" ) ;
704-
705- // Find the index of the part that matches or contains rootFolderPath
706- let startIndex = - 1 ;
707- for ( let i = 0 ; i < parts . length ; i ++ ) {
708- if (
709- parts [ i ] . includes ( rootFolderPath ) ||
710- rootFolderPath . includes ( parts [ i ] )
711- ) {
712- startIndex = i ;
713- break ;
714- }
715- }
716-
717- // If we found a matching part, get everything after it
718- if ( startIndex >= 0 && startIndex < parts . length - 1 ) {
719- return parts . slice ( startIndex + 1 ) . join ( "/" ) ;
716+ // Use direct string replacement instead of path component comparison
717+ const normalizedRoot = rootFolderPath . replace ( / \/ + $ / , "" ) ;
718+ if ( afterDoubleColon . startsWith ( normalizedRoot ) ) {
719+ let relativePath = afterDoubleColon . substring (
720+ normalizedRoot . length ,
721+ ) ;
722+ // Remove leading slash if present
723+ relativePath = relativePath . replace ( / ^ \/ + / , "" ) ;
724+
725+ if ( relativePath ) {
726+ return relativePath ;
720727 }
721728 }
722729 }
@@ -761,6 +768,7 @@ async function run(
761768 * Opens the preview in browser
762769 */
763770 function openBrowser ( ) {
771+ console . log ( `Running ${ Url . join ( pathName , filename ) } ` ) ;
764772 let url = "" ;
765773 if ( pathName === null && ! activeFile . location ) {
766774 url = `http://localhost:${ port } /__unsaved_file__` ;
0 commit comments