From 7bf0d36057594cd732247bb89f8f04aabe67c41e Mon Sep 17 00:00:00 2001
From: Jeff Young <jeff@rokeby.ie>
Date: Tue, 14 Jan 2025 18:27:09 +0000
Subject: [PATCH] Store/fetch sheetpath along with symbol UUID.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/19623
---
 eeschema/sch_file_versions.h |  3 ++-
 eeschema/schematic.cpp       | 32 +++++++++++++++++++++++++-------
 2 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/eeschema/sch_file_versions.h b/eeschema/sch_file_versions.h
index 8a1d6378f1..2324d4afca 100644
--- a/eeschema/sch_file_versions.h
+++ b/eeschema/sch_file_versions.h
@@ -113,4 +113,5 @@
 //#define SEXPR_SCHEMATIC_FILE_VERSION 20240812  // Netclass color highlighting
 //#define SEXPR_SCHEMATIC_FILE_VERSION 20240819  // Embedded Files - Update hash algorithm to Murmur3
 //#define SEXPR_SCHEMATIC_FILE_VERSION 20241004  // Use booleans for 'hide' in symbols
-#define SEXPR_SCHEMATIC_FILE_VERSION 20241209    // Private flags for SCH_FIELDs
+//#define SEXPR_SCHEMATIC_FILE_VERSION 20241209  // Private flags for SCH_FIELDs
+#define SEXPR_SCHEMATIC_FILE_VERSION 20250114    // Full paths for text variable cross references
diff --git a/eeschema/schematic.cpp b/eeschema/schematic.cpp
index 01598a2af4..6f518c30b0 100644
--- a/eeschema/schematic.cpp
+++ b/eeschema/schematic.cpp
@@ -443,8 +443,16 @@ bool SCHEMATIC::ResolveCrossReference( wxString* token, int aDepth ) const
 {
     wxString       remainder;
     wxString       ref = token->BeforeFirst( ':', &remainder );
+    KIID_PATH      path( ref );
+    KIID           uuid = path.back();
     SCH_SHEET_PATH sheetPath;
-    SCH_ITEM*      refItem = GetItem( KIID( ref ), &sheetPath );
+    SCH_ITEM*      refItem = GetItem( KIID( uuid ), &sheetPath );
+
+    if( path.size() > 1 )
+    {
+        path.pop_back();
+        sheetPath = Hierarchy().GetSheetPathByKIIDPath( path ).value_or( sheetPath );
+    }
 
     if( refItem && refItem->Type() == SCH_SYMBOL_T )
     {
@@ -549,7 +557,10 @@ wxString SCHEMATIC::ConvertRefsToKIIDs( const wxString& aSource ) const
 
                     if( ref == refSymbol->GetRef( &references[ jj ].GetSheetPath(), true ) )
                     {
-                        token = refSymbol->m_Uuid.AsString() + wxS( ":" ) + remainder;
+                        KIID_PATH path = references[ jj ].GetSheetPath().Path();
+                        path.push_back( refSymbol->m_Uuid );
+
+                        token = path.AsString() + wxS( ":" ) + remainder;
                         break;
                     }
                 }
@@ -592,16 +603,23 @@ wxString SCHEMATIC::ConvertKIIDsToRefs( const wxString& aSource ) const
 
             if( isCrossRef )
             {
-                wxString remainder;
-                wxString ref = token.BeforeFirst( ':', &remainder );
+                wxString       remainder;
+                wxString       ref = token.BeforeFirst( ':', &remainder );
+                KIID_PATH      path( ref );
+                KIID           uuid = path.back();
+                SCH_SHEET_PATH sheetPath;
+                SCH_ITEM*      refItem = GetItem( uuid, &sheetPath );
 
-                SCH_SHEET_PATH refSheetPath;
-                SCH_ITEM* refItem = GetItem( KIID( ref ), &refSheetPath );
+                if( path.size() > 1 )
+                {
+                    path.pop_back();
+                    sheetPath = Hierarchy().GetSheetPathByKIIDPath( path ).value_or( sheetPath );
+                }
 
                 if( refItem && refItem->Type() == SCH_SYMBOL_T )
                 {
                     SCH_SYMBOL* refSymbol = static_cast<SCH_SYMBOL*>( refItem );
-                    token = refSymbol->GetRef( &refSheetPath, true ) + wxS( ":" ) + remainder;
+                    token = refSymbol->GetRef( &sheetPath, true ) + wxS( ":" ) + remainder;
                 }
             }